KickJava   Java API By Example, From Geeks To Geeks.

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


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     StackedBarChartRenderer.java
21     Created on 26. September 2002
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.AbstractChartDataModel;
38 import de.progra.charting.model.ChartDataModel;
39 import de.progra.charting.model.ChartDataModelConstraints;
40 import de.progra.charting.model.StackedChartDataModelConstraints;
41
42 /**
43  * This renderer creates a stacked BarChart. This will only work on a ChartDataModel
44  * with non-numeric x-axis values, because I couldn't make any sense out of
45  * a bar chart with a numeric x-axis like in a line chart.
46  * @author mueller
47  * @version 1.0
48  */

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

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

68     public StackedBarChartRenderer(CoordSystem cs, AbstractChartDataModel model,
69                                    float boxWidth) {
70         this(cs, model);
71         this.boxWidth = boxWidth;
72     }
73
74
75     /** Finally renders the Object in the Graphics object.
76      * @param g the Graphics2D object in which to render
77      */

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

132         double currentvalue = 0.0;
133         Rectangle2D JavaDoc box = null;
134         Point2D JavaDoc oldmaxvalue;
135         Point2D JavaDoc oldminvalue;
136         
137         for(int j = 0; j < maximumDataSetLength; j++) {
138             double minvalue = 0.0;
139             double maxvalue = 0.0;
140             
141             oldmaxvalue = pointzero;
142             oldminvalue = pointzero;
143             
144             for(int i = 0; i < m.getDataSetNumber(); i++) {
145                 
146                 if(j < m.getDataSetLength(i))
147                     currentvalue = m.getValueAt(i, j).doubleValue();
148                 else
149                     currentvalue = 0.0;
150                 
151                 if(currentvalue < 0.0) {
152                     minvalue += currentvalue;
153                     yaxis1.transform(new Point2D.Float JavaDoc((float)j, (float)minvalue),
154                                      value);
155                    
156                     box =
157                         new Rectangle2D.Float JavaDoc((float)(value.getX()),
158                                               (float)Math.min(value.getY(), oldminvalue.getY()),
159                                               (float)boxwidth,
160                                               (float)Math.abs(oldminvalue.getY() - value.getY()));
161                     oldminvalue = (Point2D JavaDoc)value.clone();
162                  }
163                 else {
164                     maxvalue += currentvalue;
165                     yaxis1.transform(new Point2D.Float JavaDoc((float)j, (float)maxvalue),
166                                      value);
167                     
168                     box =
169                         new Rectangle2D.Float JavaDoc((float)(value.getX()),
170                                               (float)Math.min(value.getY(), oldmaxvalue.getY()),
171                                               (float)boxwidth,
172                                               (float)Math.abs(oldmaxvalue.getY() - value.getY()));
173                     
174                     oldmaxvalue = (Point2D JavaDoc)value.clone();
175                 }
176                     
177                 g.setColor(rcm.getColor(i));
178                 g.fill(box);
179                 g.setColor(Color.black);
180                 g.draw(box);
181             }
182         }
183    }//end render method
184

185 }
186
187
Popular Tags