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 * Changes:
038 * --------
039 * 03-Apr-2002 : Version 1, contributed by Hari. This class is based on the
040 * StandardXYItemRenderer class (DG);
041 * 09-Apr-2002 : Removed the translated zero from the drawItem method -
042 * overridden the initialise() method to calculate it (DG);
043 * 30-May-2002 : Added tool tip generator to constructor to match super
044 * class (DG);
045 * 25-Jun-2002 : Removed unnecessary local variable (DG);
046 * 05-Aug-2002 : Small modification to drawItem method to support URLs for HTML
047 * image maps (RA);
048 * 01-Oct-2002 : Fixed errors reported by Checkstyle (DG);
049 * 07-Nov-2002 : Renamed AreaXYItemRenderer --> XYAreaRenderer (DG);
050 * 25-Mar-2003 : Implemented Serializable (DG);
051 * 01-May-2003 : Modified drawItem() method signature (DG);
052 * 27-Jul-2003 : Made line and polygon properties protected rather than
053 * private (RA);
054 * 30-Jul-2003 : Modified entity constructor (CZ);
055 * 20-Aug-2003 : Implemented Cloneable and PublicCloneable (DG);
056 * 16-Sep-2003 : Changed ChartRenderingInfo --> PlotRenderingInfo (DG);
057 * 07-Oct-2003 : Added renderer state (DG);
058 * 08-Dec-2003 : Modified hotspot for chart entity (DG);
059 * 10-Feb-2004 : Changed the drawItem() method to make cut-and-paste overriding
060 * easier. Also moved state class into this class (DG);
061 * 25-Feb-2004 : Replaced CrosshairInfo with CrosshairState. Renamed
062 * XYToolTipGenerator --> XYItemLabelGenerator (DG);
063 * 15-Jul-2004 : Switched getX() with getXValue() and getY() with
064 * getYValue() (DG);
065 * 11-Nov-2004 : Now uses ShapeUtilities to translate shapes (DG);
066 * 19-Jan-2005 : Now accesses primitives only from dataset (DG);
067 * 21-Mar-2005 : Override getLegendItem() and equals() methods (DG);
068 * 20-Apr-2005 : Use generators for legend tooltips and URLs (DG);
069 * ------------- JFREECHART 1.0.x ---------------------------------------------
070 * 06-Feb-2007 : Fixed bug 1086307, crosshairs with multiple axes (DG);
071 * 14-Feb-2007 : Fixed bug in clone() (DG);
072 * 20-Apr-2007 : Updated getLegendItem() for renderer change (DG);
073 * 04-May-2007 : Set processVisibleItemsOnly flag to false (DG);
074 * 17-May-2007 : Set datasetIndex and seriesIndex in getLegendItem() (DG);
075 * 18-May-2007 : Set dataset and seriesKey for LegendItem (DG);
076 *
077 */
078
079 package org.jfree.chart.renderer.xy;
080
081 import java.awt.Graphics2D;
082 import java.awt.Paint;
083 import java.awt.Polygon;
084 import java.awt.Shape;
085 import java.awt.Stroke;
086 import java.awt.geom.GeneralPath;
087 import java.awt.geom.Line2D;
088 import java.awt.geom.Rectangle2D;
089 import java.io.IOException;
090 import java.io.ObjectInputStream;
091 import java.io.ObjectOutputStream;
092 import java.io.Serializable;
093
094 import org.jfree.chart.LegendItem;
095 import org.jfree.chart.axis.ValueAxis;
096 import org.jfree.chart.entity.EntityCollection;
097 import org.jfree.chart.event.RendererChangeEvent;
098 import org.jfree.chart.labels.XYSeriesLabelGenerator;
099 import org.jfree.chart.labels.XYToolTipGenerator;
100 import org.jfree.chart.plot.CrosshairState;
101 import org.jfree.chart.plot.PlotOrientation;
102 import org.jfree.chart.plot.PlotRenderingInfo;
103 import org.jfree.chart.plot.XYPlot;
104 import org.jfree.chart.urls.XYURLGenerator;
105 import org.jfree.data.xy.XYDataset;
106 import org.jfree.io.SerialUtilities;
107 import org.jfree.util.PublicCloneable;
108 import org.jfree.util.ShapeUtilities;
109
110 /**
111 * Area item renderer for an {@link XYPlot}. This class can draw (a) shapes at
112 * each point, or (b) lines between points, or (c) both shapes and lines,
113 * or (d) filled areas, or (e) filled areas and shapes.
114 */
115 public class XYAreaRenderer extends AbstractXYItemRenderer
116 implements XYItemRenderer,
117 Cloneable,
118 PublicCloneable,
119 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, XYToolTipGenerator toolTipGenerator,
213 XYURLGenerator urlGenerator) {
214
215 super();
216 setBaseToolTipGenerator(toolTipGenerator);
217 setURLGenerator(urlGenerator);
218
219 if (type == SHAPES) {
220 this.plotShapes = true;
221 }
222 if (type == LINES) {
223 this.plotLines = true;
224 }
225 if (type == SHAPES_AND_LINES) {
226 this.plotShapes = true;
227 this.plotLines = true;
228 }
229 if (type == AREA) {
230 this.plotArea = true;
231 }
232 if (type == AREA_AND_SHAPES) {
233 this.plotArea = true;
234 this.plotShapes = true;
235 }
236 this.showOutline = false;
237 GeneralPath area = new GeneralPath();
238 area.moveTo(0.0f, -4.0f);
239 area.lineTo(3.0f, -2.0f);
240 area.lineTo(4.0f, 4.0f);
241 area.lineTo(-4.0f, 4.0f);
242 area.lineTo(-3.0f, -2.0f);
243 area.closePath();
244 this.legendArea = area;
245
246 }
247
248 /**
249 * Returns true if shapes are being plotted by the renderer.
250 *
251 * @return <code>true</code> if shapes are being plotted by the renderer.
252 */
253 public boolean getPlotShapes() {
254 return this.plotShapes;
255 }
256
257 /**
258 * Returns true if lines are being plotted by the renderer.
259 *
260 * @return <code>true</code> if lines are being plotted by the renderer.
261 */
262 public boolean getPlotLines() {
263 return this.plotLines;
264 }
265
266 /**
267 * Returns true if Area is being plotted by the renderer.
268 *
269 * @return <code>true</code> if Area is being plotted by the renderer.
270 */
271 public boolean getPlotArea() {
272 return this.plotArea;
273 }
274
275 /**
276 * Returns a flag that controls whether or not outlines of the areas are
277 * drawn.
278 *
279 * @return The flag.
280 *
281 * @see #setOutline(boolean)
282 */
283 public boolean isOutline() {
284 return this.showOutline;
285 }
286
287 /**
288 * Sets a flag that controls whether or not outlines of the areas are drawn
289 * and sends a {@link RendererChangeEvent} to all registered listeners.
290 *
291 * @param show the flag.
292 *
293 * @see #isOutline()
294 */
295 public void setOutline(boolean show) {
296 this.showOutline = show;
297 fireChangeEvent();
298 }
299
300 /**
301 * Returns the shape used to represent an area in the legend.
302 *
303 * @return The legend area (never <code>null</code>).
304 */
305 public Shape getLegendArea() {
306 return this.legendArea;
307 }
308
309 /**
310 * Sets the shape used as an area in each legend item and sends a
311 * {@link RendererChangeEvent} to all registered listeners.
312 *
313 * @param area the area (<code>null</code> not permitted).
314 */
315 public void setLegendArea(Shape area) {
316 if (area == null) {
317 throw new IllegalArgumentException("Null 'area' argument.");
318 }
319 this.legendArea = area;
320 fireChangeEvent();
321 }
322
323 /**
324 * Initialises the renderer and returns a state object that should be
325 * passed to all subsequent calls to the drawItem() method.
326 *
327 * @param g2 the graphics device.
328 * @param dataArea the area inside the axes.
329 * @param plot the plot.
330 * @param data the data.
331 * @param info an optional info collection object to return data back to
332 * the caller.
333 *
334 * @return A state object for use by the renderer.
335 */
336 public XYItemRendererState initialise(Graphics2D g2, Rectangle2D dataArea,
337 XYPlot plot, XYDataset data, PlotRenderingInfo info) {
338 XYAreaRendererState state = new XYAreaRendererState(info);
339
340 // in the rendering process, there is special handling for item
341 // zero, so we can't support processing of visible data items only
342 state.setProcessVisibleItemsOnly(false);
343 return state;
344 }
345
346 /**
347 * Returns a default legend item for the specified series. Subclasses
348 * should override this method to generate customised items.
349 *
350 * @param datasetIndex the dataset index (zero-based).
351 * @param series the series index (zero-based).
352 *
353 * @return A legend item for the series.
354 */
355 public LegendItem getLegendItem(int datasetIndex, int series) {
356 LegendItem result = null;
357 XYPlot xyplot = getPlot();
358 if (xyplot != null) {
359 XYDataset dataset = xyplot.getDataset(datasetIndex);
360 if (dataset != null) {
361 XYSeriesLabelGenerator lg = getLegendItemLabelGenerator();
362 String label = lg.generateLabel(dataset, series);
363 String description = label;
364 String toolTipText = null;
365 if (getLegendItemToolTipGenerator() != null) {
366 toolTipText = getLegendItemToolTipGenerator().generateLabel(
367 dataset, series);
368 }
369 String urlText = null;
370 if (getLegendItemURLGenerator() != null) {
371 urlText = getLegendItemURLGenerator().generateLabel(
372 dataset, series);
373 }
374 Paint paint = lookupSeriesPaint(series);
375 result = new LegendItem(label, description, toolTipText,
376 urlText, this.legendArea, paint);
377 result.setDataset(dataset);
378 result.setDatasetIndex(datasetIndex);
379 result.setSeriesKey(dataset.getSeriesKey(series));
380 result.setSeriesIndex(series);
381 }
382 }
383 return result;
384 }
385
386 /**
387 * Draws the visual representation of a single data item.
388 *
389 * @param g2 the graphics device.
390 * @param state the renderer state.
391 * @param dataArea the area within which the data is being drawn.
392 * @param info collects information about the drawing.
393 * @param plot the plot (can be used to obtain standard color information
394 * etc).
395 * @param domainAxis the domain axis.
396 * @param rangeAxis the range axis.
397 * @param dataset the dataset.
398 * @param series the series index (zero-based).
399 * @param item the item index (zero-based).
400 * @param crosshairState crosshair information for the plot
401 * (<code>null</code> permitted).
402 * @param pass the pass index.
403 */
404 public void drawItem(Graphics2D g2, XYItemRendererState state,
405 Rectangle2D dataArea, PlotRenderingInfo info, XYPlot plot,
406 ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset,
407 int series, int item, CrosshairState crosshairState, int pass) {
408
409 if (!getItemVisible(series, item)) {
410 return;
411 }
412 XYAreaRendererState areaState = (XYAreaRendererState) state;
413
414 // get the data point...
415 double x1 = dataset.getXValue(series, item);
416 double y1 = dataset.getYValue(series, item);
417 if (Double.isNaN(y1)) {
418 y1 = 0.0;
419 }
420 double transX1 = domainAxis.valueToJava2D(x1, dataArea,
421 plot.getDomainAxisEdge());
422 double transY1 = rangeAxis.valueToJava2D(y1, dataArea,
423 plot.getRangeAxisEdge());
424
425 // get the previous point and the next point so we can calculate a
426 // "hot spot" for the area (used by the chart entity)...
427 int itemCount = dataset.getItemCount(series);
428 double x0 = dataset.getXValue(series, Math.max(item - 1, 0));
429 double y0 = dataset.getYValue(series, Math.max(item - 1, 0));
430 if (Double.isNaN(y0)) {
431 y0 = 0.0;
432 }
433 double transX0 = domainAxis.valueToJava2D(x0, dataArea,
434 plot.getDomainAxisEdge());
435 double transY0 = rangeAxis.valueToJava2D(y0, dataArea,
436 plot.getRangeAxisEdge());
437
438 double x2 = dataset.getXValue(series, Math.min(item + 1,
439 itemCount - 1));
440 double y2 = dataset.getYValue(series, Math.min(item + 1,
441 itemCount - 1));
442 if (Double.isNaN(y2)) {
443 y2 = 0.0;
444 }
445 double transX2 = domainAxis.valueToJava2D(x2, dataArea,
446 plot.getDomainAxisEdge());
447 double transY2 = rangeAxis.valueToJava2D(y2, dataArea,
448 plot.getRangeAxisEdge());
449
450 double transZero = rangeAxis.valueToJava2D(0.0, dataArea,
451 plot.getRangeAxisEdge());
452 Polygon hotspot = null;
453 if (plot.getOrientation() == PlotOrientation.HORIZONTAL) {
454 hotspot = new Polygon();
455 hotspot.addPoint((int) transZero,
456 (int) ((transX0 + transX1) / 2.0));
457 hotspot.addPoint((int) ((transY0 + transY1) / 2.0),
458 (int) ((transX0 + transX1) / 2.0));
459 hotspot.addPoint((int) transY1, (int) transX1);
460 hotspot.addPoint((int) ((transY1 + transY2) / 2.0),
461 (int) ((transX1 + transX2) / 2.0));
462 hotspot.addPoint((int) transZero,
463 (int) ((transX1 + transX2) / 2.0));
464 }
465 else { // vertical orientation
466 hotspot = new Polygon();
467 hotspot.addPoint((int) ((transX0 + transX1) / 2.0),
468 (int) transZero);
469 hotspot.addPoint((int) ((transX0 + transX1) / 2.0),
470 (int) ((transY0 + transY1) / 2.0));
471 hotspot.addPoint((int) transX1, (int) transY1);
472 hotspot.addPoint((int) ((transX1 + transX2) / 2.0),
473 (int) ((transY1 + transY2) / 2.0));
474 hotspot.addPoint((int) ((transX1 + transX2) / 2.0),
475 (int) transZero);
476 }
477
478 if (item == 0) { // create a new area polygon for the series
479 areaState.area = new Polygon();
480 // the first point is (x, 0)
481 double zero = rangeAxis.valueToJava2D(0.0, dataArea,
482 plot.getRangeAxisEdge());
483 if (plot.getOrientation() == PlotOrientation.VERTICAL) {
484 areaState.area.addPoint((int) transX1, (int) zero);
485 }
486 else if (plot.getOrientation() == PlotOrientation.HORIZONTAL) {
487 areaState.area.addPoint((int) zero, (int) transX1);
488 }
489 }
490
491 // Add each point to Area (x, y)
492 if (plot.getOrientation() == PlotOrientation.VERTICAL) {
493 areaState.area.addPoint((int) transX1, (int) transY1);
494 }
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, transX1,
510 transY1);
511 }
512 else if (orientation == PlotOrientation.HORIZONTAL) {
513 shape = ShapeUtilities.createTranslatedShape(shape, transY1,
514 transX1);
515 }
516 g2.draw(shape);
517 }
518
519 if (getPlotLines()) {
520 if (item > 0) {
521 if (plot.getOrientation() == PlotOrientation.VERTICAL) {
522 areaState.line.setLine(transX0, transY0, transX1, transY1);
523 }
524 else if (plot.getOrientation() == PlotOrientation.HORIZONTAL) {
525 areaState.line.setLine(transY0, transX0, transY1, 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 }
539 else if (orientation == PlotOrientation.HORIZONTAL) {
540 // Add the last point (x,0)
541 areaState.area.addPoint((int) transZero, (int) transX1);
542 }
543
544 g2.fill(areaState.area);
545
546 // draw an outline around the Area.
547 if (isOutline()) {
548 g2.setStroke(getItemOutlineStroke(series, item));
549 g2.setPaint(getItemOutlinePaint(series, item));
550 g2.draw(areaState.area);
551 }
552 }
553
554 int domainAxisIndex = plot.getDomainAxisIndex(domainAxis);
555 int rangeAxisIndex = plot.getRangeAxisIndex(rangeAxis);
556 updateCrosshairValues(crosshairState, x1, y1, domainAxisIndex,
557 rangeAxisIndex, transX1, transY1, orientation);
558
559 // collect entity and tool tip information...
560 EntityCollection entities = state.getEntityCollection();
561 if (entities != null && hotspot != null) {
562 addEntity(entities, hotspot, dataset, series, item, 0.0, 0.0);
563 }
564
565 }
566
567 /**
568 * Returns a clone of the renderer.
569 *
570 * @return A clone.
571 *
572 * @throws CloneNotSupportedException if the renderer cannot be cloned.
573 */
574 public Object clone() throws CloneNotSupportedException {
575 XYAreaRenderer clone = (XYAreaRenderer) super.clone();
576 clone.legendArea = ShapeUtilities.clone(this.legendArea);
577 return clone;
578 }
579
580 /**
581 * Tests this renderer for equality with an arbitrary object.
582 *
583 * @param obj the object (<code>null</code> permitted).
584 *
585 * @return A boolean.
586 */
587 public boolean equals(Object obj) {
588 if (obj == this) {
589 return true;
590 }
591 if (!(obj instanceof XYAreaRenderer)) {
592 return false;
593 }
594 XYAreaRenderer that = (XYAreaRenderer) obj;
595 if (this.plotArea != that.plotArea) {
596 return false;
597 }
598 if (this.plotLines != that.plotLines) {
599 return false;
600 }
601 if (this.plotShapes != that.plotShapes) {
602 return false;
603 }
604 if (this.showOutline != that.showOutline) {
605 return false;
606 }
607 if (!ShapeUtilities.equal(this.legendArea, that.legendArea)) {
608 return false;
609 }
610 return true;
611 }
612
613 /**
614 * Provides serialization support.
615 *
616 * @param stream the input stream.
617 *
618 * @throws IOException if there is an I/O error.
619 * @throws ClassNotFoundException if there is a classpath problem.
620 */
621 private void readObject(ObjectInputStream stream)
622 throws IOException, ClassNotFoundException {
623 stream.defaultReadObject();
624 this.legendArea = SerialUtilities.readShape(stream);
625 }
626
627 /**
628 * Provides serialization support.
629 *
630 * @param stream the output stream.
631 *
632 * @throws IOException if there is an I/O error.
633 */
634 private void writeObject(ObjectOutputStream stream) throws IOException {
635 stream.defaultWriteObject();
636 SerialUtilities.writeShape(this.legendArea, stream);
637 }
638 }