KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > progra > charting > DefaultChart


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     DefaultChart.java
21     Created on 26. Juni 2001, 22:49
22  */

23
24 package de.progra.charting;
25
26 import de.progra.charting.model.ChartDataModel;
27 import java.util.*;
28 import de.progra.charting.render.AbstractChartRenderer;
29 import de.progra.charting.render.RowColorModel;
30 import java.awt.geom.AffineTransform JavaDoc;
31 import java.awt.Rectangle JavaDoc;
32 import java.awt.Graphics2D JavaDoc;
33 import java.awt.Dimension JavaDoc;
34 import java.awt.Color JavaDoc;
35
36
37 /** The Default class to create a chart.
38  * @author mueller
39  * @version 1.0
40  */

41 public class DefaultChart extends AbstractChart {
42     
43     public static int LINEAR_X_LINEAR_Y = 0;
44     public static int NO_COORDINATE_SYSTEM = 1;
45     
46     /** Creates new empty DefaultChart.*/
47     protected DefaultChart() {
48     }
49     
50     /** Creates a new DefaultChart with the given model
51      * and title string and no coordinate system.
52      * @param model the ChartDataModel
53      * @param title the title String
54      */

55     public DefaultChart(ChartDataModel model, String JavaDoc title) {
56         this();
57         setChartDataModel(model);
58         setRowColorModel(new RowColorModel(model));
59         
60         setLegend(new Legend(getRowColorModel()));
61         setTitle(new Title(title));
62     }
63     
64     /** Creates a new DefaultChart with the given model
65      * and title string and a coordinate system.
66      * @param model the ChartDataModel
67      * @param title the title String
68      * @param coord the id of the coordinate system configuration
69      */

70     public DefaultChart(ChartDataModel model, String JavaDoc title, int coord) {
71         this(model, title);
72         
73         if(coord == this.LINEAR_X_LINEAR_Y)
74             this.setCoordSystem(new CoordSystem(model));
75     }
76     
77     /** Creates a new DefaultChart with the given model
78      * and title string and a coordinate system.
79      * @param model the ChartDataModel
80      * @param title the title String
81      * @param coord the id of the coordinate system configuration
82      * @param xaxis the x-axis' unit
83      * @param yaxis the y-axis' unit
84      */

85     public DefaultChart(ChartDataModel model, String JavaDoc title, int coord,
86                         String JavaDoc xaxis, String JavaDoc yaxis) {
87         this(model, title, coord);
88         
89         this.getCoordSystem().setXAxisUnit(xaxis);
90         this.getCoordSystem().setYAxisUnit(yaxis);
91     }
92     
93     /** Should compute the preferred size of the Chart
94      * @return <CODE>null</CODE>
95      */

96     public Dimension JavaDoc getPreferredSize() {
97         return null;
98     }
99     
100     /** Does the layout of the title, legend and coordinate system and
101      * calls the render method of all those including the ChartRenderers.
102      * @param g the <CODE>Graphics2D</CODE> object to paint in.
103      */

104     public void render(Graphics2D JavaDoc g) {
105         
106         //g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
107
// RenderingHints.VALUE_ANTIALIAS_ON);
108

109         
110         int width = (int)getBounds().getWidth();
111         int height = (int)getBounds().getHeight();
112         
113         Title t = getTitle();
114         Legend l = getLegend();
115         CoordSystem c = getCoordSystem();
116         Collection renderer = getChartRenderer().values();
117         
118         g.setColor(Color.white);
119         g.fillRect(0, 0, width, height);
120         
121         g.setColor(Color.black);
122                 
123         int titleheight = 0;
124         int legendwidth = 0;
125         
126         if(t != null) {
127             Dimension JavaDoc title = t.getPreferredSize();
128             t.setBounds(new Rectangle JavaDoc((int)(width/2
129                                       - (int)(title.getWidth()/2)),
130                                       0,
131                                       (int)title.getWidth(),
132                                       (int)title.getHeight()));
133             t.render(g);
134             titleheight = (int)t.getBounds().getHeight();
135         }
136         if(l != null) {
137             Dimension JavaDoc legend = l.getPreferredSize();
138             l.setBounds(new Rectangle JavaDoc((int)(width - legend.getWidth()),
139                                       (int)(height/2 - legend.getHeight()/2 + titleheight),
140                                       (int)legend.getWidth(),
141                                       (int)legend.getHeight()));
142             l.render(g);
143             legendwidth = (int)l.getBounds().getWidth();
144         }
145         if(c != null) {
146             c.setBounds(new Rectangle JavaDoc(0, (int)titleheight,
147                                       (int)(width - legendwidth),
148                                       (int)(height - titleheight)));
149         }
150         if(! renderer.isEmpty()) {
151             Iterator i = renderer.iterator();
152             while(i.hasNext()) {
153                 AbstractChartRenderer cr = (AbstractChartRenderer)i.next();
154                 cr.setBounds(new Rectangle JavaDoc(0, (int)titleheight,
155                                            (int)(width - legendwidth),
156                                            (int)(height - titleheight) - 5));
157                 
158                 // cr.setPointToPixelTranslator()
159

160                 cr.render(g);
161             }
162         }
163         if(c != null)
164             c.render(g);
165                 
166         /*
167         g.setColor(Color.pink);
168         
169         g.draw(t.getBounds());
170         g.draw(l.getBounds());
171         g.draw(c.getBounds());
172         g.draw(c.getInnerBounds());
173         */

174         //System.out.println("** Bounds: "+c.getBounds());
175
//System.out.println("** InnerBounds: "+c.getInnerBounds());
176
}
177  
178 }
179
Popular Tags