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 * YIntervalRenderer.java 029 * ---------------------- 030 * (C) Copyright 2002-2017, by Object Refinery Limited. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): -; 034 * 035 * Changes 036 * ------- 037 * 05-Nov-2002 : Version 1 (DG); 038 * 25-Mar-2003 : Implemented Serializable (DG); 039 * 01-May-2003 : Modified drawItem() method signature (DG); 040 * 20-Aug-2003 : Implemented Cloneable and PublicCloneable (DG); 041 * 16-Sep-2003 : Changed ChartRenderingInfo --> PlotRenderingInfo (DG); 042 * 25-Feb-2004 : Replaced CrosshairInfo with CrosshairState (DG); 043 * 27-Sep-2004 : Access double values from dataset (DG); 044 * 11-Nov-2004 : Now uses ShapeUtilities to translate shapes (DG); 045 * 11-Apr-2008 : New override for findRangeBounds() (DG); 046 * 26-May-2008 : Added item label support (DG); 047 * 27-Mar-2009 : Updated findRangeBounds() (DG); 048 * 049 */ 050 051package org.jfree.chart.renderer.xy; 052 053import java.awt.Font; 054import java.awt.Graphics2D; 055import java.awt.Paint; 056import java.awt.Shape; 057import java.awt.Stroke; 058import java.awt.geom.Line2D; 059import java.awt.geom.Point2D; 060import java.awt.geom.Rectangle2D; 061import java.io.Serializable; 062 063import org.jfree.chart.axis.ValueAxis; 064import org.jfree.chart.entity.EntityCollection; 065import org.jfree.chart.event.RendererChangeEvent; 066import org.jfree.chart.labels.ItemLabelPosition; 067import org.jfree.chart.labels.XYItemLabelGenerator; 068import org.jfree.chart.plot.CrosshairState; 069import org.jfree.chart.plot.PlotOrientation; 070import org.jfree.chart.plot.PlotRenderingInfo; 071import org.jfree.chart.plot.XYPlot; 072import org.jfree.chart.text.TextUtils; 073import org.jfree.chart.ui.RectangleEdge; 074import org.jfree.chart.util.ObjectUtils; 075import org.jfree.chart.util.PublicCloneable; 076import org.jfree.chart.util.ShapeUtils; 077import org.jfree.data.Range; 078import org.jfree.data.xy.IntervalXYDataset; 079import org.jfree.data.xy.XYDataset; 080 081/** 082 * A renderer that draws a line connecting the start and end Y values for an 083 * {@link XYPlot}. The example shown here is generated by the 084 * {@code YIntervalRendererDemo1.java} program included in the JFreeChart 085 * demo collection: 086 * <br><br> 087 * <img src="../../../../../images/YIntervalRendererSample.png" 088 * alt="YIntervalRendererSample.png"> 089 */ 090public class YIntervalRenderer extends AbstractXYItemRenderer 091 implements XYItemRenderer, Cloneable, PublicCloneable, Serializable { 092 093 /** For serialization. */ 094 private static final long serialVersionUID = -2951586537224143260L; 095 096 /** 097 * An additional item label generator. If this is non-null, the item 098 * label generated will be displayed near the lower y-value at the 099 * position given by getNegativeItemLabelPosition(). 100 * 101 * @since 1.0.10 102 */ 103 private XYItemLabelGenerator additionalItemLabelGenerator; 104 105 /** 106 * The default constructor. 107 */ 108 public YIntervalRenderer() { 109 super(); 110 this.additionalItemLabelGenerator = null; 111 } 112 113 /** 114 * Returns the generator for the item labels that appear near the lower 115 * y-value. 116 * 117 * @return The generator (possibly {@code null}). 118 * 119 * @see #setAdditionalItemLabelGenerator(XYItemLabelGenerator) 120 * 121 * @since 1.0.10 122 */ 123 public XYItemLabelGenerator getAdditionalItemLabelGenerator() { 124 return this.additionalItemLabelGenerator; 125 } 126 127 /** 128 * Sets the generator for the item labels that appear near the lower 129 * y-value and sends a {@link RendererChangeEvent} to all registered 130 * listeners. If this is set to {@code null}, no item labels will be 131 * drawn. 132 * 133 * @param generator the generator ({@code null} permitted). 134 * 135 * @see #getAdditionalItemLabelGenerator() 136 * 137 * @since 1.0.10 138 */ 139 public void setAdditionalItemLabelGenerator( 140 XYItemLabelGenerator generator) { 141 this.additionalItemLabelGenerator = generator; 142 fireChangeEvent(); 143 } 144 145 /** 146 * Returns the range of values the renderer requires to display all the 147 * items from the specified dataset. 148 * 149 * @param dataset the dataset ({@code null} permitted). 150 * 151 * @return The range ({@code null} if the dataset is {@code null} or empty). 152 */ 153 @Override 154 public Range findRangeBounds(XYDataset dataset) { 155 return findRangeBounds(dataset, true); 156 } 157 158 /** 159 * Draws the visual representation of a single data item. 160 * 161 * @param g2 the graphics device. 162 * @param state the renderer state. 163 * @param dataArea the area within which the plot is being drawn. 164 * @param info collects information about the drawing. 165 * @param plot the plot (can be used to obtain standard color 166 * information etc). 167 * @param domainAxis the domain axis. 168 * @param rangeAxis the range axis. 169 * @param dataset the dataset. 170 * @param series the series index (zero-based). 171 * @param item the item index (zero-based). 172 * @param crosshairState crosshair information for the plot 173 * ({@code null} permitted). 174 * @param pass the pass index (ignored here). 175 */ 176 @Override 177 public void drawItem(Graphics2D g2, XYItemRendererState state, 178 Rectangle2D dataArea, PlotRenderingInfo info, XYPlot plot, 179 ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset, 180 int series, int item, CrosshairState crosshairState, int pass) { 181 182 // do nothing if item is not visible 183 if (!getItemVisible(series, item)) { 184 return; 185 } 186 187 // setup for collecting optional entity info... 188 EntityCollection entities = null; 189 if (info != null) { 190 entities = info.getOwner().getEntityCollection(); 191 } 192 193 IntervalXYDataset intervalDataset = (IntervalXYDataset) dataset; 194 195 double x = intervalDataset.getXValue(series, item); 196 double yLow = intervalDataset.getStartYValue(series, item); 197 double yHigh = intervalDataset.getEndYValue(series, item); 198 199 RectangleEdge xAxisLocation = plot.getDomainAxisEdge(); 200 RectangleEdge yAxisLocation = plot.getRangeAxisEdge(); 201 202 double xx = domainAxis.valueToJava2D(x, dataArea, xAxisLocation); 203 double yyLow = rangeAxis.valueToJava2D(yLow, dataArea, yAxisLocation); 204 double yyHigh = rangeAxis.valueToJava2D(yHigh, dataArea, yAxisLocation); 205 206 Paint p = getItemPaint(series, item); 207 Stroke s = getItemStroke(series, item); 208 209 Line2D line = null; 210 Shape shape = getItemShape(series, item); 211 Shape top = null; 212 Shape bottom = null; 213 PlotOrientation orientation = plot.getOrientation(); 214 if (orientation == PlotOrientation.HORIZONTAL) { 215 line = new Line2D.Double(yyLow, xx, yyHigh, xx); 216 top = ShapeUtils.createTranslatedShape(shape, yyHigh, xx); 217 bottom = ShapeUtils.createTranslatedShape(shape, yyLow, xx); 218 } 219 else if (orientation == PlotOrientation.VERTICAL) { 220 line = new Line2D.Double(xx, yyLow, xx, yyHigh); 221 top = ShapeUtils.createTranslatedShape(shape, xx, yyHigh); 222 bottom = ShapeUtils.createTranslatedShape(shape, xx, yyLow); 223 } else { 224 throw new IllegalStateException(); 225 } 226 g2.setPaint(p); 227 g2.setStroke(s); 228 g2.draw(line); 229 230 g2.fill(top); 231 g2.fill(bottom); 232 233 // for item labels, we have a special case because there is the 234 // possibility to draw (a) the regular item label near to just the 235 // upper y-value, or (b) the regular item label near the upper y-value 236 // PLUS an additional item label near the lower y-value. 237 if (isItemLabelVisible(series, item)) { 238 drawItemLabel(g2, orientation, dataset, series, item, xx, yyHigh, 239 false); 240 drawAdditionalItemLabel(g2, orientation, dataset, series, item, 241 xx, yyLow); 242 } 243 244 // add an entity for the item... 245 Shape hotspot = ShapeUtils.createLineRegion(line, 4.0f); 246 if (entities != null && hotspot.intersects(dataArea)) { 247 addEntity(entities, hotspot, dataset, series, item, 0.0, 0.0); 248 } 249 250 } 251 252 /** 253 * Draws an item label. 254 * 255 * @param g2 the graphics device. 256 * @param orientation the orientation. 257 * @param dataset the dataset. 258 * @param series the series index (zero-based). 259 * @param item the item index (zero-based). 260 * @param x the x coordinate (in Java2D space). 261 * @param y the y coordinate (in Java2D space). 262 */ 263 private void drawAdditionalItemLabel(Graphics2D g2, 264 PlotOrientation orientation, XYDataset dataset, int series, 265 int item, double x, double y) { 266 267 if (this.additionalItemLabelGenerator == null) { 268 return; 269 } 270 271 Font labelFont = getItemLabelFont(series, item); 272 Paint paint = getItemLabelPaint(series, item); 273 g2.setFont(labelFont); 274 g2.setPaint(paint); 275 String label = this.additionalItemLabelGenerator.generateLabel(dataset, 276 series, item); 277 278 ItemLabelPosition position = getNegativeItemLabelPosition(series, item); 279 Point2D anchorPoint = calculateLabelAnchorPoint( 280 position.getItemLabelAnchor(), x, y, orientation); 281 TextUtils.drawRotatedString(label, g2, 282 (float) anchorPoint.getX(), (float) anchorPoint.getY(), 283 position.getTextAnchor(), position.getAngle(), 284 position.getRotationAnchor()); 285 } 286 287 /** 288 * Tests this renderer for equality with an arbitrary object. 289 * 290 * @param obj the object ({@code null} permitted). 291 * 292 * @return A boolean. 293 */ 294 @Override 295 public boolean equals(Object obj) { 296 if (obj == this) { 297 return true; 298 } 299 if (!(obj instanceof YIntervalRenderer)) { 300 return false; 301 } 302 YIntervalRenderer that = (YIntervalRenderer) obj; 303 if (!ObjectUtils.equal(this.additionalItemLabelGenerator, 304 that.additionalItemLabelGenerator)) { 305 return false; 306 } 307 return super.equals(obj); 308 } 309 310 /** 311 * Returns a clone of the renderer. 312 * 313 * @return A clone. 314 * 315 * @throws CloneNotSupportedException if the renderer cannot be cloned. 316 */ 317 @Override 318 public Object clone() throws CloneNotSupportedException { 319 return super.clone(); 320 } 321 322}