1 52 53 package org.jfree.chart.renderer; 54 55 import java.awt.Graphics2D ; 56 import java.awt.Paint ; 57 import java.awt.geom.Rectangle2D ; 58 import java.io.Serializable ; 59 60 import org.jfree.chart.CrosshairInfo; 61 import org.jfree.chart.axis.ValueAxis; 62 import org.jfree.chart.entity.EntityCollection; 63 import org.jfree.chart.entity.XYItemEntity; 64 import org.jfree.chart.labels.XYToolTipGenerator; 65 import org.jfree.chart.plot.PlotOrientation; 66 import org.jfree.chart.plot.PlotRenderingInfo; 67 import org.jfree.chart.plot.XYPlot; 68 import org.jfree.chart.urls.XYURLGenerator; 69 import org.jfree.data.IntervalXYDataset; 70 import org.jfree.data.XYDataset; 71 import org.jfree.ui.RectangleEdge; 72 import org.jfree.util.PublicCloneable; 73 74 79 public class XYBarRenderer extends AbstractXYItemRenderer implements XYItemRenderer, 80 Cloneable , 81 PublicCloneable, 82 Serializable { 83 84 85 private double margin; 86 87 88 private double translatedRangeZero; 89 90 93 public XYBarRenderer() { 94 super(); 95 this.margin = 0.0; 96 } 97 98 104 public XYBarRenderer(double margin) { 105 super(); 106 this.margin = margin; 107 } 108 109 118 public XYBarRenderer(double margin, 119 XYToolTipGenerator toolTipGenerator, 120 XYURLGenerator urlGenerator) { 121 122 super(toolTipGenerator, urlGenerator); 123 this.margin = margin; 124 125 } 126 127 134 public void setMargin(double margin) { 135 136 Double old = new Double (this.margin); 137 this.margin = margin; 138 firePropertyChanged("XYBarRenderer.margin", old, new Double (margin)); 139 140 } 141 142 154 public XYItemRendererState initialise(Graphics2D g2, 155 Rectangle2D dataArea, 156 XYPlot plot, 157 XYDataset data, 158 PlotRenderingInfo info) { 159 160 XYItemRendererState state = super.initialise(g2, dataArea, plot, data, info); 161 ValueAxis rangeAxis = plot.getRangeAxis(); 162 this.translatedRangeZero = rangeAxis.translateValueToJava2D(0.0, dataArea, 163 plot.getRangeAxisEdge()); 164 return state; 165 166 } 167 168 184 public void drawItem(Graphics2D g2, 185 XYItemRendererState state, 186 Rectangle2D dataArea, 187 PlotRenderingInfo info, 188 XYPlot plot, 189 ValueAxis domainAxis, 190 ValueAxis rangeAxis, 191 XYDataset dataset, 192 int series, 193 int item, 194 CrosshairInfo crosshairInfo, 195 int pass) { 196 197 IntervalXYDataset intervalData = (IntervalXYDataset) dataset; 198 199 Paint seriesPaint = getItemPaint(series, item); 200 Paint seriesOutlinePaint = getSeriesOutlinePaint(series); 201 202 Number valueNumber = intervalData.getYValue(series, item); 203 if (valueNumber == null) { 204 return; 205 } 206 207 double translatedValue = rangeAxis.translateValueToJava2D(valueNumber.doubleValue(), 208 dataArea, plot.getRangeAxisEdge()); 209 210 RectangleEdge location = plot.getDomainAxisEdge(); 211 Number startXNumber = intervalData.getStartXValue(series, item); 212 if (startXNumber == null) { 213 return; 214 } 215 double translatedStartX = domainAxis.translateValueToJava2D(startXNumber.doubleValue(), 216 dataArea, location); 217 218 Number endXNumber = intervalData.getEndXValue(series, item); 219 if (endXNumber == null) { 220 return; 221 } 222 double translatedEndX = domainAxis.translateValueToJava2D(endXNumber.doubleValue(), 223 dataArea, location); 224 225 double translatedWidth = Math.max(1, Math.abs(translatedEndX - translatedStartX)); 226 double translatedHeight = Math.abs(translatedValue - translatedRangeZero); 227 228 if (margin > 0.0) { 229 double cut = translatedWidth * margin; 230 translatedWidth = translatedWidth - cut; 231 translatedStartX = translatedStartX + cut / 2; 232 } 233 234 Rectangle2D bar = null; 235 PlotOrientation orientation = plot.getOrientation(); 236 if (orientation == PlotOrientation.HORIZONTAL) { 237 238 bar = new Rectangle2D.Double (Math.min(this.translatedRangeZero, translatedValue), 239 translatedEndX, 240 translatedHeight, translatedWidth); 241 } 242 else if (orientation == PlotOrientation.VERTICAL) { 243 244 bar = new Rectangle2D.Double (translatedStartX, 245 Math.min(this.translatedRangeZero, translatedValue), 246 translatedWidth, translatedHeight); 247 248 } 249 250 g2.setPaint(seriesPaint); 251 g2.fill(bar); 252 if (Math.abs(translatedEndX - translatedStartX) > 3) { 253 g2.setStroke(getItemStroke(series, item)); 254 g2.setPaint(seriesOutlinePaint); 255 g2.draw(bar); 256 } 257 258 if (info != null) { 260 EntityCollection entities = info.getOwner().getEntityCollection(); 261 if (entities != null) { 262 String tip = null; 263 if (getToolTipGenerator() != null) { 264 tip = getToolTipGenerator().generateToolTip(dataset, series, item); 265 } 266 String url = null; 267 if (getURLGenerator() != null) { 268 url = getURLGenerator().generateURL(dataset, series, item); 269 } 270 XYItemEntity entity = new XYItemEntity(bar, dataset, series, item, tip, url); 271 entities.addEntity(entity); 272 } 273 } 274 275 } 276 277 284 public Object clone() throws CloneNotSupportedException { 285 return super.clone(); 286 } 287 288 } 289 | Popular Tags |