KickJava   Java API By Example, From Geeks To Geeks.

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


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.geom.Ellipse2D JavaDoc;
27 import java.awt.geom.Arc2D JavaDoc;
28 import de.progra.charting.CoordSystem;
29 import java.awt.geom.AffineTransform JavaDoc;
30 import de.progra.charting.PointToPixelTranslator;
31 import java.awt.Graphics2D JavaDoc;
32 import java.awt.RenderingHints JavaDoc;
33 import java.awt.Color JavaDoc;
34 import de.progra.charting.model.ChartDataModel;
35
36 /**
37  * This renderer creates a PieChart.
38  * @author mueller
39  * @version 1.0
40  */

41 public class PieChartRenderer extends AbstractChartRenderer {
42         
43     /** Creates new PieChartRenderer
44      * @param model the DataModel that should be rendered
45      */

46     public PieChartRenderer(ChartDataModel model) {
47         super(model);
48     }
49     
50     /** Creates new PieChartRenderer
51      * @param cs the CoordSystem used to translate values into points
52      * @param model the DataModel that should be rendered
53      */

54     public PieChartRenderer(CoordSystem cs, ChartDataModel model) {
55         super(cs, model);
56     }
57
58     /** Finally renders the Object in the Graphics object.
59      * @param g the Graphics2D object in which to render
60      */

61     public void renderChart(Graphics2D JavaDoc g) {
62         Object JavaDoc rh = g.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
63         g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
64         ChartDataModel m = getChartDataModel();
65         RowColorModel rcm = getRowColorModel();
66         
67         double height = this.getBounds().getHeight();
68         double width = this.getBounds().getWidth();
69
70         int datenreihen = m.getDataSetNumber();
71         // determine shortest dataset length
72
int min_length = Integer.MAX_VALUE;
73         
74         for(int i = 0; i < datenreihen; i++)
75             min_length = Math.min(min_length, m.getDataSetLength(i));
76         
77         double center_y = getBounds().getCenterY();
78         double center_x = getBounds().getCenterX();
79         
80         double rad = Math.min(width * 0.9, height * 0.9);
81         double modelVal = 0.0;
82         for(int reihe = min_length; reihe >= 1; reihe--) {
83             
84             double kreis = (double)rad / min_length * reihe;
85             Ellipse2D.Double JavaDoc circle = new Ellipse2D.Double JavaDoc((double)center_x-kreis/2,
86                                                            (double)center_y-kreis/2,
87                                                            kreis, kreis);
88                         
89             double sum = 0;
90             double start = 0.0;
91             
92             // Paint data
93
for(int i = 0; i < datenreihen; i++) {
94                 modelVal = m.getValueAt(i, reihe - 1).doubleValue();
95                 
96                 // Catch modelVal == Not A Number
97
if(modelVal != modelVal)
98                     continue;
99                 sum += modelVal;
100             }
101             
102             for(int i = 0; i < datenreihen; i++) {
103                 double value = m.getValueAt(i, reihe - 1).doubleValue();
104                 
105                 // Catch value == Not A Number
106
if(value != value) value = 0.0;
107                 
108                 Arc2D.Double JavaDoc arc = new Arc2D.Double JavaDoc(circle.getBounds2D(),
109                                                   (double)start,
110                                                   360.0 * (double)value/(double)sum,
111                                                   Arc2D.PIE);
112                 start += 360 * (double)value/(double)sum;
113                 
114                 g.setColor(rcm.getColor(i));
115                 g.fill(arc);
116             }
117             g.setColor(Color.black);
118             g.draw(circle);
119         }
120         
121         g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, rh);
122     }
123 }
124
Popular Tags