KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > renderer > WaferMapRenderer


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this library; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24  * in the United States and other countries.]
25  *
26  * ---------------------
27  * WaferMapRenderer.java
28  * ---------------------
29  * (C) Copyright 2003-2005, by Robert Redburn and Contributors.
30  *
31  * Original Author: Robert Redburn;
32  * Contributor(s): David Gilbert (for Object Refinery Limited);
33  *
34  * $Id: WaferMapRenderer.java,v 1.6 2005/05/03 10:09:15 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 25-Nov-2003 : Version 1, contributed by Robert Redburn. Changes have been
39  * made to fit the JFreeChart coding style (DG);
40  * 20-Apr-2005 : Small update for changes to LegendItem class (DG);
41  *
42  */

43
44 package org.jfree.chart.renderer;
45
46 import java.awt.Color JavaDoc;
47 import java.awt.Paint JavaDoc;
48 import java.awt.Shape JavaDoc;
49 import java.awt.Stroke JavaDoc;
50 import java.awt.geom.Rectangle2D JavaDoc;
51 import java.util.HashMap JavaDoc;
52 import java.util.HashSet JavaDoc;
53 import java.util.Iterator JavaDoc;
54 import java.util.Map JavaDoc;
55 import java.util.Set JavaDoc;
56
57 import org.jfree.chart.LegendItem;
58 import org.jfree.chart.LegendItemCollection;
59 import org.jfree.chart.plot.DrawingSupplier;
60 import org.jfree.chart.plot.WaferMapPlot;
61 import org.jfree.data.general.WaferMapDataset;
62
63 /**
64  * A renderer for wafer map plots. Provides color managment facilities.
65  *
66  * @author Robert Redburn.
67  */

68 public class WaferMapRenderer extends AbstractRenderer {
69
70     /** paint index */
71     private Map JavaDoc paintIndex;
72     
73     /** plot */
74     private WaferMapPlot plot;
75     
76     /** paint limit */
77     private int paintLimit;
78     
79     /** default paint limit */
80     private static final int DEFAULT_PAINT_LIMIT = 35;
81     
82     /** default multivalue paint calculation */
83     public static final int POSITION_INDEX = 0;
84     
85     /** The default value index. */
86     public static final int VALUE_INDEX = 1;
87     
88     /** paint index method */
89     private int paintIndexMethod;
90     
91     /**
92      * Creates a new renderer.
93      */

94     public WaferMapRenderer() {
95         this(null, null);
96     }
97     
98     /**
99      * Creates a new renderer.
100      *
101      * @param paintLimit the paint limit.
102      * @param paintIndexMethod the paint index method.
103      */

104     public WaferMapRenderer(int paintLimit, int paintIndexMethod) {
105         this(new Integer JavaDoc(paintLimit), new Integer JavaDoc(paintIndexMethod));
106     }
107     
108     /**
109      * Creates a new renderer.
110      *
111      * @param paintLimit the paint limit.
112      * @param paintIndexMethod the paint index method.
113      */

114     public WaferMapRenderer(Integer JavaDoc paintLimit, Integer JavaDoc paintIndexMethod) {
115         
116         super();
117         this.paintIndex = new HashMap JavaDoc();
118         
119         if (paintLimit == null) {
120             this.paintLimit = DEFAULT_PAINT_LIMIT;
121         }
122         else {
123             this.paintLimit = paintLimit.intValue();
124         }
125         
126         this.paintIndexMethod = VALUE_INDEX;
127         if (paintIndexMethod != null) {
128             if (isMethodValid(paintIndexMethod.intValue())) {
129                 this.paintIndexMethod = paintIndexMethod.intValue();
130             }
131         }
132     }
133
134     /**
135      * Verifies that the passed paint index method is valid.
136      *
137      * @param method the method.
138      *
139      * @return <code>true</code> or </code>false</code>.
140      */

141     private boolean isMethodValid(int method) {
142         switch (method) {
143             case POSITION_INDEX: return true;
144             case VALUE_INDEX: return true;
145             default: return false;
146         }
147     }
148
149     /**
150      * Returns the drawing supplier from the plot.
151      *
152      * @return The drawing supplier.
153      */

154     public DrawingSupplier getDrawingSupplier() {
155         DrawingSupplier result = null;
156         WaferMapPlot p = getPlot();
157         if (p != null) {
158             result = p.getDrawingSupplier();
159         }
160         return result;
161     }
162
163     /**
164      * Returns the plot.
165      *
166      * @return The plot.
167      */

168     public WaferMapPlot getPlot() {
169         return this.plot;
170     }
171
172     /**
173      * Sets the plot and build the paint index.
174      *
175      * @param plot the plot.
176      */

177     public void setPlot(WaferMapPlot plot) {
178         this.plot = plot;
179         makePaintIndex();
180     }
181     
182     /**
183      * Returns the paint for a given chip value.
184      *
185      * @param value the value.
186      *
187      * @return The paint.
188      */

189     public Paint JavaDoc getChipColor(Number JavaDoc value) {
190         return getSeriesPaint(getPaintIndex(value));
191     }
192     
193     /**
194      * Returns the paint index for a given chip value.
195      *
196      * @param value the value.
197      *
198      * @return The paint index.
199      */

200     private int getPaintIndex(Number JavaDoc value) {
201         return ((Integer JavaDoc) this.paintIndex.get(value)).intValue();
202     }
203     
204     /**
205      * Builds a map of chip values to paint colors.
206      * paintlimit is the maximum allowed number of colors.
207      */

208     private void makePaintIndex() {
209         if (this.plot == null) {
210             return;
211         }
212         WaferMapDataset data = this.plot.getDataset();
213         Number JavaDoc dataMin = data.getMinValue();
214         Number JavaDoc dataMax = data.getMaxValue();
215         Set JavaDoc uniqueValues = data.getUniqueValues();
216         if (uniqueValues.size() <= this.paintLimit) {
217             int count = 0; // assign a color for each unique value
218
for (Iterator JavaDoc i = uniqueValues.iterator(); i.hasNext();) {
219                 this.paintIndex.put(i.next(), new Integer JavaDoc(count++));
220             }
221         }
222         else {
223             // more values than paints so map
224
// multiple values to the same color
225
switch (this.paintIndexMethod) {
226                 case POSITION_INDEX:
227                     makePositionIndex(uniqueValues);
228                     break;
229                 case VALUE_INDEX:
230                     makeValueIndex(dataMax, dataMin, uniqueValues);
231                     break;
232                 default:
233                     break;
234             }
235         }
236     }
237         
238     /**
239      * Builds the paintindex by assigning colors based on the number
240      * of unique values: totalvalues/totalcolors.
241      *
242      * @param uniqueValues the set of unique values.
243      */

244     private void makePositionIndex(Set JavaDoc uniqueValues) {
245         int valuesPerColor = (int) Math.ceil(
246             (double) uniqueValues.size() / this.paintLimit
247         );
248         int count = 0; // assign a color for each unique value
249
int paint = 0;
250         for (Iterator JavaDoc i = uniqueValues.iterator(); i.hasNext();) {
251             this.paintIndex.put(i.next(), new Integer JavaDoc(paint));
252             if (++count % valuesPerColor == 0) {
253                 paint++;
254             }
255             if (paint > this.paintLimit) {
256                 paint = this.paintLimit;
257             }
258         }
259     }
260
261     /**
262      * Builds the paintindex by assigning colors evenly across the range
263      * of values: maxValue-minValue/totalcolors
264      *
265      * @param max the maximum value.
266      * @param min the minumum value.
267      * @param uniqueValues the unique values.
268      */

269     private void makeValueIndex(Number JavaDoc max, Number JavaDoc min, Set JavaDoc uniqueValues) {
270         double valueRange = max.doubleValue() - min.doubleValue();
271         double valueStep = valueRange / this.paintLimit;
272         int paint = 0;
273         double cutPoint = min.doubleValue() + valueStep;
274         for (Iterator JavaDoc i = uniqueValues.iterator(); i.hasNext();) {
275             Number JavaDoc value = (Number JavaDoc) i.next();
276             while (value.doubleValue() > cutPoint) {
277                 cutPoint += valueStep;
278                 paint++;
279                 if (paint > this.paintLimit) {
280                     paint = this.paintLimit;
281                 }
282             }
283             this.paintIndex.put(value, new Integer JavaDoc(paint));
284         }
285     }
286
287     /**
288      * Builds the list of legend entries. called by getLegendItems in
289      * WaferMapPlot to populate the plot legend.
290      *
291      * @return The legend items.
292      */

293     public LegendItemCollection getLegendCollection() {
294         LegendItemCollection result = new LegendItemCollection();
295         if (this.paintIndex != null && this.paintIndex.size() > 0) {
296             if (this.paintIndex.size() <= this.paintLimit) {
297                 for (Iterator JavaDoc i = this.paintIndex.entrySet().iterator();
298                      i.hasNext();) {
299                     // in this case, every color has a unique value
300
Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
301                     String JavaDoc label = entry.getKey().toString();
302                     String JavaDoc description = label;
303                     Shape JavaDoc shape = new Rectangle2D.Double JavaDoc(1d, 1d, 1d, 1d);
304                     Paint JavaDoc paint = getSeriesPaint(
305                         ((Integer JavaDoc) entry.getValue()).intValue()
306                     );
307                     Paint JavaDoc outlinePaint = Color.black;
308                     Stroke JavaDoc outlineStroke = DEFAULT_STROKE;
309
310                     result.add(
311                         new LegendItem(
312                             label, description, null, null, shape, paint,
313                             outlineStroke, outlinePaint
314                         )
315                     );
316                     
317                 }
318             }
319             else {
320                 // in this case, every color has a range of values
321
Set JavaDoc unique = new HashSet JavaDoc();
322                 for (Iterator JavaDoc i = this.paintIndex.entrySet().iterator();
323                      i.hasNext();) {
324                     Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
325                     if (unique.add(entry.getValue())) {
326                         String JavaDoc label = getMinPaintValue(
327                             (Integer JavaDoc) entry.getValue()).toString()
328                             + " - " + getMaxPaintValue(
329                                 (Integer JavaDoc) entry.getValue()).toString();
330                         String JavaDoc description = label;
331                         Shape JavaDoc shape = new Rectangle2D.Double JavaDoc(1d, 1d, 1d, 1d);
332                         Paint JavaDoc paint = getSeriesPaint(
333                             ((Integer JavaDoc) entry.getValue()).intValue()
334                         );
335                         Paint JavaDoc outlinePaint = Color.black;
336                         Stroke JavaDoc outlineStroke = DEFAULT_STROKE;
337
338                         result.add(new LegendItem(
339                             label, description, null, null, shape, paint,
340                             outlineStroke, outlinePaint
341                         ));
342                     }
343                 } // end foreach map entry
344
} // end else
345
}
346         return result;
347     }
348
349     /**
350      * Returns the minimum chip value assigned to a color
351      * in the paintIndex
352      *
353      * @param index the index.
354      *
355      * @return The value.
356      */

357     private Number JavaDoc getMinPaintValue(Integer JavaDoc index) {
358         double minValue = Double.POSITIVE_INFINITY;
359         for (Iterator JavaDoc i = this.paintIndex.entrySet().iterator(); i.hasNext();) {
360             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
361             if (((Integer JavaDoc) entry.getValue()).equals(index)) {
362                 if (((Number JavaDoc) entry.getKey()).doubleValue() < minValue) {
363                     minValue = ((Number JavaDoc) entry.getKey()).doubleValue();
364                 }
365             }
366         }
367         return new Double JavaDoc(minValue);
368     }
369     
370     /**
371      * Returns the maximum chip value assigned to a color
372      * in the paintIndex
373      *
374      * @param index the index.
375      *
376      * @return The value
377      */

378     private Number JavaDoc getMaxPaintValue(Integer JavaDoc index) {
379         double maxValue = Double.NEGATIVE_INFINITY;
380         for (Iterator JavaDoc i = this.paintIndex.entrySet().iterator(); i.hasNext();) {
381             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
382             if (((Integer JavaDoc) entry.getValue()).equals(index)) {
383                 if (((Number JavaDoc) entry.getKey()).doubleValue() > maxValue) {
384                     maxValue = ((Number JavaDoc) entry.getKey()).doubleValue();
385                 }
386             }
387         }
388         return new Double JavaDoc(maxValue);
389     }
390
391
392 } // end class wafermaprenderer
393
Popular Tags