KickJava   Java API By Example, From Geeks To Geeks.

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


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  * StatisticalBarRenderer.java
24  * ---------------------------
25  * (C) Copyright 2002, 2003 by Pascal Collet and Contributors.
26  *
27  * Original Author: Pascal Collet;
28  * Contributor(s): David Gilbert (for Object Refinery Limited);
29  * Christian W. Zuckschwerdt;
30  *
31  * $Id: StatisticalBarRenderer.java,v 1.10 2003/11/03 14:21:28 mungady Exp $
32  *
33  * Changes
34  * -------
35  * 21-Aug-2002 : Version 1, contributed by Pascal Collet (DG);
36  * 01-Oct-2002 : Fixed errors reported by Checkstyle (DG);
37  * 24-Oct-2002 : Changes to dataset interface (DG);
38  * 05-Nov-2002 : Base dataset is now TableDataset not CategoryDataset (DG);
39  * 05-Feb-2003 : Updates for new DefaultStatisticalCategoryDataset (DG);
40  * 25-Mar-2003 : Implemented Serializable (DG);
41  * 30-Jul-2003 : Modified entity constructor (CZ);
42  * 06-Oct-2003 : Corrected typo in exception message (DG);
43  *
44  */

45
46 package org.jfree.chart.renderer;
47
48 import java.awt.Graphics2D JavaDoc;
49 import java.awt.Paint JavaDoc;
50 import java.awt.geom.Line2D JavaDoc;
51 import java.awt.geom.Rectangle2D JavaDoc;
52 import java.io.Serializable JavaDoc;
53
54 import org.jfree.chart.axis.CategoryAxis;
55 import org.jfree.chart.axis.ValueAxis;
56 import org.jfree.chart.plot.CategoryPlot;
57 import org.jfree.chart.plot.PlotOrientation;
58 import org.jfree.data.CategoryDataset;
59 import org.jfree.data.statistics.StatisticalCategoryDataset;
60 import org.jfree.ui.RectangleEdge;
61 import org.jfree.util.PublicCloneable;
62
63 /**
64  * A renderer that handles the drawing a bar plot where
65  * each bar has a mean value and a standard deviation line.
66  *
67  * @author Pascal Collet
68  */

69 public class StatisticalBarRenderer extends BarRenderer
70                                     implements CategoryItemRenderer,
71                                                Cloneable JavaDoc, PublicCloneable, Serializable JavaDoc {
72
73     /**
74      * Default constructor.
75      */

76     public StatisticalBarRenderer() {
77         super();
78     }
79
80     /**
81      * Draws the bar with its standard deviation line range for a single (series, category) data
82      * item.
83      *
84      * @param g2 the graphics device.
85      * @param state the renderer state.
86      * @param dataArea the data area.
87      * @param plot the plot.
88      * @param domainAxis the domain axis.
89      * @param rangeAxis the range axis.
90      * @param data the data.
91      * @param row the row index (zero-based).
92      * @param column the column index (zero-based).
93      */

94     public void drawItem(Graphics2D JavaDoc g2,
95                          CategoryItemRendererState state,
96                          Rectangle2D JavaDoc dataArea,
97                          CategoryPlot plot,
98                          CategoryAxis domainAxis,
99                          ValueAxis rangeAxis,
100                          CategoryDataset data,
101                          int row,
102                          int column) {
103
104
105         // defensive check
106
if (!(data instanceof StatisticalCategoryDataset)) {
107             throw new IllegalArgumentException JavaDoc("StatisticalBarRenderer.drawCategoryItem()"
108                 + " : the data should be of type StatisticalCategoryDataset only.");
109         }
110         StatisticalCategoryDataset statData = (StatisticalCategoryDataset) data;
111
112         PlotOrientation orientation = plot.getOrientation();
113         if (orientation == PlotOrientation.HORIZONTAL) {
114             drawHorizontalItem(g2, state, dataArea, plot, domainAxis, rangeAxis, statData,
115                                row, column);
116         }
117         else if (orientation == PlotOrientation.VERTICAL) {
118             drawVerticalItem(g2, state, dataArea, plot, domainAxis, rangeAxis, statData,
119                              row, column);
120         }
121     }
122                 
123     /**
124      * Draws an item for a plot with a horizontal orientation.
125      *
126      * @param g2 the graphics device.
127      * @param state the renderer state.
128      * @param dataArea the data area.
129      * @param plot the plot.
130      * @param domainAxis the domain axis.
131      * @param rangeAxis the range axis.
132      * @param dataset the data.
133      * @param row the row index (zero-based).
134      * @param column the column index (zero-based).
135      */

136     protected void drawHorizontalItem(Graphics2D JavaDoc g2,
137                                       CategoryItemRendererState state,
138                                       Rectangle2D JavaDoc dataArea,
139                                       CategoryPlot plot,
140                                       CategoryAxis domainAxis,
141                                       ValueAxis rangeAxis,
142                                       StatisticalCategoryDataset dataset,
143                                       int row,
144                                       int column) {
145                                      
146         RectangleEdge xAxisLocation = plot.getDomainAxisEdge();
147         
148         // BAR Y
149
double rectY = domainAxis.getCategoryStart(column, getColumnCount(), dataArea,
150                                                    xAxisLocation);
151
152         int seriesCount = getRowCount();
153         int categoryCount = getColumnCount();
154         if (seriesCount > 1) {
155             double seriesGap = dataArea.getHeight() * getItemMargin()
156                                / (categoryCount * (seriesCount - 1));
157             rectY = rectY + row * (state.getBarWidth() + seriesGap);
158         }
159         else {
160             rectY = rectY + row * state.getBarWidth();
161         }
162
163         // BAR X
164
Number JavaDoc meanValue = dataset.getMeanValue(row, column);
165
166         double value = meanValue.doubleValue();
167         double base = 0.0;
168         double lclip = getLowerClip();
169         double uclip = getUpperClip();
170
171         if (uclip <= 0.0) { // cases 1, 2, 3 and 4
172
if (value >= uclip) {
173                 return; // bar is not visible
174
}
175             base = uclip;
176             if (value <= lclip) {
177                 value = lclip;
178             }
179         }
180         else if (lclip <= 0.0) { // cases 5, 6, 7 and 8
181
if (value >= uclip) {
182                 value = uclip;
183             }
184             else {
185                 if (value <= lclip) {
186                     value = lclip;
187                 }
188             }
189         }
190         else { // cases 9, 10, 11 and 12
191
if (value <= lclip) {
192                 return; // bar is not visible
193
}
194             base = getLowerClip();
195             if (value >= uclip) {
196                value = uclip;
197             }
198         }
199
200         RectangleEdge yAxisLocation = plot.getRangeAxisEdge();
201         double transY1 = rangeAxis.translateValueToJava2D(base, dataArea, yAxisLocation);
202         double transY2 = rangeAxis.translateValueToJava2D(value, dataArea, yAxisLocation);
203         double rectX = Math.min(transY2, transY1);
204
205         double rectHeight = state.getBarWidth();
206         double rectWidth = Math.abs(transY2 - transY1);
207
208         Rectangle2D JavaDoc bar = new Rectangle2D.Double JavaDoc(rectX, rectY, rectWidth, rectHeight);
209         Paint JavaDoc seriesPaint = getItemPaint(row, column);
210         g2.setPaint(seriesPaint);
211         g2.fill(bar);
212         if (state.getBarWidth() > 3) {
213             g2.setStroke(getItemStroke(row, column));
214             g2.setPaint(getItemOutlinePaint(row, column));
215             g2.draw(bar);
216         }
217
218         // standard deviation lines
219
double valueDelta = dataset.getStdDevValue(row, column).doubleValue();
220         double highVal = rangeAxis.translateValueToJava2D(meanValue.doubleValue() + valueDelta,
221                                                          dataArea, yAxisLocation);
222         double lowVal = rangeAxis.translateValueToJava2D(meanValue.doubleValue() - valueDelta,
223                                                          dataArea, yAxisLocation);
224
225         Line2D JavaDoc line = null;
226         line = new Line2D.Double JavaDoc(lowVal, rectY + rectHeight / 2.0d,
227                                  highVal, rectY + rectHeight / 2.0d);
228         g2.draw(line);
229         line = new Line2D.Double JavaDoc(highVal, rectY + rectHeight * 0.25,
230                                  highVal, rectY + rectHeight * 0.75);
231         g2.draw(line);
232         line = new Line2D.Double JavaDoc(lowVal, rectY + rectHeight * 0.25,
233                                  lowVal, rectY + rectHeight * 0.75);
234         g2.draw(line);
235     }
236
237     /**
238      * Draws an item for a plot with a vertical orientation.
239      *
240      * @param g2 the graphics device.
241      * @param state the renderer state.
242      * @param dataArea the data area.
243      * @param plot the plot.
244      * @param domainAxis the domain axis.
245      * @param rangeAxis the range axis.
246      * @param dataset the data.
247      * @param row the row index (zero-based).
248      * @param column the column index (zero-based).
249      */

250     protected void drawVerticalItem(Graphics2D JavaDoc g2,
251                                     CategoryItemRendererState state,
252                                     Rectangle2D JavaDoc dataArea,
253                                     CategoryPlot plot,
254                                     CategoryAxis domainAxis,
255                                     ValueAxis rangeAxis,
256                                     StatisticalCategoryDataset dataset,
257                                     int row,
258                                     int column) {
259                                      
260         RectangleEdge xAxisLocation = plot.getDomainAxisEdge();
261         
262         // BAR X
263
double rectX = domainAxis.getCategoryStart(column, getColumnCount(), dataArea,
264                                                    xAxisLocation);
265
266         int seriesCount = getRowCount();
267         int categoryCount = getColumnCount();
268         if (seriesCount > 1) {
269             double seriesGap = dataArea.getWidth() * getItemMargin()
270                                / (categoryCount * (seriesCount - 1));
271             rectX = rectX + row * (state.getBarWidth() + seriesGap);
272         }
273         else {
274             rectX = rectX + row * state.getBarWidth();
275         }
276
277         // BAR Y
278
Number JavaDoc meanValue = dataset.getMeanValue(row, column);
279
280         double value = meanValue.doubleValue();
281         double base = 0.0;
282         double lclip = getLowerClip();
283         double uclip = getUpperClip();
284
285         if (uclip <= 0.0) { // cases 1, 2, 3 and 4
286
if (value >= uclip) {
287                 return; // bar is not visible
288
}
289             base = uclip;
290             if (value <= lclip) {
291                 value = lclip;
292             }
293         }
294         else if (lclip <= 0.0) { // cases 5, 6, 7 and 8
295
if (value >= uclip) {
296                 value = uclip;
297             }
298             else {
299                 if (value <= lclip) {
300                     value = lclip;
301                 }
302             }
303         }
304         else { // cases 9, 10, 11 and 12
305
if (value <= lclip) {
306                 return; // bar is not visible
307
}
308             base = getLowerClip();
309             if (value >= uclip) {
310                value = uclip;
311             }
312         }
313
314         RectangleEdge yAxisLocation = plot.getRangeAxisEdge();
315         double transY1 = rangeAxis.translateValueToJava2D(base, dataArea, yAxisLocation);
316         double transY2 = rangeAxis.translateValueToJava2D(value, dataArea, yAxisLocation);
317         double rectY = Math.min(transY2, transY1);
318
319         double rectWidth = state.getBarWidth();
320         double rectHeight = Math.abs(transY2 - transY1);
321
322         Rectangle2D JavaDoc bar = new Rectangle2D.Double JavaDoc(rectX, rectY, rectWidth, rectHeight);
323         Paint JavaDoc seriesPaint = getItemPaint(row, column);
324         g2.setPaint(seriesPaint);
325         g2.fill(bar);
326         if (state.getBarWidth() > 3) {
327             g2.setStroke(getItemStroke(row, column));
328             g2.setPaint(getItemOutlinePaint(row, column));
329             g2.draw(bar);
330         }
331
332         // standard deviation lines
333
double valueDelta = dataset.getStdDevValue(row, column).doubleValue();
334         double highVal = rangeAxis.translateValueToJava2D(meanValue.doubleValue() + valueDelta,
335                                                          dataArea, yAxisLocation);
336         double lowVal = rangeAxis.translateValueToJava2D(meanValue.doubleValue() - valueDelta,
337                                                          dataArea, yAxisLocation);
338
339         Line2D JavaDoc line = null;
340         line = new Line2D.Double JavaDoc(rectX + rectWidth / 2.0d, lowVal,
341                                  rectX + rectWidth / 2.0d, highVal);
342         g2.draw(line);
343         line = new Line2D.Double JavaDoc(rectX + rectWidth / 2.0d - 5.0d, highVal,
344                                  rectX + rectWidth / 2.0d + 5.0d, highVal);
345         g2.draw(line);
346         line = new Line2D.Double JavaDoc(rectX + rectWidth / 2.0d - 5.0d, lowVal,
347                                  rectX + rectWidth / 2.0d + 5.0d, lowVal);
348         g2.draw(line);
349     }
350
351 }
352
Popular Tags