1 61 62 package org.jfree.chart.renderer.category; 63 64 import java.awt.Graphics2D ; 65 import java.awt.Paint ; 66 import java.awt.Stroke ; 67 import java.awt.geom.Rectangle2D ; 68 import java.io.Serializable ; 69 70 import org.jfree.chart.axis.CategoryAxis; 71 import org.jfree.chart.axis.ValueAxis; 72 import org.jfree.chart.entity.CategoryItemEntity; 73 import org.jfree.chart.entity.EntityCollection; 74 import org.jfree.chart.labels.CategoryItemLabelGenerator; 75 import org.jfree.chart.labels.CategoryToolTipGenerator; 76 import org.jfree.chart.plot.CategoryPlot; 77 import org.jfree.chart.plot.PlotOrientation; 78 import org.jfree.data.category.CategoryDataset; 79 import org.jfree.data.category.IntervalCategoryDataset; 80 import org.jfree.ui.RectangleEdge; 81 import org.jfree.util.PublicCloneable; 82 83 91 public class IntervalBarRenderer extends BarRenderer 92 implements CategoryItemRenderer, 93 Cloneable , 94 PublicCloneable, 95 Serializable { 96 97 98 private static final long serialVersionUID = -5068857361615528725L; 99 100 103 public IntervalBarRenderer() { 104 super(); 105 } 106 107 121 public void drawItem(Graphics2D g2, 122 CategoryItemRendererState state, 123 Rectangle2D dataArea, 124 CategoryPlot plot, 125 CategoryAxis domainAxis, 126 ValueAxis rangeAxis, 127 CategoryDataset dataset, 128 int row, 129 int column, 130 int pass) { 131 132 if (dataset instanceof IntervalCategoryDataset) { 133 IntervalCategoryDataset d = (IntervalCategoryDataset) dataset; 134 drawInterval( 135 g2, state, dataArea, plot, domainAxis, rangeAxis, 136 d, row, column 137 ); 138 } 139 else { 140 super.drawItem( 141 g2, state, dataArea, plot, domainAxis, rangeAxis, 142 dataset, row, column, pass 143 ); 144 } 145 146 } 147 148 161 protected void drawInterval(Graphics2D g2, 162 CategoryItemRendererState state, 163 Rectangle2D dataArea, 164 CategoryPlot plot, 165 CategoryAxis domainAxis, 166 ValueAxis rangeAxis, 167 IntervalCategoryDataset dataset, 168 int row, 169 int column) { 170 171 int seriesCount = getRowCount(); 172 int categoryCount = getColumnCount(); 173 174 PlotOrientation orientation = plot.getOrientation(); 175 176 double rectX = 0.0; 177 double rectY = 0.0; 178 179 RectangleEdge domainAxisLocation = plot.getDomainAxisEdge(); 180 RectangleEdge rangeAxisLocation = plot.getRangeAxisEdge(); 181 182 Number value0 = dataset.getEndValue(row, column); 184 if (value0 == null) { 185 return; 186 } 187 double java2dValue0 = rangeAxis.valueToJava2D( 188 value0.doubleValue(), dataArea, rangeAxisLocation 189 ); 190 191 Number value1 = dataset.getStartValue(row, column); 193 if (value1 == null) { 194 return; 195 } 196 double java2dValue1 = rangeAxis.valueToJava2D( 197 value1.doubleValue(), dataArea, rangeAxisLocation 198 ); 199 200 if (java2dValue1 < java2dValue0) { 201 double temp = java2dValue1; 202 java2dValue1 = java2dValue0; 203 java2dValue0 = temp; 204 Number tempNum = value1; 205 value1 = value0; 206 value0 = tempNum; 207 } 208 209 double rectWidth = state.getBarWidth(); 211 212 double rectHeight = Math.abs(java2dValue1 - java2dValue0); 214 215 if (orientation == PlotOrientation.HORIZONTAL) { 216 rectY = domainAxis.getCategoryStart( 218 column, getColumnCount(), dataArea, domainAxisLocation 219 ); 220 if (seriesCount > 1) { 221 double seriesGap = dataArea.getHeight() * getItemMargin() 222 / (categoryCount * (seriesCount - 1)); 223 rectY = rectY + row * (state.getBarWidth() + seriesGap); 224 } 225 else { 226 rectY = rectY + row * state.getBarWidth(); 227 } 228 229 rectX = java2dValue0; 230 231 rectHeight = state.getBarWidth(); 232 rectWidth = Math.abs(java2dValue1 - java2dValue0); 233 234 } 235 else if (orientation == PlotOrientation.VERTICAL) { 236 rectX = domainAxis.getCategoryStart( 238 column, getColumnCount(), dataArea, domainAxisLocation 239 ); 240 241 if (seriesCount > 1) { 242 double seriesGap = dataArea.getWidth() * getItemMargin() 243 / (categoryCount * (seriesCount - 1)); 244 rectX = rectX + row * (state.getBarWidth() + seriesGap); 245 } 246 else { 247 rectX = rectX + row * state.getBarWidth(); 248 } 249 250 rectY = java2dValue0; 251 252 } 253 Rectangle2D bar = new Rectangle2D.Double ( 254 rectX, rectY, rectWidth, rectHeight 255 ); 256 Paint seriesPaint = getItemPaint(row, column); 257 g2.setPaint(seriesPaint); 258 g2.fill(bar); 259 260 if (state.getBarWidth() > BAR_OUTLINE_WIDTH_THRESHOLD) { 262 Stroke stroke = getItemOutlineStroke(row, column); 263 Paint paint = getItemOutlinePaint(row, column); 264 if (stroke != null && paint != null) { 265 g2.setStroke(stroke); 266 g2.setPaint(paint); 267 g2.draw(bar); 268 } 269 } 270 271 CategoryItemLabelGenerator generator 272 = getItemLabelGenerator(row, column); 273 if (generator != null && isItemLabelVisible(row, column)) { 274 drawItemLabel( 275 g2, dataset, row, column, plot, generator, bar, false 276 ); 277 } 278 279 if (state.getInfo() != null) { 281 EntityCollection entities = state.getEntityCollection(); 282 if (entities != null) { 283 String tip = null; 284 CategoryToolTipGenerator tipster 285 = getToolTipGenerator(row, column); 286 if (tipster != null) { 287 tip = tipster.generateToolTip(dataset, row, column); 288 } 289 String url = null; 290 if (getItemURLGenerator(row, column) != null) { 291 url = getItemURLGenerator(row, column).generateURL( 292 dataset, row, column 293 ); 294 } 295 CategoryItemEntity entity = new CategoryItemEntity( 296 bar, tip, url, dataset, row, 297 dataset.getColumnKey(column), column 298 ); 299 entities.add(entity); 300 } 301 } 302 303 } 304 305 } 306 | Popular Tags |