Author: scooter
Date: 2010-09-01 17:37:22 -0700 (Wed, 01 Sep 2010)
New Revision: 21664

Modified:
   
csplugins/trunk/ucsf/scooter/nodeCharts/src/main/java/nodeCharts/command/NodeChartCommandHandler.java
   
csplugins/trunk/ucsf/scooter/nodeCharts/src/main/java/nodeCharts/view/NodeChartViewer.java
   
csplugins/trunk/ucsf/scooter/nodeCharts/src/main/java/nodeCharts/view/PieChart.java
   
csplugins/trunk/ucsf/scooter/nodeCharts/src/main/java/nodeCharts/view/ValueUtils.java
   
csplugins/trunk/ucsf/scooter/nodeCharts/src/main/java/nodeCharts/view/ViewUtils.java
   csplugins/trunk/ucsf/scooter/nodeCharts/test.com
Log:
Now have colors and offsets working.  Still need to add color name lookups and
nodelist and attributelist support


Modified: 
csplugins/trunk/ucsf/scooter/nodeCharts/src/main/java/nodeCharts/command/NodeChartCommandHandler.java
===================================================================
--- 
csplugins/trunk/ucsf/scooter/nodeCharts/src/main/java/nodeCharts/command/NodeChartCommandHandler.java
       2010-09-02 00:15:38 UTC (rev 21663)
+++ 
csplugins/trunk/ucsf/scooter/nodeCharts/src/main/java/nodeCharts/command/NodeChartCommandHandler.java
       2010-09-02 00:37:22 UTC (rev 21664)
@@ -60,9 +60,13 @@
  */
 public class NodeChartCommandHandler extends AbstractCommandHandler {
        CyLogger logger;
-       private static String NODE = "node";
        Map<String, NodeChartViewer> viewerMap;
 
+       // Global commands
+       private static String NODE = "node";
+       private static String POSITION = "position";
+
+
        public NodeChartCommandHandler(String namespace, CyLogger logger) {
                super(CyCommandManager.reserveNamespace(namespace));
 
@@ -92,11 +96,19 @@
                if (node == null)
                        throw new CyCommandException("can't find node: 
'"+nodeName+"'");
 
+               Object pos = null;
+               if (args.containsKey(POSITION)) {
+                       String position = (String) args.get(POSITION);
+                       pos = ViewUtils.getPosition(position);
+                       if (pos == null)
+                               throw new CyCommandException("unknown position 
keyword or illegal position expression: "+position);
+               }
+
                NodeChartViewer viewer = viewerMap.get(command);
 
                CyNetworkView view = Cytoscape.getCurrentNetworkView();
 
-               ViewUtils.addCustomGraphics(viewer.getCustomGraphics(args, 
node, view), node, view);
+               ViewUtils.addCustomGraphics(viewer.getCustomGraphics(args, 
node, view, pos), node, view);
 
                return result;
        }

Modified: 
csplugins/trunk/ucsf/scooter/nodeCharts/src/main/java/nodeCharts/view/NodeChartViewer.java
===================================================================
--- 
csplugins/trunk/ucsf/scooter/nodeCharts/src/main/java/nodeCharts/view/NodeChartViewer.java
  2010-09-02 00:15:38 UTC (rev 21663)
+++ 
csplugins/trunk/ucsf/scooter/nodeCharts/src/main/java/nodeCharts/view/NodeChartViewer.java
  2010-09-02 00:37:22 UTC (rev 21664)
@@ -54,7 +54,7 @@
 
        public Map<String,String> getOptions();
 
-       public List<CustomGraphic> getCustomGraphics(Map<String,Object>args, 
CyNode node, CyNetworkView view) 
+       public List<CustomGraphic> getCustomGraphics(Map<String,Object>args, 
CyNode node, CyNetworkView view, Object position) 
                                                                                
     throws CyCommandException;
 
 }

Modified: 
csplugins/trunk/ucsf/scooter/nodeCharts/src/main/java/nodeCharts/view/PieChart.java
===================================================================
--- 
csplugins/trunk/ucsf/scooter/nodeCharts/src/main/java/nodeCharts/view/PieChart.java
 2010-09-02 00:15:38 UTC (rev 21663)
+++ 
csplugins/trunk/ucsf/scooter/nodeCharts/src/main/java/nodeCharts/view/PieChart.java
 2010-09-02 00:37:22 UTC (rev 21664)
@@ -35,11 +35,9 @@
 // System imports
 import java.util.ArrayList;
 import java.util.Arrays;
-import java.util.Calendar;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-import java.util.Random;
 
 import java.awt.Color;
 import java.awt.Paint;
@@ -81,7 +79,7 @@
                return options;
        }
 
-       public List<CustomGraphic> getCustomGraphics(Map<String, Object>args, 
CyNode node, CyNetworkView view) 
+       public List<CustomGraphic> getCustomGraphics(Map<String, Object>args, 
CyNode node, CyNetworkView view, Object position) 
                                                                                
       throws CyCommandException {
                // First, get our data values
                if (!args.containsKey(VALUES))
@@ -97,11 +95,7 @@
                List<String> labels = getLabels((String)args.get(LABELS));
 
                // Get our colors
-               List<Color> colors;
-               if (args.containsKey(COLORS))
-                       colors = convertColor((String)args.get(COLORS));
-               else
-                       colors = generateColors(values.size());
+               List<Color> colors = 
ValueUtils.convertInputToColor(args.get(COLORS), labels.size());
 
                // Sanity check
                if (labels.size() != values.size() ||
@@ -113,7 +107,7 @@
                List<CustomGraphic> cgList = new ArrayList<CustomGraphic>();
 
                // We need to get our bounding box in order to scale our 
graphic properly
-               Rectangle2D bbox = ViewUtils.getNodeBoundingBox(node, view);
+               Rectangle2D bbox = ViewUtils.getNodeBoundingBox(node, view, 
position);
 
                for (int slice = 0; slice < nSlices; slice++) {
                        CustomGraphic cg = createSlice(bbox, arcStart, 
labels.get(slice), values.get(slice), colors.get(slice));
@@ -143,26 +137,6 @@
                return values;
        }
 
-       private List<Color> convertColor(String input) {
-               String[] inputArray = input.split(",");
-               return null;
-       }
-
-       private List<Color> generateColors(int nColors) {
-               Calendar cal = Calendar.getInstance();
-               int seed = cal.get(Calendar.SECOND);
-               Random rand = new Random(seed);
-
-               List<Color> result = new ArrayList<Color>(nColors);
-               for (int index = 0; index < nColors; index++) {
-                       int r = rand.nextInt(255);
-                       int g = rand.nextInt(255);
-                       int b = rand.nextInt(255);
-                       result.add(index, new Color(r,g,b,200));
-               }
-               return result;
-       }
-
        private List<String> getLabels(String input) {
                String[] inputArray = input.split(",");
                return Arrays.asList(inputArray);

Modified: 
csplugins/trunk/ucsf/scooter/nodeCharts/src/main/java/nodeCharts/view/ValueUtils.java
===================================================================
--- 
csplugins/trunk/ucsf/scooter/nodeCharts/src/main/java/nodeCharts/view/ValueUtils.java
       2010-09-02 00:15:38 UTC (rev 21663)
+++ 
csplugins/trunk/ucsf/scooter/nodeCharts/src/main/java/nodeCharts/view/ValueUtils.java
       2010-09-02 00:37:22 UTC (rev 21664)
@@ -35,8 +35,14 @@
 // System imports
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Calendar;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
+import java.util.Random;
 
+import java.awt.Color;
+
 // Cytoscape imports
 import cytoscape.command.CyCommandException;
 
@@ -91,4 +97,161 @@
                String[] inputArray = input.split(",");
                return convertStringList(Arrays.asList(inputArray));
        }
+
+       public enum ColorKeyword {
+               RANDOM ("random"),
+               CONTRASTING ("contrasting"),
+               RAINBOW ("rainbow"),
+               MODULATED ("modulated");
+       
+               private String label;
+               private static Map<String, ColorKeyword>cMap;
+       
+               ColorKeyword(String label) { 
+                       this.label = label; 
+                       addKeyword(this);
+               }
+       
+               public String getLabel() {
+                       return label;
+               }
+
+               public String toString() {
+                       return label;
+               }
+       
+               private void addKeyword(ColorKeyword col) {
+                       if (cMap == null) cMap = new 
HashMap<String,ColorKeyword>();
+                       cMap.put(col.getLabel(), col);
+               }
+       
+               static ColorKeyword getColorKeyword(String label) {
+                       if (cMap.containsKey(label))
+                               return cMap.get(label);
+                       return null;
+               }
+       }
+
+       public static List<Color> convertInputToColor(Object input, int 
nColors) throws CyCommandException {
+               if (input == null) {
+                       // give the default: contrasting colors
+                       return generateContrastingColors(nColors);
+               }
+
+               // OK, we have three posibilities.  The input could be a 
keyword, a comma-separated list of colors, or
+               // a list of Color objects.  We need to figure this out first...
+               if (input instanceof List) {
+                       if ( ((List<Object>)input).get(0) instanceof Color) 
+                               return (List<Color>)input;
+                       else 
+                               throw new CyCommandException("color list not of 
type Color?");
+               } else if (input instanceof String) {
+                       String inputString = (String) input;
+                       // See if we have a csv
+                       String [] colorArray = inputString.split(",");
+                       if (colorArray.length > 1)
+                               return parseColorList(colorArray);
+                       else
+                               return parseColorKeyword(inputString.trim(), 
nColors);
+               } else 
+                       throw new CyCommandException("unknown type for color 
list");
+       }
+
+       private static List<Color> parseColorKeyword(String input, int nColors) 
throws CyCommandException {
+               ColorKeyword ckey = ColorKeyword.getColorKeyword(input);
+               if (ckey == null) 
+                       throw new CyCommandException("unknown color keyword");
+               switch (ckey) {
+               case RANDOM:
+                       return generateRandomColors(nColors);
+               case RAINBOW:
+                       return generateRainbowColors(nColors);
+               case MODULATED:
+                       return generateModulatedRainbowColors(nColors);
+               case CONTRASTING:
+               default:
+                       return generateContrastingColors(nColors);
+               }
+       }
+
+       private static List<Color> parseColorList(String[] inputArray) throws 
CyCommandException {
+               List<Color> colors = new ArrayList<Color>();
+               // A color in the array can either be a hex value or a text 
color
+               for (String colorString: inputArray) {
+                       colorString = colorString.trim();
+                       if 
(colorString.matches("^#([A-Fa-f0-9]{8}|[A-Fa-f0-9]{6})$")) {
+                               // We have a hex value with either 6 (rgb) or 8 
(rgba) digits
+                               int r = 
Integer.parseInt(colorString.substring(1,3), 16);
+                               int g = 
Integer.parseInt(colorString.substring(3,5), 16);
+                               int b = 
Integer.parseInt(colorString.substring(5,7), 16);
+                               if (colorString.length() > 7) {
+                                       int a = 
Integer.parseInt(colorString.substring(7,9), 16);
+                                       colors.add(new Color(r,g,b,a));
+                               } else {
+                                       colors.add(new Color(r,g,b));
+                               }
+                       } else {
+                               // Check for color string
+                       }
+               }
+               return colors;
+       }
+
+       private static List<Color> generateRandomColors(int nColors) {
+               // System.out.println("Generating random colors");
+               Calendar cal = Calendar.getInstance();
+               int seed = cal.get(Calendar.SECOND);
+               Random rand = new Random(seed);
+
+               List<Color> result = new ArrayList<Color>(nColors);
+               for (int index = 0; index < nColors; index++) {
+                       int r = rand.nextInt(255);
+                       int g = rand.nextInt(255);
+                       int b = rand.nextInt(255);
+                       result.add(index, new Color(r,g,b,200));
+               }
+               return result;
+       }
+
+       // Rainbow colors just divide the Hue wheel into n pieces and return 
them
+       private static List<Color> generateRainbowColors(int nColors) {
+               // System.out.println("Generating rainbow colors");
+               List<Color> values = new ArrayList<Color>();
+               for (float i = 0.0f; i < (float)nColors; i += 1.0f) {
+                       values.add(new Color(Color.HSBtoRGB(i/(float)nColors, 
1.0f, 1.0f)));
+               }
+               return values;
+       }
+
+       // Rainbow colors just divide the Hue wheel into n pieces and return 
them, but
+       // in this case, we're going to change the saturation and intensity
+       private static List<Color> generateModulatedRainbowColors(int nColors) {
+               // System.out.println("Generating modulated colors");
+               List<Color> values = new ArrayList<Color>();
+               for (float i = 0.0f; i < (float)nColors; i += 1.0f) {
+                       float sat = (Math.abs(((Number) Math.cos((8 * i) / (2 * 
Math.PI))).floatValue()) * 0.7f) 
+                                    + 0.3f;
+                       float br = (Math.abs(((Number) Math.sin(((i) / (2 * 
Math.PI)) + (Math.PI / 2)))
+                                             .floatValue()) * 0.7f) + 0.3f;
+
+                       // 
System.out.println("Color("+(i/(float)nColors)+","+sat+","+br+")");
+                       values.add(new Color(Color.HSBtoRGB(i/(float)nColors, 
sat, br)));
+               }
+               return values;
+       }
+
+       // This is like rainbow, but we alternate sides of the color wheel
+       private static List<Color> generateContrastingColors(int nColors) {
+               // System.out.println("Generating contrasting colors");
+               List<Color> values = new ArrayList<Color>();
+               float divs = ((float)nColors)/2.0f;
+               for (float i = 0.0f; i < divs; i += 1.0f) {
+                       // 
System.out.println("Color("+(i/divs)+","+1.0f+","+1.0f+")");
+                       values.add(new Color(Color.HSBtoRGB(i/divs, 1.0f, 
1.0f)));
+                       // 
System.out.println("Color("+((i/divs)+0.5f)+","+1.0f+","+1.0f+")");
+                       values.add(new Color(Color.HSBtoRGB((i/divs)+0.5f, 
1.0f, 1.0f)));
+               }
+               return values;
+       }
+
 }

Modified: 
csplugins/trunk/ucsf/scooter/nodeCharts/src/main/java/nodeCharts/view/ViewUtils.java
===================================================================
--- 
csplugins/trunk/ucsf/scooter/nodeCharts/src/main/java/nodeCharts/view/ViewUtils.java
        2010-09-02 00:15:38 UTC (rev 21663)
+++ 
csplugins/trunk/ucsf/scooter/nodeCharts/src/main/java/nodeCharts/view/ViewUtils.java
        2010-09-02 00:37:22 UTC (rev 21664)
@@ -32,9 +32,11 @@
  */
 package nodeCharts.view;
 
+import java.awt.geom.Point2D;
 import java.awt.geom.Rectangle2D;
 
 // System imports
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
@@ -47,10 +49,52 @@
 import ding.view.DNodeView;
 import giny.view.NodeView;
 
+
 /**
  * The NodeChartViewer creates the actual custom graphics
  */
 public class ViewUtils {
+
+       public enum Position {
+               CENTER ("center"),
+               EAST ("east"),
+               NORTH ("north"),
+               NORTHEAST ("northeast"),
+               NORTHWEST ("northwest"),
+               SOUTH ("south"),
+               SOUTHEAST ("southeast"),
+               SOUTHWEST ("southwest"),
+               WEST ("west");
+       
+               private String label;
+               private static Map<String, Position>pMap;
+       
+               Position(String label) { 
+                       this.label = label; 
+                       addPosition(this);
+               }
+       
+               public String getLabel() {
+                       return label;
+               }
+
+               public String toString() {
+                       return label;
+               }
+       
+               private void addPosition(Position pos) {
+                       if (pMap == null) pMap = new HashMap<String,Position>();
+                       pMap.put(pos.getLabel(), pos);
+               }
+       
+               static Position getPosition(String label) {
+                       if (pMap.containsKey(label))
+                               return pMap.get(label);
+                       return null;
+               }
+       }
+
+
        public static void addCustomGraphics(List<CustomGraphic> cgList, CyNode 
node, CyNetworkView view) {
                // Get the DNodeView
                NodeView nView = view.getNodeView(node);
@@ -59,7 +103,7 @@
                }
        }
 
-       public static Rectangle2D getNodeBoundingBox(CyNode node, CyNetworkView 
view) {
+       public static Rectangle2D getNodeBoundingBox(CyNode node, CyNetworkView 
view, Object position) {
                DNodeView nView = (DNodeView)view.getNodeView(node);
 
                // Get the affine transform 
@@ -67,6 +111,88 @@
                double width = (nView.getWidth()-nView.getBorderWidth())*0.90;
 
                // Create the bounding box.
-               return new Rectangle2D.Double(-width/2, -height/2, width, 
height);
+               Rectangle2D.Double bbox = new Rectangle2D.Double(-width/2, 
-height/2, width, height);
+               return positionAdjust(bbox, position);
        }
+
+       /**
+        * getPosition will return either a Point2D or a Position, depending on 
whether
+        * the user provided us with a position keyword or a specific value.
+        *
+        * @param position the position argument
+        * @return a Point2D representing the X,Y offset specified by the user 
or a Position
+        * enum that corresponds to the provided keyword.  <b>null</b> is 
returned if the input
+        * is illegal.
+        */
+       public static Object getPosition(String position) {
+               Position pos = Position.getPosition(position);
+               if (pos != null) 
+                       return pos;
+
+               String [] xy = position.split(",");
+               if (xy.length != 2) {
+                       return null;
+               }
+
+               try {
+                       Double x = Double.valueOf(xy[0]);
+                       Double y = Double.valueOf(xy[1]);
+                       return new Point2D.Double(x.doubleValue(), 
y.doubleValue());
+               } catch (NumberFormatException e) {
+                       return null;
+               }
+       }
+
+       private static Rectangle2D positionAdjust(Rectangle2D.Double bbox, 
Object pos) {
+               if (pos == null)
+                       return bbox;
+
+               double height = bbox.getHeight();
+               double width = bbox.getWidth();
+               double x = bbox.getX();
+               double y = bbox.getY();
+
+               if (pos instanceof Position) {
+                       Position p = (Position) pos;
+
+                       switch (p) {
+                       case EAST:
+                               x = width/2;
+                               break;
+                       case WEST:
+                               x = -width*1.5;
+                               break;
+                       case NORTH:
+                               y = -height*1.5;
+                               break;
+                       case SOUTH:
+                               y = height/2;
+                               break;
+                       case NORTHEAST:
+                               x = width/2;
+                               y = -height*1.5;
+                               break;
+                       case NORTHWEST:
+                               x = -width*1.5;
+                               y = -height*1.5;
+                               break;
+                       case SOUTHEAST:
+                               x = width/2;
+                               y = height/2;
+                               break;
+                       case SOUTHWEST:
+                               x = -width*1.5;
+                               y = height/2;
+                               break;
+                       case CENTER:
+                       default:
+                       }
+               } else if (pos instanceof Point2D.Double) {
+                       x += ((Point2D.Double)pos).getX();
+                       y += ((Point2D.Double)pos).getY();
+               }
+
+               return new Rectangle2D.Double(x,y,width,height);
+       }
+
 }

Modified: csplugins/trunk/ucsf/scooter/nodeCharts/test.com
===================================================================
--- csplugins/trunk/ucsf/scooter/nodeCharts/test.com    2010-09-02 00:15:38 UTC 
(rev 21663)
+++ csplugins/trunk/ucsf/scooter/nodeCharts/test.com    2010-09-02 00:37:22 UTC 
(rev 21664)
@@ -1,3 +1,13 @@
-nodecharts pie node="YCL067C" labellist="A,B,C,D" valuelist="1.0,2.0,0.5,5"
+nodecharts pie node="YCL067C" labellist="A,B,C,D" valuelist="1.0,2.0,0.5,5" 
colorlist="#FF0000,#00FF00,#0000FF,#0000FF80"
 nodecharts pie node="YKL101W" labellist="A,B,C,D,E,F" 
valuelist="3.0,2.0,0.5,5,1,1.5"
+nodecharts pie node="YHR084W" labellist="A,B,C,D,E,F" 
valuelist="3.0,2.0,0.5,5,1,1.5" position="north" colorlist="random"
+nodecharts pie node="YDR461W" labellist="A,B,C,D,E,F" 
valuelist="3.0,2.0,0.5,5,1,1.5" position="south" colorlist="rainbow"
+nodecharts pie node="YFL026W" labellist="A,B,C,D,E,F" 
valuelist="3.0,2.0,0.5,5,1,1.5" position="east" colorlist="modulated"
+nodecharts pie node="YGL008C" labellist="A,B,C,D,E,F" 
valuelist="3.0,2.0,0.5,5,1,1.5" position="west" colorlist="contrasting"
+nodecharts pie node="YBR112C" labellist="A,B,C,D,E,F" 
valuelist="3.0,2.0,0.5,5,1,1.5" position="northwest"
+nodecharts pie node="YCR084C" labellist="A,B,C,D,E,F" 
valuelist="3.0,2.0,0.5,5,1,1.5" position="southwest"
+nodecharts pie node="YNL145W" labellist="A,B,C,D,E,F" 
valuelist="3.0,2.0,0.5,5,1,1.5" position="northeast"
+nodecharts pie node="YIL015W" labellist="A,B,C,D,E,F" 
valuelist="3.0,2.0,0.5,5,1,1.5" position="southeast"
+nodecharts pie node="YBL069W" labellist="A,B,C,D,E,F" 
valuelist="3.0,2.0,0.5,5,1,1.5" position="10,10"
+
 network view update

-- 
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