001: /*
002: JOpenChart Java Charting Library and Toolkit
003: Copyright (C) 2001 Sebastian Müller
004: http://jopenchart.sourceforge.net
005:
006: This library is free software; you can redistribute it and/or
007: modify it under the terms of the GNU Lesser General Public
008: License as published by the Free Software Foundation; either
009: version 2.1 of the License, or (at your option) any later version.
010:
011: This library is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public
017: License along with this library; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019:
020: RowColorModel.java
021: Created on 28. August 2001, 20:02
022: */
023:
024: package de.progra.charting.render;
025:
026: import java.awt.Color;
027: import de.progra.charting.model.ChartDataModel;
028: import java.util.HashMap;
029: import java.awt.geom.RectangularShape;
030: import java.awt.geom.Ellipse2D;
031: import java.awt.geom.Rectangle2D;
032: import de.progra.charting.render.shape.Diamond2D;
033: import de.progra.charting.render.shape.Triangle2D;
034:
035: /**
036: * This class implements the correspondence between the DataSets and the
037: * colors used for rendering the charts and the legend.
038: * @author mueller
039: * @version 1.0
040: */
041: public class RowColorModel {
042:
043: private static RowColorModel instance;
044:
045: protected ChartDataModel model;
046:
047: protected final static Color[] predefinedColors = { Color.blue,
048: Color.cyan, Color.red, Color.pink, Color.yellow,
049: Color.green, Color.magenta, Color.orange, Color.darkGray,
050: Color.gray, Color.lightGray };
051:
052: public static final Ellipse2D ELLIPSE_SHAPE = new Ellipse2D.Float(
053: 0f, 0f, 5f, 5f);
054: public static final Rectangle2D SQUARE_SHAPE = new Rectangle2D.Float(
055: 0f, 0f, 5f, 5f);
056: public static final Diamond2D DIAMOND_SHAPE = new Diamond2D(0f, 0f,
057: 5f, 5f);
058: public static final Triangle2D TRIANGLE_SHAPE = new Triangle2D(0f,
059: 0f, 5f, 5f, false);
060: public static final Triangle2D TRIANGLEDOWN_SHAPE = new Triangle2D(
061: 0f, 0f, 5f, 5f, true);
062:
063: protected final static RectangularShape[] predefinedShapes = {
064: ELLIPSE_SHAPE, SQUARE_SHAPE, DIAMOND_SHAPE, TRIANGLE_SHAPE,
065: TRIANGLEDOWN_SHAPE };
066:
067: protected int predefinedColorsIdx = 0;
068:
069: protected HashMap customColors = new HashMap();
070:
071: protected HashMap customShapes = new HashMap();
072:
073: /** Creates new RowColorModel.
074: * @param model the ChartDataModel which contains the information about all the DataSets
075: */
076: public RowColorModel(ChartDataModel model) {
077: this .model = model;
078: }
079:
080: /** Use this method to get an instance of the chart's RowColorModel.
081: * @param model the ChartDataModel whose data sets will be mapped to
082: * colors.
083: * @return a new instance of RowColorModel if there's no instance
084: * of if the model has changed (esp. useful if you create multiple charts
085: * after one another).
086: * @deprecated
087: */
088: public static RowColorModel getInstance(ChartDataModel model) {
089: if (instance == null || !model.equals(instance.model))
090: instance = new RowColorModel(model);
091:
092: return instance;
093: }
094:
095: /** Computes the amount of all Legend entries, ie. DataSets.
096: * @return the amount of all rows, ie. DataSets.
097: */
098: public int getRowCount() {
099: return model.getDataSetNumber();
100: }
101:
102: /** Returns the row title of a specific DataSet.
103: * @param i the DataSet index
104: * @return the String title
105: */
106: public String getRow(int i) {
107: return model.getDataSetName(i);
108: }
109:
110: /** 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.
111: * @param row the row for which the Color should be returned
112: * @return the Color stored for the given row.
113: */
114: public Color getColor(int row) {
115:
116: // get the custom color
117: Color c = (Color) customColors.get(new Integer(row));
118:
119: // if no custom color
120: if (c == null) {
121: // see if there is a predefined color for this row
122: if (predefinedColorsIdx < predefinedColors.length) {
123: c = predefinedColors[predefinedColorsIdx++];
124: } else {
125: c = new Color((float) Math.random(), (float) Math
126: .random(), (float) Math.random());
127: }
128:
129: // remember this
130: customColors.put(new Integer(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 getShape(int row) {
145:
146: // get the custom color
147: RectangularShape c = (RectangularShape) customShapes
148: .get(new Integer(row));
149:
150: // if no custom color
151: if (c == null) {
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(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 color) {
168: customColors.put(new Integer(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 shape) {
176: customShapes.put(new Integer(row), shape);
177: }
178: }
|