1 23 24 package de.progra.charting.render; 25 26 import java.awt.Color ; 27 import de.progra.charting.model.ChartDataModel; 28 import java.util.HashMap ; 29 import java.awt.geom.RectangularShape ; 30 import java.awt.geom.Ellipse2D ; 31 import java.awt.geom.Rectangle2D ; 32 import de.progra.charting.render.shape.Diamond2D; 33 import de.progra.charting.render.shape.Triangle2D; 34 35 41 public class RowColorModel { 42 43 private static RowColorModel instance; 44 45 protected ChartDataModel model; 46 47 protected final static Color [] predefinedColors = {Color.blue, Color.cyan, Color.red, 48 Color.pink, Color.yellow, 49 Color.green, Color.magenta, Color.orange, 50 Color.darkGray, Color.gray, Color.lightGray}; 51 52 public static final Ellipse2D ELLIPSE_SHAPE = new Ellipse2D.Float (0f, 0f, 5f, 5f); 53 public static final Rectangle2D SQUARE_SHAPE = new Rectangle2D.Float (0f, 0f, 5f, 5f); 54 public static final Diamond2D DIAMOND_SHAPE = new Diamond2D(0f, 0f, 5f, 5f); 55 public static final Triangle2D TRIANGLE_SHAPE = new Triangle2D(0f, 0f, 5f, 5f, false); 56 public static final Triangle2D TRIANGLEDOWN_SHAPE = new Triangle2D(0f, 0f, 5f, 5f, true); 57 58 protected final static RectangularShape [] predefinedShapes = {ELLIPSE_SHAPE, 59 SQUARE_SHAPE, 60 DIAMOND_SHAPE, 61 TRIANGLE_SHAPE, 62 TRIANGLEDOWN_SHAPE}; 63 64 protected int predefinedColorsIdx = 0; 65 66 protected HashMap customColors = new HashMap (); 67 68 protected HashMap customShapes = new HashMap (); 69 70 73 public RowColorModel(ChartDataModel model) { 74 this.model = model; 75 } 76 77 85 public static RowColorModel getInstance(ChartDataModel model) { 86 if(instance == null || !model.equals(instance.model)) 87 instance = new RowColorModel(model); 88 89 return instance; 90 } 91 92 95 public int getRowCount() { 96 return model.getDataSetNumber(); 97 } 98 99 103 public String getRow(int i) { 104 return model.getDataSetName(i); 105 } 106 107 111 public Color getColor(int row) { 112 113 Color c = (Color )customColors.get(new Integer (row)); 115 116 if(c == null) 118 { 119 if (predefinedColorsIdx < predefinedColors.length) { 121 c = predefinedColors[predefinedColorsIdx++]; 122 } 123 else { 124 c = new Color ((float)Math.random(), 125 (float)Math.random(), 126 (float)Math.random()); 127 } 128 129 customColors.put(new Integer (row), c); 131 } 132 133 return c; 135 } 136 137 144 public RectangularShape getShape(int row) { 145 146 RectangularShape c = (RectangularShape )customShapes.get(new Integer (row)); 148 149 if(c == null) 151 { 152 c = predefinedShapes[row % predefinedShapes.length]; 154 155 customShapes.put(new Integer (row), c); 157 } 158 159 return c; 161 } 162 163 167 public void setColor(int row, Color color) { 168 customColors.put(new Integer (row), color); 169 } 170 171 175 public void setShape(int row, RectangularShape shape) { 176 customShapes.put(new Integer (row), shape); 177 } 178 } 179 | Popular Tags |