KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > renderer > XYBarRenderer


1 /* ======================================
2  * JFreeChart : a free Java chart library
3  * ======================================
4  *
5  * Project Info: http://www.jfree.org/jfreechart/index.html
6  * Project Lead: David Gilbert (david.gilbert@object-refinery.com);
7  *
8  * (C) Copyright 2000-2003, by Object Refinery Limited and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it under the terms
11  * of the GNU Lesser General Public License as published by the Free Software Foundation;
12  * either version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License along with this
19  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  * ------------------
23  * XYBarRenderer.java
24  * ------------------
25  * (C) Copyright 2001-2003, by Object Refinery Limited.
26  *
27  * Original Author: David Gilbert (for Object Refinery Limited);
28  * Contributor(s): Richard Atkinson;
29  * Christian W. Zuckschwerdt;
30  * Bill Kelemen;
31  *
32  * $Id: XYBarRenderer.java,v 1.10 2003/11/03 14:21:28 mungady Exp $
33  *
34  * Changes
35  * -------
36  * 13-Dec-2001 : Version 1, makes VerticalXYBarPlot class redundant (DG);
37  * 23-Jan-2002 : Added DrawInfo parameter to drawItem(...) method (DG);
38  * 09-Apr-2002 : Removed the translated zero from the drawItem method. Override the initialise()
39  * method to calculate it (DG);
40  * 24-May-2002 : Incorporated tooltips into chart entities (DG);
41  * 25-Jun-2002 : Removed redundant import (DG);
42  * 05-Aug-2002 : Small modification to drawItem method to support URLs for HTML image maps (RA);
43  * 25-Mar-2003 : Implemented Serializable (DG);
44  * 01-May-2003 : Modified drawItem(...) method signature (DG);
45  * 30-Jul-2003 : Modified entity constructor (CZ);
46  * 20-Aug-2003 : Implemented Cloneable and PublicCloneable (DG);
47  * 24-Aug-2003 : Added null checks in drawItem (BK);
48  * 16-Sep-2003 : Changed ChartRenderingInfo --> PlotRenderingInfo (DG);
49  * 07-Oct-2003 : Added renderer state (DG);
50  *
51  */

52
53 package org.jfree.chart.renderer;
54
55 import java.awt.Graphics2D JavaDoc;
56 import java.awt.Paint JavaDoc;
57 import java.awt.geom.Rectangle2D JavaDoc;
58 import java.io.Serializable JavaDoc;
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 /**
75  * A renderer that draws bars on an {@link XYPlot} (requires an {@link IntervalXYDataset}).
76  *
77  * @author David Gilbert
78  */

79 public class XYBarRenderer extends AbstractXYItemRenderer implements XYItemRenderer,
80                                                                      Cloneable JavaDoc,
81                                                                      PublicCloneable,
82                                                                      Serializable JavaDoc {
83
84     /** Percentage margin (to reduce the width of bars). */
85     private double margin;
86
87     /** A data value of zero translated to a Java2D value. */
88     private double translatedRangeZero;
89
90     /**
91      * The default constructor.
92      */

93     public XYBarRenderer() {
94         super();
95         this.margin = 0.0;
96     }
97
98     /**
99      * Constructs a new renderer.
100      *
101      * @param margin the percentage amount to trim from the width of each bar.
102      *
103      */

104     public XYBarRenderer(double margin) {
105         super();
106         this.margin = margin;
107     }
108
109     /**
110      * Constructs a new renderer.
111      *
112      * @param margin the percentage amount to trim from the width of each bar.
113      * @param toolTipGenerator the tool tip generator (<code>null</code> permitted).
114      * @param urlGenerator the URL generator (<code>null</code> permitted).
115      *
116      * @deprecated Use default constructor then set tooltip generator and URL generator.
117      */

118     public XYBarRenderer(double margin,
119                          XYToolTipGenerator toolTipGenerator,
120                          XYURLGenerator urlGenerator) {
121
122         super(toolTipGenerator, urlGenerator);
123         this.margin = margin;
124
125     }
126
127     /**
128      * Sets the percentage amount by which the bars are trimmed.
129      * <P>
130      * Fires a property change event.
131      *
132      * @param margin the new margin.
133      */

134     public void setMargin(double margin) {
135
136         Double JavaDoc old = new Double JavaDoc(this.margin);
137         this.margin = margin;
138         firePropertyChanged("XYBarRenderer.margin", old, new Double JavaDoc(margin));
139
140     }
141
142     /**
143      * Initialises the renderer. Here we calculate the Java2D y-coordinate for zero, since all
144      * the bars have their bases fixed at zero.
145      *
146      * @param g2 the graphics device.
147      * @param dataArea the area inside the axes.
148      * @param plot the plot.
149      * @param data the data.
150      * @param info an optional info collection object to return data back to the caller.
151      *
152      * @return The number of dataset passes required by the renderer.
153      */

154     public XYItemRendererState initialise(Graphics2D JavaDoc g2,
155                                           Rectangle2D JavaDoc 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     /**
169      * Draws the visual representation of a single data item.
170      *
171      * @param g2 the graphics device.
172      * @param state the renderer state.
173      * @param dataArea the area within which the plot is being drawn.
174      * @param info collects information about the drawing.
175      * @param plot the plot (can be used to obtain standard color information etc).
176      * @param domainAxis the domain axis.
177      * @param rangeAxis the range axis.
178      * @param dataset the dataset.
179      * @param series the series index (zero-based).
180      * @param item the item index (zero-based).
181      * @param crosshairInfo collects information about crosshairs.
182      * @param pass the pass index.
183      */

184     public void drawItem(Graphics2D JavaDoc g2,
185                          XYItemRendererState state,
186                          Rectangle2D JavaDoc 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 JavaDoc seriesPaint = getItemPaint(series, item);
200         Paint JavaDoc seriesOutlinePaint = getSeriesOutlinePaint(series);
201
202         Number JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc bar = null;
235         PlotOrientation orientation = plot.getOrientation();
236         if (orientation == PlotOrientation.HORIZONTAL) {
237
238             bar = new Rectangle2D.Double JavaDoc(Math.min(this.translatedRangeZero, translatedValue),
239                                          translatedEndX,
240                                          translatedHeight, translatedWidth);
241         }
242         else if (orientation == PlotOrientation.VERTICAL) {
243
244             bar = new Rectangle2D.Double JavaDoc(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         // add an entity for the item...
259
if (info != null) {
260             EntityCollection entities = info.getOwner().getEntityCollection();
261             if (entities != null) {
262                 String JavaDoc tip = null;
263                 if (getToolTipGenerator() != null) {
264                     tip = getToolTipGenerator().generateToolTip(dataset, series, item);
265                 }
266                 String JavaDoc 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     /**
278      * Returns a clone of the renderer.
279      *
280      * @return A clone.
281      *
282      * @throws CloneNotSupportedException if the renderer cannot be cloned.
283      */

284     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
285         return super.clone();
286     }
287
288 }
289
Popular Tags