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