1 74 75 package org.jfree.chart.renderer.xy; 76 77 import java.awt.Graphics2D ; 78 import java.awt.Paint ; 79 import java.awt.Polygon ; 80 import java.awt.Shape ; 81 import java.awt.Stroke ; 82 import java.awt.geom.GeneralPath ; 83 import java.awt.geom.Rectangle2D ; 84 import java.io.IOException ; 85 import java.io.ObjectInputStream ; 86 import java.io.ObjectOutputStream ; 87 import java.io.Serializable ; 88 89 import org.jfree.chart.LegendItem; 90 import org.jfree.chart.axis.ValueAxis; 91 import org.jfree.chart.entity.EntityCollection; 92 import org.jfree.chart.entity.XYItemEntity; 93 import org.jfree.chart.event.RendererChangeEvent; 94 import org.jfree.chart.labels.XYSeriesLabelGenerator; 95 import org.jfree.chart.labels.XYToolTipGenerator; 96 import org.jfree.chart.plot.CrosshairState; 97 import org.jfree.chart.plot.PlotOrientation; 98 import org.jfree.chart.plot.PlotRenderingInfo; 99 import org.jfree.chart.plot.XYPlot; 100 import org.jfree.chart.urls.XYURLGenerator; 101 import org.jfree.data.xy.XYDataset; 102 import org.jfree.io.SerialUtilities; 103 import org.jfree.util.PublicCloneable; 104 105 108 public class XYAreaRenderer2 extends AbstractXYItemRenderer 109 implements XYItemRenderer, 110 Cloneable , 111 PublicCloneable, 112 Serializable { 113 114 115 private static final long serialVersionUID = -7378069681579984133L; 116 117 118 private boolean plotLines; 119 120 121 private boolean showOutline; 122 123 127 private transient Shape legendArea; 128 129 132 public XYAreaRenderer2() { 133 this(null, null); 134 } 135 136 146 public XYAreaRenderer2(XYToolTipGenerator labelGenerator, 147 XYURLGenerator urlGenerator) { 148 super(); 149 this.plotLines = false; 150 this.showOutline = false; 151 setBaseToolTipGenerator(labelGenerator); 152 setURLGenerator(urlGenerator); 153 GeneralPath area = new GeneralPath (); 154 area.moveTo(0.0f, -4.0f); 155 area.lineTo(3.0f, -2.0f); 156 area.lineTo(4.0f, 4.0f); 157 area.lineTo(-4.0f, 4.0f); 158 area.lineTo(-3.0f, -2.0f); 159 area.closePath(); 160 this.legendArea = area; 161 } 162 163 169 public boolean isOutline() { 170 return this.showOutline; 171 } 172 173 178 public void setOutline(boolean show) { 179 this.showOutline = show; 180 } 181 182 187 public boolean getPlotLines() { 188 return this.plotLines; 189 } 190 191 196 public Shape getLegendArea() { 197 return this.legendArea; 198 } 199 200 206 public void setLegendArea(Shape area) { 207 if (area == null) { 208 throw new IllegalArgumentException ("Null 'area' argument."); 209 } 210 this.legendArea = area; 211 notifyListeners(new RendererChangeEvent(this)); 212 } 213 214 223 public LegendItem getLegendItem(int datasetIndex, int series) { 224 LegendItem result = null; 225 XYPlot xyplot = getPlot(); 226 if (xyplot != null) { 227 XYDataset dataset = xyplot.getDataset(datasetIndex); 228 if (dataset != null) { 229 XYSeriesLabelGenerator lg = getLegendItemLabelGenerator(); 230 String label = lg.generateLabel(dataset, series); 231 String description = label; 232 String toolTipText = null; 233 if (getLegendItemToolTipGenerator() != null) { 234 toolTipText = getLegendItemToolTipGenerator().generateLabel( 235 dataset, series); 236 } 237 String urlText = null; 238 if (getLegendItemURLGenerator() != null) { 239 urlText = getLegendItemURLGenerator().generateLabel( 240 dataset, series); 241 } 242 Paint paint = getSeriesPaint(series); 243 result = new LegendItem(label, description, toolTipText, 244 urlText, this.legendArea, paint); 245 } 246 } 247 return result; 248 } 249 250 268 public void drawItem(Graphics2D g2, 269 XYItemRendererState state, 270 Rectangle2D dataArea, 271 PlotRenderingInfo info, 272 XYPlot plot, 273 ValueAxis domainAxis, 274 ValueAxis rangeAxis, 275 XYDataset dataset, 276 int series, 277 int item, 278 CrosshairState crosshairState, 279 int pass) { 280 281 if (!getItemVisible(series, item)) { 282 return; 283 } 284 double x1 = dataset.getXValue(series, item); 286 double y1 = dataset.getYValue(series, item); 287 if (Double.isNaN(y1)) { 288 y1 = 0.0; 289 } 290 291 double transX1 = domainAxis.valueToJava2D(x1, dataArea, 292 plot.getDomainAxisEdge()); 293 double transY1 = rangeAxis.valueToJava2D(y1, dataArea, 294 plot.getRangeAxisEdge()); 295 296 double x0 = dataset.getXValue(series, Math.max(item - 1, 0)); 299 double y0 = dataset.getYValue(series, Math.max(item - 1, 0)); 300 if (Double.isNaN(y0)) { 301 y0 = 0.0; 302 } 303 double transX0 = domainAxis.valueToJava2D(x0, dataArea, 304 plot.getDomainAxisEdge()); 305 double transY0 = rangeAxis.valueToJava2D(y0, dataArea, 306 plot.getRangeAxisEdge()); 307 308 int itemCount = dataset.getItemCount(series); 309 double x2 = dataset.getXValue(series, Math.min(item + 1, 310 itemCount - 1)); 311 double y2 = dataset.getYValue(series, Math.min(item + 1, 312 itemCount - 1)); 313 if (Double.isNaN(y2)) { 314 y2 = 0.0; 315 } 316 double transX2 = domainAxis.valueToJava2D(x2, dataArea, 317 plot.getDomainAxisEdge()); 318 double transY2 = rangeAxis.valueToJava2D(y2, dataArea, 319 plot.getRangeAxisEdge()); 320 321 double transZero = rangeAxis.valueToJava2D(0.0, dataArea, 322 plot.getRangeAxisEdge()); 323 Polygon hotspot = null; 324 if (plot.getOrientation() == PlotOrientation.HORIZONTAL) { 325 hotspot = new Polygon (); 326 hotspot.addPoint((int) transZero, 327 (int) ((transX0 + transX1) / 2.0)); 328 hotspot.addPoint((int) ((transY0 + transY1) / 2.0), 329 (int) ((transX0 + transX1) / 2.0)); 330 hotspot.addPoint((int) transY1, (int) transX1); 331 hotspot.addPoint((int) ((transY1 + transY2) / 2.0), 332 (int) ((transX1 + transX2) / 2.0)); 333 hotspot.addPoint((int) transZero, 334 (int) ((transX1 + transX2) / 2.0)); 335 } 336 else { hotspot = new Polygon (); 338 hotspot.addPoint((int) ((transX0 + transX1) / 2.0), 339 (int) transZero); 340 hotspot.addPoint((int) ((transX0 + transX1) / 2.0), 341 (int) ((transY0 + transY1) / 2.0)); 342 hotspot.addPoint((int) transX1, (int) transY1); 343 hotspot.addPoint((int) ((transX1 + transX2) / 2.0), 344 (int) ((transY1 + transY2) / 2.0)); 345 hotspot.addPoint((int) ((transX1 + transX2) / 2.0), 346 (int) transZero); 347 } 348 349 PlotOrientation orientation = plot.getOrientation(); 350 Paint paint = getItemPaint(series, item); 351 Stroke stroke = getItemStroke(series, item); 352 g2.setPaint(paint); 353 g2.setStroke(stroke); 354 355 if (getPlotLines()) { 356 if (item > 0) { 357 if (plot.getOrientation() == PlotOrientation.VERTICAL) { 358 state.workingLine.setLine(transX0, transY0, transX1, 359 transY1); 360 } 361 else if (plot.getOrientation() == PlotOrientation.HORIZONTAL) { 362 state.workingLine.setLine(transY0, transX0, transY1, 363 transX1); 364 } 365 g2.draw(state.workingLine); 366 } 367 } 368 369 g2.fill(hotspot); 372 373 if (isOutline()) { 375 g2.setStroke(getSeriesOutlineStroke(series)); 376 g2.setPaint(getSeriesOutlinePaint(series)); 377 g2.draw(hotspot); 378 } 379 updateCrosshairValues( 380 crosshairState, x1, y1, transX1, transY1, orientation 381 ); 382 383 if (state.getInfo() != null) { 385 EntityCollection entities = state.getEntityCollection(); 386 if (entities != null && hotspot != null) { 387 String tip = null; 388 XYToolTipGenerator generator = getToolTipGenerator( 389 series, item 390 ); 391 if (generator != null) { 392 tip = generator.generateToolTip(dataset, series, item); 393 } 394 String url = null; 395 if (getURLGenerator() != null) { 396 url = getURLGenerator().generateURL(dataset, series, item); 397 } 398 XYItemEntity entity = new XYItemEntity(hotspot, dataset, 399 series, item, tip, url); 400 entities.add(entity); 401 } 402 } 403 404 } 405 406 413 public Object clone() throws CloneNotSupportedException { 414 return super.clone(); 415 } 416 417 425 private void readObject(ObjectInputStream stream) 426 throws IOException , ClassNotFoundException { 427 stream.defaultReadObject(); 428 this.legendArea = SerialUtilities.readShape(stream); 429 } 430 431 438 private void writeObject(ObjectOutputStream stream) throws IOException { 439 stream.defaultWriteObject(); 440 SerialUtilities.writeShape(this.legendArea, stream); 441 } 442 443 } 444 445 | Popular Tags |