1 67 68 package org.jfree.chart.renderer.category; 69 70 import java.awt.Graphics2D ; 71 import java.awt.Paint ; 72 import java.awt.Shape ; 73 import java.awt.Stroke ; 74 import java.awt.geom.GeneralPath ; 75 import java.awt.geom.Rectangle2D ; 76 import java.io.Serializable ; 77 78 import org.jfree.chart.LegendItem; 79 import org.jfree.chart.axis.CategoryAxis; 80 import org.jfree.chart.axis.ValueAxis; 81 import org.jfree.chart.entity.EntityCollection; 82 import org.jfree.chart.event.RendererChangeEvent; 83 import org.jfree.chart.plot.CategoryPlot; 84 import org.jfree.chart.plot.PlotOrientation; 85 import org.jfree.chart.renderer.AreaRendererEndType; 86 import org.jfree.data.category.CategoryDataset; 87 import org.jfree.ui.RectangleEdge; 88 import org.jfree.util.PublicCloneable; 89 90 94 public class AreaRenderer extends AbstractCategoryItemRenderer 95 implements Cloneable , PublicCloneable, Serializable { 96 97 98 private static final long serialVersionUID = -4231878281385812757L; 99 100 101 private AreaRendererEndType endType; 102 103 106 public AreaRenderer() { 107 super(); 108 this.endType = AreaRendererEndType.TAPER; 109 } 110 111 119 public AreaRendererEndType getEndType() { 120 return this.endType; 121 } 122 123 131 public void setEndType(AreaRendererEndType type) { 132 if (type == null) { 133 throw new IllegalArgumentException ("Null 'type' argument."); 134 } 135 this.endType = type; 136 notifyListeners(new RendererChangeEvent(this)); 137 } 138 139 147 public LegendItem getLegendItem(int datasetIndex, int series) { 148 149 CategoryPlot cp = getPlot(); 150 if (cp == null) { 151 return null; 152 } 153 154 CategoryDataset dataset; 155 dataset = cp.getDataset(datasetIndex); 156 String label = getLegendItemLabelGenerator().generateLabel(dataset, 157 series); 158 String description = label; 159 String toolTipText = null; 160 if (getLegendItemToolTipGenerator() != null) { 161 toolTipText = getLegendItemToolTipGenerator().generateLabel( 162 dataset, series); 163 } 164 String urlText = null; 165 if (getLegendItemURLGenerator() != null) { 166 urlText = getLegendItemURLGenerator().generateLabel(dataset, 167 series); 168 } 169 Shape shape = new Rectangle2D.Double (-4.0, -4.0, 8.0, 8.0); 170 Paint paint = getSeriesPaint(series); 171 Paint outlinePaint = getSeriesOutlinePaint(series); 172 Stroke outlineStroke = getSeriesOutlineStroke(series); 173 174 return new LegendItem(label, description, toolTipText, urlText, 175 shape, paint, outlineStroke, outlinePaint); 176 177 } 178 179 193 public void drawItem(Graphics2D g2, 194 CategoryItemRendererState state, 195 Rectangle2D dataArea, 196 CategoryPlot plot, 197 CategoryAxis domainAxis, 198 ValueAxis rangeAxis, 199 CategoryDataset dataset, 200 int row, 201 int column, 202 int pass) { 203 204 Number value = dataset.getValue(row, column); 206 if (value != null) { 207 PlotOrientation orientation = plot.getOrientation(); 208 RectangleEdge axisEdge = plot.getDomainAxisEdge(); 209 int count = dataset.getColumnCount(); 210 float x0 = (float) domainAxis.getCategoryStart(column, count, 211 dataArea, axisEdge); 212 float x1 = (float) domainAxis.getCategoryMiddle(column, count, 213 dataArea, axisEdge); 214 float x2 = (float) domainAxis.getCategoryEnd(column, count, 215 dataArea, axisEdge); 216 217 x0 = Math.round(x0); 218 x1 = Math.round(x1); 219 x2 = Math.round(x2); 220 221 if (this.endType == AreaRendererEndType.TRUNCATE) { 222 if (column == 0) { 223 x0 = x1; 224 } 225 else if (column == getColumnCount() - 1) { 226 x2 = x1; 227 } 228 } 229 230 double yy1 = value.doubleValue(); 231 232 double yy0 = 0.0; 233 if (column > 0) { 234 Number n0 = dataset.getValue(row, column - 1); 235 if (n0 != null) { 236 yy0 = (n0.doubleValue() + yy1) / 2.0; 237 } 238 } 239 240 double yy2 = 0.0; 241 if (column < dataset.getColumnCount() - 1) { 242 Number n2 = dataset.getValue(row, column + 1); 243 if (n2 != null) { 244 yy2 = (n2.doubleValue() + yy1) / 2.0; 245 } 246 } 247 248 RectangleEdge edge = plot.getRangeAxisEdge(); 249 float y0 = (float) rangeAxis.valueToJava2D(yy0, dataArea, edge); 250 float y1 = (float) rangeAxis.valueToJava2D(yy1, dataArea, edge); 251 float y2 = (float) rangeAxis.valueToJava2D(yy2, dataArea, edge); 252 float yz = (float) rangeAxis.valueToJava2D(0.0, dataArea, edge); 253 254 g2.setPaint(getItemPaint(row, column)); 255 g2.setStroke(getItemStroke(row, column)); 256 257 GeneralPath area = new GeneralPath (); 258 259 if (orientation == PlotOrientation.VERTICAL) { 260 area.moveTo(x0, yz); 261 area.lineTo(x0, y0); 262 area.lineTo(x1, y1); 263 area.lineTo(x2, y2); 264 area.lineTo(x2, yz); 265 } 266 else if (orientation == PlotOrientation.HORIZONTAL) { 267 area.moveTo(yz, x0); 268 area.lineTo(y0, x0); 269 area.lineTo(y1, x1); 270 area.lineTo(y2, x2); 271 area.lineTo(yz, x2); 272 } 273 area.closePath(); 274 275 g2.setPaint(getItemPaint(row, column)); 276 g2.fill(area); 277 278 if (isItemLabelVisible(row, column)) { 280 drawItemLabel(g2, orientation, dataset, row, column, x1, y1, 281 (value.doubleValue() < 0.0)); 282 } 283 284 EntityCollection entities = state.getEntityCollection(); 286 if (entities != null) { 287 addItemEntity(entities, dataset, row, column, area); 288 } 289 } 290 291 } 292 293 300 public boolean equals(Object obj) { 301 if (obj == this) { 302 return true; 303 } 304 if (!(obj instanceof AreaRenderer)) { 305 return false; 306 } 307 AreaRenderer that = (AreaRenderer) obj; 308 if (!this.endType.equals(that.endType)) { 309 return false; 310 } 311 return super.equals(obj); 312 } 313 314 321 public Object clone() throws CloneNotSupportedException { 322 return super.clone(); 323 } 324 325 } 326 | Popular Tags |