KickJava   Java API By Example, From Geeks To Geeks.

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


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  * IntervalBarRenderer.java
24  * ------------------------
25  * (C) Copyright 2002, 2003 by Jeremy Bowman.
26  *
27  * Original Author: Jeremy Bowman;
28  * Contributor(s): David Gilbert (for Object Refinery Limited);
29  * Christian W. Zuckschwerdt;
30  *
31  * $Id: IntervalBarRenderer.java,v 1.16 2003/11/03 14:21:27 mungady Exp $
32  *
33  * Changes
34  * -------
35  * 29-Apr-2002 : Version 1, contributed by Jeremy Bowman (DG);
36  * 11-May-2002 : Use CategoryPlot.getLabelsVisible() (JB);
37  * 29-May-2002 : Added constructors (DG);
38  * 26-Jun-2002 : Added axis to initialise method (DG);
39  * 20-Sep-2002 : Added basic support for chart entities (DG);
40  * 24-Oct-2002 : Amendments for changes in CategoryDataset interface and CategoryToolTipGenerator
41  * interface (DG);
42  * 05-Nov-2002 : Base dataset is now TableDataset not CategoryDataset (DG);
43  * 25-Mar-2003 : Implemented Serializable (DG);
44  * 30-Jul-2003 : Modified entity constructor (CZ);
45  * 19-Aug-2003 : Implemented Cloneable and PublicCloneable (DG);
46  * 08-Sep-2003 : Added checks for null values (DG);
47  * 07-Oct-2003 : Added renderer state (DG);
48  * 21-Oct-2003 : Bar width moved into renderer state (DG);
49  *
50  */

51
52 package org.jfree.chart.renderer;
53
54 import java.awt.Graphics2D JavaDoc;
55 import java.awt.Paint JavaDoc;
56 import java.awt.Stroke JavaDoc;
57 import java.awt.geom.Rectangle2D JavaDoc;
58 import java.io.Serializable JavaDoc;
59
60 import org.jfree.chart.axis.CategoryAxis;
61 import org.jfree.chart.axis.ValueAxis;
62 import org.jfree.chart.entity.CategoryItemEntity;
63 import org.jfree.chart.entity.EntityCollection;
64 import org.jfree.chart.labels.CategoryItemLabelGenerator;
65 import org.jfree.chart.plot.CategoryPlot;
66 import org.jfree.chart.plot.PlotOrientation;
67 import org.jfree.data.CategoryDataset;
68 import org.jfree.data.IntervalCategoryDataset;
69 import org.jfree.data.MultiIntervalCategoryDataset;
70 import org.jfree.ui.RectangleEdge;
71 import org.jfree.util.PublicCloneable;
72
73
74 /**
75  * A renderer that handles the drawing of bars for a bar plot where
76  * each bar has a high and low value.
77  * <p>
78  * For use with the {@link CategoryPlot} class.
79  *
80  * @author Jeremy Bowman
81  */

82 public class IntervalBarRenderer extends BarRenderer
83         implements CategoryItemRenderer, Cloneable JavaDoc, PublicCloneable, Serializable JavaDoc {
84
85     /**
86      * Constructs a new renderer.
87      */

88     public IntervalBarRenderer() {
89         super();
90     }
91
92     /**
93      * Draws the bar for a single (series, category) data item.
94      *
95      * @param g2 the graphics device.
96      * @param state the renderer state.
97      * @param dataArea the data area.
98      * @param plot the plot.
99      * @param domainAxis the domain axis.
100      * @param rangeAxis the range axis.
101      * @param dataset the dataset.
102      * @param row the row index (zero-based).
103      * @param column the column index (zero-based).
104      */

105     public void drawItem(Graphics2D JavaDoc g2,
106                          CategoryItemRendererState state,
107                          Rectangle2D JavaDoc dataArea,
108                          CategoryPlot plot,
109                          CategoryAxis domainAxis,
110                          ValueAxis rangeAxis,
111                          CategoryDataset dataset,
112                          int row,
113                          int column) {
114
115          if (dataset instanceof MultiIntervalCategoryDataset) {
116              MultiIntervalCategoryDataset d = (MultiIntervalCategoryDataset) dataset;
117              drawSubIntervals(g2, state, dataArea, plot, domainAxis, rangeAxis, d, row, column);
118          }
119          else if (dataset instanceof IntervalCategoryDataset) {
120              IntervalCategoryDataset d = (IntervalCategoryDataset) dataset;
121              drawInterval(g2, state, dataArea, plot, domainAxis, rangeAxis, d, row, column);
122          }
123          else {
124              super.drawItem(g2, state, dataArea, plot, domainAxis, rangeAxis, dataset, row, column);
125          }
126      }
127                           
128      /**
129       * Draws a single interval.
130       *
131       * @param g2 the graphics device.
132       * @param state the renderer state.
133       * @param dataArea the data plot area.
134       * @param plot the plot.
135       * @param domainAxis the domain axis.
136       * @param rangeAxis the range axis.
137       * @param dataset the data.
138       * @param row the row index (zero-based).
139       * @param column the column index (zero-based).
140       */

141      protected void drawInterval(Graphics2D JavaDoc g2,
142                                  CategoryItemRendererState state,
143                                  Rectangle2D JavaDoc dataArea,
144                                  CategoryPlot plot,
145                                  CategoryAxis domainAxis,
146                                  ValueAxis rangeAxis,
147                                  IntervalCategoryDataset dataset,
148                                  int row,
149                                  int column) {
150
151         int seriesCount = getRowCount();
152         int categoryCount = getColumnCount();
153
154         PlotOrientation orientation = plot.getOrientation();
155         
156         double rectX = 0.0;
157         double rectY = 0.0;
158
159         RectangleEdge domainAxisLocation = plot.getDomainAxisEdge();
160         RectangleEdge rangeAxisLocation = plot.getRangeAxisEdge();
161         
162         // Y0
163
Number JavaDoc value0 = dataset.getEndValue(row, column);
164         if (value0 == null) {
165             return;
166         }
167         double java2dValue0 = rangeAxis.translateValueToJava2D(value0.doubleValue(), dataArea,
168                                                                rangeAxisLocation);
169
170         // Y1
171
Number JavaDoc value1 = dataset.getStartValue(row, column);
172         if (value1 == null) {
173             return;
174         }
175         double java2dValue1 = rangeAxis.translateValueToJava2D(value1.doubleValue(), dataArea,
176                                                                rangeAxisLocation);
177
178         if (java2dValue1 < java2dValue0) {
179             double temp = java2dValue1;
180             java2dValue1 = java2dValue0;
181             java2dValue0 = temp;
182             Number JavaDoc tempNum = value1;
183             value1 = value0;
184             value0 = tempNum;
185         }
186
187         // BAR WIDTH
188
double rectWidth = state.getBarWidth();
189
190         // BAR HEIGHT
191
double rectHeight = Math.abs(java2dValue1 - java2dValue0);
192
193         if (orientation == PlotOrientation.HORIZONTAL) {
194             // BAR Y
195
rectY = domainAxis.getCategoryStart(column, getColumnCount(), dataArea,
196                                                 domainAxisLocation);
197             if (seriesCount > 1) {
198                 double seriesGap = dataArea.getHeight() * getItemMargin()
199                                    / (categoryCount * (seriesCount - 1));
200                 rectY = rectY + row * (state.getBarWidth() + seriesGap);
201             }
202             else {
203                 rectY = rectY + row * state.getBarWidth();
204             }
205             
206             rectX = java2dValue0;
207
208             rectHeight = state.getBarWidth();
209             rectWidth = Math.abs(java2dValue1 - java2dValue0);
210
211         }
212         else if (orientation == PlotOrientation.VERTICAL) {
213             // BAR X
214
rectX = domainAxis.getCategoryStart(column, getColumnCount(), dataArea,
215                                                 domainAxisLocation);
216
217             if (seriesCount > 1) {
218                 double seriesGap = dataArea.getWidth() * getItemMargin()
219                                    / (categoryCount * (seriesCount - 1));
220                 rectX = rectX + row * (state.getBarWidth() + seriesGap);
221             }
222             else {
223                 rectX = rectX + row * state.getBarWidth();
224             }
225
226             rectY = java2dValue0;
227
228         }
229         Rectangle2D JavaDoc bar = new Rectangle2D.Double JavaDoc(rectX, rectY, rectWidth, rectHeight);
230         Paint JavaDoc seriesPaint = getItemPaint(row, column);
231         g2.setPaint(seriesPaint);
232         g2.fill(bar);
233         
234         // draw the outline...
235
if (state.getBarWidth() > BAR_OUTLINE_WIDTH_THRESHOLD) {
236             Stroke JavaDoc stroke = getItemOutlineStroke(row, column);
237             Paint JavaDoc paint = getItemOutlinePaint(row, column);
238             if (stroke != null && paint != null) {
239                 g2.setStroke(stroke);
240                 g2.setPaint(paint);
241                 g2.draw(bar);
242             }
243         }
244         
245         CategoryItemLabelGenerator generator = getItemLabelGenerator(row, column);
246         if (generator != null && isItemLabelVisible(row, column)) {
247             drawItemLabel(g2, dataset, row, column, plot, generator, bar, false);
248         }
249
250         // collect entity and tool tip information...
251
if (state.getInfo() != null) {
252             EntityCollection entities = state.getInfo().getOwner().getEntityCollection();
253             if (entities != null) {
254                 String JavaDoc tip = null;
255                 if (generator != null) {
256                     tip = generator.generateToolTip(dataset, row, column);
257                 }
258                 String JavaDoc url = null;
259                 if (getItemURLGenerator(row, column) != null) {
260                     url = getItemURLGenerator(row, column).generateURL(dataset, row, column);
261                 }
262                 CategoryItemEntity entity = new CategoryItemEntity(
263                     bar, tip, url, dataset, row, dataset.getColumnKey(column), column
264                 );
265                 entities.addEntity(entity);
266             }
267         }
268
269     }
270     
271     /**
272      * Draws the sub-intervals for one item.
273      *
274      * @param g2 the graphics device.
275      * @param state the renderer state.
276      * @param dataArea the data plot area.
277      * @param plot the plot.
278      * @param domainAxis the domain axis.
279      * @param rangeAxis the range axis.
280      * @param dataset the data.
281      * @param row the row index (zero-based).
282      * @param column the column index (zero-based).
283      */

284     private void drawSubIntervals(Graphics2D JavaDoc g2,
285                                   CategoryItemRendererState state,
286                                   Rectangle2D JavaDoc dataArea,
287                                   CategoryPlot plot,
288                                   CategoryAxis domainAxis,
289                                   ValueAxis rangeAxis,
290                                   MultiIntervalCategoryDataset dataset,
291                                   int row,
292                                   int column) {
293
294         int count = dataset.getSubIntervalCount(row, column);
295         if (count == 0) {
296             drawInterval(g2, state, dataArea, plot, domainAxis, rangeAxis, dataset, row, column);
297         }
298
299         for (int subinterval = 0; subinterval < count; subinterval++) {
300             
301             RectangleEdge domainAxisLocation = plot.getDomainAxisEdge();
302             RectangleEdge rangeAxisLocation = plot.getRangeAxisEdge();
303
304             // X0
305
Number JavaDoc value0 = dataset.getStartValue(row, column, subinterval);
306             if (value0 == null) {
307                 return;
308             }
309             double translatedValue0 = rangeAxis.translateValueToJava2D(value0.doubleValue(),
310                                                                        dataArea,
311                                                                        rangeAxisLocation);
312     
313             // X1
314
Number JavaDoc value1 = dataset.getEndValue(row, column, subinterval);
315             if (value1 == null) {
316                 return;
317             }
318             double translatedValue1 = rangeAxis.translateValueToJava2D(value1.doubleValue(),
319                                                                        dataArea,
320                                                                        rangeAxisLocation);
321     
322             if (translatedValue1 < translatedValue0) {
323                 double temp = translatedValue1;
324                 translatedValue1 = translatedValue0;
325                 translatedValue0 = temp;
326             }
327     
328             // Y
329
double rectY = domainAxis.getCategoryStart(column, getColumnCount(), dataArea,
330                                                        domainAxisLocation);
331     
332             int rows = getRowCount();
333             int columns = getColumnCount();
334             if (rows > 1) {
335                 double seriesGap = dataArea.getHeight() * getItemMargin()
336                                    / (columns * (rows - 1));
337                 rectY = rectY + row * (state.getBarWidth() + seriesGap);
338             }
339             else {
340                 rectY = rectY + row * state.getBarWidth();
341             }
342     
343             // WIDTH
344
double rectWidth = Math.abs(translatedValue1 - translatedValue0);
345     
346             // HEIGHT
347
double rectHeight = state.getBarWidth();
348     
349             // DRAW THE BAR...
350
Rectangle2D JavaDoc bar = null;
351             
352             if (plot.getOrientation() == PlotOrientation.HORIZONTAL) {
353                 bar = new Rectangle2D.Double JavaDoc(translatedValue0, rectY,
354                                              rectWidth, rectHeight);
355             }
356             else if (plot.getOrientation() == PlotOrientation.VERTICAL) {
357                 bar = new Rectangle2D.Double JavaDoc(rectY, translatedValue0, rectHeight, rectWidth);
358             }
359     
360             Paint JavaDoc seriesPaint = getItemPaint(row, column);
361             g2.setPaint(seriesPaint);
362             g2.fill(bar);
363             if (state.getBarWidth() > BAR_OUTLINE_WIDTH_THRESHOLD) {
364                 g2.setStroke(getItemStroke(row, column));
365                 g2.setPaint(getItemOutlinePaint(row, column));
366                 g2.draw(bar);
367             }
368     
369             // collect entity and tool tip information...
370
if (state.getInfo() != null) {
371                 EntityCollection entities = state.getInfo().getOwner().getEntityCollection();
372                 if (entities != null) {
373                     String JavaDoc tip = null;
374                     if (getItemLabelGenerator(row, column) != null) {
375                         tip = getItemLabelGenerator(row, column).generateToolTip(dataset,
376                                                                           row, column);
377                     }
378                     String JavaDoc url = null;
379                     if (getItemURLGenerator(row, column) != null) {
380                         url = getItemURLGenerator(row, column).generateURL(dataset, row, column);
381                     }
382                     CategoryItemEntity entity = new CategoryItemEntity(
383                         bar, tip, url, dataset, row, dataset.getColumnKey(column), column
384                     );
385                     entities.addEntity(entity);
386                 }
387             }
388         }
389     }
390
391
392 }
393
Popular Tags