001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2014, 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 * LevelRenderer.java 029 * ------------------ 030 * (C) Copyright 2004-2014, by Object Refinery Limited. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): Peter Kolb (patch 2511330); 034 * 035 * Changes 036 * ------- 037 * 09-Jan-2004 : Version 1 (DG); 038 * 05-Nov-2004 : Modified drawItem() signature (DG); 039 * 20-Apr-2005 : Renamed CategoryLabelGenerator 040 * --> CategoryItemLabelGenerator (DG); 041 * ------------- JFREECHART 1.0.x --------------------------------------------- 042 * 23-Jan-2006 : Renamed getMaxItemWidth() --> getMaximumItemWidth() (DG); 043 * 13-May-2008 : Code clean-up (DG); 044 * 26-Jun-2008 : Added crosshair support (DG); 045 * 23-Jan-2009 : Set more appropriate default shape in legend (DG); 046 * 23-Jan-2009 : Added support for seriesVisible flags - see patch 047 * 2511330 (PK) 048 * 049 */ 050 051package org.jfree.chart.renderer.category; 052 053import java.awt.Color; 054import java.awt.Graphics2D; 055import java.awt.Paint; 056import java.awt.Stroke; 057import java.awt.geom.Line2D; 058import java.awt.geom.Rectangle2D; 059import java.io.Serializable; 060 061import org.jfree.chart.HashUtilities; 062import org.jfree.chart.axis.CategoryAxis; 063import org.jfree.chart.axis.ValueAxis; 064import org.jfree.chart.entity.EntityCollection; 065import org.jfree.chart.event.RendererChangeEvent; 066import org.jfree.chart.labels.CategoryItemLabelGenerator; 067import org.jfree.chart.plot.CategoryPlot; 068import org.jfree.chart.plot.PlotOrientation; 069import org.jfree.chart.plot.PlotRenderingInfo; 070import org.jfree.data.category.CategoryDataset; 071import org.jfree.ui.RectangleEdge; 072import org.jfree.util.PublicCloneable; 073 074/** 075 * A {@link CategoryItemRenderer} that draws individual data items as 076 * horizontal lines, spaced in the same way as bars in a bar chart. The 077 * example shown here is generated by the 078 * <code>OverlaidBarChartDemo2.java</code> program included in the JFreeChart 079 * Demo Collection: 080 * <br><br> 081 * <img src="../../../../../images/LevelRendererSample.png" 082 * alt="LevelRendererSample.png"> 083 */ 084public class LevelRenderer extends AbstractCategoryItemRenderer 085 implements Cloneable, PublicCloneable, Serializable { 086 087 /** For serialization. */ 088 private static final long serialVersionUID = -8204856624355025117L; 089 090 /** The default item margin percentage. */ 091 public static final double DEFAULT_ITEM_MARGIN = 0.20; 092 093 /** The margin between items within a category. */ 094 private double itemMargin; 095 096 /** The maximum item width as a percentage of the available space. */ 097 private double maxItemWidth; 098 099 /** 100 * Creates a new renderer with default settings. 101 */ 102 public LevelRenderer() { 103 super(); 104 this.itemMargin = DEFAULT_ITEM_MARGIN; 105 this.maxItemWidth = 1.0; // 100 percent, so it will not apply unless 106 // changed 107 setBaseLegendShape(new Rectangle2D.Float(-5.0f, -1.0f, 10.0f, 2.0f)); 108 // set the outline paint to fully transparent, then the legend shape 109 // will just have the same colour as the lines drawn by the renderer 110 setBaseOutlinePaint(new Color(0, 0, 0, 0)); 111 } 112 113 /** 114 * Returns the item margin. 115 * 116 * @return The margin. 117 * 118 * @see #setItemMargin(double) 119 */ 120 public double getItemMargin() { 121 return this.itemMargin; 122 } 123 124 /** 125 * Sets the item margin and sends a {@link RendererChangeEvent} to all 126 * registered listeners. The value is expressed as a percentage of the 127 * available width for plotting all the bars, with the resulting amount to 128 * be distributed between all the bars evenly. 129 * 130 * @param percent the new margin. 131 * 132 * @see #getItemMargin() 133 */ 134 public void setItemMargin(double percent) { 135 this.itemMargin = percent; 136 fireChangeEvent(); 137 } 138 139 /** 140 * Returns the maximum width, as a percentage of the available drawing 141 * space. 142 * 143 * @return The maximum width. 144 * 145 * @see #setMaximumItemWidth(double) 146 */ 147 public double getMaximumItemWidth() { 148 return getMaxItemWidth(); 149 } 150 151 /** 152 * Sets the maximum item width, which is specified as a percentage of the 153 * available space for all items, and sends a {@link RendererChangeEvent} 154 * to all registered listeners. 155 * 156 * @param percent the percent. 157 * 158 * @see #getMaximumItemWidth() 159 */ 160 public void setMaximumItemWidth(double percent) { 161 setMaxItemWidth(percent); 162 } 163 164 /** 165 * Initialises the renderer and returns a state object that will be passed 166 * to subsequent calls to the drawItem method. 167 * <p> 168 * This method gets called once at the start of the process of drawing a 169 * chart. 170 * 171 * @param g2 the graphics device. 172 * @param dataArea the area in which the data is to be plotted. 173 * @param plot the plot. 174 * @param rendererIndex the renderer index. 175 * @param info collects chart rendering information for return to caller. 176 * 177 * @return The renderer state. 178 */ 179 @Override 180 public CategoryItemRendererState initialise(Graphics2D g2, 181 Rectangle2D dataArea, CategoryPlot plot, int rendererIndex, 182 PlotRenderingInfo info) { 183 184 CategoryItemRendererState state = super.initialise(g2, dataArea, plot, 185 rendererIndex, info); 186 calculateItemWidth(plot, dataArea, rendererIndex, state); 187 return state; 188 189 } 190 191 /** 192 * Calculates the bar width and stores it in the renderer state. 193 * 194 * @param plot the plot. 195 * @param dataArea the data area. 196 * @param rendererIndex the renderer index. 197 * @param state the renderer state. 198 */ 199 protected void calculateItemWidth(CategoryPlot plot, 200 Rectangle2D dataArea, int rendererIndex, 201 CategoryItemRendererState state) { 202 203 CategoryAxis domainAxis = getDomainAxis(plot, rendererIndex); 204 CategoryDataset dataset = plot.getDataset(rendererIndex); 205 if (dataset != null) { 206 int columns = dataset.getColumnCount(); 207 int rows = state.getVisibleSeriesCount() >= 0 208 ? state.getVisibleSeriesCount() : dataset.getRowCount(); 209 double space = 0.0; 210 PlotOrientation orientation = plot.getOrientation(); 211 if (orientation == PlotOrientation.HORIZONTAL) { 212 space = dataArea.getHeight(); 213 } 214 else if (orientation == PlotOrientation.VERTICAL) { 215 space = dataArea.getWidth(); 216 } 217 double maxWidth = space * getMaximumItemWidth(); 218 double categoryMargin = 0.0; 219 double currentItemMargin = 0.0; 220 if (columns > 1) { 221 categoryMargin = domainAxis.getCategoryMargin(); 222 } 223 if (rows > 1) { 224 currentItemMargin = getItemMargin(); 225 } 226 double used = space * (1 - domainAxis.getLowerMargin() 227 - domainAxis.getUpperMargin() 228 - categoryMargin - currentItemMargin); 229 if ((rows * columns) > 0) { 230 state.setBarWidth(Math.min(used / (rows * columns), maxWidth)); 231 } 232 else { 233 state.setBarWidth(Math.min(used, maxWidth)); 234 } 235 } 236 } 237 238 /** 239 * Calculates the coordinate of the first "side" of a bar. This will be 240 * the minimum x-coordinate for a vertical bar, and the minimum 241 * y-coordinate for a horizontal bar. 242 * 243 * @param plot the plot. 244 * @param orientation the plot orientation. 245 * @param dataArea the data area. 246 * @param domainAxis the domain axis. 247 * @param state the renderer state (has the bar width precalculated). 248 * @param row the row index. 249 * @param column the column index. 250 * 251 * @return The coordinate. 252 */ 253 protected double calculateBarW0(CategoryPlot plot, 254 PlotOrientation orientation, Rectangle2D dataArea, 255 CategoryAxis domainAxis, CategoryItemRendererState state, int row, 256 int column) { 257 // calculate bar width... 258 double space; 259 if (orientation == PlotOrientation.HORIZONTAL) { 260 space = dataArea.getHeight(); 261 } 262 else { 263 space = dataArea.getWidth(); 264 } 265 double barW0 = domainAxis.getCategoryStart(column, getColumnCount(), 266 dataArea, plot.getDomainAxisEdge()); 267 int seriesCount = state.getVisibleSeriesCount(); 268 if (seriesCount < 0) { 269 seriesCount = getRowCount(); 270 } 271 int categoryCount = getColumnCount(); 272 if (seriesCount > 1) { 273 double seriesGap = space * getItemMargin() 274 / (categoryCount * (seriesCount - 1)); 275 double seriesW = calculateSeriesWidth(space, domainAxis, 276 categoryCount, seriesCount); 277 barW0 = barW0 + row * (seriesW + seriesGap) 278 + (seriesW / 2.0) - (state.getBarWidth() / 2.0); 279 } 280 else { 281 barW0 = domainAxis.getCategoryMiddle(column, getColumnCount(), 282 dataArea, plot.getDomainAxisEdge()) - state.getBarWidth() 283 / 2.0; 284 } 285 return barW0; 286 } 287 288 /** 289 * Draws the bar for a single (series, category) data item. 290 * 291 * @param g2 the graphics device. 292 * @param state the renderer state. 293 * @param dataArea the data area. 294 * @param plot the plot. 295 * @param domainAxis the domain axis. 296 * @param rangeAxis the range axis. 297 * @param dataset the dataset. 298 * @param row the row index (zero-based). 299 * @param column the column index (zero-based). 300 * @param pass the pass index. 301 */ 302 @Override 303 public void drawItem(Graphics2D g2, CategoryItemRendererState state, 304 Rectangle2D dataArea, CategoryPlot plot, CategoryAxis domainAxis, 305 ValueAxis rangeAxis, CategoryDataset dataset, int row, int column, 306 int pass) { 307 308 // nothing is drawn if the row index is not included in the list with 309 // the indices of the visible rows... 310 int visibleRow = state.getVisibleSeriesIndex(row); 311 if (visibleRow < 0) { 312 return; 313 } 314 315 // nothing is drawn for null values... 316 Number dataValue = dataset.getValue(row, column); 317 if (dataValue == null) { 318 return; 319 } 320 321 double value = dataValue.doubleValue(); 322 323 PlotOrientation orientation = plot.getOrientation(); 324 double barW0 = calculateBarW0(plot, orientation, dataArea, domainAxis, 325 state, visibleRow, column); 326 RectangleEdge edge = plot.getRangeAxisEdge(); 327 double barL = rangeAxis.valueToJava2D(value, dataArea, edge); 328 329 // draw the bar... 330 Line2D line; 331 double x, y; 332 if (orientation == PlotOrientation.HORIZONTAL) { 333 x = barL; 334 y = barW0 + state.getBarWidth() / 2.0; 335 line = new Line2D.Double(barL, barW0, barL, 336 barW0 + state.getBarWidth()); 337 } 338 else { 339 x = barW0 + state.getBarWidth() / 2.0; 340 y = barL; 341 line = new Line2D.Double(barW0, barL, barW0 + state.getBarWidth(), 342 barL); 343 } 344 Stroke itemStroke = getItemStroke(row, column); 345 Paint itemPaint = getItemPaint(row, column); 346 g2.setStroke(itemStroke); 347 g2.setPaint(itemPaint); 348 g2.draw(line); 349 350 CategoryItemLabelGenerator generator = getItemLabelGenerator(row, 351 column); 352 if (generator != null && isItemLabelVisible(row, column)) { 353 drawItemLabel(g2, orientation, dataset, row, column, x, y, 354 (value < 0.0)); 355 } 356 357 // submit the current data point as a crosshair candidate 358 int datasetIndex = plot.indexOf(dataset); 359 updateCrosshairValues(state.getCrosshairState(), 360 dataset.getRowKey(row), dataset.getColumnKey(column), value, 361 datasetIndex, barW0, barL, orientation); 362 363 // collect entity and tool tip information... 364 EntityCollection entities = state.getEntityCollection(); 365 if (entities != null) { 366 addItemEntity(entities, dataset, row, column, line.getBounds()); 367 } 368 369 } 370 371 /** 372 * Calculates the available space for each series. 373 * 374 * @param space the space along the entire axis (in Java2D units). 375 * @param axis the category axis. 376 * @param categories the number of categories. 377 * @param series the number of series. 378 * 379 * @return The width of one series. 380 */ 381 protected double calculateSeriesWidth(double space, CategoryAxis axis, 382 int categories, int series) { 383 double factor = 1.0 - getItemMargin() - axis.getLowerMargin() 384 - axis.getUpperMargin(); 385 if (categories > 1) { 386 factor = factor - axis.getCategoryMargin(); 387 } 388 return (space * factor) / (categories * series); 389 } 390 391 /** 392 * Returns the Java2D coordinate for the middle of the specified data item. 393 * 394 * @param rowKey the row key. 395 * @param columnKey the column key. 396 * @param dataset the dataset. 397 * @param axis the axis. 398 * @param area the drawing area. 399 * @param edge the edge along which the axis lies. 400 * 401 * @return The Java2D coordinate. 402 * 403 * @since 1.0.11 404 */ 405 @Override 406 public double getItemMiddle(Comparable rowKey, Comparable columnKey, 407 CategoryDataset dataset, CategoryAxis axis, Rectangle2D area, 408 RectangleEdge edge) { 409 return axis.getCategorySeriesMiddle(columnKey, rowKey, dataset, 410 this.itemMargin, area, edge); 411 } 412 413 /** 414 * Tests an object for equality with this instance. 415 * 416 * @param obj the object (<code>null</code> permitted). 417 * 418 * @return A boolean. 419 */ 420 @Override 421 public boolean equals(Object obj) { 422 if (obj == this) { 423 return true; 424 } 425 if (!(obj instanceof LevelRenderer)) { 426 return false; 427 } 428 LevelRenderer that = (LevelRenderer) obj; 429 if (this.itemMargin != that.itemMargin) { 430 return false; 431 } 432 if (this.maxItemWidth != that.maxItemWidth) { 433 return false; 434 } 435 return super.equals(obj); 436 } 437 438 /** 439 * Returns a hash code for this instance. 440 * 441 * @return A hash code. 442 */ 443 @Override 444 public int hashCode() { 445 int hash = super.hashCode(); 446 hash = HashUtilities.hashCode(hash, this.itemMargin); 447 hash = HashUtilities.hashCode(hash, this.maxItemWidth); 448 return hash; 449 } 450 451 /** 452 * Returns the maximum width, as a percentage of the available drawing 453 * space. 454 * 455 * @return The maximum width. 456 * 457 * @deprecated Use {@link #getMaximumItemWidth()} instead. 458 */ 459 public double getMaxItemWidth() { 460 return this.maxItemWidth; 461 } 462 463 /** 464 * Sets the maximum item width, which is specified as a percentage of the 465 * available space for all items, and sends a {@link RendererChangeEvent} 466 * to all registered listeners. 467 * 468 * @param percent the percent. 469 * 470 * @deprecated Use {@link #setMaximumItemWidth(double)} instead. 471 */ 472 public void setMaxItemWidth(double percent) { 473 this.maxItemWidth = percent; 474 fireChangeEvent(); 475 } 476 477}