1 46 47 package org.jfree.chart.renderer.category; 48 49 import java.awt.Graphics2D ; 50 import java.awt.geom.Line2D ; 51 import java.awt.geom.Rectangle2D ; 52 import java.io.Serializable ; 53 54 import org.jfree.chart.axis.CategoryAxis; 55 import org.jfree.chart.axis.ValueAxis; 56 import org.jfree.chart.event.RendererChangeEvent; 57 import org.jfree.chart.plot.CategoryPlot; 58 import org.jfree.chart.plot.PlotOrientation; 59 import org.jfree.chart.renderer.xy.XYStepRenderer; 60 import org.jfree.data.category.CategoryDataset; 61 import org.jfree.util.PublicCloneable; 62 63 69 public class CategoryStepRenderer extends AbstractCategoryItemRenderer 70 implements Cloneable , PublicCloneable, 71 Serializable { 72 73 74 private static final long serialVersionUID = -5121079703118261470L; 75 76 77 public static final int STAGGER_WIDTH = 5; 79 83 private boolean stagger = false; 84 85 86 private transient Line2D line = new Line2D.Double (0.0, 0.0, 0.0, 0.0); 87 88 91 public CategoryStepRenderer() { 92 this(false); 93 } 94 95 101 public CategoryStepRenderer(boolean stagger) { 102 this.stagger = stagger; 103 } 104 105 110 public boolean getStagger() { 111 return this.stagger; 112 } 113 114 121 public void setStagger(boolean shouldStagger) { 122 this.stagger = shouldStagger; 123 notifyListeners(new RendererChangeEvent(this)); 124 } 125 126 136 protected void drawLine(Graphics2D g2, PlotOrientation orientation, 137 double x0, double y0, double x1, double y1) { 138 139 if (orientation == PlotOrientation.VERTICAL) { 140 this.line.setLine(x0, y0, x1, y1); 141 g2.draw(this.line); 142 } 143 else if (orientation == PlotOrientation.HORIZONTAL) { 144 this.line.setLine(y0, x0, y1, x1); g2.draw(this.line); 146 } 147 } 149 150 164 public void drawItem(Graphics2D g2, 165 CategoryItemRendererState state, 166 Rectangle2D dataArea, 167 CategoryPlot plot, 168 CategoryAxis domainAxis, 169 ValueAxis rangeAxis, 170 CategoryDataset dataset, 171 int row, 172 int column, 173 int pass) { 174 175 Number value = dataset.getValue(row, column); 176 if (value == null) { 177 return; 178 } 179 PlotOrientation orientation = plot.getOrientation(); 180 181 double x1s = domainAxis.getCategoryStart( 183 column, getColumnCount(), dataArea, plot.getDomainAxisEdge() 184 ); 185 double x1 = domainAxis.getCategoryMiddle( 186 column, getColumnCount(), dataArea, plot.getDomainAxisEdge() 187 ); 188 double x1e = 2 * x1 - x1s; double y1 = rangeAxis.valueToJava2D( 190 value.doubleValue(), dataArea, plot.getRangeAxisEdge() 191 ); 192 g2.setPaint(getItemPaint(row, column)); 193 g2.setStroke(getItemStroke(row, column)); 194 195 if (column != 0) { 196 Number previousValue = dataset.getValue(row, column - 1); 197 if (previousValue != null) { 198 double previous = previousValue.doubleValue(); 200 double x0s = domainAxis.getCategoryStart( 201 column - 1, getColumnCount(), dataArea, 202 plot.getDomainAxisEdge() 203 ); 204 double x0 = domainAxis.getCategoryMiddle( 205 column - 1, getColumnCount(), dataArea, 206 plot.getDomainAxisEdge() 207 ); 208 double x0e = 2 * x0 - x0s; double y0 = rangeAxis.valueToJava2D( 210 previous, dataArea, plot.getRangeAxisEdge() 211 ); 212 if (getStagger()) { 213 int xStagger = row * STAGGER_WIDTH; 214 if (xStagger > (x1s - x0e)) { 215 xStagger = (int) (x1s - x0e); 216 } 217 x1s = x0e + xStagger; 218 } 219 drawLine(g2, orientation, x0e, y0, x1s, y0); 220 222 drawLine(g2, orientation, x1s, y0, x1s, y1); } 224 } 225 drawLine(g2, orientation, x1s, y1, x1e, y1); 227 if (isItemLabelVisible(row, column)) { 229 drawItemLabel( 230 g2, orientation, dataset, row, column, x1, y1, 231 (value.doubleValue() < 0.0) 232 ); 233 } 234 261 262 } 263 264 271 public boolean equals(Object obj) { 272 if (obj == this) { 273 return true; 274 } 275 if (!(obj instanceof CategoryStepRenderer)) { 276 return false; 277 } 278 if (!super.equals(obj)) { 279 return false; 280 } 281 CategoryStepRenderer that = (CategoryStepRenderer) obj; 282 if (this.stagger != that.stagger) { 283 return false; 284 } 285 return true; 286 } 287 288 } 289 | Popular Tags |