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