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 * XYDotRenderer.java 029 * ------------------ 030 * (C) Copyright 2002-2017, by Object Refinery Limited. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): Christian W. Zuckschwerdt; 034 * 035 * Changes (from 29-Oct-2002) 036 * -------------------------- 037 * 29-Oct-2002 : Added standard header (DG); 038 * 25-Mar-2003 : Implemented Serializable (DG); 039 * 01-May-2003 : Modified drawItem() method signature (DG); 040 * 30-Jul-2003 : Modified entity constructor (CZ); 041 * 20-Aug-2003 : Implemented Cloneable and PublicCloneable (DG); 042 * 16-Sep-2003 : Changed ChartRenderingInfo --> PlotRenderingInfo (DG); 043 * 25-Feb-2004 : Replaced CrosshairInfo with CrosshairState (DG); 044 * 19-Jan-2005 : Now uses only primitives from dataset (DG); 045 * ------------- JFREECHART 1.0.x --------------------------------------------- 046 * 10-Jul-2006 : Added dotWidth and dotHeight attributes (DG); 047 * 06-Feb-2007 : Fixed bug 1086307, crosshairs with multiple axes (DG); 048 * 09-Nov-2007 : Added legend shape attribute, plus override for 049 * getLegendItem() (DG); 050 * 17-Jun-2008 : Apply legend shape, font and paint attributes (DG); 051 * 03-Jul-2013 : Use ParamChecks (DG); 052 * 18-Feb-2017 : Updates for crosshairs (bug #36) (DG); 053 * 054 */ 055 056package org.jfree.chart.renderer.xy; 057 058import java.awt.Graphics2D; 059import java.awt.Paint; 060import java.awt.Shape; 061import java.awt.geom.Rectangle2D; 062import java.io.IOException; 063import java.io.ObjectInputStream; 064import java.io.ObjectOutputStream; 065 066import org.jfree.chart.LegendItem; 067import org.jfree.chart.axis.ValueAxis; 068import org.jfree.chart.event.RendererChangeEvent; 069import org.jfree.chart.plot.CrosshairState; 070import org.jfree.chart.plot.PlotOrientation; 071import org.jfree.chart.plot.PlotRenderingInfo; 072import org.jfree.chart.plot.XYPlot; 073import org.jfree.chart.ui.RectangleEdge; 074import org.jfree.chart.util.Args; 075import org.jfree.chart.util.PublicCloneable; 076import org.jfree.chart.util.SerialUtils; 077import org.jfree.chart.util.ShapeUtils; 078import org.jfree.data.xy.XYDataset; 079 080/** 081 * A renderer that draws a small dot at each data point for an {@link XYPlot}. 082 * The example shown here is generated by the 083 * {@code ScatterPlotDemo4.java} program included in the JFreeChart 084 * demo collection: 085 * <br><br> 086 * <img src="../../../../../images/XYDotRendererSample.png" 087 * alt="XYDotRendererSample.png"> 088 */ 089public class XYDotRenderer extends AbstractXYItemRenderer 090 implements XYItemRenderer, PublicCloneable { 091 092 /** For serialization. */ 093 private static final long serialVersionUID = -2764344339073566425L; 094 095 /** The dot width. */ 096 private int dotWidth; 097 098 /** The dot height. */ 099 private int dotHeight; 100 101 /** 102 * The shape that is used to represent an item in the legend. 103 * 104 * @since 1.0.7 105 */ 106 private transient Shape legendShape; 107 108 /** 109 * Constructs a new renderer. 110 */ 111 public XYDotRenderer() { 112 super(); 113 this.dotWidth = 1; 114 this.dotHeight = 1; 115 this.legendShape = new Rectangle2D.Double(-3.0, -3.0, 6.0, 6.0); 116 } 117 118 /** 119 * Returns the dot width (the default value is 1). 120 * 121 * @return The dot width. 122 * 123 * @since 1.0.2 124 * @see #setDotWidth(int) 125 */ 126 public int getDotWidth() { 127 return this.dotWidth; 128 } 129 130 /** 131 * Sets the dot width and sends a {@link RendererChangeEvent} to all 132 * registered listeners. 133 * 134 * @param w the new width (must be greater than zero). 135 * 136 * @throws IllegalArgumentException if {@code w} is less than one. 137 * 138 * @since 1.0.2 139 * @see #getDotWidth() 140 */ 141 public void setDotWidth(int w) { 142 if (w < 1) { 143 throw new IllegalArgumentException("Requires w > 0."); 144 } 145 this.dotWidth = w; 146 fireChangeEvent(); 147 } 148 149 /** 150 * Returns the dot height (the default value is 1). 151 * 152 * @return The dot height. 153 * 154 * @since 1.0.2 155 * @see #setDotHeight(int) 156 */ 157 public int getDotHeight() { 158 return this.dotHeight; 159 } 160 161 /** 162 * Sets the dot height and sends a {@link RendererChangeEvent} to all 163 * registered listeners. 164 * 165 * @param h the new height (must be greater than zero). 166 * 167 * @throws IllegalArgumentException if {@code h} is less than one. 168 * 169 * @since 1.0.2 170 * @see #getDotHeight() 171 */ 172 public void setDotHeight(int h) { 173 if (h < 1) { 174 throw new IllegalArgumentException("Requires h > 0."); 175 } 176 this.dotHeight = h; 177 fireChangeEvent(); 178 } 179 180 /** 181 * Returns the shape used to represent an item in the legend. 182 * 183 * @return The legend shape (never {@code null}). 184 * 185 * @see #setLegendShape(Shape) 186 * 187 * @since 1.0.7 188 */ 189 public Shape getLegendShape() { 190 return this.legendShape; 191 } 192 193 /** 194 * Sets the shape used as a line in each legend item and sends a 195 * {@link RendererChangeEvent} to all registered listeners. 196 * 197 * @param shape the shape ({@code null} not permitted). 198 * 199 * @see #getLegendShape() 200 * 201 * @since 1.0.7 202 */ 203 public void setLegendShape(Shape shape) { 204 Args.nullNotPermitted(shape, "shape"); 205 this.legendShape = shape; 206 fireChangeEvent(); 207 } 208 209 /** 210 * Draws the visual representation of a single data item. 211 * 212 * @param g2 the graphics device. 213 * @param state the renderer state. 214 * @param dataArea the area within which the data is being drawn. 215 * @param info collects information about the drawing. 216 * @param plot the plot (can be used to obtain standard color 217 * information etc). 218 * @param domainAxis the domain (horizontal) axis. 219 * @param rangeAxis the range (vertical) axis. 220 * @param dataset the dataset. 221 * @param series the series index (zero-based). 222 * @param item the item index (zero-based). 223 * @param crosshairState crosshair information for the plot 224 * ({@code null} permitted). 225 * @param pass the pass index. 226 */ 227 @Override 228 public void drawItem(Graphics2D g2, XYItemRendererState state, 229 Rectangle2D dataArea, PlotRenderingInfo info, XYPlot plot, 230 ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset, 231 int series, int item, CrosshairState crosshairState, int pass) { 232 233 // do nothing if item is not visible 234 if (!getItemVisible(series, item)) { 235 return; 236 } 237 238 // get the data point... 239 double x = dataset.getXValue(series, item); 240 double y = dataset.getYValue(series, item); 241 double adjx = (this.dotWidth - 1) / 2.0; 242 double adjy = (this.dotHeight - 1) / 2.0; 243 if (!Double.isNaN(y)) { 244 RectangleEdge xAxisLocation = plot.getDomainAxisEdge(); 245 RectangleEdge yAxisLocation = plot.getRangeAxisEdge(); 246 double transX = domainAxis.valueToJava2D(x, dataArea, 247 xAxisLocation) - adjx; 248 double transY = rangeAxis.valueToJava2D(y, dataArea, yAxisLocation) 249 - adjy; 250 251 g2.setPaint(getItemPaint(series, item)); 252 PlotOrientation orientation = plot.getOrientation(); 253 if (orientation == PlotOrientation.HORIZONTAL) { 254 g2.fillRect((int) transY, (int) transX, this.dotHeight, 255 this.dotWidth); 256 } 257 else if (orientation == PlotOrientation.VERTICAL) { 258 g2.fillRect((int) transX, (int) transY, this.dotWidth, 259 this.dotHeight); 260 } 261 262 int datasetIndex = plot.indexOf(dataset); 263 updateCrosshairValues(crosshairState, x, y, datasetIndex, 264 transX, transY, orientation); 265 } 266 267 } 268 269 /** 270 * Returns a legend item for the specified series. 271 * 272 * @param datasetIndex the dataset index (zero-based). 273 * @param series the series index (zero-based). 274 * 275 * @return A legend item for the series (possibly {@code null}). 276 */ 277 @Override 278 public LegendItem getLegendItem(int datasetIndex, int series) { 279 280 // if the renderer isn't assigned to a plot, then we don't have a 281 // dataset... 282 XYPlot plot = getPlot(); 283 if (plot == null) { 284 return null; 285 } 286 287 XYDataset dataset = plot.getDataset(datasetIndex); 288 if (dataset == null) { 289 return null; 290 } 291 292 LegendItem result = null; 293 if (getItemVisible(series, 0)) { 294 String label = getLegendItemLabelGenerator().generateLabel(dataset, 295 series); 296 String description = label; 297 String toolTipText = null; 298 if (getLegendItemToolTipGenerator() != null) { 299 toolTipText = getLegendItemToolTipGenerator().generateLabel( 300 dataset, series); 301 } 302 String urlText = null; 303 if (getLegendItemURLGenerator() != null) { 304 urlText = getLegendItemURLGenerator().generateLabel( 305 dataset, series); 306 } 307 Paint fillPaint = lookupSeriesPaint(series); 308 result = new LegendItem(label, description, toolTipText, urlText, 309 getLegendShape(), fillPaint); 310 result.setLabelFont(lookupLegendTextFont(series)); 311 Paint labelPaint = lookupLegendTextPaint(series); 312 if (labelPaint != null) { 313 result.setLabelPaint(labelPaint); 314 } 315 result.setSeriesKey(dataset.getSeriesKey(series)); 316 result.setSeriesIndex(series); 317 result.setDataset(dataset); 318 result.setDatasetIndex(datasetIndex); 319 } 320 321 return result; 322 323 } 324 325 /** 326 * Tests this renderer for equality with an arbitrary object. This method 327 * returns {@code true} if and only if: 328 * 329 * <ul> 330 * <li>{@code obj} is not {@code null};</li> 331 * <li>{@code obj} is an instance of {@code XYDotRenderer};</li> 332 * <li>both renderers have the same attribute values. 333 * </ul> 334 * 335 * @param obj the object ({@code null} permitted). 336 * 337 * @return A boolean. 338 */ 339 @Override 340 public boolean equals(Object obj) { 341 if (obj == this) { 342 return true; 343 } 344 if (!(obj instanceof XYDotRenderer)) { 345 return false; 346 } 347 XYDotRenderer that = (XYDotRenderer) obj; 348 if (this.dotWidth != that.dotWidth) { 349 return false; 350 } 351 if (this.dotHeight != that.dotHeight) { 352 return false; 353 } 354 if (!ShapeUtils.equal(this.legendShape, that.legendShape)) { 355 return false; 356 } 357 return super.equals(obj); 358 } 359 360 /** 361 * Returns a clone of the renderer. 362 * 363 * @return A clone. 364 * 365 * @throws CloneNotSupportedException if the renderer cannot be cloned. 366 */ 367 @Override 368 public Object clone() throws CloneNotSupportedException { 369 return super.clone(); 370 } 371 372 /** 373 * Provides serialization support. 374 * 375 * @param stream the input stream. 376 * 377 * @throws IOException if there is an I/O error. 378 * @throws ClassNotFoundException if there is a classpath problem. 379 */ 380 private void readObject(ObjectInputStream stream) 381 throws IOException, ClassNotFoundException { 382 stream.defaultReadObject(); 383 this.legendShape = SerialUtils.readShape(stream); 384 } 385 386 /** 387 * Provides serialization support. 388 * 389 * @param stream the output stream. 390 * 391 * @throws IOException if there is an I/O error. 392 */ 393 private void writeObject(ObjectOutputStream stream) throws IOException { 394 stream.defaultWriteObject(); 395 SerialUtils.writeShape(this.legendShape, stream); 396 } 397 398}