1 51 52 package org.jfree.chart.renderer; 53 54 import java.awt.Graphics2D ; 55 import java.awt.Paint ; 56 import java.awt.Stroke ; 57 import java.awt.geom.Rectangle2D ; 58 import java.io.Serializable ; 59 60 import org.jfree.chart.axis.CategoryAxis; 61 import org.jfree.chart.axis.ValueAxis; 62 import org.jfree.chart.entity.CategoryItemEntity; 63 import org.jfree.chart.entity.EntityCollection; 64 import org.jfree.chart.labels.CategoryItemLabelGenerator; 65 import org.jfree.chart.plot.CategoryPlot; 66 import org.jfree.chart.plot.PlotOrientation; 67 import org.jfree.data.CategoryDataset; 68 import org.jfree.data.IntervalCategoryDataset; 69 import org.jfree.data.MultiIntervalCategoryDataset; 70 import org.jfree.ui.RectangleEdge; 71 import org.jfree.util.PublicCloneable; 72 73 74 82 public class IntervalBarRenderer extends BarRenderer 83 implements CategoryItemRenderer, Cloneable , PublicCloneable, Serializable { 84 85 88 public IntervalBarRenderer() { 89 super(); 90 } 91 92 105 public void drawItem(Graphics2D g2, 106 CategoryItemRendererState state, 107 Rectangle2D dataArea, 108 CategoryPlot plot, 109 CategoryAxis domainAxis, 110 ValueAxis rangeAxis, 111 CategoryDataset dataset, 112 int row, 113 int column) { 114 115 if (dataset instanceof MultiIntervalCategoryDataset) { 116 MultiIntervalCategoryDataset d = (MultiIntervalCategoryDataset) dataset; 117 drawSubIntervals(g2, state, dataArea, plot, domainAxis, rangeAxis, d, row, column); 118 } 119 else if (dataset instanceof IntervalCategoryDataset) { 120 IntervalCategoryDataset d = (IntervalCategoryDataset) dataset; 121 drawInterval(g2, state, dataArea, plot, domainAxis, rangeAxis, d, row, column); 122 } 123 else { 124 super.drawItem(g2, state, dataArea, plot, domainAxis, rangeAxis, dataset, row, column); 125 } 126 } 127 128 141 protected void drawInterval(Graphics2D g2, 142 CategoryItemRendererState state, 143 Rectangle2D dataArea, 144 CategoryPlot plot, 145 CategoryAxis domainAxis, 146 ValueAxis rangeAxis, 147 IntervalCategoryDataset dataset, 148 int row, 149 int column) { 150 151 int seriesCount = getRowCount(); 152 int categoryCount = getColumnCount(); 153 154 PlotOrientation orientation = plot.getOrientation(); 155 156 double rectX = 0.0; 157 double rectY = 0.0; 158 159 RectangleEdge domainAxisLocation = plot.getDomainAxisEdge(); 160 RectangleEdge rangeAxisLocation = plot.getRangeAxisEdge(); 161 162 Number value0 = dataset.getEndValue(row, column); 164 if (value0 == null) { 165 return; 166 } 167 double java2dValue0 = rangeAxis.translateValueToJava2D(value0.doubleValue(), dataArea, 168 rangeAxisLocation); 169 170 Number value1 = dataset.getStartValue(row, column); 172 if (value1 == null) { 173 return; 174 } 175 double java2dValue1 = rangeAxis.translateValueToJava2D(value1.doubleValue(), dataArea, 176 rangeAxisLocation); 177 178 if (java2dValue1 < java2dValue0) { 179 double temp = java2dValue1; 180 java2dValue1 = java2dValue0; 181 java2dValue0 = temp; 182 Number tempNum = value1; 183 value1 = value0; 184 value0 = tempNum; 185 } 186 187 double rectWidth = state.getBarWidth(); 189 190 double rectHeight = Math.abs(java2dValue1 - java2dValue0); 192 193 if (orientation == PlotOrientation.HORIZONTAL) { 194 rectY = domainAxis.getCategoryStart(column, getColumnCount(), dataArea, 196 domainAxisLocation); 197 if (seriesCount > 1) { 198 double seriesGap = dataArea.getHeight() * getItemMargin() 199 / (categoryCount * (seriesCount - 1)); 200 rectY = rectY + row * (state.getBarWidth() + seriesGap); 201 } 202 else { 203 rectY = rectY + row * state.getBarWidth(); 204 } 205 206 rectX = java2dValue0; 207 208 rectHeight = state.getBarWidth(); 209 rectWidth = Math.abs(java2dValue1 - java2dValue0); 210 211 } 212 else if (orientation == PlotOrientation.VERTICAL) { 213 rectX = domainAxis.getCategoryStart(column, getColumnCount(), dataArea, 215 domainAxisLocation); 216 217 if (seriesCount > 1) { 218 double seriesGap = dataArea.getWidth() * getItemMargin() 219 / (categoryCount * (seriesCount - 1)); 220 rectX = rectX + row * (state.getBarWidth() + seriesGap); 221 } 222 else { 223 rectX = rectX + row * state.getBarWidth(); 224 } 225 226 rectY = java2dValue0; 227 228 } 229 Rectangle2D bar = new Rectangle2D.Double (rectX, rectY, rectWidth, rectHeight); 230 Paint seriesPaint = getItemPaint(row, column); 231 g2.setPaint(seriesPaint); 232 g2.fill(bar); 233 234 if (state.getBarWidth() > BAR_OUTLINE_WIDTH_THRESHOLD) { 236 Stroke stroke = getItemOutlineStroke(row, column); 237 Paint paint = getItemOutlinePaint(row, column); 238 if (stroke != null && paint != null) { 239 g2.setStroke(stroke); 240 g2.setPaint(paint); 241 g2.draw(bar); 242 } 243 } 244 245 CategoryItemLabelGenerator generator = getItemLabelGenerator(row, column); 246 if (generator != null && isItemLabelVisible(row, column)) { 247 drawItemLabel(g2, dataset, row, column, plot, generator, bar, false); 248 } 249 250 if (state.getInfo() != null) { 252 EntityCollection entities = state.getInfo().getOwner().getEntityCollection(); 253 if (entities != null) { 254 String tip = null; 255 if (generator != null) { 256 tip = generator.generateToolTip(dataset, row, column); 257 } 258 String url = null; 259 if (getItemURLGenerator(row, column) != null) { 260 url = getItemURLGenerator(row, column).generateURL(dataset, row, column); 261 } 262 CategoryItemEntity entity = new CategoryItemEntity( 263 bar, tip, url, dataset, row, dataset.getColumnKey(column), column 264 ); 265 entities.addEntity(entity); 266 } 267 } 268 269 } 270 271 284 private void drawSubIntervals(Graphics2D g2, 285 CategoryItemRendererState state, 286 Rectangle2D dataArea, 287 CategoryPlot plot, 288 CategoryAxis domainAxis, 289 ValueAxis rangeAxis, 290 MultiIntervalCategoryDataset dataset, 291 int row, 292 int column) { 293 294 int count = dataset.getSubIntervalCount(row, column); 295 if (count == 0) { 296 drawInterval(g2, state, dataArea, plot, domainAxis, rangeAxis, dataset, row, column); 297 } 298 299 for (int subinterval = 0; subinterval < count; subinterval++) { 300 301 RectangleEdge domainAxisLocation = plot.getDomainAxisEdge(); 302 RectangleEdge rangeAxisLocation = plot.getRangeAxisEdge(); 303 304 Number value0 = dataset.getStartValue(row, column, subinterval); 306 if (value0 == null) { 307 return; 308 } 309 double translatedValue0 = rangeAxis.translateValueToJava2D(value0.doubleValue(), 310 dataArea, 311 rangeAxisLocation); 312 313 Number value1 = dataset.getEndValue(row, column, subinterval); 315 if (value1 == null) { 316 return; 317 } 318 double translatedValue1 = rangeAxis.translateValueToJava2D(value1.doubleValue(), 319 dataArea, 320 rangeAxisLocation); 321 322 if (translatedValue1 < translatedValue0) { 323 double temp = translatedValue1; 324 translatedValue1 = translatedValue0; 325 translatedValue0 = temp; 326 } 327 328 double rectY = domainAxis.getCategoryStart(column, getColumnCount(), dataArea, 330 domainAxisLocation); 331 332 int rows = getRowCount(); 333 int columns = getColumnCount(); 334 if (rows > 1) { 335 double seriesGap = dataArea.getHeight() * getItemMargin() 336 / (columns * (rows - 1)); 337 rectY = rectY + row * (state.getBarWidth() + seriesGap); 338 } 339 else { 340 rectY = rectY + row * state.getBarWidth(); 341 } 342 343 double rectWidth = Math.abs(translatedValue1 - translatedValue0); 345 346 double rectHeight = state.getBarWidth(); 348 349 Rectangle2D bar = null; 351 352 if (plot.getOrientation() == PlotOrientation.HORIZONTAL) { 353 bar = new Rectangle2D.Double (translatedValue0, rectY, 354 rectWidth, rectHeight); 355 } 356 else if (plot.getOrientation() == PlotOrientation.VERTICAL) { 357 bar = new Rectangle2D.Double (rectY, translatedValue0, rectHeight, rectWidth); 358 } 359 360 Paint seriesPaint = getItemPaint(row, column); 361 g2.setPaint(seriesPaint); 362 g2.fill(bar); 363 if (state.getBarWidth() > BAR_OUTLINE_WIDTH_THRESHOLD) { 364 g2.setStroke(getItemStroke(row, column)); 365 g2.setPaint(getItemOutlinePaint(row, column)); 366 g2.draw(bar); 367 } 368 369 if (state.getInfo() != null) { 371 EntityCollection entities = state.getInfo().getOwner().getEntityCollection(); 372 if (entities != null) { 373 String tip = null; 374 if (getItemLabelGenerator(row, column) != null) { 375 tip = getItemLabelGenerator(row, column).generateToolTip(dataset, 376 row, column); 377 } 378 String url = null; 379 if (getItemURLGenerator(row, column) != null) { 380 url = getItemURLGenerator(row, column).generateURL(dataset, row, column); 381 } 382 CategoryItemEntity entity = new CategoryItemEntity( 383 bar, tip, url, dataset, row, dataset.getColumnKey(column), column 384 ); 385 entities.addEntity(entity); 386 } 387 } 388 } 389 } 390 391 392 } 393 | Popular Tags |