KickJava   Java API By Example, From Geeks To Geeks.

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


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     PieChartRenderer.java
21     Created on 7. August 2001, 18:14
22 */

23
24 package de.progra.charting.render;
25
26 import java.awt.font.FontRenderContext JavaDoc;
27 import java.awt.font.TextLayout JavaDoc;
28 import java.awt.geom.Ellipse2D JavaDoc;
29 import java.awt.geom.Arc2D JavaDoc;
30 import java.awt.geom.GeneralPath JavaDoc;
31 import java.awt.geom.Line2D JavaDoc;
32
33 import de.progra.charting.CoordSystem;
34 import java.awt.geom.AffineTransform JavaDoc;
35 import de.progra.charting.PointToPixelTranslator;
36
37 import java.awt.Font JavaDoc;
38 import java.awt.Graphics2D JavaDoc;
39 import java.awt.Paint JavaDoc;
40 import java.awt.RenderingHints JavaDoc;
41 import java.awt.Color JavaDoc;
42 import de.progra.charting.model.ChartDataModel;
43
44 /**
45  * This renderer creates a PieChart.
46  * @author tbee
47  * @version 1.0
48  */

49 public class RadarChartRenderer extends AbstractChartRenderer {
50         
51     /** Creates new PieChartRenderer
52      * @param model the DataModel that should be rendered
53      */

54     public RadarChartRenderer(ChartDataModel model) {
55         super(model);
56     }
57     
58     /** Creates new PieChartRenderer
59      * @param cs the CoordSystem used to translate values into points
60      * @param model the DataModel that should be rendered
61      */

62     public RadarChartRenderer(CoordSystem cs, ChartDataModel model) {
63         super(cs, model);
64     }
65
66     /** Finally renders the Object in the Graphics object.
67      * @param g the Graphics2D object in which to render
68      */

69     public void renderChart(Graphics2D JavaDoc g) {
70         // remember current anti aliasing rendering hint
71
// then activate it
72
Object JavaDoc rh = g.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
73         g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
74         
75         // get the models
76
ChartDataModel m = getChartDataModel();
77         RowColorModel rcm = getRowColorModel();
78
79         // get the available drawing space
80
double height = this.getBounds().getHeight();
81         double width = this.getBounds().getWidth();
82
83         // the number of dataset
84
int lNumberOfRows = m.getDataSetNumber();
85         
86         // determine shortest dataset length, this is the number of axis that we need to draw
87
int lNumberOfColumns = Integer.MAX_VALUE;
88         for(int i = 0; i < lNumberOfRows; i++)
89             lNumberOfColumns = Math.min(lNumberOfColumns, m.getDataSetLength(i));
90         
91         double[] maxvalues = new double[lNumberOfColumns];
92         
93         // get center
94
double center_y = getBounds().getCenterY();
95         double center_x = getBounds().getCenterX();
96
97         // determine the radius
98
double lRadius = Math.min(width * 0.9, height * 0.9) / 2;
99
100         // scan through the datasets
101
for(int lRow = 0; lRow < lNumberOfRows; lRow++)
102         {
103             // scan through the values in the dataset
104
GeneralPath JavaDoc filledPolygon = new GeneralPath JavaDoc(GeneralPath.WIND_EVEN_ODD, lNumberOfColumns);
105             for (int lCol = 0; lCol < lNumberOfColumns; lCol++)
106             {
107                 // get the value
108
double lValue = m.getValueAt(lRow, lCol).doubleValue();
109                 
110                 // determine the scale
111
double lMaxValue = maxvalues[lCol];
112                 
113                 if(lMaxValue == 0.0) {
114                     for(int row = 0; row < lNumberOfRows; row++)
115                         lMaxValue = Math.max(lMaxValue, m.getValueAt(row, lCol).doubleValue() * 1.1);
116                     
117                     maxvalues[lCol] = lMaxValue;
118                 }
119                 
120                 double lScaledValue = lValue / lMaxValue;
121                 double lLineValue = lRadius * lScaledValue;
122
123                 // determine rotation: there are 2PI/noOfCols vertexes, this is vertex no lCol
124
// -1 : we want to rotate clockwise
125
// + PI: rotate 180 degree to get the first column pointing up
126
double lRotation = (-1 * (2 * Math.PI / lNumberOfColumns) * lCol) + Math.PI;
127
128                 // determine the end points
129
double lX = center_x + (lLineValue * Math.sin(lRotation));
130                 double lY = center_y + (lLineValue * Math.cos(lRotation));
131
132                 // draw the line
133
Line2D JavaDoc lLine = new Line2D.Double JavaDoc(center_x, center_y, lX, lY);
134                 g.setColor(Color.black);
135                 g.draw(lLine);
136
137                 // add to polygone
138
if (lCol == 0) filledPolygon.moveTo((float)lX, (float)lY);
139                 else filledPolygon.lineTo((float)lX, (float)lY);
140             }
141             
142             // draw the polygone
143
filledPolygon.closePath();
144             g.setPaint( rcm.getColor(lRow) );
145             g.draw(filledPolygon);
146         }
147         
148         double lRotation;
149         double lX;
150         double lY;
151         TextLayout JavaDoc lLabel;
152
153         // draw the lines
154
for (int lCol = 0; lCol < lNumberOfColumns; lCol++)
155         {
156             // determine rotation: there are 2PI/noOfCols vertexes, this is vertex no lCol
157
// -1 : we want to rotate clockwise
158
// + PI: rotate 180 degree to get the first column pointing up
159
// Math.PI ... - Math.PI
160
lRotation = (-1 * (2 * Math.PI / lNumberOfColumns) * lCol) + Math.PI;
161
162             // determine the end points
163
lX = center_x + (lRadius * Math.sin(lRotation));
164             lY = center_y + (lRadius * Math.cos(lRotation));
165
166             // draw the line
167
Line2D JavaDoc lLine = new Line2D.Double JavaDoc(center_x, center_y, lX, lY );
168             g.setColor(Color.black);
169             g.draw(lLine);
170
171             // draw the label
172
lLabel = new TextLayout JavaDoc("" + model.getColumnValueAt(lCol), new Font JavaDoc("Courier", Font.BOLD, 9), new FontRenderContext JavaDoc(null, true, false));
173             g.setColor(Color.black);
174             
175             // Move the labels in the lower half circle down a bit, so the upper left corner touches the axis
176
if ((lRotation <= Math.PI / 2) && (lRotation >= -Math.PI / 2))
177                 lY += lLabel.getBounds().getHeight();
178             
179             // Move the labels in the left half circle a bit left, so the upper right corner touches the axis
180
if (lRotation <= 0)
181                 lX -= lLabel.getBounds().getWidth();
182             
183             lLabel.draw(g, (float)lX, (float)lY);
184         }
185
186         // reset rendering hint
187
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, rh);
188     }
189 }
190
Popular Tags