1 45 package org.jfree.chart.renderer; 46 47 import java.awt.Graphics2D ; 48 import java.awt.Paint ; 49 import java.awt.Shape ; 50 import java.awt.Stroke ; 51 import java.awt.geom.Line2D ; 52 import java.awt.geom.Rectangle2D ; 53 import java.io.Serializable ; 54 55 import org.jfree.chart.CrosshairInfo; 56 import org.jfree.chart.axis.ValueAxis; 57 import org.jfree.chart.entity.EntityCollection; 58 import org.jfree.chart.entity.XYItemEntity; 59 import org.jfree.chart.labels.XYToolTipGenerator; 60 import org.jfree.chart.plot.PlotOrientation; 61 import org.jfree.chart.plot.PlotRenderingInfo; 62 import org.jfree.chart.plot.XYPlot; 63 import org.jfree.chart.urls.XYURLGenerator; 64 import org.jfree.data.XYDataset; 65 import org.jfree.ui.RectangleEdge; 66 import org.jfree.util.PublicCloneable; 67 68 74 public class XYStepRenderer extends AbstractXYItemRenderer implements XYItemRenderer, 75 Cloneable , 76 PublicCloneable, 77 Serializable { 78 79 80 private transient Line2D line; 81 82 85 public XYStepRenderer() { 86 super(); 87 this.line = new Line2D.Double (0.0, 0.0, 0.0, 0.0); 88 } 89 90 96 public XYStepRenderer(XYToolTipGenerator toolTipGenerator, 97 XYURLGenerator urlGenerator) { 98 99 100 super(); 101 setToolTipGenerator(toolTipGenerator); 102 setURLGenerator(urlGenerator); 103 this.line = new Line2D.Double (0.0, 0.0, 0.0, 0.0); 104 105 } 106 107 123 public void drawItem(Graphics2D g2, 124 XYItemRendererState state, 125 Rectangle2D dataArea, 126 PlotRenderingInfo info, 127 XYPlot plot, 128 ValueAxis horizontalAxis, 129 ValueAxis verticalAxis, 130 XYDataset dataset, 131 int series, 132 int item, 133 CrosshairInfo crosshairInfo, 134 int pass) { 135 136 Paint seriesPaint = getItemPaint(series, item); 137 Stroke seriesStroke = getItemStroke(series, item); 138 g2.setPaint(seriesPaint); 139 g2.setStroke(seriesStroke); 140 141 Number x1 = dataset.getXValue(series, item); 143 Number y1 = dataset.getYValue(series, item); 144 if (y1 == null) { 145 return; 146 } 147 148 RectangleEdge xAxisLocation = plot.getDomainAxisEdge(); 149 RectangleEdge yAxisLocation = plot.getRangeAxisEdge(); 150 double transX1 = horizontalAxis.translateValueToJava2D(x1.doubleValue(), dataArea, 151 xAxisLocation); 152 double transY1 = verticalAxis.translateValueToJava2D(y1.doubleValue(), dataArea, 153 yAxisLocation); 154 155 if (item > 0) { 156 Number x0 = dataset.getXValue(series, item - 1); 158 Number y0 = dataset.getYValue(series, item - 1); 159 if (y0 != null) { 160 double transX0 = horizontalAxis.translateValueToJava2D(x0.doubleValue(), dataArea, 161 xAxisLocation); 162 double transY0 = verticalAxis.translateValueToJava2D(y0.doubleValue(), dataArea, 163 yAxisLocation); 164 165 PlotOrientation orientation = plot.getOrientation(); 166 if (orientation == PlotOrientation.HORIZONTAL) { 167 if (transY0 == transY1) { line.setLine(transY0, transX0, transY1, transX1); 170 g2.draw(line); 171 } 172 else { line.setLine(transY0, transX0, transY1, transX0); 174 g2.draw(line); 175 line.setLine(transY1, transX0, transY1, transX1); 176 g2.draw(line); 177 } 178 } 179 else if (orientation == PlotOrientation.VERTICAL) { 180 if (transY0 == transY1) { line.setLine(transX0, transY0, transX1, transY1); 183 g2.draw(line); 184 } 185 else { line.setLine(transX0, transY0, transX1, transY0); 187 g2.draw(line); 188 line.setLine(transX1, transY0, transX1, transY1); 189 g2.draw(line); 190 } 191 } 192 193 } 194 } 195 196 if (plot.isDomainCrosshairLockedOnData()) { 198 if (plot.isRangeCrosshairLockedOnData()) { 199 crosshairInfo.updateCrosshairPoint(x1.doubleValue(), y1.doubleValue(), 201 transX1, transY1); 202 } 203 else { 204 crosshairInfo.updateCrosshairX(x1.doubleValue()); 206 207 } 208 } 209 else { 210 if (plot.isRangeCrosshairLockedOnData()) { 211 crosshairInfo.updateCrosshairY(y1.doubleValue()); 213 } 214 } 215 217 if (state.getInfo() != null) { 218 EntityCollection entities = state.getInfo().getOwner().getEntityCollection(); 219 if (entities != null) { 220 Shape shape = plot.getOrientation() == PlotOrientation.VERTICAL 221 ? new Rectangle2D.Double (transX1 - 2, transY1 - 2, 4.0, 4.0) 222 : new Rectangle2D.Double (transY1 - 2, transX1 - 2, 4.0, 4.0); 223 if (shape != null) { 224 String tip = null; 225 if (getToolTipGenerator() != null) { 226 tip = getToolTipGenerator().generateToolTip(dataset, series, item); 227 } 228 String url = null; 229 if (getURLGenerator() != null) { 230 url = getURLGenerator().generateURL(dataset, series, item); 231 } 232 XYItemEntity entity = new XYItemEntity(shape, dataset, series, item, tip, url); 233 entities.addEntity(entity); 234 } 235 } 236 } 237 } 238 239 246 public Object clone() throws CloneNotSupportedException { 247 return super.clone(); 248 } 249 250 } 251 | Popular Tags |