KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > progra > charting > render > BarChartRenderer


1 /*
2     JOpenChart Java Charting Library and Toolkit
3     Copyright (C) 2001 Sebastian Müller
4     http://jopenchart.sourceforge.net
5
6     This library is free software; you can redistribute it and/or
7     modify it under the terms of the GNU Lesser General Public
8     License as published by the Free Software Foundation; either
9     version 2.1 of the License, or (at your option) any later version.
10
11     This library is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14     Lesser General Public License for more details.
15
16     You should have received a copy of the GNU Lesser General Public
17     License along with this library; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
20     BarChartRenderer.java
21     Created on 31. October 2001, 12:32
22 */

23
24 package de.progra.charting.render;
25
26 import java.awt.Color JavaDoc;
27 import java.awt.Font JavaDoc;
28 import java.awt.Graphics2D JavaDoc;
29 import java.awt.geom.AffineTransform JavaDoc;
30 import java.awt.geom.Point2D JavaDoc;
31 import java.awt.geom.Rectangle2D JavaDoc;
32 import java.awt.font.LineMetrics JavaDoc;
33 import java.awt.font.FontRenderContext JavaDoc;
34 import java.text.DecimalFormat JavaDoc;
35 import de.progra.charting.PointToPixelTranslator;
36 import de.progra.charting.CoordSystem;
37 import de.progra.charting.model.ChartDataModel;
38 import de.progra.charting.model.ChartDataModelConstraints;
39
40 /**
41  * This renderer creates a BarChart. This will only work on a ChartDataModel
42  * with non-numeric x-axis values, because I couldn't make any sense out of
43  * a bar chart with a numeric x-axis like in a line chart.
44  * @author mueller
45  * @version 1.0
46  */

47 public class BarChartRenderer extends AbstractChartRenderer {
48     protected DecimalFormat JavaDoc barTopFormat;
49     protected float boxWidth = 1.0f;
50     protected Font JavaDoc barTopFont;
51     
52     /** Creates new BarChartRenderer
53      * @param cs the CoordSystem used to translate into pixel space
54      * @param model the DataModel that should be rendered
55      */

56     public BarChartRenderer(CoordSystem cs, ChartDataModel model) {
57         super(cs, model);
58     }
59
60     /** Creates new BarChartRenderer
61      * @param cs the CoordSystem used to translate into pixel space
62      * @param model the DataModel that should be rendered
63      * @param topFormat if not null, the values that each box represents will be
64      * printed at the top of the box.
65      * @param boxWidth a value between 0.0 and 1.0 representing how much of the
66      * alloted space each box should consume. If 1.0 is passed, then each box will
67      * completely fill it's allotted space, alternately I suggest you don't pass 0.0
68      */

69     public BarChartRenderer(CoordSystem cs, ChartDataModel model,
70                             DecimalFormat JavaDoc topFormat, Font JavaDoc topFont, float boxWidth) {
71         super(cs, model);
72         barTopFormat = topFormat;
73         this.barTopFont = topFont;
74         this.boxWidth = boxWidth;
75     }
76
77
78     /** Finally renders the Object in the Graphics object.
79      * @param g the Graphics2D object in which to render
80      */

81     public void renderChart(Graphics2D JavaDoc g) {
82         ChartDataModel m = getChartDataModel();
83         ChartDataModelConstraints con = m.getChartDataModelConstraints(CoordSystem.FIRST_YAXIS);
84         
85         if(m.isColumnNumeric())
86             return;
87         
88         RowColorModel rcm = getRowColorModel();
89         AffineTransform JavaDoc yaxis1 = getTransform(CoordSystem.FIRST_YAXIS);
90         AffineTransform JavaDoc yaxis2 = getTransform(CoordSystem.SECOND_YAXIS);
91         
92         int datasetcount = m.getDataSetNumber();
93             
94         Point2D JavaDoc point1 = yaxis1.transform(new Point2D.Float JavaDoc((float)con.getMinimumColumnValue(),
95                                                            con.getMaximumValue().floatValue()),
96                                           null);
97         Point2D JavaDoc point2 = yaxis1.transform(new Point2D.Float JavaDoc((float)con.getMaximumColumnValue(),
98                                                              con.getMaximumValue().floatValue()),
99                                           null);
100         Point2D JavaDoc value = point1;
101         
102         int dataunitwidth = (int)((point2.getX() - point1.getX()) / con.getMaximumColumnValue());
103         int boxwidth = (int)(dataunitwidth * boxWidth / datasetcount);
104         float margin = (float)(dataunitwidth * ((1.0 - boxWidth)/2f));
105         
106         Point2D JavaDoc pointzero;
107         if(con.getMinimumValue().floatValue() > 0)
108            pointzero = yaxis1.transform(new Point2D.Float JavaDoc((float)con.getMinimumColumnValue(),
109                                                          con.getMinimumValue().floatValue()),
110                                         null);
111         else if(con.getMaximumValue().floatValue() < 0)
112            pointzero = yaxis1.transform(new Point2D.Float JavaDoc((float)con.getMinimumColumnValue(),
113                                                          con.getMaximumValue().floatValue()),
114                                         null);
115         else
116            pointzero = yaxis1.transform(new Point2D.Float JavaDoc((float)con.getMinimumColumnValue(),
117                                                          0f),
118                                         null);
119          
120         FontRenderContext JavaDoc columnTopfrc = null;
121         LineMetrics JavaDoc lm = null;
122         DecimalFormat JavaDoc df = null;
123         Rectangle2D JavaDoc fontRec = null;
124         String JavaDoc columnTop = null;
125         if (barTopFormat != null) {
126             g.setFont(barTopFont);
127             columnTopfrc = new FontRenderContext JavaDoc(null, false, false);
128         }
129         /* We paint the values starting at x-value "0".
130          * As we only render BarCharts for ChartDataModels with
131          * non-numeric x-axis values we don't have to read those
132          * values from the data model. You can look in
133          * ObjectChartDataModel to see, how the x-axis bounds
134          * are defined: the minimum value is always 0, the maximum
135          * value is the amount of non-numeric x-axis values.
136          */

137         for(int i = 0; i < datasetcount; i++) {
138             //System.out.println("** DataSet "+i);
139

140             for(int j = 0; j < m.getDataSetLength(i); j++) {
141                 yaxis1.transform(new Point2D.Float JavaDoc((float)j, m.getValueAt(i, j).floatValue()),
142                                  value);
143                 
144                 Rectangle2D JavaDoc box =
145                     new Rectangle2D.Float JavaDoc((float)(value.getX() + margin + i*boxwidth),
146                                           (float)Math.min(value.getY(), pointzero.getY()),
147                                           (float)boxwidth,
148                                           (float)Math.abs(pointzero.getY() - value.getY()));
149                     
150                 g.setColor(rcm.getColor(i));
151                 g.fill(box);
152                 g.setColor(Color.black);
153                 g.draw(box);
154
155                 if (barTopFormat != null) {
156                     //get value for zero'th set, index j
157
columnTop = barTopFormat.format(model.getValueAt(i,j).doubleValue());
158                     fontRec = barTopFont.getStringBounds(columnTop, columnTopfrc);
159                     lm = barTopFont.getLineMetrics(columnTop, columnTopfrc);
160                     g.drawString(columnTop,
161                         (float)(value.getX() + i*boxwidth +
162                                 (boxwidth - fontRec.getWidth())/2) - lm.getLeading(),
163                         (float)(Math.min(value.getY(), pointzero.getY())-lm.getDescent()));
164                 }
165             }
166         }
167    }//end render method
168

169 }
170
Popular Tags