001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2017, 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 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 025 * Other names may be trademarks of their respective owners.] 026 * 027 * -------------------- 028 * XYAreaRenderer2.java 029 * -------------------- 030 * (C) Copyright 2004-2017, 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 * Martin Krauskopf; 037 * Ulrich Voigt (patch #312); 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 049 * HTML 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 062 * overriding easier. Also moved state class into this 063 * class (DG); 064 * 25-Feb-2004 : Replaced CrosshairInfo with CrosshairState. Renamed 065 * XYToolTipGenerator --> XYItemLabelGenerator (DG); 066 * 15-Jul-2004 : Switched getX() with getXValue() and getY() with 067 * getYValue() (DG); 068 * 11-Nov-2004 : Now uses ShapeUtilities to translate shapes (DG); 069 * 19-Jan-2005 : Now accesses only primitives from the dataset (DG); 070 * 21-Mar-2005 : Override getLegendItem() (DG); 071 * 20-Apr-2005 : Use generators for legend tooltips and URLs (DG); 072 * ------------- JFREECHART 1.0.x --------------------------------------------- 073 * 30-Nov-2006 : Fixed equals() and clone() implementations (DG); 074 * 06-Feb-2007 : Fixed bug 1086307, crosshairs with multiple axes (DG); 075 * 20-Apr-2007 : Updated getLegendItem() and drawItem() for renderer 076 * change (DG); 077 * 17-May-2007 : Set datasetIndex and seriesIndex in getLegendItem() (DG); 078 * 18-May-2007 : Set dataset and seriesKey for LegendItem (DG); 079 * 17-Jun-2008 : Apply legend font and paint attributes (DG); 080 * 06-Oct-2011 : Avoid GeneralPath methods requiring Java 1.5 (MK); 081 * 03-Jul-2013 : Use ParamChecks (DG); 082 * 04-Aug-2014 : Restrict entity hotspot to plot area (patch #312) (UV); 083 * 18-Feb-2017 : Updates for crosshairs (bug #36) (DG); 084 * 085 */ 086 087package org.jfree.chart.renderer.xy; 088 089import java.awt.Graphics2D; 090import java.awt.Paint; 091import java.awt.Shape; 092import java.awt.Stroke; 093import java.awt.geom.Area; 094import java.awt.geom.GeneralPath; 095import java.awt.geom.Rectangle2D; 096import java.io.IOException; 097import java.io.ObjectInputStream; 098import java.io.ObjectOutputStream; 099 100import org.jfree.chart.LegendItem; 101import org.jfree.chart.axis.ValueAxis; 102import org.jfree.chart.entity.EntityCollection; 103import org.jfree.chart.entity.XYItemEntity; 104import org.jfree.chart.event.RendererChangeEvent; 105import org.jfree.chart.labels.XYSeriesLabelGenerator; 106import org.jfree.chart.labels.XYToolTipGenerator; 107import org.jfree.chart.plot.CrosshairState; 108import org.jfree.chart.plot.PlotOrientation; 109import org.jfree.chart.plot.PlotRenderingInfo; 110import org.jfree.chart.plot.XYPlot; 111import org.jfree.chart.urls.XYURLGenerator; 112import org.jfree.chart.util.Args; 113import org.jfree.chart.util.PublicCloneable; 114import org.jfree.chart.util.SerialUtils; 115import org.jfree.chart.util.ShapeUtils; 116import org.jfree.data.xy.XYDataset; 117 118/** 119 * Area item renderer for an {@link XYPlot}. The example shown here is 120 * generated by the {@code XYAreaRenderer2Demo1.java} program included in 121 * the JFreeChart demo collection: 122 * <br><br> 123 * <img src="../../../../../images/XYAreaRenderer2Sample.png" 124 * alt="XYAreaRenderer2Sample.png"> 125 */ 126public class XYAreaRenderer2 extends AbstractXYItemRenderer 127 implements XYItemRenderer, PublicCloneable { 128 129 /** For serialization. */ 130 private static final long serialVersionUID = -7378069681579984133L; 131 132 /** A flag that controls whether or not the outline is shown. */ 133 private boolean showOutline; 134 135 /** 136 * The shape used to represent an area in each legend item (this should 137 * never be {@code null}). 138 */ 139 private transient Shape legendArea; 140 141 /** 142 * Constructs a new renderer. 143 */ 144 public XYAreaRenderer2() { 145 this(null, null); 146 } 147 148 /** 149 * Constructs a new renderer. 150 * 151 * @param labelGenerator the tool tip generator to use ({@code null} 152 * permitted). 153 * @param urlGenerator the URL generator ({@code null} permitted). 154 */ 155 public XYAreaRenderer2(XYToolTipGenerator labelGenerator, 156 XYURLGenerator urlGenerator) { 157 super(); 158 this.showOutline = false; 159 setDefaultToolTipGenerator(labelGenerator); 160 setURLGenerator(urlGenerator); 161 GeneralPath area = new GeneralPath(); 162 area.moveTo(0.0f, -4.0f); 163 area.lineTo(3.0f, -2.0f); 164 area.lineTo(4.0f, 4.0f); 165 area.lineTo(-4.0f, 4.0f); 166 area.lineTo(-3.0f, -2.0f); 167 area.closePath(); 168 this.legendArea = area; 169 } 170 171 /** 172 * Returns a flag that controls whether or not outlines of the areas are 173 * drawn. 174 * 175 * @return The flag. 176 * 177 * @see #setOutline(boolean) 178 */ 179 public boolean isOutline() { 180 return this.showOutline; 181 } 182 183 /** 184 * Sets a flag that controls whether or not outlines of the areas are 185 * drawn, and sends a {@link RendererChangeEvent} to all registered 186 * listeners. 187 * 188 * @param show the flag. 189 * 190 * @see #isOutline() 191 */ 192 public void setOutline(boolean show) { 193 this.showOutline = show; 194 fireChangeEvent(); 195 } 196 197 /** 198 * Returns the shape used to represent an area in the legend. 199 * 200 * @return The legend area (never {@code null}). 201 * 202 * @see #setLegendArea(Shape) 203 */ 204 public Shape getLegendArea() { 205 return this.legendArea; 206 } 207 208 /** 209 * Sets the shape used as an area in each legend item and sends a 210 * {@link RendererChangeEvent} to all registered listeners. 211 * 212 * @param area the area ({@code null} not permitted). 213 * 214 * @see #getLegendArea() 215 */ 216 public void setLegendArea(Shape area) { 217 Args.nullNotPermitted(area, "area"); 218 this.legendArea = area; 219 fireChangeEvent(); 220 } 221 222 /** 223 * Returns a default legend item for the specified series. Subclasses 224 * should override this method to generate customised items. 225 * 226 * @param datasetIndex the dataset index (zero-based). 227 * @param series the series index (zero-based). 228 * 229 * @return A legend item for the series. 230 */ 231 @Override 232 public LegendItem getLegendItem(int datasetIndex, int series) { 233 LegendItem result = null; 234 XYPlot xyplot = getPlot(); 235 if (xyplot != null) { 236 XYDataset dataset = xyplot.getDataset(datasetIndex); 237 if (dataset != null) { 238 XYSeriesLabelGenerator lg = getLegendItemLabelGenerator(); 239 String label = lg.generateLabel(dataset, series); 240 String description = label; 241 String toolTipText = null; 242 if (getLegendItemToolTipGenerator() != null) { 243 toolTipText = getLegendItemToolTipGenerator().generateLabel( 244 dataset, series); 245 } 246 String urlText = null; 247 if (getLegendItemURLGenerator() != null) { 248 urlText = getLegendItemURLGenerator().generateLabel( 249 dataset, series); 250 } 251 Paint paint = lookupSeriesPaint(series); 252 result = new LegendItem(label, description, toolTipText, 253 urlText, this.legendArea, paint); 254 result.setLabelFont(lookupLegendTextFont(series)); 255 Paint labelPaint = lookupLegendTextPaint(series); 256 if (labelPaint != null) { 257 result.setLabelPaint(labelPaint); 258 } 259 result.setDataset(dataset); 260 result.setDatasetIndex(datasetIndex); 261 result.setSeriesKey(dataset.getSeriesKey(series)); 262 result.setSeriesIndex(series); 263 } 264 } 265 return result; 266 } 267 268 /** 269 * Draws the visual representation of a single data item. 270 * 271 * @param g2 the graphics device. 272 * @param state the renderer state. 273 * @param dataArea the area within which the data is being drawn. 274 * @param info collects information about the drawing. 275 * @param plot the plot (can be used to obtain standard color 276 * information etc). 277 * @param domainAxis the domain axis. 278 * @param rangeAxis the range axis. 279 * @param dataset the dataset. 280 * @param series the series index (zero-based). 281 * @param item the item index (zero-based). 282 * @param crosshairState crosshair information for the plot 283 * ({@code null} permitted). 284 * @param pass the pass index. 285 */ 286 @Override 287 public void drawItem(Graphics2D g2, XYItemRendererState state, 288 Rectangle2D dataArea, PlotRenderingInfo info, XYPlot plot, 289 ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset, 290 int series, int item, CrosshairState crosshairState, int pass) { 291 292 if (!getItemVisible(series, item)) { 293 return; 294 } 295 // get the data point... 296 double x1 = dataset.getXValue(series, item); 297 double y1 = dataset.getYValue(series, item); 298 if (Double.isNaN(y1)) { 299 y1 = 0.0; 300 } 301 302 double transX1 = domainAxis.valueToJava2D(x1, dataArea, 303 plot.getDomainAxisEdge()); 304 double transY1 = rangeAxis.valueToJava2D(y1, dataArea, 305 plot.getRangeAxisEdge()); 306 307 // get the previous point and the next point so we can calculate a 308 // "hot spot" for the area (used by the chart entity)... 309 double x0 = dataset.getXValue(series, Math.max(item - 1, 0)); 310 double y0 = dataset.getYValue(series, Math.max(item - 1, 0)); 311 if (Double.isNaN(y0)) { 312 y0 = 0.0; 313 } 314 double transX0 = domainAxis.valueToJava2D(x0, dataArea, 315 plot.getDomainAxisEdge()); 316 double transY0 = rangeAxis.valueToJava2D(y0, dataArea, 317 plot.getRangeAxisEdge()); 318 319 int itemCount = dataset.getItemCount(series); 320 double x2 = dataset.getXValue(series, Math.min(item + 1, 321 itemCount - 1)); 322 double y2 = dataset.getYValue(series, Math.min(item + 1, 323 itemCount - 1)); 324 if (Double.isNaN(y2)) { 325 y2 = 0.0; 326 } 327 double transX2 = domainAxis.valueToJava2D(x2, dataArea, 328 plot.getDomainAxisEdge()); 329 double transY2 = rangeAxis.valueToJava2D(y2, dataArea, 330 plot.getRangeAxisEdge()); 331 332 double transZero = rangeAxis.valueToJava2D(0.0, dataArea, 333 plot.getRangeAxisEdge()); 334 GeneralPath hotspot = new GeneralPath(); 335 if (plot.getOrientation() == PlotOrientation.HORIZONTAL) { 336 moveTo(hotspot, transZero, ((transX0 + transX1) / 2.0)); 337 lineTo(hotspot, ((transY0 + transY1) / 2.0), 338 ((transX0 + transX1) / 2.0)); 339 lineTo(hotspot, transY1, transX1); 340 lineTo(hotspot, ((transY1 + transY2) / 2.0), 341 ((transX1 + transX2) / 2.0)); 342 lineTo(hotspot, transZero, ((transX1 + transX2) / 2.0)); 343 } 344 else { // vertical orientation 345 moveTo(hotspot, ((transX0 + transX1) / 2.0), transZero); 346 lineTo(hotspot, ((transX0 + transX1) / 2.0), 347 ((transY0 + transY1) / 2.0)); 348 lineTo(hotspot, transX1, transY1); 349 lineTo(hotspot, ((transX1 + transX2) / 2.0), 350 ((transY1 + transY2) / 2.0)); 351 lineTo(hotspot, ((transX1 + transX2) / 2.0), transZero); 352 } 353 hotspot.closePath(); 354 355 PlotOrientation orientation = plot.getOrientation(); 356 Paint paint = getItemPaint(series, item); 357 Stroke stroke = getItemStroke(series, item); 358 g2.setPaint(paint); 359 g2.setStroke(stroke); 360 361 // Check if the item is the last item for the series. 362 // and number of items > 0. We can't draw an area for a single point. 363 g2.fill(hotspot); 364 365 // draw an outline around the Area. 366 if (isOutline()) { 367 g2.setStroke(lookupSeriesOutlineStroke(series)); 368 g2.setPaint(lookupSeriesOutlinePaint(series)); 369 g2.draw(hotspot); 370 } 371 int datasetIndex = plot.indexOf(dataset); 372 updateCrosshairValues(crosshairState, x1, y1, datasetIndex, 373 transX1, transY1, orientation); 374 375 // collect entity and tool tip information... 376 if (state.getInfo() != null) { 377 EntityCollection entities = state.getEntityCollection(); 378 if (entities != null) { 379 // limit the entity hotspot area to the data area 380 Area dataAreaHotspot = new Area(hotspot); 381 dataAreaHotspot.intersect(new Area(dataArea)); 382 if (!dataAreaHotspot.isEmpty()) { 383 String tip = null; 384 XYToolTipGenerator generator = getToolTipGenerator(series, 385 item); 386 if (generator != null) { 387 tip = generator.generateToolTip(dataset, series, item); 388 } 389 String url = null; 390 if (getURLGenerator() != null) { 391 url = getURLGenerator().generateURL(dataset, series, 392 item); 393 } 394 XYItemEntity entity = new XYItemEntity(dataAreaHotspot, 395 dataset, series, item, tip, url); 396 entities.add(entity); 397 } 398 } 399 } 400 401 } 402 403 /** 404 * Tests this renderer for equality with an arbitrary object. 405 * 406 * @param obj the object ({@code null} not permitted). 407 * 408 * @return A boolean. 409 */ 410 @Override 411 public boolean equals(Object obj) { 412 if (obj == this) { 413 return true; 414 } 415 if (!(obj instanceof XYAreaRenderer2)) { 416 return false; 417 } 418 XYAreaRenderer2 that = (XYAreaRenderer2) obj; 419 if (this.showOutline != that.showOutline) { 420 return false; 421 } 422 if (!ShapeUtils.equal(this.legendArea, that.legendArea)) { 423 return false; 424 } 425 return super.equals(obj); 426 } 427 428 /** 429 * Returns a clone of the renderer. 430 * 431 * @return A clone. 432 * 433 * @throws CloneNotSupportedException if the renderer cannot be cloned. 434 */ 435 @Override 436 public Object clone() throws CloneNotSupportedException { 437 XYAreaRenderer2 clone = (XYAreaRenderer2) super.clone(); 438 clone.legendArea = ShapeUtils.clone(this.legendArea); 439 return clone; 440 } 441 442 /** 443 * Provides serialization support. 444 * 445 * @param stream the input stream. 446 * 447 * @throws IOException if there is an I/O error. 448 * @throws ClassNotFoundException if there is a classpath problem. 449 */ 450 private void readObject(ObjectInputStream stream) 451 throws IOException, ClassNotFoundException { 452 stream.defaultReadObject(); 453 this.legendArea = SerialUtils.readShape(stream); 454 } 455 456 /** 457 * Provides serialization support. 458 * 459 * @param stream the output stream. 460 * 461 * @throws IOException if there is an I/O error. 462 */ 463 private void writeObject(ObjectOutputStream stream) throws IOException { 464 stream.defaultWriteObject(); 465 SerialUtils.writeShape(this.legendArea, stream); 466 } 467 468} 469