001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2020, 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 * ScatterRenderer.java 029 * -------------------- 030 * (C) Copyright 2007-2020, by Object Refinery Limited and Contributors. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): David Forslund; 034 * Peter Kolb (patches 2497611, 2791407); 035 * 036 */ 037 038package org.jfree.chart.renderer.category; 039 040import java.awt.Graphics2D; 041import java.awt.Paint; 042import java.awt.Shape; 043import java.awt.Stroke; 044import java.awt.geom.Line2D; 045import java.awt.geom.Rectangle2D; 046import java.io.IOException; 047import java.io.ObjectInputStream; 048import java.io.ObjectOutputStream; 049import java.io.Serializable; 050import java.util.List; 051 052import org.jfree.chart.LegendItem; 053import org.jfree.chart.axis.CategoryAxis; 054import org.jfree.chart.axis.ValueAxis; 055import org.jfree.chart.event.RendererChangeEvent; 056import org.jfree.chart.plot.CategoryPlot; 057import org.jfree.chart.plot.PlotOrientation; 058import org.jfree.chart.util.BooleanList; 059import org.jfree.chart.util.ObjectUtils; 060import org.jfree.chart.util.PublicCloneable; 061import org.jfree.chart.util.ShapeUtils; 062import org.jfree.data.Range; 063import org.jfree.data.category.CategoryDataset; 064import org.jfree.data.statistics.MultiValueCategoryDataset; 065 066/** 067 * A renderer that handles the multiple values from a 068 * {@link MultiValueCategoryDataset} by plotting a shape for each value for 069 * each given item in the dataset. The example shown here is generated by 070 * the {@code ScatterRendererDemo1.java} program included in the 071 * JFreeChart Demo Collection: 072 * <br><br> 073 * <img src="doc-files/ScatterRendererSample.png" alt="ScatterRendererSample.png"> 074 * 075 * @since 1.0.7 076 */ 077public class ScatterRenderer extends AbstractCategoryItemRenderer 078 implements Cloneable, PublicCloneable, Serializable { 079 080 /** 081 * A table of flags that control (per series) whether or not shapes are 082 * filled. 083 */ 084 private BooleanList seriesShapesFilled; 085 086 /** 087 * The default value returned by the getShapeFilled() method. 088 */ 089 private boolean baseShapesFilled; 090 091 /** 092 * A flag that controls whether the fill paint is used for filling 093 * shapes. 094 */ 095 private boolean useFillPaint; 096 097 /** 098 * A flag that controls whether outlines are drawn for shapes. 099 */ 100 private boolean drawOutlines; 101 102 /** 103 * A flag that controls whether the outline paint is used for drawing shape 104 * outlines - if not, the regular series paint is used. 105 */ 106 private boolean useOutlinePaint; 107 108 /** 109 * A flag that controls whether or not the x-position for each item is 110 * offset within the category according to the series. 111 */ 112 private boolean useSeriesOffset; 113 114 /** 115 * The item margin used for series offsetting - this allows the positioning 116 * to match the bar positions of the {@link BarRenderer} class. 117 */ 118 private double itemMargin; 119 120 /** 121 * Constructs a new renderer. 122 */ 123 public ScatterRenderer() { 124 this.seriesShapesFilled = new BooleanList(); 125 this.baseShapesFilled = true; 126 this.useFillPaint = false; 127 this.drawOutlines = false; 128 this.useOutlinePaint = false; 129 this.useSeriesOffset = true; 130 this.itemMargin = 0.20; 131 } 132 133 /** 134 * Returns the flag that controls whether or not the x-position for each 135 * data item is offset within the category according to the series. 136 * 137 * @return A boolean. 138 * 139 * @see #setUseSeriesOffset(boolean) 140 */ 141 public boolean getUseSeriesOffset() { 142 return this.useSeriesOffset; 143 } 144 145 /** 146 * Sets the flag that controls whether or not the x-position for each 147 * data item is offset within its category according to the series, and 148 * sends a {@link RendererChangeEvent} to all registered listeners. 149 * 150 * @param offset the offset. 151 * 152 * @see #getUseSeriesOffset() 153 */ 154 public void setUseSeriesOffset(boolean offset) { 155 this.useSeriesOffset = offset; 156 fireChangeEvent(); 157 } 158 159 /** 160 * Returns the item margin, which is the gap between items within a 161 * category (expressed as a percentage of the overall category width). 162 * This can be used to match the offset alignment with the bars drawn by 163 * a {@link BarRenderer}). 164 * 165 * @return The item margin. 166 * 167 * @see #setItemMargin(double) 168 * @see #getUseSeriesOffset() 169 */ 170 public double getItemMargin() { 171 return this.itemMargin; 172 } 173 174 /** 175 * Sets the item margin, which is the gap between items within a category 176 * (expressed as a percentage of the overall category width), and sends 177 * a {@link RendererChangeEvent} to all registered listeners. 178 * 179 * @param margin the margin (0.0 <= margin < 1.0). 180 * 181 * @see #getItemMargin() 182 * @see #getUseSeriesOffset() 183 */ 184 public void setItemMargin(double margin) { 185 if (margin < 0.0 || margin >= 1.0) { 186 throw new IllegalArgumentException("Requires 0.0 <= margin < 1.0."); 187 } 188 this.itemMargin = margin; 189 fireChangeEvent(); 190 } 191 192 /** 193 * Returns {@code true} if outlines should be drawn for shapes, and 194 * {@code false} otherwise. 195 * 196 * @return A boolean. 197 * 198 * @see #setDrawOutlines(boolean) 199 */ 200 public boolean getDrawOutlines() { 201 return this.drawOutlines; 202 } 203 204 /** 205 * Sets the flag that controls whether outlines are drawn for 206 * shapes, and sends a {@link RendererChangeEvent} to all registered 207 * listeners. 208 * <p>In some cases, shapes look better if they do NOT have an outline, but 209 * this flag allows you to set your own preference.</p> 210 * 211 * @param flag the flag. 212 * 213 * @see #getDrawOutlines() 214 */ 215 public void setDrawOutlines(boolean flag) { 216 this.drawOutlines = flag; 217 fireChangeEvent(); 218 } 219 220 /** 221 * Returns the flag that controls whether the outline paint is used for 222 * shape outlines. If not, the regular series paint is used. 223 * 224 * @return A boolean. 225 * 226 * @see #setUseOutlinePaint(boolean) 227 */ 228 public boolean getUseOutlinePaint() { 229 return this.useOutlinePaint; 230 } 231 232 /** 233 * Sets the flag that controls whether the outline paint is used for shape 234 * outlines, and sends a {@link RendererChangeEvent} to all registered 235 * listeners. 236 * 237 * @param use the flag. 238 * 239 * @see #getUseOutlinePaint() 240 */ 241 public void setUseOutlinePaint(boolean use) { 242 this.useOutlinePaint = use; 243 fireChangeEvent(); 244 } 245 246 // SHAPES FILLED 247 248 /** 249 * Returns the flag used to control whether or not the shape for an item 250 * is filled. The default implementation passes control to the 251 * {@code getSeriesShapesFilled} method. You can override this method 252 * if you require different behaviour. 253 * 254 * @param series the series index (zero-based). 255 * @param item the item index (zero-based). 256 * @return A boolean. 257 */ 258 public boolean getItemShapeFilled(int series, int item) { 259 return getSeriesShapesFilled(series); 260 } 261 262 /** 263 * Returns the flag used to control whether or not the shapes for a series 264 * are filled. 265 * 266 * @param series the series index (zero-based). 267 * @return A boolean. 268 */ 269 public boolean getSeriesShapesFilled(int series) { 270 Boolean flag = this.seriesShapesFilled.getBoolean(series); 271 if (flag != null) { 272 return flag; 273 } 274 else { 275 return this.baseShapesFilled; 276 } 277 278 } 279 280 /** 281 * Sets the 'shapes filled' flag for a series and sends a 282 * {@link RendererChangeEvent} to all registered listeners. 283 * 284 * @param series the series index (zero-based). 285 * @param filled the flag. 286 */ 287 public void setSeriesShapesFilled(int series, Boolean filled) { 288 this.seriesShapesFilled.setBoolean(series, filled); 289 fireChangeEvent(); 290 } 291 292 /** 293 * Sets the 'shapes filled' flag for a series and sends a 294 * {@link RendererChangeEvent} to all registered listeners. 295 * 296 * @param series the series index (zero-based). 297 * @param filled the flag. 298 */ 299 public void setSeriesShapesFilled(int series, boolean filled) { 300 this.seriesShapesFilled.setBoolean(series, filled); 301 fireChangeEvent(); 302 } 303 304 /** 305 * Returns the base 'shape filled' attribute. 306 * 307 * @return The base flag. 308 */ 309 public boolean getBaseShapesFilled() { 310 return this.baseShapesFilled; 311 } 312 313 /** 314 * Sets the base 'shapes filled' flag and sends a 315 * {@link RendererChangeEvent} to all registered listeners. 316 * 317 * @param flag the flag. 318 */ 319 public void setBaseShapesFilled(boolean flag) { 320 this.baseShapesFilled = flag; 321 fireChangeEvent(); 322 } 323 324 /** 325 * Returns {@code true} if the renderer should use the fill paint 326 * setting to fill shapes, and {@code false} if it should just 327 * use the regular paint. 328 * 329 * @return A boolean. 330 */ 331 public boolean getUseFillPaint() { 332 return this.useFillPaint; 333 } 334 335 /** 336 * Sets the flag that controls whether the fill paint is used to fill 337 * shapes, and sends a {@link RendererChangeEvent} to all 338 * registered listeners. 339 * 340 * @param flag the flag. 341 */ 342 public void setUseFillPaint(boolean flag) { 343 this.useFillPaint = flag; 344 fireChangeEvent(); 345 } 346 347 /** 348 * Returns the range of values the renderer requires to display all the 349 * items from the specified dataset. This takes into account the range 350 * between the min/max values, possibly ignoring invisible series. 351 * 352 * @param dataset the dataset ({@code null} permitted). 353 * 354 * @return The range (or {@code null} if the dataset is 355 * {@code null} or empty). 356 */ 357 @Override 358 public Range findRangeBounds(CategoryDataset dataset) { 359 return findRangeBounds(dataset, true); 360 } 361 362 /** 363 * Draw a single data item. 364 * 365 * @param g2 the graphics device. 366 * @param state the renderer state. 367 * @param dataArea the area in which the data is drawn. 368 * @param plot the plot. 369 * @param domainAxis the domain axis. 370 * @param rangeAxis the range axis. 371 * @param dataset the dataset. 372 * @param row the row index (zero-based). 373 * @param column the column index (zero-based). 374 * @param pass the pass index. 375 */ 376 @Override 377 public void drawItem(Graphics2D g2, CategoryItemRendererState state, 378 Rectangle2D dataArea, CategoryPlot plot, CategoryAxis domainAxis, 379 ValueAxis rangeAxis, CategoryDataset dataset, int row, int column, 380 int pass) { 381 382 // do nothing if item is not visible 383 if (!getItemVisible(row, column)) { 384 return; 385 } 386 int visibleRow = state.getVisibleSeriesIndex(row); 387 if (visibleRow < 0) { 388 return; 389 } 390 int visibleRowCount = state.getVisibleSeriesCount(); 391 392 PlotOrientation orientation = plot.getOrientation(); 393 394 MultiValueCategoryDataset d = (MultiValueCategoryDataset) dataset; 395 List values = d.getValues(row, column); 396 if (values == null) { 397 return; 398 } 399 int valueCount = values.size(); 400 for (int i = 0; i < valueCount; i++) { 401 // current data point... 402 double x1; 403 if (this.useSeriesOffset) { 404 x1 = domainAxis.getCategorySeriesMiddle(column, 405 dataset.getColumnCount(), visibleRow, visibleRowCount, 406 this.itemMargin, dataArea, plot.getDomainAxisEdge()); 407 } 408 else { 409 x1 = domainAxis.getCategoryMiddle(column, getColumnCount(), 410 dataArea, plot.getDomainAxisEdge()); 411 } 412 Number n = (Number) values.get(i); 413 double value = n.doubleValue(); 414 double y1 = rangeAxis.valueToJava2D(value, dataArea, 415 plot.getRangeAxisEdge()); 416 417 Shape shape = getItemShape(row, column); 418 if (orientation == PlotOrientation.HORIZONTAL) { 419 shape = ShapeUtils.createTranslatedShape(shape, y1, x1); 420 } 421 else if (orientation == PlotOrientation.VERTICAL) { 422 shape = ShapeUtils.createTranslatedShape(shape, x1, y1); 423 } 424 if (getItemShapeFilled(row, column)) { 425 if (this.useFillPaint) { 426 g2.setPaint(getItemFillPaint(row, column)); 427 } 428 else { 429 g2.setPaint(getItemPaint(row, column)); 430 } 431 g2.fill(shape); 432 } 433 if (this.drawOutlines) { 434 if (this.useOutlinePaint) { 435 g2.setPaint(getItemOutlinePaint(row, column)); 436 } 437 else { 438 g2.setPaint(getItemPaint(row, column)); 439 } 440 g2.setStroke(getItemOutlineStroke(row, column)); 441 g2.draw(shape); 442 } 443 } 444 445 } 446 447 /** 448 * Returns a legend item for a series. 449 * 450 * @param datasetIndex the dataset index (zero-based). 451 * @param series the series index (zero-based). 452 * 453 * @return The legend item. 454 */ 455 @Override 456 public LegendItem getLegendItem(int datasetIndex, int series) { 457 458 CategoryPlot cp = getPlot(); 459 if (cp == null) { 460 return null; 461 } 462 463 if (isSeriesVisible(series) && isSeriesVisibleInLegend(series)) { 464 CategoryDataset dataset = cp.getDataset(datasetIndex); 465 String label = getLegendItemLabelGenerator().generateLabel( 466 dataset, series); 467 String description = label; 468 String toolTipText = null; 469 if (getLegendItemToolTipGenerator() != null) { 470 toolTipText = getLegendItemToolTipGenerator().generateLabel( 471 dataset, series); 472 } 473 String urlText = null; 474 if (getLegendItemURLGenerator() != null) { 475 urlText = getLegendItemURLGenerator().generateLabel( 476 dataset, series); 477 } 478 Shape shape = lookupLegendShape(series); 479 Paint paint = lookupSeriesPaint(series); 480 Paint fillPaint = (this.useFillPaint 481 ? getItemFillPaint(series, 0) : paint); 482 boolean shapeOutlineVisible = this.drawOutlines; 483 Paint outlinePaint = (this.useOutlinePaint 484 ? getItemOutlinePaint(series, 0) : paint); 485 Stroke outlineStroke = lookupSeriesOutlineStroke(series); 486 LegendItem result = new LegendItem(label, description, toolTipText, 487 urlText, true, shape, getItemShapeFilled(series, 0), 488 fillPaint, shapeOutlineVisible, outlinePaint, outlineStroke, 489 false, new Line2D.Double(-7.0, 0.0, 7.0, 0.0), 490 getItemStroke(series, 0), getItemPaint(series, 0)); 491 result.setLabelFont(lookupLegendTextFont(series)); 492 Paint labelPaint = lookupLegendTextPaint(series); 493 if (labelPaint != null) { 494 result.setLabelPaint(labelPaint); 495 } 496 result.setDataset(dataset); 497 result.setDatasetIndex(datasetIndex); 498 result.setSeriesKey(dataset.getRowKey(series)); 499 result.setSeriesIndex(series); 500 return result; 501 } 502 return null; 503 504 } 505 506 /** 507 * Tests this renderer for equality with an arbitrary object. 508 * 509 * @param obj the object ({@code null} permitted). 510 * @return A boolean. 511 */ 512 @Override 513 public boolean equals(Object obj) { 514 if (obj == this) { 515 return true; 516 } 517 if (!(obj instanceof ScatterRenderer)) { 518 return false; 519 } 520 ScatterRenderer that = (ScatterRenderer) obj; 521 if (!ObjectUtils.equal(this.seriesShapesFilled, 522 that.seriesShapesFilled)) { 523 return false; 524 } 525 if (this.baseShapesFilled != that.baseShapesFilled) { 526 return false; 527 } 528 if (this.useFillPaint != that.useFillPaint) { 529 return false; 530 } 531 if (this.drawOutlines != that.drawOutlines) { 532 return false; 533 } 534 if (this.useOutlinePaint != that.useOutlinePaint) { 535 return false; 536 } 537 if (this.useSeriesOffset != that.useSeriesOffset) { 538 return false; 539 } 540 if (this.itemMargin != that.itemMargin) { 541 return false; 542 } 543 return super.equals(obj); 544 } 545 546 /** 547 * Returns an independent copy of the renderer. 548 * 549 * @return A clone. 550 * 551 * @throws CloneNotSupportedException should not happen. 552 */ 553 @Override 554 public Object clone() throws CloneNotSupportedException { 555 ScatterRenderer clone = (ScatterRenderer) super.clone(); 556 clone.seriesShapesFilled 557 = (BooleanList) this.seriesShapesFilled.clone(); 558 return clone; 559 } 560 561 /** 562 * Provides serialization support. 563 * 564 * @param stream the output stream. 565 * @throws java.io.IOException if there is an I/O error. 566 */ 567 private void writeObject(ObjectOutputStream stream) throws IOException { 568 stream.defaultWriteObject(); 569 570 } 571 572 /** 573 * Provides serialization support. 574 * 575 * @param stream the input stream. 576 * @throws java.io.IOException if there is an I/O error. 577 * @throws ClassNotFoundException if there is a classpath problem. 578 */ 579 private void readObject(ObjectInputStream stream) 580 throws IOException, ClassNotFoundException { 581 stream.defaultReadObject(); 582 583 } 584 585}