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 * LayeredBarRenderer.java 029 * ----------------------- 030 * (C) Copyright 2003-2020, by Arnaud Lelievre and Contributors. 031 * 032 * Original Author: Arnaud Lelievre (for Garden); 033 * Contributor(s): David Gilbert (for Object Refinery Limited); 034 * Zoheb Borbora; 035 * 036 * Changes 037 * ------- 038 * 28-Aug-2003 : Version 1 (AL); 039 * 16-Sep-2003 : Changed ChartRenderingInfo --> PlotRenderingInfo (DG); 040 * 07-Oct-2003 : Added renderer state (DG); 041 * 21-Oct-2003 : Bar width moved to renderer state (DG); 042 * 05-Nov-2004 : Modified drawItem() signature (DG); 043 * 20-Apr-2005 : Renamed CategoryLabelGenerator 044 * --> CategoryItemLabelGenerator (DG); 045 * 17-Nov-2005 : Added support for gradient paint (DG); 046 * ------------- JFREECHART 1.0.x --------------------------------------------- 047 * 18-Aug-2006 : Fixed the bar width calculation to respect the maximum bar 048 * width setting (thanks to Zoheb Borbora) (DG); 049 * 02-Feb-2007 : Removed author tags all over JFreeChart sources (DG); 050 * 19-May-2009 : Fixed FindBugs warnings, patch by Michal Wozniak (DG); 051 * 24-Aug-2014 : Add element hinting for JFreeSVG (DG); 052 * 053 */ 054 055package org.jfree.chart.renderer.category; 056 057import java.awt.GradientPaint; 058import java.awt.Graphics2D; 059import java.awt.Paint; 060import java.awt.Stroke; 061import java.awt.geom.Rectangle2D; 062import java.io.Serializable; 063 064import org.jfree.chart.axis.CategoryAxis; 065import org.jfree.chart.axis.ValueAxis; 066import org.jfree.chart.entity.EntityCollection; 067import org.jfree.chart.labels.CategoryItemLabelGenerator; 068import org.jfree.chart.plot.CategoryPlot; 069import org.jfree.chart.plot.PlotOrientation; 070import org.jfree.chart.ui.GradientPaintTransformer; 071import org.jfree.chart.ui.RectangleEdge; 072import org.jfree.chart.util.ObjectList; 073import org.jfree.data.category.CategoryDataset; 074 075/** 076 * A {@link CategoryItemRenderer} that represents data using bars which are 077 * superimposed. The example shown here is generated by the 078 * {@code LayeredBarChartDemo1.java} program included in the JFreeChart 079 * Demo Collection: 080 * <br><br> 081 * <img src="../../../../../images/LayeredBarRendererSample.png" 082 * alt="LayeredBarRendererSample.png"> 083 */ 084public class LayeredBarRenderer extends BarRenderer implements Serializable { 085 086 /** For serialization. */ 087 private static final long serialVersionUID = -8716572894780469487L; 088 089 /** A list of the width of each series bar. */ 090 protected ObjectList seriesBarWidthList; 091 092 /** 093 * Default constructor. 094 */ 095 public LayeredBarRenderer() { 096 super(); 097 this.seriesBarWidthList = new ObjectList(); 098 } 099 100 /** 101 * Returns the bar width for a series, or {@code Double.NaN} if no 102 * width has been set. 103 * 104 * @param series the series index (zero based). 105 * 106 * @return The width for the series (1.0=100%, it is the maximum). 107 */ 108 public double getSeriesBarWidth(int series) { 109 double result = Double.NaN; 110 Number n = (Number) this.seriesBarWidthList.get(series); 111 if (n != null) { 112 result = n.doubleValue(); 113 } 114 return result; 115 } 116 117 /** 118 * Sets the width of the bars of a series. 119 * 120 * @param series the series index (zero based). 121 * @param width the width of the series bar in percentage (1.0=100%, it is 122 * the maximum). 123 */ 124 public void setSeriesBarWidth(int series, double width) { 125 this.seriesBarWidthList.set(series, width); 126 } 127 128 /** 129 * Calculates the bar width and stores it in the renderer state. 130 * 131 * @param plot the plot. 132 * @param dataArea the data area. 133 * @param rendererIndex the renderer index. 134 * @param state the renderer state. 135 */ 136 @Override 137 protected void calculateBarWidth(CategoryPlot plot, Rectangle2D dataArea, 138 int rendererIndex, CategoryItemRendererState state) { 139 140 // calculate the bar width - this calculation differs from the 141 // BarRenderer calculation because the bars are layered on top of one 142 // another, so there is effectively only one bar per category for 143 // the purpose of the bar width calculation 144 CategoryAxis domainAxis = getDomainAxis(plot, rendererIndex); 145 CategoryDataset dataset = plot.getDataset(rendererIndex); 146 if (dataset != null) { 147 int columns = dataset.getColumnCount(); 148 int rows = dataset.getRowCount(); 149 double space = 0.0; 150 PlotOrientation orientation = plot.getOrientation(); 151 if (orientation == PlotOrientation.HORIZONTAL) { 152 space = dataArea.getHeight(); 153 } 154 else if (orientation == PlotOrientation.VERTICAL) { 155 space = dataArea.getWidth(); 156 } 157 double maxWidth = space * getMaximumBarWidth(); 158 double categoryMargin = 0.0; 159 if (columns > 1) { 160 categoryMargin = domainAxis.getCategoryMargin(); 161 } 162 double used = space * (1 - domainAxis.getLowerMargin() 163 - domainAxis.getUpperMargin() - categoryMargin); 164 if ((rows * columns) > 0) { 165 state.setBarWidth(Math.min(used / (dataset.getColumnCount()), 166 maxWidth)); 167 } 168 else { 169 state.setBarWidth(Math.min(used, maxWidth)); 170 } 171 } 172 } 173 174 /** 175 * Draws the bar for one item in the dataset. 176 * 177 * @param g2 the graphics device. 178 * @param state the renderer state. 179 * @param dataArea the plot area. 180 * @param plot the plot. 181 * @param domainAxis the domain (category) axis. 182 * @param rangeAxis the range (value) axis. 183 * @param data the data. 184 * @param row the row index (zero-based). 185 * @param column the column index (zero-based). 186 * @param pass the pass index. 187 */ 188 @Override 189 public void drawItem(Graphics2D g2, CategoryItemRendererState state, 190 Rectangle2D dataArea, CategoryPlot plot, CategoryAxis domainAxis, 191 ValueAxis rangeAxis, CategoryDataset data, int row, int column, 192 int pass) { 193 194 PlotOrientation orientation = plot.getOrientation(); 195 if (orientation.isHorizontal()) { 196 drawHorizontalItem(g2, state, dataArea, plot, domainAxis, 197 rangeAxis, data, row, column); 198 } else if (orientation.isVertical()) { 199 drawVerticalItem(g2, state, dataArea, plot, domainAxis, rangeAxis, 200 data, row, column); 201 } 202 203 } 204 205 /** 206 * Draws the bar for a single (series, category) data item. 207 * 208 * @param g2 the graphics device. 209 * @param state the renderer state. 210 * @param dataArea the data area. 211 * @param plot the plot. 212 * @param domainAxis the domain axis. 213 * @param rangeAxis the range axis. 214 * @param dataset the dataset. 215 * @param row the row index (zero-based). 216 * @param column the column index (zero-based). 217 */ 218 protected void drawHorizontalItem(Graphics2D g2, 219 CategoryItemRendererState state, Rectangle2D dataArea, 220 CategoryPlot plot, CategoryAxis domainAxis, ValueAxis rangeAxis, 221 CategoryDataset dataset, int row, int column) { 222 223 // nothing is drawn for null values... 224 Number dataValue = dataset.getValue(row, column); 225 if (dataValue == null) { 226 return; 227 } 228 229 // X 230 double value = dataValue.doubleValue(); 231 double base = getBase(); 232 double lclip = getLowerClip(); 233 double uclip = getUpperClip(); 234 if (uclip <= 0.0) { // cases 1, 2, 3 and 4 235 if (value >= uclip) { 236 return; // bar is not visible 237 } 238 base = uclip; 239 if (value <= lclip) { 240 value = lclip; 241 } 242 } else if (lclip <= 0.0) { // cases 5, 6, 7 and 8 243 if (value >= uclip) { 244 value = uclip; 245 } 246 else { 247 if (value <= lclip) { 248 value = lclip; 249 } 250 } 251 } else { // cases 9, 10, 11 and 12 252 if (value <= lclip) { 253 return; // bar is not visible 254 } 255 base = lclip; 256 if (value >= uclip) { 257 value = uclip; 258 } 259 } 260 261 RectangleEdge edge = plot.getRangeAxisEdge(); 262 double transX1 = rangeAxis.valueToJava2D(base, dataArea, edge); 263 double transX2 = rangeAxis.valueToJava2D(value, dataArea, edge); 264 double rectX = Math.min(transX1, transX2); 265 double rectWidth = Math.abs(transX2 - transX1); 266 267 // Y 268 double rectY = domainAxis.getCategoryMiddle(column, getColumnCount(), 269 dataArea, plot.getDomainAxisEdge()) - state.getBarWidth() / 2.0; 270 271 int seriesCount = getRowCount(); 272 273 // draw the bar... 274 double shift = 0.0; 275 double rectHeight; 276 double widthFactor = 1.0; 277 double seriesBarWidth = getSeriesBarWidth(row); 278 if (!Double.isNaN(seriesBarWidth)) { 279 widthFactor = seriesBarWidth; 280 } 281 rectHeight = widthFactor * state.getBarWidth(); 282 rectY = rectY + (1 - widthFactor) * state.getBarWidth() / 2.0; 283 if (seriesCount > 1) { 284 shift = rectHeight * 0.20 / (seriesCount - 1); 285 } 286 287 Rectangle2D bar = new Rectangle2D.Double(rectX, 288 (rectY + ((seriesCount - 1 - row) * shift)), rectWidth, 289 (rectHeight - (seriesCount - 1 - row) * shift * 2)); 290 291 if (state.getElementHinting()) { 292 beginElementGroup(g2, dataset.getRowKey(row), 293 dataset.getColumnKey(column)); 294 } 295 296 Paint itemPaint = getItemPaint(row, column); 297 GradientPaintTransformer t = getGradientPaintTransformer(); 298 if (t != null && itemPaint instanceof GradientPaint) { 299 itemPaint = t.transform((GradientPaint) itemPaint, bar); 300 } 301 g2.setPaint(itemPaint); 302 g2.fill(bar); 303 304 // draw the outline... 305 if (isDrawBarOutline() 306 && state.getBarWidth() > BAR_OUTLINE_WIDTH_THRESHOLD) { 307 Stroke stroke = getItemOutlineStroke(row, column); 308 Paint paint = getItemOutlinePaint(row, column); 309 if (stroke != null && paint != null) { 310 g2.setStroke(stroke); 311 g2.setPaint(paint); 312 g2.draw(bar); 313 } 314 } 315 316 CategoryItemLabelGenerator generator = getItemLabelGenerator(row, 317 column); 318 if (generator != null && isItemLabelVisible(row, column)) { 319 drawItemLabel(g2, dataset, row, column, plot, generator, bar, 320 value < base); 321 } 322 323 // collect entity and tool tip information... 324 EntityCollection entities = state.getEntityCollection(); 325 if (entities != null) { 326 addItemEntity(entities, dataset, row, column, bar); 327 } 328 } 329 330 /** 331 * Draws the bar for a single (series, category) data item. 332 * 333 * @param g2 the graphics device. 334 * @param state the renderer state. 335 * @param dataArea the data area. 336 * @param plot the plot. 337 * @param domainAxis the domain axis. 338 * @param rangeAxis the range axis. 339 * @param dataset the dataset. 340 * @param row the row index (zero-based). 341 * @param column the column index (zero-based). 342 */ 343 protected void drawVerticalItem(Graphics2D g2, 344 CategoryItemRendererState state, Rectangle2D dataArea, 345 CategoryPlot plot, CategoryAxis domainAxis, ValueAxis rangeAxis, 346 CategoryDataset dataset, int row, int column) { 347 348 // nothing is drawn for null values... 349 Number dataValue = dataset.getValue(row, column); 350 if (dataValue == null) { 351 return; 352 } 353 354 // BAR X 355 double rectX = domainAxis.getCategoryMiddle(column, getColumnCount(), 356 dataArea, plot.getDomainAxisEdge()) - state.getBarWidth() / 2.0; 357 358 int seriesCount = getRowCount(); 359 360 // BAR Y 361 double value = dataValue.doubleValue(); 362 double base = getBase(); 363 double lclip = getLowerClip(); 364 double uclip = getUpperClip(); 365 366 if (uclip <= 0.0) { // cases 1, 2, 3 and 4 367 if (value >= uclip) { 368 return; // bar is not visible 369 } 370 base = uclip; 371 if (value <= lclip) { 372 value = lclip; 373 } 374 } else if (lclip <= 0.0) { // cases 5, 6, 7 and 8 375 if (value >= uclip) { 376 value = uclip; 377 } else { 378 if (value <= lclip) { 379 value = lclip; 380 } 381 } 382 } else { // cases 9, 10, 11 and 12 383 if (value <= lclip) { 384 return; // bar is not visible 385 } 386 base = getLowerClip(); 387 if (value >= uclip) { 388 value = uclip; 389 } 390 } 391 392 RectangleEdge edge = plot.getRangeAxisEdge(); 393 double transY1 = rangeAxis.valueToJava2D(base, dataArea, edge); 394 double transY2 = rangeAxis.valueToJava2D(value, dataArea, edge); 395 double rectY = Math.min(transY2, transY1); 396 397 double rectWidth; 398 double rectHeight = Math.abs(transY2 - transY1); 399 400 // draw the bar... 401 double shift = 0.0; 402 double widthFactor = 1.0; 403 double seriesBarWidth = getSeriesBarWidth(row); 404 if (!Double.isNaN(seriesBarWidth)) { 405 widthFactor = seriesBarWidth; 406 } 407 rectWidth = widthFactor * state.getBarWidth(); 408 rectX = rectX + (1 - widthFactor) * state.getBarWidth() / 2.0; 409 if (seriesCount > 1) { 410 // needs to be improved !!! 411 shift = rectWidth * 0.20 / (seriesCount - 1); 412 } 413 414 Rectangle2D bar = new Rectangle2D.Double( 415 (rectX + ((seriesCount - 1 - row) * shift)), rectY, 416 (rectWidth - (seriesCount - 1 - row) * shift * 2), rectHeight); 417 418 if (state.getElementHinting()) { 419 beginElementGroup(g2, dataset.getRowKey(row), 420 dataset.getColumnKey(column)); 421 } 422 423 Paint itemPaint = getItemPaint(row, column); 424 GradientPaintTransformer t = getGradientPaintTransformer(); 425 if (t != null && itemPaint instanceof GradientPaint) { 426 itemPaint = t.transform((GradientPaint) itemPaint, bar); 427 } 428 g2.setPaint(itemPaint); 429 g2.fill(bar); 430 431 if (isDrawBarOutline() && state.getBarWidth() 432 > BAR_OUTLINE_WIDTH_THRESHOLD) { 433 g2.setStroke(getItemOutlineStroke(row, column)); 434 g2.setPaint(getItemOutlinePaint(row, column)); 435 g2.draw(bar); 436 } 437 438 if (state.getElementHinting()) { 439 endElementGroup(g2); 440 } 441 442 // draw the item labels if there are any... 443 CategoryItemLabelGenerator generator = getItemLabelGenerator(row, 444 column); 445 if (generator != null && isItemLabelVisible(row, column)) { 446 drawItemLabel(g2, dataset, row, column, plot, generator, bar, 447 value < base); 448 } 449 450 // collect entity and tool tip information... 451 EntityCollection entities = state.getEntityCollection(); 452 if (entities != null) { 453 addItemEntity(entities, dataset, row, column, bar); 454 } 455 } 456 457}