Author: clopes
Date: 2011-09-27 12:20:51 -0700 (Tue, 27 Sep 2011)
New Revision: 26986

Modified:
   cytoscapeweb/trunk/cytoscapeweb/html-template/js/cytoscapeweb.js
   cytoscapeweb/trunk/cytoscapeweb/src/org/cytoscapeweb/util/GraphUtils.as
   cytoscapeweb/trunk/cytoscapeweb/src/org/cytoscapeweb/util/Labels.as
   cytoscapeweb/trunk/cytoscapeweb/src/org/cytoscapeweb/util/Nodes.as
   cytoscapeweb/trunk/cytoscapeweb/src/org/cytoscapeweb/util/VisualProperties.as
   
cytoscapeweb/trunk/cytoscapeweb/src/org/cytoscapeweb/view/components/GraphView.as
   
cytoscapeweb/trunk/cytoscapeweb/src/org/cytoscapeweb/view/components/GraphVis.as
   cytoscapeweb/trunk/cytoscapeweb/src/org/cytoscapeweb/view/render/Labeler.as
   
cytoscapeweb/trunk/cytoscapeweb/src/org/cytoscapeweb/view/render/NodeRenderer.as
Log:
Implemented feature #2591: Autosize node according to label

Modified: cytoscapeweb/trunk/cytoscapeweb/html-template/js/cytoscapeweb.js
===================================================================
--- cytoscapeweb/trunk/cytoscapeweb/html-template/js/cytoscapeweb.js    
2011-09-27 19:07:33 UTC (rev 26985)
+++ cytoscapeweb/trunk/cytoscapeweb/html-template/js/cytoscapeweb.js    
2011-09-27 19:20:51 UTC (rev 26986)
@@ -2249,6 +2249,7 @@
      * The absolute node size (in pixels), when the zoom level is 100%. It is 
the highest value of height and width.
      * Notice that this value is not scaled, so if you want the real 
visualized size, you need to multiply
      * this value by the current network scale, which is provided by {@link 
org.cytoscapeweb.Visualization#zoom}.
+     * If you set the size to <code>"auto"</code>, the node will be 
automatically resized to enclose its label text.
      * @property
      * @name size
      * @type Number

Modified: 
cytoscapeweb/trunk/cytoscapeweb/src/org/cytoscapeweb/util/GraphUtils.as
===================================================================
--- cytoscapeweb/trunk/cytoscapeweb/src/org/cytoscapeweb/util/GraphUtils.as     
2011-09-27 19:07:33 UTC (rev 26985)
+++ cytoscapeweb/trunk/cytoscapeweb/src/org/cytoscapeweb/util/GraphUtils.as     
2011-09-27 19:20:51 UTC (rev 26986)
@@ -43,7 +43,6 @@
        import flash.text.TextField;
        import flash.utils.Dictionary;
        
-       import org.cytoscapeweb.model.data.VisualStyleVO;
        import org.cytoscapeweb.util.methods.$each;
        import org.cytoscapeweb.view.layout.PackingAlgorithms;
        
@@ -302,7 +301,7 @@
             }
         }
         
-        public static function calculateGraphDimension(nodes:DataList, 
layout:String, style:VisualStyleVO):Rectangle {            
+        public static function calculateGraphDimension(nodes:DataList, 
layout:String):Rectangle {            
             // The minimum square edge when we have only one node:
             var side:Number = 40;
             var numNodes:Number = nodes.length;
@@ -314,34 +313,34 @@
             
             if (numNodes > 1) {
                 if (layout === Layouts.CIRCLE || layout === Layouts.RADIAL) {
+                    // Based on the desired distance between the adjacent 
nodes, imagine an inscribed 
+                    // regular polygon that has N sides, and then calculate 
the circle radius:
+                    // 1. number of sides = number of nodes:
+                    var N:Number = nodes.length;
+                    // 2. Each side should have a desired size (distance 
between the adjacent nodes):
+                    var S:Number = 0;
+                    for each (n in nodes) {
+                        if (!GraphUtils.isFilteredOut(n)) {
+                            S = Math.max(n.width, n.height);
+                        }
+                    }
+                    if (isNaN(S)) S = 40;
+                    
                     if (numNodes === 2) {
-                        side *= 1.5;
+                        side = 2.1 * S;
                     } else {
-                        // Based on the desired distance between the adjacent 
nodes, imagine an inscribed 
-                        // regular polygon that has N sides, and then 
calculate the circle radius:
-                        // 1. number of sides = number of nodes:
-                        var N:Number = nodes.length;
-                        // 2. Each side should have a desired size (distance 
between the adjacent nodes):
-                        var S:Number = 0;
-                        for each (n in nodes) {
-                            if (!GraphUtils.isFilteredOut(n))
-                                S = Math.max(S, 
style.getValue(VisualProperties.NODE_SIZE, n.data));
-                        }
-                        S /= 2;
                         // 3. If we connect two adjacent vertices to the 
center, the angle between these two 
                         // lines is 360/N degrees, or 2*pi/N radians:
                         var theta:Number = 2 * Math.PI / N;
-                        // 4. To find the circle radius, using Trigonometry:
+                        // 4. To find the circle diameter, using Trigonometry:
                         // sin(theta/2) = opposite/hypotenuse
-                        var r:Number = S / Math.sin(theta/2) * 2;
-                        // 5. Finally, the square side should be the circle 
diameter (2r):
-                        side = 2 * r;
+                        side = (2 * S) / Math.sin(theta/2);
                     }
                 } else if (layout === Layouts.FORCE_DIRECTED) {
                     var area:Number = 0;
                     for each (n in nodes) {
                         if (!GraphUtils.isFilteredOut(n)) {
-                            var s:Number = 
style.getValue(VisualProperties.NODE_SIZE, n.data);
+                            var s:Number = Math.max(n.width, n.height);
                             area += 9 * s * s;
                         }
                     }

Modified: cytoscapeweb/trunk/cytoscapeweb/src/org/cytoscapeweb/util/Labels.as
===================================================================
--- cytoscapeweb/trunk/cytoscapeweb/src/org/cytoscapeweb/util/Labels.as 
2011-09-27 19:07:33 UTC (rev 26985)
+++ cytoscapeweb/trunk/cytoscapeweb/src/org/cytoscapeweb/util/Labels.as 
2011-09-27 19:20:51 UTC (rev 26986)
@@ -106,12 +106,12 @@
         }
         
         public static function labelHAnchor(d:DataSprite):int {
-            if (d is NodeSprite && d.props.wrapText) return TextSprite.CENTER;
+            if (d is NodeSprite && d.props.autoSize) return TextSprite.CENTER;
             return 
Anchors.toFlareAnchor(style.getValue(VisualProperties.NODE_LABEL_HANCHOR, 
d.data));
         }
         
         public static function labelVAnchor(d:DataSprite):int {
-            if (d is NodeSprite && d.props.wrapText) return TextSprite.MIDDLE;
+            if (d is NodeSprite && d.props.autoSize) return TextSprite.MIDDLE;
             return 
Anchors.toFlareAnchor(style.getValue(VisualProperties.NODE_LABEL_VANCHOR, 
d.data));
         }
         

Modified: cytoscapeweb/trunk/cytoscapeweb/src/org/cytoscapeweb/util/Nodes.as
===================================================================
--- cytoscapeweb/trunk/cytoscapeweb/src/org/cytoscapeweb/util/Nodes.as  
2011-09-27 19:07:33 UTC (rev 26985)
+++ cytoscapeweb/trunk/cytoscapeweb/src/org/cytoscapeweb/util/Nodes.as  
2011-09-27 19:20:51 UTC (rev 26986)
@@ -79,7 +79,7 @@
                     shape: shape,
                     "props.width": width,
                     "props.height": height,
-                    "props.wrapText": wrapText,
+                    "props.autoSize": autoSize,
                     size: size,
                     fillColor: fillColor,
                     lineColor: lineColor, 
@@ -119,7 +119,7 @@
             return h;
         }
         
-        public static function wrapText(n:NodeSprite):Boolean {
+        public static function autoSize(n:NodeSprite):Boolean {
             var size:Number = style.getValue(VisualProperties.NODE_SIZE, 
n.data);
             var wrap:Boolean = size === -1;
             

Modified: 
cytoscapeweb/trunk/cytoscapeweb/src/org/cytoscapeweb/util/VisualProperties.as
===================================================================
--- 
cytoscapeweb/trunk/cytoscapeweb/src/org/cytoscapeweb/util/VisualProperties.as   
    2011-09-27 19:07:33 UTC (rev 26985)
+++ 
cytoscapeweb/trunk/cytoscapeweb/src/org/cytoscapeweb/util/VisualProperties.as   
    2011-09-27 19:20:51 UTC (rev 26986)
@@ -191,7 +191,7 @@
                     }
                 } else if (isNumber(name)) {
                     if (name === NODE_SIZE && (val is String) && 
-                        String(val).toLowerCase() === "wraptext") {
+                        String(val).toLowerCase() === "auto") {
                         val = -1;
                     } else {
                         val = Number(value);
@@ -226,6 +226,9 @@
                 value = name === NODE_COLOR && value < 0 ? "transparent" : 
Utils.rgbColorAsString(uint(value));
             } else if (isNumber(name)) {
                 value = Number(value);
+                
+                if (name === NODE_SIZE && value < 0)
+                    value = "auto";
             } else if (isString(name)) {
                 if (value == null) value = "";
                 else value = value.toString();

Modified: 
cytoscapeweb/trunk/cytoscapeweb/src/org/cytoscapeweb/view/components/GraphView.as
===================================================================
--- 
cytoscapeweb/trunk/cytoscapeweb/src/org/cytoscapeweb/view/components/GraphView.as
   2011-09-27 19:07:33 UTC (rev 26985)
+++ 
cytoscapeweb/trunk/cytoscapeweb/src/org/cytoscapeweb/view/components/GraphView.as
   2011-09-27 19:20:51 UTC (rev 26986)
@@ -142,7 +142,7 @@
             resize();
             
             var par:Parallel = new Parallel();
-            vis.bounds = GraphUtils.calculateGraphDimension(vis.data.nodes, 
name, _style);
+            vis.bounds = GraphUtils.calculateGraphDimension(vis.data.nodes, 
name);
             var t:Transition = vis.applyLayout(layout);
             par.add(t);
           
@@ -406,11 +406,10 @@
                
                private function createVisualization(data:Data, 
layoutName:String):GraphVis {
                    vis = new GraphVis(data, _config);
-                   var b:Rectangle = 
GraphUtils.calculateGraphDimension(data.nodes, layoutName, _style);
-            vis.bounds = b;
-
             addChild(vis);
+            
             vis.refreshVisualProperties(_style);
+            vis.bounds = GraphUtils.calculateGraphDimension(data.nodes, 
layoutName);
             
             return vis;
                }

Modified: 
cytoscapeweb/trunk/cytoscapeweb/src/org/cytoscapeweb/view/components/GraphVis.as
===================================================================
--- 
cytoscapeweb/trunk/cytoscapeweb/src/org/cytoscapeweb/view/components/GraphVis.as
    2011-09-27 19:07:33 UTC (rev 26985)
+++ 
cytoscapeweb/trunk/cytoscapeweb/src/org/cytoscapeweb/view/components/GraphVis.as
    2011-09-27 19:20:51 UTC (rev 26986)
@@ -225,7 +225,6 @@
                 if (_config.nodeLabelsVisible) updateLabels(Data.NODES);
                 if (_config.edgeLabelsVisible) updateLabels(Data.EDGES);
             }
-
             // Tooltips:
             // ---------------------------------------------------------
             tooltipControl.showDelay = 
_style.getValue(VisualProperties.TOOLTIP_DELAY) as Number;
@@ -274,7 +273,7 @@
                         var d:Data = _dataList[i];
                         
                         if (d.nodes.length > 1) {
-                            var rect:Rectangle = 
GraphUtils.calculateGraphDimension(d.nodes, _layoutName, _style); 
+                            var rect:Rectangle = 
GraphUtils.calculateGraphDimension(d.nodes, _layoutName); 
                             var root:NodeSprite = Layouts.rootNode(d);
                             
                             layout = createLayout(layoutObj, d, rect, root);
@@ -360,6 +359,7 @@
             data.visit(function(ds:DataSprite):Boolean {
                 var lb:TextSprite = labels.getValue(ds);
                 if (lb != null) lb.visible = visible && ds.visible;
+                if (ds is NodeSprite && ds.props.autoSize) ds.dirty();
                 return false;
             }, group);
         }

Modified: 
cytoscapeweb/trunk/cytoscapeweb/src/org/cytoscapeweb/view/render/Labeler.as
===================================================================
--- cytoscapeweb/trunk/cytoscapeweb/src/org/cytoscapeweb/view/render/Labeler.as 
2011-09-27 19:07:33 UTC (rev 26985)
+++ cytoscapeweb/trunk/cytoscapeweb/src/org/cytoscapeweb/view/render/Labeler.as 
2011-09-27 19:20:51 UTC (rev 26986)
@@ -45,6 +45,7 @@
     import flash.text.TextFormat;
     import flash.text.TextFormatAlign;
     
+    import org.cytoscapeweb.util.NodeShapes;
     import org.cytoscapeweb.util.Utils;
     import org.cytoscapeweb.util.methods.$each;
     
@@ -210,7 +211,10 @@
                 if      (label.verticalAnchor == TextSprite.TOP)     myYOffset 
+= d.height/2;
                 else if (label.verticalAnchor == TextSprite.BOTTOM)  myYOffset 
-= d.height/2;
                 
-                if (d.props.wrapText) d.dirty();
+                if (d.props.autoSize) {
+                    d.render();
+                    if (d.shape == NodeShapes.TRIANGLE) myYOffset += 
d.height/4;
+                }
             }
             
             label.x = x + myXOffset;

Modified: 
cytoscapeweb/trunk/cytoscapeweb/src/org/cytoscapeweb/view/render/NodeRenderer.as
===================================================================
--- 
cytoscapeweb/trunk/cytoscapeweb/src/org/cytoscapeweb/view/render/NodeRenderer.as
    2011-09-27 19:07:33 UTC (rev 26985)
+++ 
cytoscapeweb/trunk/cytoscapeweb/src/org/cytoscapeweb/view/render/NodeRenderer.as
    2011-09-27 19:20:51 UTC (rev 26986)
@@ -55,7 +55,7 @@
 
     public class NodeRenderer extends ShapeRenderer {
        
-       private static const WRAP_PAD:Number = 10;
+       private static const WRAP_PAD:Number = 5;
        
         private static var _instance:NodeRenderer = new NodeRenderer();
         public static function get instance():NodeRenderer { return _instance; 
}
@@ -102,14 +102,33 @@
                 var w:Number = d.props.width;
                 var h:Number = d.props.height;
                 
-                if (d.props.wrapText) {
-                    w = h = WRAP_PAD;
+                if (d.props.autoSize) {
+                    var lbl:TextSprite = d.props.label;
+                    var hf:Number = 1, wf:Number = 1;
                     
-                    if (d.props.label) {
-                        var lbl:TextSprite = d.props.label;
-                        w += isNaN(lbl.width) ? 0 : lbl.width;
-                        h += isNaN(lbl.height) ? 0 : lbl.height;
+                    if (lbl != null && lbl.visible) {
+                        w = isNaN(lbl.width) ? 0 : lbl.width;
+                        h = isNaN(lbl.height) ? 0 : lbl.height;
+                        
+                        // TODO: it is just an approximation--calculate more 
size accurately
+                        switch (d.shape) {
+                            case NodeShapes.TRIANGLE:      hf = wf = 2.0; 
break;
+                            case NodeShapes.V:             hf = wf = 3.2; 
break;
+                            case NodeShapes.OCTAGON:       hf = wf = 1.4; 
break;
+                            case NodeShapes.HEXAGON:       hf = wf = 1.6; 
break;
+                            case NodeShapes.PARALLELOGRAM: wf = 2.6;      
break;
+                            case NodeShapes.DIAMOND:       hf = wf = 2.0; 
break;
+                            case NodeShapes.ELLIPSE:       hf = wf = 1.4; 
break;
+                        }
+                        
+                        w *= wf;
+                        h *= hf;
+                    } else {
+                        w = h = 3 * WRAP_PAD;
                     }
+                    
+                    w += 2 * WRAP_PAD;
+                    h += 2 * WRAP_PAD;
                 } else {
                     if (isNaN(w) || w < 0) w = size;
                     if (isNaN(h) || h < 0) h = size;

-- 
You received this message because you are subscribed to the Google Groups 
"cytoscape-cvs" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/cytoscape-cvs?hl=en.

Reply via email to