1 45 46 package org.jfree.chart.renderer; 47 48 import java.awt.Graphics2D ; 49 import java.awt.Paint ; 50 import java.awt.geom.Line2D ; 51 import java.awt.geom.Rectangle2D ; 52 import java.io.Serializable ; 53 54 import org.jfree.chart.axis.CategoryAxis; 55 import org.jfree.chart.axis.ValueAxis; 56 import org.jfree.chart.plot.CategoryPlot; 57 import org.jfree.chart.plot.PlotOrientation; 58 import org.jfree.data.CategoryDataset; 59 import org.jfree.data.statistics.StatisticalCategoryDataset; 60 import org.jfree.ui.RectangleEdge; 61 import org.jfree.util.PublicCloneable; 62 63 69 public class StatisticalBarRenderer extends BarRenderer 70 implements CategoryItemRenderer, 71 Cloneable , PublicCloneable, Serializable { 72 73 76 public StatisticalBarRenderer() { 77 super(); 78 } 79 80 94 public void drawItem(Graphics2D g2, 95 CategoryItemRendererState state, 96 Rectangle2D dataArea, 97 CategoryPlot plot, 98 CategoryAxis domainAxis, 99 ValueAxis rangeAxis, 100 CategoryDataset data, 101 int row, 102 int column) { 103 104 105 if (!(data instanceof StatisticalCategoryDataset)) { 107 throw new IllegalArgumentException ("StatisticalBarRenderer.drawCategoryItem()" 108 + " : the data should be of type StatisticalCategoryDataset only."); 109 } 110 StatisticalCategoryDataset statData = (StatisticalCategoryDataset) data; 111 112 PlotOrientation orientation = plot.getOrientation(); 113 if (orientation == PlotOrientation.HORIZONTAL) { 114 drawHorizontalItem(g2, state, dataArea, plot, domainAxis, rangeAxis, statData, 115 row, column); 116 } 117 else if (orientation == PlotOrientation.VERTICAL) { 118 drawVerticalItem(g2, state, dataArea, plot, domainAxis, rangeAxis, statData, 119 row, column); 120 } 121 } 122 123 136 protected void drawHorizontalItem(Graphics2D g2, 137 CategoryItemRendererState state, 138 Rectangle2D dataArea, 139 CategoryPlot plot, 140 CategoryAxis domainAxis, 141 ValueAxis rangeAxis, 142 StatisticalCategoryDataset dataset, 143 int row, 144 int column) { 145 146 RectangleEdge xAxisLocation = plot.getDomainAxisEdge(); 147 148 double rectY = domainAxis.getCategoryStart(column, getColumnCount(), dataArea, 150 xAxisLocation); 151 152 int seriesCount = getRowCount(); 153 int categoryCount = getColumnCount(); 154 if (seriesCount > 1) { 155 double seriesGap = dataArea.getHeight() * getItemMargin() 156 / (categoryCount * (seriesCount - 1)); 157 rectY = rectY + row * (state.getBarWidth() + seriesGap); 158 } 159 else { 160 rectY = rectY + row * state.getBarWidth(); 161 } 162 163 Number meanValue = dataset.getMeanValue(row, column); 165 166 double value = meanValue.doubleValue(); 167 double base = 0.0; 168 double lclip = getLowerClip(); 169 double uclip = getUpperClip(); 170 171 if (uclip <= 0.0) { if (value >= uclip) { 173 return; } 175 base = uclip; 176 if (value <= lclip) { 177 value = lclip; 178 } 179 } 180 else if (lclip <= 0.0) { if (value >= uclip) { 182 value = uclip; 183 } 184 else { 185 if (value <= lclip) { 186 value = lclip; 187 } 188 } 189 } 190 else { if (value <= lclip) { 192 return; } 194 base = getLowerClip(); 195 if (value >= uclip) { 196 value = uclip; 197 } 198 } 199 200 RectangleEdge yAxisLocation = plot.getRangeAxisEdge(); 201 double transY1 = rangeAxis.translateValueToJava2D(base, dataArea, yAxisLocation); 202 double transY2 = rangeAxis.translateValueToJava2D(value, dataArea, yAxisLocation); 203 double rectX = Math.min(transY2, transY1); 204 205 double rectHeight = state.getBarWidth(); 206 double rectWidth = Math.abs(transY2 - transY1); 207 208 Rectangle2D bar = new Rectangle2D.Double (rectX, rectY, rectWidth, rectHeight); 209 Paint seriesPaint = getItemPaint(row, column); 210 g2.setPaint(seriesPaint); 211 g2.fill(bar); 212 if (state.getBarWidth() > 3) { 213 g2.setStroke(getItemStroke(row, column)); 214 g2.setPaint(getItemOutlinePaint(row, column)); 215 g2.draw(bar); 216 } 217 218 double valueDelta = dataset.getStdDevValue(row, column).doubleValue(); 220 double highVal = rangeAxis.translateValueToJava2D(meanValue.doubleValue() + valueDelta, 221 dataArea, yAxisLocation); 222 double lowVal = rangeAxis.translateValueToJava2D(meanValue.doubleValue() - valueDelta, 223 dataArea, yAxisLocation); 224 225 Line2D line = null; 226 line = new Line2D.Double (lowVal, rectY + rectHeight / 2.0d, 227 highVal, rectY + rectHeight / 2.0d); 228 g2.draw(line); 229 line = new Line2D.Double (highVal, rectY + rectHeight * 0.25, 230 highVal, rectY + rectHeight * 0.75); 231 g2.draw(line); 232 line = new Line2D.Double (lowVal, rectY + rectHeight * 0.25, 233 lowVal, rectY + rectHeight * 0.75); 234 g2.draw(line); 235 } 236 237 250 protected void drawVerticalItem(Graphics2D g2, 251 CategoryItemRendererState state, 252 Rectangle2D dataArea, 253 CategoryPlot plot, 254 CategoryAxis domainAxis, 255 ValueAxis rangeAxis, 256 StatisticalCategoryDataset dataset, 257 int row, 258 int column) { 259 260 RectangleEdge xAxisLocation = plot.getDomainAxisEdge(); 261 262 double rectX = domainAxis.getCategoryStart(column, getColumnCount(), dataArea, 264 xAxisLocation); 265 266 int seriesCount = getRowCount(); 267 int categoryCount = getColumnCount(); 268 if (seriesCount > 1) { 269 double seriesGap = dataArea.getWidth() * getItemMargin() 270 / (categoryCount * (seriesCount - 1)); 271 rectX = rectX + row * (state.getBarWidth() + seriesGap); 272 } 273 else { 274 rectX = rectX + row * state.getBarWidth(); 275 } 276 277 Number meanValue = dataset.getMeanValue(row, column); 279 280 double value = meanValue.doubleValue(); 281 double base = 0.0; 282 double lclip = getLowerClip(); 283 double uclip = getUpperClip(); 284 285 if (uclip <= 0.0) { if (value >= uclip) { 287 return; } 289 base = uclip; 290 if (value <= lclip) { 291 value = lclip; 292 } 293 } 294 else if (lclip <= 0.0) { if (value >= uclip) { 296 value = uclip; 297 } 298 else { 299 if (value <= lclip) { 300 value = lclip; 301 } 302 } 303 } 304 else { if (value <= lclip) { 306 return; } 308 base = getLowerClip(); 309 if (value >= uclip) { 310 value = uclip; 311 } 312 } 313 314 RectangleEdge yAxisLocation = plot.getRangeAxisEdge(); 315 double transY1 = rangeAxis.translateValueToJava2D(base, dataArea, yAxisLocation); 316 double transY2 = rangeAxis.translateValueToJava2D(value, dataArea, yAxisLocation); 317 double rectY = Math.min(transY2, transY1); 318 319 double rectWidth = state.getBarWidth(); 320 double rectHeight = Math.abs(transY2 - transY1); 321 322 Rectangle2D bar = new Rectangle2D.Double (rectX, rectY, rectWidth, rectHeight); 323 Paint seriesPaint = getItemPaint(row, column); 324 g2.setPaint(seriesPaint); 325 g2.fill(bar); 326 if (state.getBarWidth() > 3) { 327 g2.setStroke(getItemStroke(row, column)); 328 g2.setPaint(getItemOutlinePaint(row, column)); 329 g2.draw(bar); 330 } 331 332 double valueDelta = dataset.getStdDevValue(row, column).doubleValue(); 334 double highVal = rangeAxis.translateValueToJava2D(meanValue.doubleValue() + valueDelta, 335 dataArea, yAxisLocation); 336 double lowVal = rangeAxis.translateValueToJava2D(meanValue.doubleValue() - valueDelta, 337 dataArea, yAxisLocation); 338 339 Line2D line = null; 340 line = new Line2D.Double (rectX + rectWidth / 2.0d, lowVal, 341 rectX + rectWidth / 2.0d, highVal); 342 g2.draw(line); 343 line = new Line2D.Double (rectX + rectWidth / 2.0d - 5.0d, highVal, 344 rectX + rectWidth / 2.0d + 5.0d, highVal); 345 g2.draw(line); 346 line = new Line2D.Double (rectX + rectWidth / 2.0d - 5.0d, lowVal, 347 rectX + rectWidth / 2.0d + 5.0d, lowVal); 348 g2.draw(line); 349 } 350 351 } 352 | Popular Tags |