001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jfreechart/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * -------------------------
028: * CategoryStepRenderer.java
029: * -------------------------
030: *
031: * (C) Copyright 2004-2007, by Brian Cole and Contributors.
032: *
033: * Original Author: Brian Cole;
034: * Contributor(s): David Gilbert (for Object Refinery Limited);
035: *
036: * $Id: CategoryStepRenderer.java,v 1.5.2.5 2007/05/18 10:28:27 mungady Exp $
037: *
038: * Changes
039: * -------
040: * 21-Apr-2004 : Version 1, contributed by Brian Cole (DG);
041: * 22-Apr-2004 : Fixed Checkstyle complaints (DG);
042: * 05-Nov-2004 : Modified drawItem() signature (DG);
043: * 08-Mar-2005 : Added equals() method (DG);
044: * ------------- JFREECHART 1.0.x ---------------------------------------------
045: * 30-Nov-2006 : Added checks for series visibility (DG);
046: * 22-Feb-2007 : Use new state object for reusable line, enable chart entities
047: * (for tooltips, URLs), added new getLegendItem() override (DG);
048: * 20-Apr-2007 : Updated getLegendItem() for renderer change (DG);
049: * 18-May-2007 : Set dataset and seriesKey for LegendItem (DG);
050: *
051: */
052:
053: package org.jfree.chart.renderer.category;
054:
055: import java.awt.Graphics2D;
056: import java.awt.Paint;
057: import java.awt.Shape;
058: import java.awt.geom.Line2D;
059: import java.awt.geom.Rectangle2D;
060: import java.io.Serializable;
061:
062: import org.jfree.chart.LegendItem;
063: import org.jfree.chart.axis.CategoryAxis;
064: import org.jfree.chart.axis.ValueAxis;
065: import org.jfree.chart.entity.EntityCollection;
066: import org.jfree.chart.event.RendererChangeEvent;
067: import org.jfree.chart.plot.CategoryPlot;
068: import org.jfree.chart.plot.PlotOrientation;
069: import org.jfree.chart.plot.PlotRenderingInfo;
070: import org.jfree.chart.renderer.xy.XYStepRenderer;
071: import org.jfree.data.category.CategoryDataset;
072: import org.jfree.util.PublicCloneable;
073:
074: /**
075: * A "step" renderer similar to {@link XYStepRenderer} but
076: * that can be used with the {@link CategoryPlot} class.
077: */
078: public class CategoryStepRenderer extends AbstractCategoryItemRenderer
079: implements Cloneable, PublicCloneable, Serializable {
080:
081: /**
082: * State information for the renderer.
083: */
084: protected static class State extends CategoryItemRendererState {
085:
086: /**
087: * A working line for re-use to avoid creating large numbers of
088: * objects.
089: */
090: public Line2D line;
091:
092: /**
093: * Creates a new state instance.
094: *
095: * @param info collects plot rendering information (<code>null</code>
096: * permitted).
097: */
098: public State(PlotRenderingInfo info) {
099: super (info);
100: this .line = new Line2D.Double();
101: }
102:
103: }
104:
105: /** For serialization. */
106: private static final long serialVersionUID = -5121079703118261470L;
107:
108: /** The stagger width. */
109: public static final int STAGGER_WIDTH = 5; // could make this configurable
110:
111: /**
112: * A flag that controls whether or not the steps for multiple series are
113: * staggered.
114: */
115: private boolean stagger = false;
116:
117: /**
118: * Creates a new renderer (stagger defaults to <code>false</code>).
119: */
120: public CategoryStepRenderer() {
121: this (false);
122: }
123:
124: /**
125: * Creates a new renderer.
126: *
127: * @param stagger should the horizontal part of the step be staggered by
128: * series?
129: */
130: public CategoryStepRenderer(boolean stagger) {
131: this .stagger = stagger;
132: }
133:
134: /**
135: * Returns the flag that controls whether the series steps are staggered.
136: *
137: * @return A boolean.
138: */
139: public boolean getStagger() {
140: return this .stagger;
141: }
142:
143: /**
144: * Sets the flag that controls whether or not the series steps are
145: * staggered and sends a {@link RendererChangeEvent} to all registered
146: * listeners.
147: *
148: * @param shouldStagger a boolean.
149: */
150: public void setStagger(boolean shouldStagger) {
151: this .stagger = shouldStagger;
152: notifyListeners(new RendererChangeEvent(this ));
153: }
154:
155: /**
156: * Returns a legend item for a series.
157: *
158: * @param datasetIndex the dataset index (zero-based).
159: * @param series the series index (zero-based).
160: *
161: * @return The legend item.
162: */
163: public LegendItem getLegendItem(int datasetIndex, int series) {
164:
165: CategoryPlot p = getPlot();
166: if (p == null) {
167: return null;
168: }
169:
170: // check that a legend item needs to be displayed...
171: if (!isSeriesVisible(series)
172: || !isSeriesVisibleInLegend(series)) {
173: return null;
174: }
175:
176: CategoryDataset dataset = p.getDataset(datasetIndex);
177: String label = getLegendItemLabelGenerator().generateLabel(
178: dataset, series);
179: String description = label;
180: String toolTipText = null;
181: if (getLegendItemToolTipGenerator() != null) {
182: toolTipText = getLegendItemToolTipGenerator()
183: .generateLabel(dataset, series);
184: }
185: String urlText = null;
186: if (getLegendItemURLGenerator() != null) {
187: urlText = getLegendItemURLGenerator().generateLabel(
188: dataset, series);
189: }
190: Shape shape = new Rectangle2D.Double(-4.0, -3.0, 8.0, 6.0);
191: Paint paint = lookupSeriesPaint(series);
192:
193: LegendItem item = new LegendItem(label, description,
194: toolTipText, urlText, shape, paint);
195: item.setSeriesKey(dataset.getRowKey(series));
196: item.setSeriesIndex(series);
197: item.setDataset(dataset);
198: item.setDatasetIndex(datasetIndex);
199: return item;
200: }
201:
202: /**
203: * Creates a new state instance. This method is called from
204: * {@link #initialise(Graphics2D, Rectangle2D, CategoryPlot, int,
205: * PlotRenderingInfo)}, and we override it to ensure that the state
206: * contains a working Line2D instance.
207: *
208: * @param info the plot rendering info (<code>null</code> is permitted).
209: *
210: * @return A new state instance.
211: */
212: protected CategoryItemRendererState createState(
213: PlotRenderingInfo info) {
214: return new State(info);
215: }
216:
217: /**
218: * Draws a line taking into account the specified orientation.
219: * <p>
220: * In version 1.0.5, the signature of this method was changed by the
221: * addition of the 'state' parameter. This is an incompatible change, but
222: * is considered a low risk because it is unlikely that anyone has
223: * subclassed this renderer. If this *does* cause trouble for you, please
224: * report it as a bug.
225: *
226: * @param g2 the graphics device.
227: * @param state the renderer state.
228: * @param orientation the plot orientation.
229: * @param x0 the x-coordinate for the start of the line.
230: * @param y0 the y-coordinate for the start of the line.
231: * @param x1 the x-coordinate for the end of the line.
232: * @param y1 the y-coordinate for the end of the line.
233: */
234: protected void drawLine(Graphics2D g2, State state,
235: PlotOrientation orientation, double x0, double y0,
236: double x1, double y1) {
237:
238: if (orientation == PlotOrientation.VERTICAL) {
239: state.line.setLine(x0, y0, x1, y1);
240: g2.draw(state.line);
241: } else if (orientation == PlotOrientation.HORIZONTAL) {
242: state.line.setLine(y0, x0, y1, x1); // switch x and y
243: g2.draw(state.line);
244: }
245:
246: }
247:
248: /**
249: * Draw a single data item.
250: *
251: * @param g2 the graphics device.
252: * @param state the renderer state.
253: * @param dataArea the area in which the data is drawn.
254: * @param plot the plot.
255: * @param domainAxis the domain axis.
256: * @param rangeAxis the range axis.
257: * @param dataset the dataset.
258: * @param row the row index (zero-based).
259: * @param column the column index (zero-based).
260: * @param pass the pass index.
261: */
262: public void drawItem(Graphics2D g2,
263: CategoryItemRendererState state, Rectangle2D dataArea,
264: CategoryPlot plot, CategoryAxis domainAxis,
265: ValueAxis rangeAxis, CategoryDataset dataset, int row,
266: int column, int pass) {
267:
268: // do nothing if item is not visible
269: if (!getItemVisible(row, column)) {
270: return;
271: }
272:
273: Number value = dataset.getValue(row, column);
274: if (value == null) {
275: return;
276: }
277: PlotOrientation orientation = plot.getOrientation();
278:
279: // current data point...
280: double x1s = domainAxis.getCategoryStart(column,
281: getColumnCount(), dataArea, plot.getDomainAxisEdge());
282: double x1 = domainAxis.getCategoryMiddle(column,
283: getColumnCount(), dataArea, plot.getDomainAxisEdge());
284: double x1e = 2 * x1 - x1s; // or: x1s + 2*(x1-x1s)
285: double y1 = rangeAxis.valueToJava2D(value.doubleValue(),
286: dataArea, plot.getRangeAxisEdge());
287: g2.setPaint(getItemPaint(row, column));
288: g2.setStroke(getItemStroke(row, column));
289:
290: if (column != 0) {
291: Number previousValue = dataset.getValue(row, column - 1);
292: if (previousValue != null) {
293: // previous data point...
294: double previous = previousValue.doubleValue();
295: double x0s = domainAxis.getCategoryStart(column - 1,
296: getColumnCount(), dataArea, plot
297: .getDomainAxisEdge());
298: double x0 = domainAxis.getCategoryMiddle(column - 1,
299: getColumnCount(), dataArea, plot
300: .getDomainAxisEdge());
301: double x0e = 2 * x0 - x0s; // or: x0s + 2*(x0-x0s)
302: double y0 = rangeAxis.valueToJava2D(previous, dataArea,
303: plot.getRangeAxisEdge());
304: if (getStagger()) {
305: int xStagger = row * STAGGER_WIDTH;
306: if (xStagger > (x1s - x0e)) {
307: xStagger = (int) (x1s - x0e);
308: }
309: x1s = x0e + xStagger;
310: }
311: drawLine(g2, (State) state, orientation, x0e, y0, x1s,
312: y0);
313: // extend x0's flat bar
314:
315: drawLine(g2, (State) state, orientation, x1s, y0, x1s,
316: y1);
317: // upright bar
318: }
319: }
320: drawLine(g2, (State) state, orientation, x1s, y1, x1e, y1);
321: // x1's flat bar
322:
323: // draw the item labels if there are any...
324: if (isItemLabelVisible(row, column)) {
325: drawItemLabel(g2, orientation, dataset, row, column, x1,
326: y1, (value.doubleValue() < 0.0));
327: }
328:
329: // add an item entity, if this information is being collected
330: EntityCollection entities = state.getEntityCollection();
331: if (entities != null) {
332: Rectangle2D hotspot = new Rectangle2D.Double();
333: if (orientation == PlotOrientation.VERTICAL) {
334: hotspot.setRect(x1s, y1, x1e - x1s, 4.0);
335: } else {
336: hotspot.setRect(y1 - 2.0, x1s, 4.0, x1e - x1s);
337: }
338: addItemEntity(entities, dataset, row, column, hotspot);
339: }
340:
341: }
342:
343: /**
344: * Tests this renderer for equality with an arbitrary object.
345: *
346: * @param obj the object (<code>null</code> permitted).
347: *
348: * @return A boolean.
349: */
350: public boolean equals(Object obj) {
351: if (obj == this ) {
352: return true;
353: }
354: if (!(obj instanceof CategoryStepRenderer)) {
355: return false;
356: }
357: CategoryStepRenderer that = (CategoryStepRenderer) obj;
358: if (this .stagger != that.stagger) {
359: return false;
360: }
361: return super.equals(obj);
362: }
363:
364: }
|