1 46 47 package org.jfree.chart.renderer; 48 49 import java.awt.Graphics2D ; 50 import java.awt.Paint ; 51 import java.awt.geom.Rectangle2D ; 52 import java.io.Serializable ; 53 54 import org.jfree.chart.CrosshairInfo; 55 import org.jfree.chart.axis.ValueAxis; 56 import org.jfree.chart.entity.EntityCollection; 57 import org.jfree.chart.entity.XYItemEntity; 58 import org.jfree.chart.plot.PlotOrientation; 59 import org.jfree.chart.plot.PlotRenderingInfo; 60 import org.jfree.chart.plot.XYPlot; 61 import org.jfree.data.IntervalXYDataset; 62 import org.jfree.data.XYDataset; 63 import org.jfree.ui.RectangleEdge; 64 import org.jfree.util.PublicCloneable; 65 66 74 public class ClusteredXYBarRenderer extends XYBarRenderer implements Cloneable , 75 PublicCloneable, 76 Serializable { 77 78 79 private double margin; 80 81 82 private double translatedRangeZero; 83 84 85 private boolean centerBarAtStartValue; 86 87 90 public ClusteredXYBarRenderer() { 91 this(0.0, false); 92 } 93 94 100 public ClusteredXYBarRenderer(double margin, boolean centerBarAtStartValue) { 101 super(margin); 102 this.margin = margin; 103 this.centerBarAtStartValue = centerBarAtStartValue; 104 } 105 106 119 public XYItemRendererState initialise(Graphics2D g2, 120 Rectangle2D dataArea, 121 XYPlot plot, 122 XYDataset data, 123 PlotRenderingInfo info) { 124 125 XYItemRendererState state = super.initialise(g2, dataArea, plot, data, info); 126 ValueAxis rangeAxis = plot.getRangeAxis(); 127 this.translatedRangeZero = rangeAxis.translateValueToJava2D(0.0, dataArea, 128 plot.getRangeAxisEdge()); 129 return state; 130 131 } 132 133 138 public void setMargin(double margin) { 139 this.margin = margin; 140 super.setMargin(margin); 141 } 142 143 164 public void drawItem(Graphics2D g2, 165 XYItemRendererState state, 166 Rectangle2D dataArea, 167 PlotRenderingInfo info, 168 XYPlot plot, ValueAxis domainAxis, ValueAxis rangeAxis, 169 XYDataset dataset, int series, int item, 170 CrosshairInfo crosshairInfo, 171 int pass) { 172 173 IntervalXYDataset intervalData = (IntervalXYDataset) dataset; 174 175 Paint seriesPaint = getItemPaint(series, item); 176 Paint seriesOutlinePaint = getItemOutlinePaint(series, item); 177 178 Number y = intervalData.getYValue(series, item); 179 if(y == null) { 180 return; 181 } 182 RectangleEdge yAxisLocation = plot.getRangeAxisEdge(); 183 double translatedY = rangeAxis.translateValueToJava2D(y.doubleValue(), dataArea, yAxisLocation); 184 185 RectangleEdge xAxisLocation = plot.getDomainAxisEdge(); 186 double x1 = intervalData.getStartXValue(series, item).doubleValue(); 187 double translatedX1 = domainAxis.translateValueToJava2D(x1, dataArea, xAxisLocation); 188 189 double x2 = intervalData.getEndXValue(series, item).doubleValue(); 190 double translatedX2 = domainAxis.translateValueToJava2D(x2, dataArea, xAxisLocation); 191 192 double translatedWidth = Math.max(1, Math.abs(translatedX2 - translatedX1)); 193 double translatedHeight = Math.abs(translatedY - translatedRangeZero); 194 195 if (centerBarAtStartValue) { 196 translatedX1 -= translatedWidth / 2; 197 } 198 199 if (margin > 0.0) { 200 double cut = translatedWidth * margin; 201 translatedWidth = translatedWidth - cut; 202 translatedX1 = translatedX1 + cut / 2; 203 } 204 205 int numSeries = dataset.getSeriesCount(); 206 double seriesBarWidth = translatedWidth / numSeries; 207 208 Rectangle2D bar = null; 209 PlotOrientation orientation = plot.getOrientation(); 210 if (orientation == PlotOrientation.HORIZONTAL) { 211 bar = new Rectangle2D.Double (Math.min(this.translatedRangeZero, translatedY), 212 translatedX1 - seriesBarWidth * (numSeries - series), 213 translatedHeight, seriesBarWidth); 214 } 215 else if (orientation == PlotOrientation.VERTICAL) { 216 217 bar = new Rectangle2D.Double (translatedX1 + seriesBarWidth * series, 218 Math.min(this.translatedRangeZero, translatedY), 219 seriesBarWidth, translatedHeight); 220 221 } 222 g2.setPaint(seriesPaint); 223 g2.fill(bar); 224 if (Math.abs(translatedX2 - translatedX1) > 3) { 225 g2.setStroke(getItemStroke(series, item)); 226 g2.setPaint(seriesOutlinePaint); 227 g2.draw(bar); 228 } 229 230 if (info != null) { 232 EntityCollection entities = info.getOwner().getEntityCollection(); 233 if (entities != null) { 234 String tip = null; 235 if (getToolTipGenerator() != null) { 236 tip = getToolTipGenerator().generateToolTip(dataset, series, item); 237 } 238 String url = null; 239 if (getURLGenerator() != null) { 240 url = getURLGenerator().generateURL(dataset, series, item); 241 } 242 XYItemEntity entity = new XYItemEntity(bar, dataset, series, item, tip, url); 243 entities.addEntity(entity); 244 } 245 } 246 247 } 248 249 256 public Object clone() throws CloneNotSupportedException { 257 return super.clone(); 258 } 259 260 } 261 262 | Popular Tags |