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.7 2007/02/14 15:05:32 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 * 075 */ 076 077 package org.jfree.chart.renderer.xy; 078 079 import java.awt.Graphics2D; 080 import java.awt.Paint; 081 import java.awt.Polygon; 082 import java.awt.Shape; 083 import java.awt.Stroke; 084 import java.awt.geom.GeneralPath; 085 import java.awt.geom.Line2D; 086 import java.awt.geom.Rectangle2D; 087 import java.io.IOException; 088 import java.io.ObjectInputStream; 089 import java.io.ObjectOutputStream; 090 import java.io.Serializable; 091 092 import org.jfree.chart.LegendItem; 093 import org.jfree.chart.axis.ValueAxis; 094 import org.jfree.chart.entity.EntityCollection; 095 import org.jfree.chart.entity.XYItemEntity; 096 import org.jfree.chart.event.RendererChangeEvent; 097 import org.jfree.chart.labels.XYSeriesLabelGenerator; 098 import org.jfree.chart.labels.XYToolTipGenerator; 099 import org.jfree.chart.plot.CrosshairState; 100 import org.jfree.chart.plot.PlotOrientation; 101 import org.jfree.chart.plot.PlotRenderingInfo; 102 import org.jfree.chart.plot.XYPlot; 103 import org.jfree.chart.urls.XYURLGenerator; 104 import org.jfree.data.xy.XYDataset; 105 import org.jfree.io.SerialUtilities; 106 import org.jfree.util.PublicCloneable; 107 import org.jfree.util.ShapeUtilities; 108 109 /** 110 * Area item renderer for an {@link XYPlot}. This class can draw (a) shapes at 111 * each point, or (b) lines between points, or (c) both shapes and lines, 112 * or (d) filled areas, or (e) filled areas and shapes. 113 */ 114 public class XYAreaRenderer extends AbstractXYItemRenderer 115 implements XYItemRenderer, 116 Cloneable, 117 PublicCloneable, 118 Serializable { 119 120 /** For serialization. */ 121 private static final long serialVersionUID = -4481971353973876747L; 122 123 /** 124 * A state object used by this renderer. 125 */ 126 static class XYAreaRendererState extends XYItemRendererState { 127 128 /** Working storage for the area under one series. */ 129 public Polygon area; 130 131 /** Working line that can be recycled. */ 132 public Line2D line; 133 134 /** 135 * Creates a new state. 136 * 137 * @param info the plot rendering info. 138 */ 139 public XYAreaRendererState(PlotRenderingInfo info) { 140 super(info); 141 this.area = new Polygon(); 142 this.line = new Line2D.Double(); 143 } 144 145 } 146 147 /** Useful constant for specifying the type of rendering (shapes only). */ 148 public static final int SHAPES = 1; 149 150 /** Useful constant for specifying the type of rendering (lines only). */ 151 public static final int LINES = 2; 152 153 /** 154 * Useful constant for specifying the type of rendering (shapes and lines). 155 */ 156 public static final int SHAPES_AND_LINES = 3; 157 158 /** Useful constant for specifying the type of rendering (area only). */ 159 public static final int AREA = 4; 160 161 /** 162 * Useful constant for specifying the type of rendering (area and shapes). 163 */ 164 public static final int AREA_AND_SHAPES = 5; 165 166 /** A flag indicating whether or not shapes are drawn at each XY point. */ 167 private boolean plotShapes; 168 169 /** A flag indicating whether or not lines are drawn between XY points. */ 170 private boolean plotLines; 171 172 /** A flag indicating whether or not Area are drawn at each XY point. */ 173 private boolean plotArea; 174 175 /** A flag that controls whether or not the outline is shown. */ 176 private boolean showOutline; 177 178 /** 179 * The shape used to represent an area in each legend item (this should 180 * never be <code>null</code>). 181 */ 182 private transient Shape legendArea; 183 184 /** 185 * Constructs a new renderer. 186 */ 187 public XYAreaRenderer() { 188 this(AREA); 189 } 190 191 /** 192 * Constructs a new renderer. 193 * 194 * @param type the type of the renderer. 195 */ 196 public XYAreaRenderer(int type) { 197 this(type, null, null); 198 } 199 200 /** 201 * Constructs a new renderer. To specify the type of renderer, use one of 202 * the constants: <code>SHAPES</code>, <code>LINES</code>, 203 * <code>SHAPES_AND_LINES</code>, <code>AREA</code> or 204 * <code>AREA_AND_SHAPES</code>. 205 * 206 * @param type the type of renderer. 207 * @param toolTipGenerator the tool tip generator to use 208 * (<code>null</code> permitted). 209 * @param urlGenerator the URL generator (<code>null</code> permitted). 210 */ 211 public XYAreaRenderer(int type, XYToolTipGenerator toolTipGenerator, 212 XYURLGenerator urlGenerator) { 213 214 super(); 215 setBaseToolTipGenerator(toolTipGenerator); 216 setURLGenerator(urlGenerator); 217 218 if (type == SHAPES) { 219 this.plotShapes = true; 220 } 221 if (type == LINES) { 222 this.plotLines = true; 223 } 224 if (type == SHAPES_AND_LINES) { 225 this.plotShapes = true; 226 this.plotLines = true; 227 } 228 if (type == AREA) { 229 this.plotArea = true; 230 } 231 if (type == AREA_AND_SHAPES) { 232 this.plotArea = true; 233 this.plotShapes = true; 234 } 235 this.showOutline = false; 236 GeneralPath area = new GeneralPath(); 237 area.moveTo(0.0f, -4.0f); 238 area.lineTo(3.0f, -2.0f); 239 area.lineTo(4.0f, 4.0f); 240 area.lineTo(-4.0f, 4.0f); 241 area.lineTo(-3.0f, -2.0f); 242 area.closePath(); 243 this.legendArea = area; 244 245 } 246 247 /** 248 * Returns true if shapes are being plotted by the renderer. 249 * 250 * @return <code>true</code> if shapes are being plotted by the renderer. 251 */ 252 public boolean getPlotShapes() { 253 return this.plotShapes; 254 } 255 256 /** 257 * Returns true if lines are being plotted by the renderer. 258 * 259 * @return <code>true</code> if lines are being plotted by the renderer. 260 */ 261 public boolean getPlotLines() { 262 return this.plotLines; 263 } 264 265 /** 266 * Returns true if Area is being plotted by the renderer. 267 * 268 * @return <code>true</code> if Area is being plotted by the renderer. 269 */ 270 public boolean getPlotArea() { 271 return this.plotArea; 272 } 273 274 /** 275 * Returns a flag that controls whether or not outlines of the areas are 276 * drawn. 277 * 278 * @return The flag. 279 * 280 * @see #setOutline(boolean) 281 */ 282 public boolean isOutline() { 283 return this.showOutline; 284 } 285 286 /** 287 * Sets a flag that controls whether or not outlines of the areas are drawn 288 * and sends a {@link RendererChangeEvent} to all registered listeners. 289 * 290 * @param show the flag. 291 * 292 * @see #isOutline() 293 */ 294 public void setOutline(boolean show) { 295 this.showOutline = show; 296 notifyListeners(new RendererChangeEvent(this)); 297 } 298 299 /** 300 * Returns the shape used to represent an area in the legend. 301 * 302 * @return The legend area (never <code>null</code>). 303 */ 304 public Shape getLegendArea() { 305 return this.legendArea; 306 } 307 308 /** 309 * Sets the shape used as an area in each legend item and sends a 310 * {@link RendererChangeEvent} to all registered listeners. 311 * 312 * @param area the area (<code>null</code> not permitted). 313 */ 314 public void setLegendArea(Shape area) { 315 if (area == null) { 316 throw new IllegalArgumentException("Null 'area' argument."); 317 } 318 this.legendArea = area; 319 notifyListeners(new RendererChangeEvent(this)); 320 } 321 322 /** 323 * Initialises the renderer and returns a state object that should be 324 * passed to all subsequent calls to the drawItem() method. 325 * 326 * @param g2 the graphics device. 327 * @param dataArea the area inside the axes. 328 * @param plot the plot. 329 * @param data the data. 330 * @param info an optional info collection object to return data back to 331 * the caller. 332 * 333 * @return A state object for use by the renderer. 334 */ 335 public XYItemRendererState initialise(Graphics2D g2, Rectangle2D dataArea, 336 XYPlot plot, XYDataset data, PlotRenderingInfo info) { 337 XYAreaRendererState state = new XYAreaRendererState(info); 338 return state; 339 } 340 341 /** 342 * Returns a default legend item for the specified series. Subclasses 343 * should override this method to generate customised items. 344 * 345 * @param datasetIndex the dataset index (zero-based). 346 * @param series the series index (zero-based). 347 * 348 * @return A legend item for the series. 349 */ 350 public LegendItem getLegendItem(int datasetIndex, int series) { 351 LegendItem result = null; 352 XYPlot xyplot = getPlot(); 353 if (xyplot != null) { 354 XYDataset dataset = xyplot.getDataset(datasetIndex); 355 if (dataset != null) { 356 XYSeriesLabelGenerator lg = getLegendItemLabelGenerator(); 357 String label = lg.generateLabel(dataset, series); 358 String description = label; 359 String toolTipText = null; 360 if (getLegendItemToolTipGenerator() != null) { 361 toolTipText = getLegendItemToolTipGenerator().generateLabel( 362 dataset, series); 363 } 364 String urlText = null; 365 if (getLegendItemURLGenerator() != null) { 366 urlText = getLegendItemURLGenerator().generateLabel( 367 dataset, series); 368 } 369 Paint paint = getSeriesPaint(series); 370 result = new LegendItem(label, description, toolTipText, 371 urlText, this.legendArea, paint); 372 } 373 } 374 return result; 375 } 376 377 /** 378 * Draws the visual representation of a single data item. 379 * 380 * @param g2 the graphics device. 381 * @param state the renderer state. 382 * @param dataArea the area within which the data is being drawn. 383 * @param info collects information about the drawing. 384 * @param plot the plot (can be used to obtain standard color information 385 * etc). 386 * @param domainAxis the domain axis. 387 * @param rangeAxis the range axis. 388 * @param dataset the dataset. 389 * @param series the series index (zero-based). 390 * @param item the item index (zero-based). 391 * @param crosshairState crosshair information for the plot 392 * (<code>null</code> permitted). 393 * @param pass the pass index. 394 */ 395 public void drawItem(Graphics2D g2, XYItemRendererState state, 396 Rectangle2D dataArea, PlotRenderingInfo info, XYPlot plot, 397 ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset, 398 int series, int item, CrosshairState crosshairState, int pass) { 399 400 if (!getItemVisible(series, item)) { 401 return; 402 } 403 XYAreaRendererState areaState = (XYAreaRendererState) state; 404 405 // get the data point... 406 double x1 = dataset.getXValue(series, item); 407 double y1 = dataset.getYValue(series, item); 408 if (Double.isNaN(y1)) { 409 y1 = 0.0; 410 } 411 double transX1 = domainAxis.valueToJava2D(x1, dataArea, 412 plot.getDomainAxisEdge()); 413 double transY1 = rangeAxis.valueToJava2D(y1, dataArea, 414 plot.getRangeAxisEdge()); 415 416 // get the previous point and the next point so we can calculate a 417 // "hot spot" for the area (used by the chart entity)... 418 int itemCount = dataset.getItemCount(series); 419 double x0 = dataset.getXValue(series, Math.max(item - 1, 0)); 420 double y0 = dataset.getYValue(series, Math.max(item - 1, 0)); 421 if (Double.isNaN(y0)) { 422 y0 = 0.0; 423 } 424 double transX0 = domainAxis.valueToJava2D(x0, dataArea, 425 plot.getDomainAxisEdge()); 426 double transY0 = rangeAxis.valueToJava2D(y0, dataArea, 427 plot.getRangeAxisEdge()); 428 429 double x2 = dataset.getXValue(series, Math.min(item + 1, 430 itemCount - 1)); 431 double y2 = dataset.getYValue(series, Math.min(item + 1, 432 itemCount - 1)); 433 if (Double.isNaN(y2)) { 434 y2 = 0.0; 435 } 436 double transX2 = domainAxis.valueToJava2D(x2, dataArea, 437 plot.getDomainAxisEdge()); 438 double transY2 = rangeAxis.valueToJava2D(y2, dataArea, 439 plot.getRangeAxisEdge()); 440 441 double transZero = rangeAxis.valueToJava2D(0.0, dataArea, 442 plot.getRangeAxisEdge()); 443 Polygon hotspot = null; 444 if (plot.getOrientation() == PlotOrientation.HORIZONTAL) { 445 hotspot = new Polygon(); 446 hotspot.addPoint((int) transZero, 447 (int) ((transX0 + transX1) / 2.0)); 448 hotspot.addPoint((int) ((transY0 + transY1) / 2.0), 449 (int) ((transX0 + transX1) / 2.0)); 450 hotspot.addPoint((int) transY1, (int) transX1); 451 hotspot.addPoint((int) ((transY1 + transY2) / 2.0), 452 (int) ((transX1 + transX2) / 2.0)); 453 hotspot.addPoint((int) transZero, 454 (int) ((transX1 + transX2) / 2.0)); 455 } 456 else { // vertical orientation 457 hotspot = new Polygon(); 458 hotspot.addPoint((int) ((transX0 + transX1) / 2.0), 459 (int) transZero); 460 hotspot.addPoint((int) ((transX0 + transX1) / 2.0), 461 (int) ((transY0 + transY1) / 2.0)); 462 hotspot.addPoint((int) transX1, (int) transY1); 463 hotspot.addPoint((int) ((transX1 + transX2) / 2.0), 464 (int) ((transY1 + transY2) / 2.0)); 465 hotspot.addPoint((int) ((transX1 + transX2) / 2.0), 466 (int) transZero); 467 } 468 469 if (item == 0) { // create a new area polygon for the series 470 areaState.area = new Polygon(); 471 // the first point is (x, 0) 472 double zero = rangeAxis.valueToJava2D(0.0, dataArea, 473 plot.getRangeAxisEdge()); 474 if (plot.getOrientation() == PlotOrientation.VERTICAL) { 475 areaState.area.addPoint((int) transX1, (int) zero); 476 } 477 else if (plot.getOrientation() == PlotOrientation.HORIZONTAL) { 478 areaState.area.addPoint((int) zero, (int) transX1); 479 } 480 } 481 482 // Add each point to Area (x, y) 483 if (plot.getOrientation() == PlotOrientation.VERTICAL) { 484 areaState.area.addPoint((int) transX1, (int) transY1); 485 } 486 else if (plot.getOrientation() == PlotOrientation.HORIZONTAL) { 487 areaState.area.addPoint((int) transY1, (int) transX1); 488 } 489 490 PlotOrientation orientation = plot.getOrientation(); 491 Paint paint = getItemPaint(series, item); 492 Stroke stroke = getItemStroke(series, item); 493 g2.setPaint(paint); 494 g2.setStroke(stroke); 495 496 Shape shape = null; 497 if (getPlotShapes()) { 498 shape = getItemShape(series, item); 499 if (orientation == PlotOrientation.VERTICAL) { 500 shape = ShapeUtilities.createTranslatedShape(shape, transX1, 501 transY1); 502 } 503 else if (orientation == PlotOrientation.HORIZONTAL) { 504 shape = ShapeUtilities.createTranslatedShape(shape, transY1, 505 transX1); 506 } 507 g2.draw(shape); 508 } 509 510 if (getPlotLines()) { 511 if (item > 0) { 512 if (plot.getOrientation() == PlotOrientation.VERTICAL) { 513 areaState.line.setLine(transX0, transY0, transX1, transY1); 514 } 515 else if (plot.getOrientation() == PlotOrientation.HORIZONTAL) { 516 areaState.line.setLine(transY0, transX0, transY1, transX1); 517 } 518 g2.draw(areaState.line); 519 } 520 } 521 522 // Check if the item is the last item for the series. 523 // and number of items > 0. We can't draw an area for a single point. 524 if (getPlotArea() && item > 0 && item == (itemCount - 1)) { 525 526 if (orientation == PlotOrientation.VERTICAL) { 527 // Add the last point (x,0) 528 areaState.area.addPoint((int) transX1, (int) transZero); 529 } 530 else if (orientation == PlotOrientation.HORIZONTAL) { 531 // Add the last point (x,0) 532 areaState.area.addPoint((int) transZero, (int) transX1); 533 } 534 535 g2.fill(areaState.area); 536 537 // draw an outline around the Area. 538 if (isOutline()) { 539 g2.setStroke(getItemOutlineStroke(series, item)); 540 g2.setPaint(getItemOutlinePaint(series, item)); 541 g2.draw(areaState.area); 542 } 543 } 544 545 int domainAxisIndex = plot.getDomainAxisIndex(domainAxis); 546 int rangeAxisIndex = plot.getRangeAxisIndex(rangeAxis); 547 updateCrosshairValues(crosshairState, x1, y1, domainAxisIndex, 548 rangeAxisIndex, transX1, transY1, orientation); 549 550 // collect entity and tool tip information... 551 if (state.getInfo() != null) { 552 EntityCollection entities = state.getEntityCollection(); 553 if (entities != null && hotspot != null) { 554 String tip = null; 555 XYToolTipGenerator generator 556 = getToolTipGenerator(series, item); 557 if (generator != null) { 558 tip = generator.generateToolTip(dataset, series, item); 559 } 560 String url = null; 561 if (getURLGenerator() != null) { 562 url = getURLGenerator().generateURL(dataset, series, item); 563 } 564 XYItemEntity entity = new XYItemEntity(hotspot, dataset, 565 series, item, tip, url); 566 entities.add(entity); 567 } 568 } 569 570 } 571 572 /** 573 * Returns a clone of the renderer. 574 * 575 * @return A clone. 576 * 577 * @throws CloneNotSupportedException if the renderer cannot be cloned. 578 */ 579 public Object clone() throws CloneNotSupportedException { 580 XYAreaRenderer clone = (XYAreaRenderer) super.clone(); 581 clone.legendArea = ShapeUtilities.clone(this.legendArea); 582 return clone; 583 } 584 585 /** 586 * Tests this renderer for equality with an arbitrary object. 587 * 588 * @param obj the object (<code>null</code> permitted). 589 * 590 * @return A boolean. 591 */ 592 public boolean equals(Object obj) { 593 if (obj == this) { 594 return true; 595 } 596 if (!(obj instanceof XYAreaRenderer)) { 597 return false; 598 } 599 XYAreaRenderer that = (XYAreaRenderer) obj; 600 if (this.plotArea != that.plotArea) { 601 return false; 602 } 603 if (this.plotLines != that.plotLines) { 604 return false; 605 } 606 if (this.plotShapes != that.plotShapes) { 607 return false; 608 } 609 if (this.showOutline != that.showOutline) { 610 return false; 611 } 612 if (!ShapeUtilities.equal(this.legendArea, that.legendArea)) { 613 return false; 614 } 615 return true; 616 } 617 618 /** 619 * Provides serialization support. 620 * 621 * @param stream the input stream. 622 * 623 * @throws IOException if there is an I/O error. 624 * @throws ClassNotFoundException if there is a classpath problem. 625 */ 626 private void readObject(ObjectInputStream stream) 627 throws IOException, ClassNotFoundException { 628 stream.defaultReadObject(); 629 this.legendArea = SerialUtilities.readShape(stream); 630 } 631 632 /** 633 * Provides serialization support. 634 * 635 * @param stream the output stream. 636 * 637 * @throws IOException if there is an I/O error. 638 */ 639 private void writeObject(ObjectOutputStream stream) throws IOException { 640 stream.defaultWriteObject(); 641 SerialUtilities.writeShape(this.legendArea, stream); 642 } 643 }