1 52 53 package org.jfree.chart.renderer; 54 55 import java.awt.Graphics2D ; 56 import java.awt.Polygon ; 57 import java.awt.Shape ; 58 import java.awt.geom.Rectangle2D ; 59 import java.io.Serializable ; 60 61 import org.jfree.chart.axis.CategoryAxis; 62 import org.jfree.chart.axis.ValueAxis; 63 import org.jfree.chart.entity.CategoryItemEntity; 64 import org.jfree.chart.entity.EntityCollection; 65 import org.jfree.chart.labels.CategoryItemLabelGenerator; 66 import org.jfree.chart.plot.CategoryPlot; 67 import org.jfree.chart.plot.PlotOrientation; 68 import org.jfree.data.CategoryDataset; 69 import org.jfree.ui.RectangleEdge; 70 import org.jfree.util.PublicCloneable; 71 72 77 public class StackedAreaRenderer extends AreaRenderer 78 implements Cloneable , PublicCloneable, Serializable { 79 80 83 public StackedAreaRenderer() { 84 super(); 85 } 86 87 92 public RangeType getRangeType() { 93 return RangeType.STACKED; 94 } 95 96 109 public void drawItem(Graphics2D g2, 110 CategoryItemRendererState state, 111 Rectangle2D dataArea, 112 CategoryPlot plot, 113 CategoryAxis domainAxis, 114 ValueAxis rangeAxis, 115 CategoryDataset dataset, 116 int row, 117 int column) { 118 119 Number value = dataset.getValue(row, column); 121 if (value == null) { 122 return; 123 } 124 125 double x1 = domainAxis.getCategoryMiddle(column, getColumnCount(), dataArea, 128 plot.getDomainAxisEdge()); 129 double y1 = 0.0; double y1Untranslated = value.doubleValue(); 131 132 g2.setPaint(getItemPaint(row, column)); 133 g2.setStroke(getItemStroke(row, column)); 134 135 if (column != 0) { 136 137 Number previousValue = dataset.getValue(row, column - 1); 138 if (previousValue != null) { 139 140 double x0 = domainAxis.getCategoryMiddle(column - 1, 141 getColumnCount(), dataArea, 142 plot.getDomainAxisEdge()); 143 double y0Untranslated = previousValue.doubleValue(); 144 145 double previousHeightx0Untranslated = getPreviousHeight(dataset, row, column - 1); 148 double previousHeightx1Untranslated = getPreviousHeight(dataset, row, column); 149 150 y0Untranslated += previousHeightx0Untranslated; 152 y1Untranslated += previousHeightx1Untranslated; 153 154 RectangleEdge location = plot.getRangeAxisEdge(); 156 double previousHeightx0 157 = rangeAxis.translateValueToJava2D(previousHeightx0Untranslated, dataArea, 158 location); 159 double previousHeightx1 160 = rangeAxis.translateValueToJava2D(previousHeightx1Untranslated, dataArea, 161 location); 162 163 double y0 = rangeAxis.translateValueToJava2D(y0Untranslated, dataArea, location); 165 y1 = rangeAxis.translateValueToJava2D(y1Untranslated, dataArea, location); 166 167 Polygon p = null; 168 PlotOrientation orientation = plot.getOrientation(); 169 if (orientation == PlotOrientation.HORIZONTAL) { 170 p = new Polygon (); 171 p.addPoint((int) y0, (int) x0); 172 p.addPoint((int) y1, (int) x1); 173 p.addPoint((int) previousHeightx1, (int) x1); 174 p.addPoint((int) previousHeightx0, (int) x0); 175 } 176 else if (orientation == PlotOrientation.VERTICAL) { 177 p = new Polygon (); 178 p.addPoint((int) x0, (int) y0); 179 p.addPoint((int) x1, (int) y1); 180 p.addPoint((int) x1, (int) previousHeightx1); 181 p.addPoint((int) x0, (int) previousHeightx0); 182 } 183 g2.setPaint(getItemPaint(row, column)); 184 g2.setStroke(getItemStroke(row, column)); 185 g2.fill(p); 186 } 187 188 } 189 190 if (state.getInfo() != null) { 192 EntityCollection entities = state.getInfo().getOwner().getEntityCollection(); 193 Shape shape = new Rectangle2D.Double (x1 - 3.0, y1 - 3.0, 6.0, 6.0); 194 if (entities != null && shape != null) { 195 String tip = null; 196 CategoryItemLabelGenerator generator = getItemLabelGenerator(row, column); 197 if (generator != null) { 198 tip = generator.generateToolTip(dataset, row, column); 199 } 200 String url = null; 201 if (getItemURLGenerator(row, column) != null) { 202 url = getItemURLGenerator(row, column).generateURL(dataset, row, column); 203 } 204 CategoryItemEntity entity = new CategoryItemEntity( 205 shape, tip, url, dataset, row, dataset.getColumnKey(column), column 206 ); 207 entities.addEntity(entity); 208 } 209 } 210 211 } 212 213 225 protected double getPreviousHeight(CategoryDataset data, int series, int category) { 226 227 double result = 0.0; 228 229 Number tmp; 230 for (int i = 0; i < series; i++) { 231 tmp = data.getValue(i, category); 232 if (tmp != null) { 233 result += tmp.doubleValue(); 234 } 235 } 236 237 return result; 238 239 } 240 241 } 242 | Popular Tags |