1 61 62 package org.jfree.chart.renderer; 63 64 import java.awt.Color ; 65 import java.awt.Graphics2D ; 66 import java.awt.Paint ; 67 import java.awt.geom.GeneralPath ; 68 import java.awt.geom.Rectangle2D ; 69 import java.io.Serializable ; 70 71 import org.jfree.chart.axis.CategoryAxis; 72 import org.jfree.chart.axis.ValueAxis; 73 import org.jfree.chart.entity.CategoryItemEntity; 74 import org.jfree.chart.entity.EntityCollection; 75 import org.jfree.chart.labels.CategoryItemLabelGenerator; 76 import org.jfree.chart.plot.CategoryPlot; 77 import org.jfree.chart.plot.PlotOrientation; 78 import org.jfree.data.CategoryDataset; 79 import org.jfree.ui.RectangleEdge; 80 import org.jfree.util.PublicCloneable; 81 82 88 public class StackedBarRenderer3D extends BarRenderer3D 89 implements Cloneable , PublicCloneable, Serializable { 90 91 98 public StackedBarRenderer3D() { 99 super(); 100 } 101 102 108 public StackedBarRenderer3D(double xOffset, double yOffset) { 109 super(xOffset, yOffset); 110 } 111 112 117 public RangeType getRangeType() { 118 return RangeType.STACKED; 119 } 120 121 129 protected void calculateBarWidth(CategoryPlot plot, 130 Rectangle2D dataArea, 131 Integer rendererIndex, 132 CategoryItemRendererState state) { 133 134 CategoryAxis domainAxis = getDomainAxis(plot, rendererIndex); 136 CategoryDataset data = getDataset(plot, rendererIndex); 137 if (data != null) { 138 PlotOrientation orientation = plot.getOrientation(); 139 double space = 0.0; 140 if (orientation == PlotOrientation.HORIZONTAL) { 141 space = dataArea.getHeight(); 142 } 143 else if (orientation == PlotOrientation.VERTICAL) { 144 space = dataArea.getWidth(); 145 } 146 double maxWidth = space * getMaxBarWidth(); 147 int columns = data.getColumnCount(); 148 double categoryMargin = 0.0; 149 if (columns > 1) { 150 categoryMargin = domainAxis.getCategoryMargin(); 151 } 152 153 double used = space * (1 - domainAxis.getLowerMargin() - domainAxis.getUpperMargin() 154 - categoryMargin); 155 if (columns > 0) { 156 state.setBarWidth(Math.min(used / columns, maxWidth)); 157 } 158 else { 159 state.setBarWidth(Math.min(used, maxWidth)); 160 } 161 } 162 163 } 164 165 178 public void drawItem(Graphics2D g2, 179 CategoryItemRendererState state, 180 Rectangle2D dataArea, 181 CategoryPlot plot, 182 CategoryAxis domainAxis, 183 ValueAxis rangeAxis, 184 CategoryDataset dataset, 185 int row, 186 int column) { 187 188 Number dataValue = dataset.getValue(row, column); 190 if (dataValue == null) { 191 return; 192 } 193 194 double value = dataValue.doubleValue(); 195 196 Rectangle2D adjusted = new Rectangle2D.Double (dataArea.getX(), 197 dataArea.getY() + getYOffset(), 198 dataArea.getWidth() - getXOffset(), 199 dataArea.getHeight() - getYOffset()); 200 201 PlotOrientation orientation = plot.getOrientation(); 202 203 double barW0 = domainAxis.getCategoryMiddle(column, getColumnCount(), adjusted, 204 plot.getDomainAxisEdge()) 205 - state.getBarWidth() / 2.0; 206 207 double positiveBase = 0.0; 208 double negativeBase = 0.0; 209 for (int i = 0; i < row; i++) { 210 Number v = dataset.getValue(i, column); 211 if (v != null) { 212 double d = v.doubleValue(); 213 if (d > 0) { 214 positiveBase = positiveBase + d; 215 } 216 else { 217 negativeBase = negativeBase + d; 218 } 219 } 220 } 221 222 double translatedBase; 223 double translatedValue; 224 RectangleEdge location = plot.getRangeAxisEdge(); 225 if (value > 0.0) { 226 translatedBase = rangeAxis.translateValueToJava2D(positiveBase, adjusted, location); 227 translatedValue = rangeAxis.translateValueToJava2D(positiveBase + value, adjusted, 228 location); 229 } 230 else { 231 translatedBase = rangeAxis.translateValueToJava2D(negativeBase, adjusted, location); 232 translatedValue = rangeAxis.translateValueToJava2D(negativeBase + value, adjusted, 233 location); 234 } 235 double barL0 = Math.min(translatedBase, translatedValue); 236 double barLength = Math.max(Math.abs(translatedValue - translatedBase), 237 getMinimumBarLength()); 238 239 Rectangle2D bar = null; 240 if (orientation == PlotOrientation.HORIZONTAL) { 241 bar = new Rectangle2D.Double (barL0, barW0, barLength, state.getBarWidth()); 242 } 243 else { 244 bar = new Rectangle2D.Double (barW0, barL0, state.getBarWidth(), barLength); 245 } 246 Paint itemPaint = getItemPaint(row, column); 247 g2.setPaint(itemPaint); 248 g2.fill(bar); 249 250 double x0 = bar.getMinX(); 251 double x1 = x0 + getXOffset(); 252 double x2 = bar.getMaxX(); 253 double x3 = x2 + getXOffset(); 254 255 double y0 = bar.getMinY() - getYOffset(); 256 double y1 = bar.getMinY(); 257 double y2 = bar.getMaxY() - getYOffset(); 258 double y3 = bar.getMaxY(); 259 260 GeneralPath bar3dRight = null; 261 GeneralPath bar3dTop = null; 262 if (value > 0.0 || orientation == PlotOrientation.VERTICAL) { 263 bar3dRight = new GeneralPath (); 264 bar3dRight.moveTo((float) x2, (float) y3); 265 bar3dRight.lineTo((float) x2, (float) y1); 266 bar3dRight.lineTo((float) x3, (float) y0); 267 bar3dRight.lineTo((float) x3, (float) y2); 268 bar3dRight.closePath(); 269 270 if (itemPaint instanceof Color ) { 271 g2.setPaint(((Color ) itemPaint).darker()); 272 } 273 g2.fill(bar3dRight); 274 } 275 276 if (value > 0.0 || orientation == PlotOrientation.HORIZONTAL) { 277 bar3dTop = new GeneralPath (); 278 bar3dTop.moveTo((float) x0, (float) y1); 279 bar3dTop.lineTo((float) x1, (float) y0); 280 bar3dTop.lineTo((float) x3, (float) y0); 281 bar3dTop.lineTo((float) x2, (float) y1); 282 bar3dTop.closePath(); 283 g2.fill(bar3dTop); 284 } 285 286 if (state.getBarWidth() > 3) { 287 g2.setStroke(getItemOutlineStroke(row, column)); 288 g2.setPaint(getItemOutlinePaint(row, column)); 289 g2.draw(bar); 290 if (bar3dRight != null) { 291 g2.draw(bar3dRight); 292 } 293 if (bar3dTop != null) { 294 g2.draw(bar3dTop); 295 } 296 } 297 298 if (state.getInfo() != null) { 300 EntityCollection entities = state.getInfo().getOwner().getEntityCollection(); 301 if (entities != null) { 302 String tip = null; 303 CategoryItemLabelGenerator generator = getItemLabelGenerator(row, column); 304 if (generator != null) { 305 tip = generator.generateToolTip(dataset, row, column); 306 } 307 String url = null; 308 if (getItemURLGenerator(row, column) != null) { 309 url = getItemURLGenerator(row, column).generateURL(dataset, row, column); 310 } 311 CategoryItemEntity entity = new CategoryItemEntity( 312 bar, tip, url, dataset, row, dataset.getColumnKey(column), column 313 ); 314 entities.addEntity(entity); 315 } 316 } 317 318 } 319 320 } 321 | Popular Tags |