001 /* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2011, 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 * AreaRenderer.java 029 * ----------------- 030 * (C) Copyright 2002-2009, by Jon Iles and Contributors. 031 * 032 * Original Author: Jon Iles; 033 * Contributor(s): David Gilbert (for Object Refinery Limited); 034 * Christian W. Zuckschwerdt; 035 * 036 * Changes: 037 * -------- 038 * 21-May-2002 : Version 1, contributed by John Iles (DG); 039 * 29-May-2002 : Now extends AbstractCategoryItemRenderer (DG); 040 * 11-Jun-2002 : Updated Javadoc comments (DG); 041 * 25-Jun-2002 : Removed unnecessary imports (DG); 042 * 01-Oct-2002 : Fixed errors reported by Checkstyle (DG); 043 * 10-Oct-2002 : Added constructors and basic entity support (DG); 044 * 24-Oct-2002 : Amendments for changes in CategoryDataset interface and 045 * CategoryToolTipGenerator interface (DG); 046 * 05-Nov-2002 : Replaced references to CategoryDataset with TableDataset (DG); 047 * 06-Nov-2002 : Renamed drawCategoryItem() --> drawItem() and now using axis 048 * for category spacing. Renamed AreaCategoryItemRenderer 049 * --> AreaRenderer (DG); 050 * 17-Jan-2003 : Moved plot classes into a separate package (DG); 051 * 25-Mar-2003 : Implemented Serializable (DG); 052 * 10-Apr-2003 : Changed CategoryDataset to KeyedValues2DDataset in 053 * drawItem() method (DG); 054 * 12-May-2003 : Modified to take into account the plot orientation (DG); 055 * 30-Jul-2003 : Modified entity constructor (CZ); 056 * 13-Aug-2003 : Implemented Cloneable (DG); 057 * 07-Oct-2003 : Added renderer state (DG); 058 * 05-Nov-2004 : Modified drawItem() signature (DG); 059 * 20-Apr-2005 : Apply tooltips and URLs to legend items (DG); 060 * 09-Jun-2005 : Use addItemEntity() method from superclass (DG); 061 * ------------- JFREECHART 1.0.x --------------------------------------------- 062 * 11-Oct-2006 : Fixed bug in equals() method (DG); 063 * 30-Nov-2006 : Added checks for series visibility (DG); 064 * 20-Apr-2007 : Updated getLegendItem() for renderer change (DG); 065 * 17-May-2007 : Set datasetIndex and seriesIndex in getLegendItem() (DG); 066 * 18-May-2007 : Set dataset and seriesKey for LegendItem (DG); 067 * 17-Jun-2008 : Apply legend shape, font and paint attributes (DG); 068 * 26-Jun-2008 : Added crosshair support (DG); 069 * 26-May-2009 : Support AreaRendererEndType.LEVEL (DG); 070 * 27-May-2009 : Fixed item label anchor for horizontal orientation (DG); 071 * 072 */ 073 074 package org.jfree.chart.renderer.category; 075 076 import java.awt.Graphics2D; 077 import java.awt.Paint; 078 import java.awt.Shape; 079 import java.awt.Stroke; 080 import java.awt.geom.GeneralPath; 081 import java.awt.geom.Rectangle2D; 082 import java.io.Serializable; 083 084 import org.jfree.chart.LegendItem; 085 import org.jfree.chart.axis.CategoryAxis; 086 import org.jfree.chart.axis.ValueAxis; 087 import org.jfree.chart.entity.EntityCollection; 088 import org.jfree.chart.event.RendererChangeEvent; 089 import org.jfree.chart.plot.CategoryPlot; 090 import org.jfree.chart.plot.PlotOrientation; 091 import org.jfree.chart.renderer.AreaRendererEndType; 092 import org.jfree.data.category.CategoryDataset; 093 import org.jfree.ui.RectangleEdge; 094 import org.jfree.util.PublicCloneable; 095 096 /** 097 * A category item renderer that draws area charts. You can use this renderer 098 * with the {@link CategoryPlot} class. The example shown here is generated 099 * by the <code>AreaChartDemo1.java</code> program included in the JFreeChart 100 * Demo Collection: 101 * <br><br> 102 * <img src="../../../../../images/AreaRendererSample.png" 103 * alt="AreaRendererSample.png" /> 104 */ 105 public class AreaRenderer extends AbstractCategoryItemRenderer 106 implements Cloneable, PublicCloneable, Serializable { 107 108 /** For serialization. */ 109 private static final long serialVersionUID = -4231878281385812757L; 110 111 /** A flag that controls how the ends of the areas are drawn. */ 112 private AreaRendererEndType endType; 113 114 /** 115 * Creates a new renderer. 116 */ 117 public AreaRenderer() { 118 super(); 119 this.endType = AreaRendererEndType.TAPER; 120 setBaseLegendShape(new Rectangle2D.Double(-4.0, -4.0, 8.0, 8.0)); 121 } 122 123 /** 124 * Returns a token that controls how the renderer draws the end points. 125 * The default value is {@link AreaRendererEndType#TAPER}. 126 * 127 * @return The end type (never <code>null</code>). 128 * 129 * @see #setEndType 130 */ 131 public AreaRendererEndType getEndType() { 132 return this.endType; 133 } 134 135 /** 136 * Sets a token that controls how the renderer draws the end points, and 137 * sends a {@link RendererChangeEvent} to all registered listeners. 138 * 139 * @param type the end type (<code>null</code> not permitted). 140 * 141 * @see #getEndType() 142 */ 143 public void setEndType(AreaRendererEndType type) { 144 if (type == null) { 145 throw new IllegalArgumentException("Null 'type' argument."); 146 } 147 this.endType = type; 148 fireChangeEvent(); 149 } 150 151 /** 152 * Returns a legend item for a series. 153 * 154 * @param datasetIndex the dataset index (zero-based). 155 * @param series the series index (zero-based). 156 * 157 * @return The legend item. 158 */ 159 public LegendItem getLegendItem(int datasetIndex, int series) { 160 161 // if there is no plot, there is no dataset to access... 162 CategoryPlot cp = getPlot(); 163 if (cp == null) { 164 return null; 165 } 166 167 // check that a legend item needs to be displayed... 168 if (!isSeriesVisible(series) || !isSeriesVisibleInLegend(series)) { 169 return null; 170 } 171 172 CategoryDataset dataset = cp.getDataset(datasetIndex); 173 String label = getLegendItemLabelGenerator().generateLabel(dataset, 174 series); 175 String description = label; 176 String toolTipText = null; 177 if (getLegendItemToolTipGenerator() != null) { 178 toolTipText = getLegendItemToolTipGenerator().generateLabel( 179 dataset, series); 180 } 181 String urlText = null; 182 if (getLegendItemURLGenerator() != null) { 183 urlText = getLegendItemURLGenerator().generateLabel(dataset, 184 series); 185 } 186 Shape shape = lookupLegendShape(series); 187 Paint paint = lookupSeriesPaint(series); 188 Paint outlinePaint = lookupSeriesOutlinePaint(series); 189 Stroke outlineStroke = lookupSeriesOutlineStroke(series); 190 191 LegendItem result = new LegendItem(label, description, toolTipText, 192 urlText, shape, paint, outlineStroke, outlinePaint); 193 result.setLabelFont(lookupLegendTextFont(series)); 194 Paint labelPaint = lookupLegendTextPaint(series); 195 if (labelPaint != null) { 196 result.setLabelPaint(labelPaint); 197 } 198 result.setDataset(dataset); 199 result.setDatasetIndex(datasetIndex); 200 result.setSeriesKey(dataset.getRowKey(series)); 201 result.setSeriesIndex(series); 202 return result; 203 204 } 205 206 /** 207 * Draw a single data item. 208 * 209 * @param g2 the graphics device. 210 * @param state the renderer state. 211 * @param dataArea the data plot area. 212 * @param plot the plot. 213 * @param domainAxis the domain axis. 214 * @param rangeAxis the range axis. 215 * @param dataset the dataset. 216 * @param row the row index (zero-based). 217 * @param column the column index (zero-based). 218 * @param pass the pass index. 219 */ 220 public void drawItem(Graphics2D g2, CategoryItemRendererState state, 221 Rectangle2D dataArea, CategoryPlot plot, CategoryAxis domainAxis, 222 ValueAxis rangeAxis, CategoryDataset dataset, int row, int column, 223 int pass) { 224 225 // do nothing if item is not visible or null 226 if (!getItemVisible(row, column)) { 227 return; 228 } 229 Number value = dataset.getValue(row, column); 230 if (value == null) { 231 return; 232 } 233 PlotOrientation orientation = plot.getOrientation(); 234 RectangleEdge axisEdge = plot.getDomainAxisEdge(); 235 int count = dataset.getColumnCount(); 236 float x0 = (float) domainAxis.getCategoryStart(column, count, dataArea, 237 axisEdge); 238 float x1 = (float) domainAxis.getCategoryMiddle(column, count, 239 dataArea, axisEdge); 240 float x2 = (float) domainAxis.getCategoryEnd(column, count, dataArea, 241 axisEdge); 242 243 x0 = Math.round(x0); 244 x1 = Math.round(x1); 245 x2 = Math.round(x2); 246 247 if (this.endType == AreaRendererEndType.TRUNCATE) { 248 if (column == 0) { 249 x0 = x1; 250 } 251 else if (column == getColumnCount() - 1) { 252 x2 = x1; 253 } 254 } 255 256 double yy1 = value.doubleValue(); 257 258 double yy0 = 0.0; 259 if (this.endType == AreaRendererEndType.LEVEL) { 260 yy0 = yy1; 261 } 262 if (column > 0) { 263 Number n0 = dataset.getValue(row, column - 1); 264 if (n0 != null) { 265 yy0 = (n0.doubleValue() + yy1) / 2.0; 266 } 267 } 268 269 double yy2 = 0.0; 270 if (column < dataset.getColumnCount() - 1) { 271 Number n2 = dataset.getValue(row, column + 1); 272 if (n2 != null) { 273 yy2 = (n2.doubleValue() + yy1) / 2.0; 274 } 275 } 276 else if (this.endType == AreaRendererEndType.LEVEL) { 277 yy2 = yy1; 278 } 279 280 RectangleEdge edge = plot.getRangeAxisEdge(); 281 float y0 = (float) rangeAxis.valueToJava2D(yy0, dataArea, edge); 282 float y1 = (float) rangeAxis.valueToJava2D(yy1, dataArea, edge); 283 float y2 = (float) rangeAxis.valueToJava2D(yy2, dataArea, edge); 284 float yz = (float) rangeAxis.valueToJava2D(0.0, dataArea, edge); 285 double labelXX = x1; 286 double labelYY = y1; 287 g2.setPaint(getItemPaint(row, column)); 288 g2.setStroke(getItemStroke(row, column)); 289 290 GeneralPath area = new GeneralPath(); 291 292 if (orientation == PlotOrientation.VERTICAL) { 293 area.moveTo(x0, yz); 294 area.lineTo(x0, y0); 295 area.lineTo(x1, y1); 296 area.lineTo(x2, y2); 297 area.lineTo(x2, yz); 298 } 299 else if (orientation == PlotOrientation.HORIZONTAL) { 300 area.moveTo(yz, x0); 301 area.lineTo(y0, x0); 302 area.lineTo(y1, x1); 303 area.lineTo(y2, x2); 304 area.lineTo(yz, x2); 305 double temp = labelXX; 306 labelXX = labelYY; 307 labelYY = temp; 308 } 309 area.closePath(); 310 311 g2.setPaint(getItemPaint(row, column)); 312 g2.fill(area); 313 314 // draw the item labels if there are any... 315 if (isItemLabelVisible(row, column)) { 316 drawItemLabel(g2, orientation, dataset, row, column, labelXX, 317 labelYY, (value.doubleValue() < 0.0)); 318 } 319 320 // submit the current data point as a crosshair candidate 321 int datasetIndex = plot.indexOf(dataset); 322 updateCrosshairValues(state.getCrosshairState(), 323 dataset.getRowKey(row), dataset.getColumnKey(column), yy1, 324 datasetIndex, x1, y1, orientation); 325 326 // add an item entity, if this information is being collected 327 EntityCollection entities = state.getEntityCollection(); 328 if (entities != null) { 329 addItemEntity(entities, dataset, row, column, area); 330 } 331 332 } 333 334 /** 335 * Tests this instance for equality with an arbitrary object. 336 * 337 * @param obj the object to test (<code>null</code> permitted). 338 * 339 * @return A boolean. 340 */ 341 public boolean equals(Object obj) { 342 if (obj == this) { 343 return true; 344 } 345 if (!(obj instanceof AreaRenderer)) { 346 return false; 347 } 348 AreaRenderer that = (AreaRenderer) obj; 349 if (!this.endType.equals(that.endType)) { 350 return false; 351 } 352 return super.equals(obj); 353 } 354 355 /** 356 * Returns an independent copy of the renderer. 357 * 358 * @return A clone. 359 * 360 * @throws CloneNotSupportedException should not happen. 361 */ 362 public Object clone() throws CloneNotSupportedException { 363 return super.clone(); 364 } 365 366 }