1 61 62 package org.jfree.chart.renderer.xy; 63 64 import java.awt.Graphics2D ; 65 import java.awt.Paint ; 66 import java.awt.geom.Rectangle2D ; 67 import java.io.Serializable ; 68 69 import org.jfree.chart.axis.ValueAxis; 70 import org.jfree.chart.entity.EntityCollection; 71 import org.jfree.chart.entity.XYItemEntity; 72 import org.jfree.chart.labels.XYItemLabelGenerator; 73 import org.jfree.chart.labels.XYToolTipGenerator; 74 import org.jfree.chart.plot.CrosshairState; 75 import org.jfree.chart.plot.PlotOrientation; 76 import org.jfree.chart.plot.PlotRenderingInfo; 77 import org.jfree.chart.plot.XYPlot; 78 import org.jfree.data.xy.IntervalXYDataset; 79 import org.jfree.data.xy.XYDataset; 80 import org.jfree.ui.RectangleEdge; 81 import org.jfree.util.PublicCloneable; 82 83 94 public class ClusteredXYBarRenderer extends XYBarRenderer 95 implements Cloneable , PublicCloneable, 96 Serializable { 97 98 99 private static final long serialVersionUID = 5864462149177133147L; 100 101 102 private boolean centerBarAtStartValue; 103 104 107 public ClusteredXYBarRenderer() { 108 this(0.0, false); 109 } 110 111 118 public ClusteredXYBarRenderer(double margin, 119 boolean centerBarAtStartValue) { 120 super(margin); 121 this.centerBarAtStartValue = centerBarAtStartValue; 122 } 123 124 147 public void drawItem(Graphics2D g2, 148 XYItemRendererState state, 149 Rectangle2D dataArea, 150 PlotRenderingInfo info, 151 XYPlot plot, 152 ValueAxis domainAxis, 153 ValueAxis rangeAxis, 154 XYDataset dataset, int series, int item, 155 CrosshairState crosshairState, 156 int pass) { 157 158 IntervalXYDataset intervalDataset = (IntervalXYDataset) dataset; 159 160 Paint seriesPaint = getItemPaint(series, item); 161 162 double value0; 163 double value1; 164 if (getUseYInterval()) { 165 value0 = intervalDataset.getStartYValue(series, item); 166 value1 = intervalDataset.getEndYValue(series, item); 167 } 168 else { 169 value0 = getBase(); 170 value1 = intervalDataset.getYValue(series, item); 171 } 172 if (Double.isNaN(value0) || Double.isNaN(value1)) { 173 return; 174 } 175 176 double translatedValue0 = rangeAxis.valueToJava2D(value0, dataArea, 177 plot.getRangeAxisEdge()); 178 double translatedValue1 = rangeAxis.valueToJava2D(value1, dataArea, 179 plot.getRangeAxisEdge()); 180 181 RectangleEdge xAxisLocation = plot.getDomainAxisEdge(); 182 double x1 = intervalDataset.getStartXValue(series, item); 183 double translatedX1 = domainAxis.valueToJava2D(x1, dataArea, 184 xAxisLocation); 185 186 double x2 = intervalDataset.getEndXValue(series, item); 187 double translatedX2 = domainAxis.valueToJava2D(x2, dataArea, 188 xAxisLocation); 189 190 double translatedWidth = Math.max(1, Math.abs(translatedX2 191 - translatedX1)); 192 double translatedHeight = Math.abs(translatedValue0 - translatedValue1); 193 194 if (this.centerBarAtStartValue) { 195 translatedX1 -= translatedWidth / 2; 196 } 197 198 PlotOrientation orientation = plot.getOrientation(); 199 double m = getMargin(); 200 if (m > 0.0) { 201 double cut = translatedWidth * getMargin(); 202 translatedWidth = translatedWidth - cut; 203 if (orientation == PlotOrientation.HORIZONTAL) 204 translatedX1 = translatedX1 - cut / 2; 205 else 206 translatedX1 = translatedX1 + cut / 2; 207 } 208 209 int numSeries = dataset.getSeriesCount(); 210 double seriesBarWidth = translatedWidth / numSeries; 211 212 Rectangle2D bar = null; 213 if (orientation == PlotOrientation.HORIZONTAL) { 214 bar = new Rectangle2D.Double (Math.min(translatedValue0, 215 translatedValue1), translatedX1 - seriesBarWidth 216 * (numSeries - series), translatedHeight, seriesBarWidth); 217 } 218 else if (orientation == PlotOrientation.VERTICAL) { 219 bar = new Rectangle2D.Double (translatedX1 + seriesBarWidth * series, 220 Math.min(translatedValue0, translatedValue1), 221 seriesBarWidth, translatedHeight); 222 } 223 g2.setPaint(seriesPaint); 224 g2.fill(bar); 225 if (isDrawBarOutline() && Math.abs(translatedX2 - translatedX1) > 3) { 226 g2.setStroke(getItemOutlineStroke(series, item)); 227 g2.setPaint(getItemOutlinePaint(series, item)); 228 g2.draw(bar); 229 } 230 231 if (isItemLabelVisible(series, item)) { 232 XYItemLabelGenerator generator = getItemLabelGenerator(series, 233 item); 234 drawItemLabel(g2, dataset, series, item, plot, generator, bar, 235 value1 < 0.0); 236 } 237 238 if (info != null) { 240 EntityCollection entities = info.getOwner().getEntityCollection(); 241 if (entities != null) { 242 String tip = null; 243 XYToolTipGenerator generator 244 = getToolTipGenerator(series, item); 245 if (generator != null) { 246 tip = generator.generateToolTip(dataset, series, item); 247 } 248 String url = null; 249 if (getURLGenerator() != null) { 250 url = getURLGenerator().generateURL(dataset, series, item); 251 } 252 XYItemEntity entity = new XYItemEntity(bar, dataset, series, 253 item, tip, url); 254 entities.add(entity); 255 } 256 } 257 258 } 259 260 270 public boolean equals(Object obj) { 271 if (obj == this) { 272 return true; 273 } 274 if (!(obj instanceof ClusteredXYBarRenderer)) { 275 return false; 276 } 277 if (!super.equals(obj)) { 278 return false; 279 } 280 ClusteredXYBarRenderer that = (ClusteredXYBarRenderer) obj; 281 if (this.centerBarAtStartValue != that.centerBarAtStartValue) { 282 return false; 283 } 284 return true; 285 } 286 287 294 public Object clone() throws CloneNotSupportedException { 295 return super.clone(); 296 } 297 298 } 299 | Popular Tags |