KickJava   Java API By Example, From Geeks To Geeks.

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


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  * ClusteredXYBarRenderer.java
24  * ---------------------------
25  * (C) Copyright 2003, by Paolo Cova and Contributors.
26  *
27  * Original Author: Paolo Cova;
28  * Contributor(s): David Gilbert (for Object Refinery Limited);
29  * Christian W. Zuckschwerdt;
30  * Matthias Rose;
31  *
32  * $Id: ClusteredXYBarRenderer.java,v 1.16 2003/11/14 16:04:45 mungady Exp $
33  *
34  * Changes
35  * -------
36  * 24-Jan-2003 : Version 1, contributed by Paolo Cova (DG);
37  * 25-Mar-2003 : Implemented Serializable (DG);
38  * 01-May-2003 : Modified drawItem(...) method signature (DG);
39  * 30-Jul-2003 : Modified entity constructor (CZ);
40  * 20-Aug-2003 : Implemented Cloneable and PublicCloneable (DG);
41  * 16-Sep-2003 : Changed ChartRenderingInfo --> PlotRenderingInfo (DG);
42  * 07-Oct-2003 : Added renderer state (DG);
43  * 03-Nov-2003 : In draw method added state parameter and y==null value handling (MR);
44  *
45  */

46
47 package org.jfree.chart.renderer;
48
49 import java.awt.Graphics2D JavaDoc;
50 import java.awt.Paint JavaDoc;
51 import java.awt.geom.Rectangle2D JavaDoc;
52 import java.io.Serializable JavaDoc;
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 /**
67  * An extension of {@link XYBarRenderer} that displays bars for different
68  * series values at the same x next to each other. The assumption here is
69  * that for each x (time or else) there is a y value for each series. If
70  * this is not the case, there will be spaces between bars for a given x.
71  *
72  * @author Paolo Cova
73  */

74 public class ClusteredXYBarRenderer extends XYBarRenderer implements Cloneable JavaDoc,
75                                                                      PublicCloneable,
76                                                                      Serializable JavaDoc {
77
78     /** Percentage margin (to reduce the width of bars). */
79     private double margin;
80
81     /** A data value of zero translated to a Java2D value. */
82     private double translatedRangeZero;
83
84     /** Determines whether bar center should be interval start. */
85     private boolean centerBarAtStartValue;
86
87     /**
88      * Default constructor. Bar margin is set to 0.0.
89     */

90     public ClusteredXYBarRenderer() {
91         this(0.0, false);
92     }
93
94     /**
95     * Constructs a new XY clustered bar renderer.
96     *
97     * @param margin the percentage amount to trim from the width of each bar.
98     * @param centerBarAtStartValue If true, bars will be centered on the start of the time period.
99     */

100     public ClusteredXYBarRenderer(double margin, boolean centerBarAtStartValue) {
101         super(margin);
102         this.margin = margin;
103         this.centerBarAtStartValue = centerBarAtStartValue;
104     }
105
106     /**
107     * Initialises the renderer. Here we calculate the Java2D y-coordinate for zero, since all
108     * the bars have their bases fixed at zero. Copied from superclass to
109     * initialize local variables.
110     *
111     * @param g2 the graphics device.
112     * @param dataArea the area inside the axes.
113     * @param plot the plot.
114     * @param data the data.
115     * @param info an optional info collection object to return data back to the caller.
116     *
117     * @return The number of passes required by the renderer.
118     */

119     public XYItemRendererState initialise(Graphics2D JavaDoc g2,
120                                           Rectangle2D JavaDoc 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     /**
134      * Sets the margin.
135      *
136      * @param margin the margin.
137      */

138     public void setMargin(double margin) {
139         this.margin = margin;
140         super.setMargin(margin);
141     }
142
143     /**
144      * Draws the visual representation of a single data item. This method
145      * is mostly copied from the superclass, the change is that in the
146      * calculated space for a singe bar we draw bars for each series next to
147      * each other. The width of each bar is the available width divided by
148      * the number of series. Bars for each series are drawn in order left to
149      * right.
150      *
151      * @param g2 the graphics device.
152      * @param state the renderer state.
153      * @param dataArea the area within which the plot is being drawn.
154      * @param info collects information about the drawing.
155      * @param plot the plot (can be used to obtain standard color information etc).
156      * @param domainAxis the domain axis.
157      * @param rangeAxis the range axis.
158      * @param dataset the dataset.
159      * @param series the series index.
160      * @param item the item index.
161      * @param crosshairInfo collects information about crosshairs.
162      * @param pass the pass index.
163      */

164     public void drawItem(Graphics2D JavaDoc g2,
165                                                  XYItemRendererState state,
166                          Rectangle2D JavaDoc 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 JavaDoc seriesPaint = getItemPaint(series, item);
176         Paint JavaDoc seriesOutlinePaint = getItemOutlinePaint(series, item);
177
178                 Number JavaDoc 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 JavaDoc bar = null;
209         PlotOrientation orientation = plot.getOrientation();
210         if (orientation == PlotOrientation.HORIZONTAL) {
211             bar = new Rectangle2D.Double JavaDoc(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 JavaDoc(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         // add an entity for the item...
231
if (info != null) {
232             EntityCollection entities = info.getOwner().getEntityCollection();
233             if (entities != null) {
234                 String JavaDoc tip = null;
235                 if (getToolTipGenerator() != null) {
236                     tip = getToolTipGenerator().generateToolTip(dataset, series, item);
237                 }
238                 String JavaDoc 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     /**
250      * Returns a clone of the renderer.
251      *
252      * @return A clone.
253      *
254      * @throws CloneNotSupportedException if the renderer cannot be cloned.
255      */

256     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
257         return super.clone();
258     }
259     
260 }
261
262
Popular Tags