1:
59:
60: package ;
61:
62: import ;
63: import ;
64: import ;
65: import ;
66: import ;
67: import ;
68: import ;
69: import ;
70: import ;
71: import ;
72: import ;
73: import ;
74: import ;
75: import ;
76: import ;
77: import ;
78:
79: import ;
80:
81: import ;
82: import ;
83: import ;
84: import ;
85: import ;
86: import ;
87: import ;
88: import ;
89: import ;
90:
91:
98: public class MinMaxCategoryRenderer extends AbstractCategoryItemRenderer {
99:
100:
101: private static final long serialVersionUID = 2935615937671064911L;
102:
103:
104: private boolean plotLines = false;
105:
106:
109: private transient Paint groupPaint = Color.black;
110:
111:
114: private transient Stroke groupStroke = new BasicStroke(1.0f);
115:
116:
117: private transient Icon minIcon = getIcon(new Arc2D.Double(-4, -4, 8, 8, 0,
118: 360, Arc2D.OPEN), null, Color.black);
119:
120:
121: private transient Icon maxIcon = getIcon(new Arc2D.Double(-4, -4, 8, 8, 0,
122: 360, Arc2D.OPEN), null, Color.black);
123:
124:
125: private transient Icon objectIcon = getIcon(new Line2D.Double(-4, 0, 4, 0),
126: false, true);
127:
128:
129: private int lastCategory = -1;
130:
131:
132: private double min;
133:
134:
135: private double max;
136:
137:
140: public MinMaxCategoryRenderer() {
141: super();
142: }
143:
144:
152: public boolean isDrawLines() {
153: return this.plotLines;
154: }
155:
156:
165: public void setDrawLines(boolean draw) {
166: if (this.plotLines != draw) {
167: this.plotLines = draw;
168: fireChangeEvent();
169: }
170:
171: }
172:
173:
181: public Paint getGroupPaint() {
182: return this.groupPaint;
183: }
184:
185:
194: public void setGroupPaint(Paint paint) {
195: if (paint == null) {
196: throw new IllegalArgumentException("Null 'paint' argument.");
197: }
198: this.groupPaint = paint;
199: fireChangeEvent();
200: }
201:
202:
210: public Stroke getGroupStroke() {
211: return this.groupStroke;
212: }
213:
214:
221: public void setGroupStroke(Stroke stroke) {
222: if (stroke == null) {
223: throw new IllegalArgumentException("Null 'stroke' argument.");
224: }
225: this.groupStroke = stroke;
226: fireChangeEvent();
227: }
228:
229:
236: public Icon getObjectIcon() {
237: return this.objectIcon;
238: }
239:
240:
248: public void setObjectIcon(Icon icon) {
249: if (icon == null) {
250: throw new IllegalArgumentException("Null 'icon' argument.");
251: }
252: this.objectIcon = icon;
253: fireChangeEvent();
254: }
255:
256:
264: public Icon getMaxIcon() {
265: return this.maxIcon;
266: }
267:
268:
277: public void setMaxIcon(Icon icon) {
278: if (icon == null) {
279: throw new IllegalArgumentException("Null 'icon' argument.");
280: }
281: this.maxIcon = icon;
282: fireChangeEvent();
283: }
284:
285:
293: public Icon getMinIcon() {
294: return this.minIcon;
295: }
296:
297:
306: public void setMinIcon(Icon icon) {
307: if (icon == null) {
308: throw new IllegalArgumentException("Null 'icon' argument.");
309: }
310: this.minIcon = icon;
311: fireChangeEvent();
312: }
313:
314:
328: public void drawItem(Graphics2D g2, CategoryItemRendererState state,
329: Rectangle2D dataArea, CategoryPlot plot, CategoryAxis domainAxis,
330: ValueAxis rangeAxis, CategoryDataset dataset, int row, int column,
331: int pass) {
332:
333:
334: Number value = dataset.getValue(row, column);
335: if (value != null) {
336:
337: double x1 = domainAxis.getCategoryMiddle(column, getColumnCount(),
338: dataArea, plot.getDomainAxisEdge());
339: double y1 = rangeAxis.valueToJava2D(value.doubleValue(), dataArea,
340: plot.getRangeAxisEdge());
341: g2.setPaint(getItemPaint(row, column));
342: g2.setStroke(getItemStroke(row, column));
343: Shape shape = null;
344: shape = new Rectangle2D.Double(x1 - 4, y1 - 4, 8.0, 8.0);
345:
346: PlotOrientation orient = plot.getOrientation();
347: if (orient == PlotOrientation.VERTICAL) {
348: this.objectIcon.paintIcon(null, g2, (int) x1, (int) y1);
349: }
350: else {
351: this.objectIcon.paintIcon(null, g2, (int) y1, (int) x1);
352: }
353:
354: if (this.lastCategory == column) {
355: if (this.min > value.doubleValue()) {
356: this.min = value.doubleValue();
357: }
358: if (this.max < value.doubleValue()) {
359: this.max = value.doubleValue();
360: }
361:
362:
363: if (dataset.getRowCount() - 1 == row) {
364: g2.setPaint(this.groupPaint);
365: g2.setStroke(this.groupStroke);
366: double minY = rangeAxis.valueToJava2D(this.min, dataArea,
367: plot.getRangeAxisEdge());
368: double maxY = rangeAxis.valueToJava2D(this.max, dataArea,
369: plot.getRangeAxisEdge());
370:
371: if (orient == PlotOrientation.VERTICAL) {
372: g2.draw(new Line2D.Double(x1, minY, x1, maxY));
373: this.minIcon.paintIcon(null, g2, (int) x1, (int) minY);
374: this.maxIcon.paintIcon(null, g2, (int) x1, (int) maxY);
375: }
376: else {
377: g2.draw(new Line2D.Double(minY, x1, maxY, x1));
378: this.minIcon.paintIcon(null, g2, (int) minY, (int) x1);
379: this.maxIcon.paintIcon(null, g2, (int) maxY, (int) x1);
380: }
381: }
382: }
383: else {
384: this.lastCategory = column;
385: this.min = value.doubleValue();
386: this.max = value.doubleValue();
387: }
388:
389:
390: if (this.plotLines) {
391: if (column != 0) {
392: Number previousValue = dataset.getValue(row, column - 1);
393: if (previousValue != null) {
394:
395: double previous = previousValue.doubleValue();
396: double x0 = domainAxis.getCategoryMiddle(column - 1,
397: getColumnCount(), dataArea,
398: plot.getDomainAxisEdge());
399: double y0 = rangeAxis.valueToJava2D(previous, dataArea,
400: plot.getRangeAxisEdge());
401: g2.setPaint(getItemPaint(row, column));
402: g2.setStroke(getItemStroke(row, column));
403: Line2D line;
404: if (orient == PlotOrientation.VERTICAL) {
405: line = new Line2D.Double(x0, y0, x1, y1);
406: }
407: else {
408: line = new Line2D.Double(y0, x0, y1, x1);
409: }
410: g2.draw(line);
411: }
412: }
413: }
414:
415:
416: EntityCollection entities = state.getEntityCollection();
417: if (entities != null && shape != null) {
418: addItemEntity(entities, dataset, row, column, shape);
419: }
420: }
421: }
422:
423:
434: public boolean equals(Object obj) {
435: if (obj == this) {
436: return true;
437: }
438: if (!(obj instanceof MinMaxCategoryRenderer)) {
439: return false;
440: }
441: MinMaxCategoryRenderer that = (MinMaxCategoryRenderer) obj;
442: if (this.plotLines != that.plotLines) {
443: return false;
444: }
445: if (!PaintUtilities.equal(this.groupPaint, that.groupPaint)) {
446: return false;
447: }
448: if (!this.groupStroke.equals(that.groupStroke)) {
449: return false;
450: }
451: return super.equals(obj);
452: }
453:
454:
463: private Icon getIcon(Shape shape, final Paint fillPaint,
464: final Paint outlinePaint) {
465:
466: final int width = shape.getBounds().width;
467: final int height = shape.getBounds().height;
468: final GeneralPath path = new GeneralPath(shape);
469: return new Icon() {
470: public void paintIcon(Component c, Graphics g, int x, int y) {
471: Graphics2D g2 = (Graphics2D) g;
472: path.transform(AffineTransform.getTranslateInstance(x, y));
473: if (fillPaint != null) {
474: g2.setPaint(fillPaint);
475: g2.fill(path);
476: }
477: if (outlinePaint != null) {
478: g2.setPaint(outlinePaint);
479: g2.draw(path);
480: }
481: path.transform(AffineTransform.getTranslateInstance(-x, -y));
482: }
483:
484: public int getIconWidth() {
485: return width;
486: }
487:
488: public int getIconHeight() {
489: return height;
490: }
491:
492: };
493: }
494:
495:
504: private Icon getIcon(Shape shape, final boolean fill,
505: final boolean outline) {
506: final int width = shape.getBounds().width;
507: final int height = shape.getBounds().height;
508: final GeneralPath path = new GeneralPath(shape);
509: return new Icon() {
510: public void paintIcon(Component c, Graphics g, int x, int y) {
511: Graphics2D g2 = (Graphics2D) g;
512: path.transform(AffineTransform.getTranslateInstance(x, y));
513: if (fill) {
514: g2.fill(path);
515: }
516: if (outline) {
517: g2.draw(path);
518: }
519: path.transform(AffineTransform.getTranslateInstance(-x, -y));
520: }
521:
522: public int getIconWidth() {
523: return width;
524: }
525:
526: public int getIconHeight() {
527: return height;
528: }
529: };
530: }
531:
532:
539: private void writeObject(ObjectOutputStream stream) throws IOException {
540: stream.defaultWriteObject();
541: SerialUtilities.writeStroke(this.groupStroke, stream);
542: SerialUtilities.writePaint(this.groupPaint, stream);
543: }
544:
545:
553: private void readObject(ObjectInputStream stream)
554: throws IOException, ClassNotFoundException {
555: stream.defaultReadObject();
556: this.groupStroke = SerialUtilities.readStroke(stream);
557: this.groupPaint = SerialUtilities.readPaint(stream);
558:
559: this.minIcon = getIcon(new Arc2D.Double(-4, -4, 8, 8, 0, 360,
560: Arc2D.OPEN), null, Color.black);
561: this.maxIcon = getIcon(new Arc2D.Double(-4, -4, 8, 8, 0, 360,
562: Arc2D.OPEN), null, Color.black);
563: this.objectIcon = getIcon(new Line2D.Double(-4, 0, 4, 0), false, true);
564: }
565:
566: }