1 43 44 package org.jfree.chart.renderer; 45 46 import java.awt.Color ; 47 import java.awt.Paint ; 48 import java.awt.Shape ; 49 import java.awt.Stroke ; 50 import java.awt.geom.Rectangle2D ; 51 import java.util.HashMap ; 52 import java.util.HashSet ; 53 import java.util.Iterator ; 54 import java.util.Map ; 55 import java.util.Set ; 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 68 public class WaferMapRenderer extends AbstractRenderer { 69 70 71 private Map paintIndex; 72 73 74 private WaferMapPlot plot; 75 76 77 private int paintLimit; 78 79 80 private static final int DEFAULT_PAINT_LIMIT = 35; 81 82 83 public static final int POSITION_INDEX = 0; 84 85 86 public static final int VALUE_INDEX = 1; 87 88 89 private int paintIndexMethod; 90 91 94 public WaferMapRenderer() { 95 this(null, null); 96 } 97 98 104 public WaferMapRenderer(int paintLimit, int paintIndexMethod) { 105 this(new Integer (paintLimit), new Integer (paintIndexMethod)); 106 } 107 108 114 public WaferMapRenderer(Integer paintLimit, Integer paintIndexMethod) { 115 116 super(); 117 this.paintIndex = new HashMap (); 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 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 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 168 public WaferMapPlot getPlot() { 169 return this.plot; 170 } 171 172 177 public void setPlot(WaferMapPlot plot) { 178 this.plot = plot; 179 makePaintIndex(); 180 } 181 182 189 public Paint getChipColor(Number value) { 190 return getSeriesPaint(getPaintIndex(value)); 191 } 192 193 200 private int getPaintIndex(Number value) { 201 return ((Integer ) this.paintIndex.get(value)).intValue(); 202 } 203 204 208 private void makePaintIndex() { 209 if (this.plot == null) { 210 return; 211 } 212 WaferMapDataset data = this.plot.getDataset(); 213 Number dataMin = data.getMinValue(); 214 Number dataMax = data.getMaxValue(); 215 Set uniqueValues = data.getUniqueValues(); 216 if (uniqueValues.size() <= this.paintLimit) { 217 int count = 0; for (Iterator i = uniqueValues.iterator(); i.hasNext();) { 219 this.paintIndex.put(i.next(), new Integer (count++)); 220 } 221 } 222 else { 223 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 244 private void makePositionIndex(Set uniqueValues) { 245 int valuesPerColor = (int) Math.ceil( 246 (double) uniqueValues.size() / this.paintLimit 247 ); 248 int count = 0; int paint = 0; 250 for (Iterator i = uniqueValues.iterator(); i.hasNext();) { 251 this.paintIndex.put(i.next(), new Integer (paint)); 252 if (++count % valuesPerColor == 0) { 253 paint++; 254 } 255 if (paint > this.paintLimit) { 256 paint = this.paintLimit; 257 } 258 } 259 } 260 261 269 private void makeValueIndex(Number max, Number min, Set 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 i = uniqueValues.iterator(); i.hasNext();) { 275 Number value = (Number ) 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 (paint)); 284 } 285 } 286 287 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 i = this.paintIndex.entrySet().iterator(); 298 i.hasNext();) { 299 Map.Entry entry = (Map.Entry ) i.next(); 301 String label = entry.getKey().toString(); 302 String description = label; 303 Shape shape = new Rectangle2D.Double (1d, 1d, 1d, 1d); 304 Paint paint = getSeriesPaint( 305 ((Integer ) entry.getValue()).intValue() 306 ); 307 Paint outlinePaint = Color.black; 308 Stroke 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 Set unique = new HashSet (); 322 for (Iterator i = this.paintIndex.entrySet().iterator(); 323 i.hasNext();) { 324 Map.Entry entry = (Map.Entry ) i.next(); 325 if (unique.add(entry.getValue())) { 326 String label = getMinPaintValue( 327 (Integer ) entry.getValue()).toString() 328 + " - " + getMaxPaintValue( 329 (Integer ) entry.getValue()).toString(); 330 String description = label; 331 Shape shape = new Rectangle2D.Double (1d, 1d, 1d, 1d); 332 Paint paint = getSeriesPaint( 333 ((Integer ) entry.getValue()).intValue() 334 ); 335 Paint outlinePaint = Color.black; 336 Stroke outlineStroke = DEFAULT_STROKE; 337 338 result.add(new LegendItem( 339 label, description, null, null, shape, paint, 340 outlineStroke, outlinePaint 341 )); 342 } 343 } } } 346 return result; 347 } 348 349 357 private Number getMinPaintValue(Integer index) { 358 double minValue = Double.POSITIVE_INFINITY; 359 for (Iterator i = this.paintIndex.entrySet().iterator(); i.hasNext();) { 360 Map.Entry entry = (Map.Entry ) i.next(); 361 if (((Integer ) entry.getValue()).equals(index)) { 362 if (((Number ) entry.getKey()).doubleValue() < minValue) { 363 minValue = ((Number ) entry.getKey()).doubleValue(); 364 } 365 } 366 } 367 return new Double (minValue); 368 } 369 370 378 private Number getMaxPaintValue(Integer index) { 379 double maxValue = Double.NEGATIVE_INFINITY; 380 for (Iterator i = this.paintIndex.entrySet().iterator(); i.hasNext();) { 381 Map.Entry entry = (Map.Entry ) i.next(); 382 if (((Integer ) entry.getValue()).equals(index)) { 383 if (((Number ) entry.getKey()).doubleValue() > maxValue) { 384 maxValue = ((Number ) entry.getKey()).doubleValue(); 385 } 386 } 387 } 388 return new Double (maxValue); 389 } 390 391 392 } | Popular Tags |