KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > progra > charting > render > RowColorModel


1 /*
2     JOpenChart Java Charting Library and Toolkit
3     Copyright (C) 2001 Sebastian Müller
4     http://jopenchart.sourceforge.net
5
6     This library is free software; you can redistribute it and/or
7     modify it under the terms of the GNU Lesser General Public
8     License as published by the Free Software Foundation; either
9     version 2.1 of the License, or (at your option) any later version.
10
11     This library is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14     Lesser General Public License for more details.
15
16     You should have received a copy of the GNU Lesser General Public
17     License along with this library; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
20     RowColorModel.java
21     Created on 28. August 2001, 20:02
22 */

23
24 package de.progra.charting.render;
25
26 import java.awt.Color JavaDoc;
27 import de.progra.charting.model.ChartDataModel;
28 import java.util.HashMap JavaDoc;
29 import java.awt.geom.RectangularShape JavaDoc;
30 import java.awt.geom.Ellipse2D JavaDoc;
31 import java.awt.geom.Rectangle2D JavaDoc;
32 import de.progra.charting.render.shape.Diamond2D;
33 import de.progra.charting.render.shape.Triangle2D;
34
35 /**
36  * This class implements the correspondence between the DataSets and the
37  * colors used for rendering the charts and the legend.
38  * @author mueller
39  * @version 1.0
40  */

41 public class RowColorModel {
42
43     private static RowColorModel instance;
44     
45     protected ChartDataModel model;
46     
47     protected final static Color JavaDoc[] 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 JavaDoc ELLIPSE_SHAPE = new Ellipse2D.Float JavaDoc(0f, 0f, 5f, 5f);
53     public static final Rectangle2D JavaDoc SQUARE_SHAPE = new Rectangle2D.Float JavaDoc(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 JavaDoc[] 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 JavaDoc customColors = new HashMap JavaDoc();
67     
68     protected HashMap JavaDoc customShapes = new HashMap JavaDoc();
69     
70     /** Creates new RowColorModel.
71      * @param model the ChartDataModel which contains the information about all the DataSets
72      */

73     public RowColorModel(ChartDataModel model) {
74         this.model = model;
75     }
76     
77     /** Use this method to get an instance of the chart's RowColorModel.
78      * @param model the ChartDataModel whose data sets will be mapped to
79      * colors.
80      * @return a new instance of RowColorModel if there's no instance
81      * of if the model has changed (esp. useful if you create multiple charts
82      * after one another).
83      * @deprecated
84      */

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     /** Computes the amount of all Legend entries, ie. DataSets.
93      * @return the amount of all rows, ie. DataSets.
94      */

95     public int getRowCount() {
96         return model.getDataSetNumber();
97     }
98     
99     /** Returns the row title of a specific DataSet.
100      * @param i the DataSet index
101      * @return the String title
102      */

103     public String JavaDoc getRow(int i) {
104         return model.getDataSetName(i);
105     }
106
107     /** Computes the Color for a DataSet. For the first DataSets the stored Colors like <CODE>Color.red</CODE> etc are used. If there are more DataSets than stored colors, random colors are used.
108      * @param row the row for which the Color should be returned
109      * @return the Color stored for the given row.
110      */

111     public Color JavaDoc getColor(int row) {
112         
113         // get the custom color
114
Color JavaDoc c = (Color JavaDoc)customColors.get(new Integer JavaDoc(row));
115         
116         // if no custom color
117
if(c == null)
118         {
119             // see if there is a predefined color for this row
120
if (predefinedColorsIdx < predefinedColors.length) {
121                 c = predefinedColors[predefinedColorsIdx++];
122             }
123             else {
124                 c = new Color JavaDoc((float)Math.random(),
125                                (float)Math.random(),
126                                (float)Math.random());
127             }
128           
129             // remember this
130
customColors.put(new Integer JavaDoc(row), c);
131         }
132         
133         // done
134
return c;
135     }
136     
137     /** Returns the Shape for a DataSet. By default, the Shapes from
138      * the <code>predefinedShapes</code> array are cycled through unless you define
139      * your own shape to data binding using
140      * <code>setShape(int row, RectangularShape shape)</code>.
141      * @param row the row for which the Shape should be returned
142      * @return the Shape stored for the given row.
143      */

144     public RectangularShape JavaDoc getShape(int row) {
145         
146         // get the custom color
147
RectangularShape JavaDoc c = (RectangularShape JavaDoc)customShapes.get(new Integer JavaDoc(row));
148         
149         // if no custom color
150
if(c == null)
151         {
152             // calculate the matching predefined shape by a modulo operation
153
c = predefinedShapes[row % predefinedShapes.length];
154             
155             // remember this
156
customShapes.put(new Integer JavaDoc(row), c);
157         }
158         
159         // done
160
return c;
161     }
162     
163     /** Force a certain color for a row
164      * @param row the row for which the Color should be set
165      * @param color the color that is associated with the row
166      */

167     public void setColor(int row, Color JavaDoc color) {
168         customColors.put(new Integer JavaDoc(row), color);
169     }
170     
171     /** Force a certain Shape for a row
172      * @param row the row for which the Shape should be set
173      * @param shape the RectangularShape that is associated with the row
174      */

175     public void setShape(int row, RectangularShape JavaDoc shape) {
176         customShapes.put(new Integer JavaDoc(row), shape);
177     }
178 }
179
Popular Tags