001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jfreechart/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * -------------------
028: * XYAreaRenderer.java
029: * -------------------
030: * (C) Copyright 2002-2007, by Hari and Contributors.
031: *
032: * Original Author: Hari (ourhari@hotmail.com);
033: * Contributor(s): David Gilbert (for Object Refinery Limited);
034: * Richard Atkinson;
035: * Christian W. Zuckschwerdt;
036: *
037: * $Id: XYAreaRenderer.java,v 1.12.2.11 2007/05/18 10:28:31 mungady Exp $
038: *
039: * Changes:
040: * --------
041: * 03-Apr-2002 : Version 1, contributed by Hari. This class is based on the
042: * StandardXYItemRenderer class (DG);
043: * 09-Apr-2002 : Removed the translated zero from the drawItem method -
044: * overridden the initialise() method to calculate it (DG);
045: * 30-May-2002 : Added tool tip generator to constructor to match super
046: * class (DG);
047: * 25-Jun-2002 : Removed unnecessary local variable (DG);
048: * 05-Aug-2002 : Small modification to drawItem method to support URLs for HTML
049: * image maps (RA);
050: * 01-Oct-2002 : Fixed errors reported by Checkstyle (DG);
051: * 07-Nov-2002 : Renamed AreaXYItemRenderer --> XYAreaRenderer (DG);
052: * 25-Mar-2003 : Implemented Serializable (DG);
053: * 01-May-2003 : Modified drawItem() method signature (DG);
054: * 27-Jul-2003 : Made line and polygon properties protected rather than
055: * private (RA);
056: * 30-Jul-2003 : Modified entity constructor (CZ);
057: * 20-Aug-2003 : Implemented Cloneable and PublicCloneable (DG);
058: * 16-Sep-2003 : Changed ChartRenderingInfo --> PlotRenderingInfo (DG);
059: * 07-Oct-2003 : Added renderer state (DG);
060: * 08-Dec-2003 : Modified hotspot for chart entity (DG);
061: * 10-Feb-2004 : Changed the drawItem() method to make cut-and-paste overriding
062: * easier. Also moved state class into this class (DG);
063: * 25-Feb-2004 : Replaced CrosshairInfo with CrosshairState. Renamed
064: * XYToolTipGenerator --> XYItemLabelGenerator (DG);
065: * 15-Jul-2004 : Switched getX() with getXValue() and getY() with
066: * getYValue() (DG);
067: * 11-Nov-2004 : Now uses ShapeUtilities to translate shapes (DG);
068: * 19-Jan-2005 : Now accesses primitives only from dataset (DG);
069: * 21-Mar-2005 : Override getLegendItem() and equals() methods (DG);
070: * 20-Apr-2005 : Use generators for legend tooltips and URLs (DG);
071: * ------------- JFREECHART 1.0.x ---------------------------------------------
072: * 06-Feb-2007 : Fixed bug 1086307, crosshairs with multiple axes (DG);
073: * 14-Feb-2007 : Fixed bug in clone() (DG);
074: * 20-Apr-2007 : Updated getLegendItem() for renderer change (DG);
075: * 04-May-2007 : Set processVisibleItemsOnly flag to false (DG);
076: * 17-May-2007 : Set datasetIndex and seriesIndex in getLegendItem() (DG);
077: * 18-May-2007 : Set dataset and seriesKey for LegendItem (DG);
078: *
079: */
080:
081: package org.jfree.chart.renderer.xy;
082:
083: import java.awt.Graphics2D;
084: import java.awt.Paint;
085: import java.awt.Polygon;
086: import java.awt.Shape;
087: import java.awt.Stroke;
088: import java.awt.geom.GeneralPath;
089: import java.awt.geom.Line2D;
090: import java.awt.geom.Rectangle2D;
091: import java.io.IOException;
092: import java.io.ObjectInputStream;
093: import java.io.ObjectOutputStream;
094: import java.io.Serializable;
095:
096: import org.jfree.chart.LegendItem;
097: import org.jfree.chart.axis.ValueAxis;
098: import org.jfree.chart.entity.EntityCollection;
099: import org.jfree.chart.entity.XYItemEntity;
100: import org.jfree.chart.event.RendererChangeEvent;
101: import org.jfree.chart.labels.XYSeriesLabelGenerator;
102: import org.jfree.chart.labels.XYToolTipGenerator;
103: import org.jfree.chart.plot.CrosshairState;
104: import org.jfree.chart.plot.PlotOrientation;
105: import org.jfree.chart.plot.PlotRenderingInfo;
106: import org.jfree.chart.plot.XYPlot;
107: import org.jfree.chart.urls.XYURLGenerator;
108: import org.jfree.data.xy.XYDataset;
109: import org.jfree.io.SerialUtilities;
110: import org.jfree.util.PublicCloneable;
111: import org.jfree.util.ShapeUtilities;
112:
113: /**
114: * Area item renderer for an {@link XYPlot}. This class can draw (a) shapes at
115: * each point, or (b) lines between points, or (c) both shapes and lines,
116: * or (d) filled areas, or (e) filled areas and shapes.
117: */
118: public class XYAreaRenderer extends AbstractXYItemRenderer implements
119: XYItemRenderer, Cloneable, PublicCloneable, Serializable {
120:
121: /** For serialization. */
122: private static final long serialVersionUID = -4481971353973876747L;
123:
124: /**
125: * A state object used by this renderer.
126: */
127: static class XYAreaRendererState extends XYItemRendererState {
128:
129: /** Working storage for the area under one series. */
130: public Polygon area;
131:
132: /** Working line that can be recycled. */
133: public Line2D line;
134:
135: /**
136: * Creates a new state.
137: *
138: * @param info the plot rendering info.
139: */
140: public XYAreaRendererState(PlotRenderingInfo info) {
141: super (info);
142: this .area = new Polygon();
143: this .line = new Line2D.Double();
144: }
145:
146: }
147:
148: /** Useful constant for specifying the type of rendering (shapes only). */
149: public static final int SHAPES = 1;
150:
151: /** Useful constant for specifying the type of rendering (lines only). */
152: public static final int LINES = 2;
153:
154: /**
155: * Useful constant for specifying the type of rendering (shapes and lines).
156: */
157: public static final int SHAPES_AND_LINES = 3;
158:
159: /** Useful constant for specifying the type of rendering (area only). */
160: public static final int AREA = 4;
161:
162: /**
163: * Useful constant for specifying the type of rendering (area and shapes).
164: */
165: public static final int AREA_AND_SHAPES = 5;
166:
167: /** A flag indicating whether or not shapes are drawn at each XY point. */
168: private boolean plotShapes;
169:
170: /** A flag indicating whether or not lines are drawn between XY points. */
171: private boolean plotLines;
172:
173: /** A flag indicating whether or not Area are drawn at each XY point. */
174: private boolean plotArea;
175:
176: /** A flag that controls whether or not the outline is shown. */
177: private boolean showOutline;
178:
179: /**
180: * The shape used to represent an area in each legend item (this should
181: * never be <code>null</code>).
182: */
183: private transient Shape legendArea;
184:
185: /**
186: * Constructs a new renderer.
187: */
188: public XYAreaRenderer() {
189: this (AREA);
190: }
191:
192: /**
193: * Constructs a new renderer.
194: *
195: * @param type the type of the renderer.
196: */
197: public XYAreaRenderer(int type) {
198: this (type, null, null);
199: }
200:
201: /**
202: * Constructs a new renderer. To specify the type of renderer, use one of
203: * the constants: <code>SHAPES</code>, <code>LINES</code>,
204: * <code>SHAPES_AND_LINES</code>, <code>AREA</code> or
205: * <code>AREA_AND_SHAPES</code>.
206: *
207: * @param type the type of renderer.
208: * @param toolTipGenerator the tool tip generator to use
209: * (<code>null</code> permitted).
210: * @param urlGenerator the URL generator (<code>null</code> permitted).
211: */
212: public XYAreaRenderer(int type,
213: XYToolTipGenerator toolTipGenerator,
214: XYURLGenerator urlGenerator) {
215:
216: super ();
217: setBaseToolTipGenerator(toolTipGenerator);
218: setURLGenerator(urlGenerator);
219:
220: if (type == SHAPES) {
221: this .plotShapes = true;
222: }
223: if (type == LINES) {
224: this .plotLines = true;
225: }
226: if (type == SHAPES_AND_LINES) {
227: this .plotShapes = true;
228: this .plotLines = true;
229: }
230: if (type == AREA) {
231: this .plotArea = true;
232: }
233: if (type == AREA_AND_SHAPES) {
234: this .plotArea = true;
235: this .plotShapes = true;
236: }
237: this .showOutline = false;
238: GeneralPath area = new GeneralPath();
239: area.moveTo(0.0f, -4.0f);
240: area.lineTo(3.0f, -2.0f);
241: area.lineTo(4.0f, 4.0f);
242: area.lineTo(-4.0f, 4.0f);
243: area.lineTo(-3.0f, -2.0f);
244: area.closePath();
245: this .legendArea = area;
246:
247: }
248:
249: /**
250: * Returns true if shapes are being plotted by the renderer.
251: *
252: * @return <code>true</code> if shapes are being plotted by the renderer.
253: */
254: public boolean getPlotShapes() {
255: return this .plotShapes;
256: }
257:
258: /**
259: * Returns true if lines are being plotted by the renderer.
260: *
261: * @return <code>true</code> if lines are being plotted by the renderer.
262: */
263: public boolean getPlotLines() {
264: return this .plotLines;
265: }
266:
267: /**
268: * Returns true if Area is being plotted by the renderer.
269: *
270: * @return <code>true</code> if Area is being plotted by the renderer.
271: */
272: public boolean getPlotArea() {
273: return this .plotArea;
274: }
275:
276: /**
277: * Returns a flag that controls whether or not outlines of the areas are
278: * drawn.
279: *
280: * @return The flag.
281: *
282: * @see #setOutline(boolean)
283: */
284: public boolean isOutline() {
285: return this .showOutline;
286: }
287:
288: /**
289: * Sets a flag that controls whether or not outlines of the areas are drawn
290: * and sends a {@link RendererChangeEvent} to all registered listeners.
291: *
292: * @param show the flag.
293: *
294: * @see #isOutline()
295: */
296: public void setOutline(boolean show) {
297: this .showOutline = show;
298: notifyListeners(new RendererChangeEvent(this ));
299: }
300:
301: /**
302: * Returns the shape used to represent an area in the legend.
303: *
304: * @return The legend area (never <code>null</code>).
305: */
306: public Shape getLegendArea() {
307: return this .legendArea;
308: }
309:
310: /**
311: * Sets the shape used as an area in each legend item and sends a
312: * {@link RendererChangeEvent} to all registered listeners.
313: *
314: * @param area the area (<code>null</code> not permitted).
315: */
316: public void setLegendArea(Shape area) {
317: if (area == null) {
318: throw new IllegalArgumentException("Null 'area' argument.");
319: }
320: this .legendArea = area;
321: notifyListeners(new RendererChangeEvent(this ));
322: }
323:
324: /**
325: * Initialises the renderer and returns a state object that should be
326: * passed to all subsequent calls to the drawItem() method.
327: *
328: * @param g2 the graphics device.
329: * @param dataArea the area inside the axes.
330: * @param plot the plot.
331: * @param data the data.
332: * @param info an optional info collection object to return data back to
333: * the caller.
334: *
335: * @return A state object for use by the renderer.
336: */
337: public XYItemRendererState initialise(Graphics2D g2,
338: Rectangle2D dataArea, XYPlot plot, XYDataset data,
339: PlotRenderingInfo info) {
340: XYAreaRendererState state = new XYAreaRendererState(info);
341:
342: // in the rendering process, there is special handling for item
343: // zero, so we can't support processing of visible data items only
344: state.setProcessVisibleItemsOnly(false);
345: return state;
346: }
347:
348: /**
349: * Returns a default legend item for the specified series. Subclasses
350: * should override this method to generate customised items.
351: *
352: * @param datasetIndex the dataset index (zero-based).
353: * @param series the series index (zero-based).
354: *
355: * @return A legend item for the series.
356: */
357: public LegendItem getLegendItem(int datasetIndex, int series) {
358: LegendItem result = null;
359: XYPlot xyplot = getPlot();
360: if (xyplot != null) {
361: XYDataset dataset = xyplot.getDataset(datasetIndex);
362: if (dataset != null) {
363: XYSeriesLabelGenerator lg = getLegendItemLabelGenerator();
364: String label = lg.generateLabel(dataset, series);
365: String description = label;
366: String toolTipText = null;
367: if (getLegendItemToolTipGenerator() != null) {
368: toolTipText = getLegendItemToolTipGenerator()
369: .generateLabel(dataset, series);
370: }
371: String urlText = null;
372: if (getLegendItemURLGenerator() != null) {
373: urlText = getLegendItemURLGenerator()
374: .generateLabel(dataset, series);
375: }
376: Paint paint = lookupSeriesPaint(series);
377: result = new LegendItem(label, description,
378: toolTipText, urlText, this .legendArea, paint);
379: result.setDataset(dataset);
380: result.setDatasetIndex(datasetIndex);
381: result.setSeriesKey(dataset.getSeriesKey(series));
382: result.setSeriesIndex(series);
383: }
384: }
385: return result;
386: }
387:
388: /**
389: * Draws the visual representation of a single data item.
390: *
391: * @param g2 the graphics device.
392: * @param state the renderer state.
393: * @param dataArea the area within which the data is being drawn.
394: * @param info collects information about the drawing.
395: * @param plot the plot (can be used to obtain standard color information
396: * etc).
397: * @param domainAxis the domain axis.
398: * @param rangeAxis the range axis.
399: * @param dataset the dataset.
400: * @param series the series index (zero-based).
401: * @param item the item index (zero-based).
402: * @param crosshairState crosshair information for the plot
403: * (<code>null</code> permitted).
404: * @param pass the pass index.
405: */
406: public void drawItem(Graphics2D g2, XYItemRendererState state,
407: Rectangle2D dataArea, PlotRenderingInfo info, XYPlot plot,
408: ValueAxis domainAxis, ValueAxis rangeAxis,
409: XYDataset dataset, int series, int item,
410: CrosshairState crosshairState, int pass) {
411:
412: if (!getItemVisible(series, item)) {
413: return;
414: }
415: XYAreaRendererState areaState = (XYAreaRendererState) state;
416:
417: // get the data point...
418: double x1 = dataset.getXValue(series, item);
419: double y1 = dataset.getYValue(series, item);
420: if (Double.isNaN(y1)) {
421: y1 = 0.0;
422: }
423: double transX1 = domainAxis.valueToJava2D(x1, dataArea, plot
424: .getDomainAxisEdge());
425: double transY1 = rangeAxis.valueToJava2D(y1, dataArea, plot
426: .getRangeAxisEdge());
427:
428: // get the previous point and the next point so we can calculate a
429: // "hot spot" for the area (used by the chart entity)...
430: int itemCount = dataset.getItemCount(series);
431: double x0 = dataset.getXValue(series, Math.max(item - 1, 0));
432: double y0 = dataset.getYValue(series, Math.max(item - 1, 0));
433: if (Double.isNaN(y0)) {
434: y0 = 0.0;
435: }
436: double transX0 = domainAxis.valueToJava2D(x0, dataArea, plot
437: .getDomainAxisEdge());
438: double transY0 = rangeAxis.valueToJava2D(y0, dataArea, plot
439: .getRangeAxisEdge());
440:
441: double x2 = dataset.getXValue(series, Math.min(item + 1,
442: itemCount - 1));
443: double y2 = dataset.getYValue(series, Math.min(item + 1,
444: itemCount - 1));
445: if (Double.isNaN(y2)) {
446: y2 = 0.0;
447: }
448: double transX2 = domainAxis.valueToJava2D(x2, dataArea, plot
449: .getDomainAxisEdge());
450: double transY2 = rangeAxis.valueToJava2D(y2, dataArea, plot
451: .getRangeAxisEdge());
452:
453: double transZero = rangeAxis.valueToJava2D(0.0, dataArea, plot
454: .getRangeAxisEdge());
455: Polygon hotspot = null;
456: if (plot.getOrientation() == PlotOrientation.HORIZONTAL) {
457: hotspot = new Polygon();
458: hotspot.addPoint((int) transZero,
459: (int) ((transX0 + transX1) / 2.0));
460: hotspot.addPoint((int) ((transY0 + transY1) / 2.0),
461: (int) ((transX0 + transX1) / 2.0));
462: hotspot.addPoint((int) transY1, (int) transX1);
463: hotspot.addPoint((int) ((transY1 + transY2) / 2.0),
464: (int) ((transX1 + transX2) / 2.0));
465: hotspot.addPoint((int) transZero,
466: (int) ((transX1 + transX2) / 2.0));
467: } else { // vertical orientation
468: hotspot = new Polygon();
469: hotspot.addPoint((int) ((transX0 + transX1) / 2.0),
470: (int) transZero);
471: hotspot.addPoint((int) ((transX0 + transX1) / 2.0),
472: (int) ((transY0 + transY1) / 2.0));
473: hotspot.addPoint((int) transX1, (int) transY1);
474: hotspot.addPoint((int) ((transX1 + transX2) / 2.0),
475: (int) ((transY1 + transY2) / 2.0));
476: hotspot.addPoint((int) ((transX1 + transX2) / 2.0),
477: (int) transZero);
478: }
479:
480: if (item == 0) { // create a new area polygon for the series
481: areaState.area = new Polygon();
482: // the first point is (x, 0)
483: double zero = rangeAxis.valueToJava2D(0.0, dataArea, plot
484: .getRangeAxisEdge());
485: if (plot.getOrientation() == PlotOrientation.VERTICAL) {
486: areaState.area.addPoint((int) transX1, (int) zero);
487: } else if (plot.getOrientation() == PlotOrientation.HORIZONTAL) {
488: areaState.area.addPoint((int) zero, (int) transX1);
489: }
490: }
491:
492: // Add each point to Area (x, y)
493: if (plot.getOrientation() == PlotOrientation.VERTICAL) {
494: areaState.area.addPoint((int) transX1, (int) transY1);
495: } else if (plot.getOrientation() == PlotOrientation.HORIZONTAL) {
496: areaState.area.addPoint((int) transY1, (int) transX1);
497: }
498:
499: PlotOrientation orientation = plot.getOrientation();
500: Paint paint = getItemPaint(series, item);
501: Stroke stroke = getItemStroke(series, item);
502: g2.setPaint(paint);
503: g2.setStroke(stroke);
504:
505: Shape shape = null;
506: if (getPlotShapes()) {
507: shape = getItemShape(series, item);
508: if (orientation == PlotOrientation.VERTICAL) {
509: shape = ShapeUtilities.createTranslatedShape(shape,
510: transX1, transY1);
511: } else if (orientation == PlotOrientation.HORIZONTAL) {
512: shape = ShapeUtilities.createTranslatedShape(shape,
513: transY1, transX1);
514: }
515: g2.draw(shape);
516: }
517:
518: if (getPlotLines()) {
519: if (item > 0) {
520: if (plot.getOrientation() == PlotOrientation.VERTICAL) {
521: areaState.line.setLine(transX0, transY0, transX1,
522: transY1);
523: } else if (plot.getOrientation() == PlotOrientation.HORIZONTAL) {
524: areaState.line.setLine(transY0, transX0, transY1,
525: transX1);
526: }
527: g2.draw(areaState.line);
528: }
529: }
530:
531: // Check if the item is the last item for the series.
532: // and number of items > 0. We can't draw an area for a single point.
533: if (getPlotArea() && item > 0 && item == (itemCount - 1)) {
534:
535: if (orientation == PlotOrientation.VERTICAL) {
536: // Add the last point (x,0)
537: areaState.area.addPoint((int) transX1, (int) transZero);
538: } else if (orientation == PlotOrientation.HORIZONTAL) {
539: // Add the last point (x,0)
540: areaState.area.addPoint((int) transZero, (int) transX1);
541: }
542:
543: g2.fill(areaState.area);
544:
545: // draw an outline around the Area.
546: if (isOutline()) {
547: g2.setStroke(getItemOutlineStroke(series, item));
548: g2.setPaint(getItemOutlinePaint(series, item));
549: g2.draw(areaState.area);
550: }
551: }
552:
553: int domainAxisIndex = plot.getDomainAxisIndex(domainAxis);
554: int rangeAxisIndex = plot.getRangeAxisIndex(rangeAxis);
555: updateCrosshairValues(crosshairState, x1, y1, domainAxisIndex,
556: rangeAxisIndex, transX1, transY1, orientation);
557:
558: // collect entity and tool tip information...
559: if (state.getInfo() != null) {
560: EntityCollection entities = state.getEntityCollection();
561: if (entities != null && hotspot != null) {
562: String tip = null;
563: XYToolTipGenerator generator = getToolTipGenerator(
564: series, item);
565: if (generator != null) {
566: tip = generator.generateToolTip(dataset, series,
567: item);
568: }
569: String url = null;
570: if (getURLGenerator() != null) {
571: url = getURLGenerator().generateURL(dataset,
572: series, item);
573: }
574: XYItemEntity entity = new XYItemEntity(hotspot,
575: dataset, series, item, tip, url);
576: entities.add(entity);
577: }
578: }
579:
580: }
581:
582: /**
583: * Returns a clone of the renderer.
584: *
585: * @return A clone.
586: *
587: * @throws CloneNotSupportedException if the renderer cannot be cloned.
588: */
589: public Object clone() throws CloneNotSupportedException {
590: XYAreaRenderer clone = (XYAreaRenderer) super .clone();
591: clone.legendArea = ShapeUtilities.clone(this .legendArea);
592: return clone;
593: }
594:
595: /**
596: * Tests this renderer for equality with an arbitrary object.
597: *
598: * @param obj the object (<code>null</code> permitted).
599: *
600: * @return A boolean.
601: */
602: public boolean equals(Object obj) {
603: if (obj == this ) {
604: return true;
605: }
606: if (!(obj instanceof XYAreaRenderer)) {
607: return false;
608: }
609: XYAreaRenderer that = (XYAreaRenderer) obj;
610: if (this .plotArea != that.plotArea) {
611: return false;
612: }
613: if (this .plotLines != that.plotLines) {
614: return false;
615: }
616: if (this .plotShapes != that.plotShapes) {
617: return false;
618: }
619: if (this .showOutline != that.showOutline) {
620: return false;
621: }
622: if (!ShapeUtilities.equal(this .legendArea, that.legendArea)) {
623: return false;
624: }
625: return true;
626: }
627:
628: /**
629: * Provides serialization support.
630: *
631: * @param stream the input stream.
632: *
633: * @throws IOException if there is an I/O error.
634: * @throws ClassNotFoundException if there is a classpath problem.
635: */
636: private void readObject(ObjectInputStream stream)
637: throws IOException, ClassNotFoundException {
638: stream.defaultReadObject();
639: this .legendArea = SerialUtilities.readShape(stream);
640: }
641:
642: /**
643: * Provides serialization support.
644: *
645: * @param stream the output stream.
646: *
647: * @throws IOException if there is an I/O error.
648: */
649: private void writeObject(ObjectOutputStream stream)
650: throws IOException {
651: stream.defaultWriteObject();
652: SerialUtilities.writeShape(this.legendArea, stream);
653: }
654: }
|