KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > progra > charting > swing > ChartPanel


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     ChartPanel.java
21     Created on 6. September 2001, 14:10
22 */

23
24 package de.progra.charting.swing;
25
26 import javax.swing.JPanel JavaDoc;
27 import de.progra.charting.render.AbstractChartRenderer;
28 import de.progra.charting.event.*;
29 import de.progra.charting.Chart;
30 import de.progra.charting.Legend;
31 import de.progra.charting.Title;
32 import de.progra.charting.CoordSystem;
33 import de.progra.charting.DefaultChart;
34 import java.awt.geom.Rectangle2D JavaDoc;
35 import java.awt.Rectangle JavaDoc;
36 import java.awt.Graphics JavaDoc;
37 import java.awt.Graphics2D JavaDoc;
38 import java.awt.Dimension JavaDoc;
39 import java.util.Map JavaDoc;
40 import de.progra.charting.model.ChartDataModel;
41
42 /**
43  * This Panel provides the possibility to include a Chart into a Swing
44  * Application. I choose not to make every Chart extend JComponent because
45  * of the overhead this would have meant. Instead, this class is an adaptor.
46  * It implements the Chart interface and contains a DefaultChart instance
47  * to which all Chart calls are promoted.
48  * @author mueller
49  */

50 public class ChartPanel extends JPanel JavaDoc implements Chart {
51
52     /** The chart instance to which all method calls are promoted.*/
53     DefaultChart chart;
54     
55     /** Creates new ChartPanel */
56     private ChartPanel() {
57     }
58     
59     /** Creates a new ChartPanel with the given model
60      * and title string.
61      * @param model the ChartDataModel
62      * @param title the title String
63      */

64     public ChartPanel(ChartDataModel model, String JavaDoc title) {
65         this();
66         chart = new DefaultChart(model, title);
67     }
68
69     /** Creates a new ChartPanel with the given model
70      * and title string and a coordinate system.
71      * @param model the ChartDataModel
72      * @param title the title String
73      * @param coord the id of the coordinate system configuration
74      */

75     public ChartPanel(ChartDataModel model, String JavaDoc title, int coord) {
76         this();
77         chart = new DefaultChart(model, title, coord);
78     }
79
80     /** This method is write-protected by the IDE but isn't used at all.
81      */

82     private void initComponents() {//GEN-BEGIN:initComponents
83

84         setLayout(new java.awt.BorderLayout JavaDoc());
85         
86     }//GEN-END:initComponents
87

88     /** Adds a ChartRenderer with a specific z-coordinate.
89      * @param renderer the ChartRenderer
90      * @param z its z-coordinate.
91      */

92     public void addChartRenderer(AbstractChartRenderer renderer, int z) {
93         chart.addChartRenderer(renderer, z);
94     }
95
96     /** Returns the Bounds for the ChartPanel.
97      * @return the bounds
98      */

99     public Rectangle JavaDoc getBounds() {
100         return chart.getBounds();
101     }
102
103     /** Returns the ChartDataModel.
104      * @return the ChartDataModel
105      */

106     public ChartDataModel getChartDataModel() {
107         return chart.getChartDataModel();
108     }
109     
110     /** Returns the Map of all ChartRenderers.
111      * @return the Map of Renderers.
112      */

113     public Map JavaDoc getChartRenderer() {
114         return chart.getChartRenderer();
115     }
116     
117     /** Returns the ChartRenderer with a specific z-coordinate.
118      * @param z the z-coordinate of the desired ChartRenderer.
119      * @return the ChartRenderer or <CODE>null</CODE> if none has been found.
120      */

121     public AbstractChartRenderer getChartRenderer(int z) {
122         return chart.getChartRenderer(z);
123     }
124     
125     /** Returns the coordinate system.
126      * @return the Coordinate System for the Chart. Could be <CODE>null</CODE>.
127      */

128     public CoordSystem getCoordSystem() {
129         return chart.getCoordSystem();
130     }
131     
132     /** Returns this chart's legend.
133      * @return the Legend for this Chart. Could be <CODE>null</CODE>.
134      */

135     public Legend getLegend() {
136         return chart.getLegend();
137     }
138     
139     /** Returns the title for this chart.
140      * @return this Chart's Title. Could be <CODE>null</CODE>.
141      */

142     public Title getTitle() {
143         return chart.getTitle();
144     }
145     
146     /** Sets the Bounds for this Chart.
147      * @param r the <CODE>Rectangle</CODE> object defining the bounds
148      */

149     public void setBounds(Rectangle JavaDoc r) {
150         chart.setBounds(r);
151     }
152     
153     /** Stores the ChartDataModel for this Chart.
154      * @param model the ChartDataModel
155      */

156     public void setChartDataModel(ChartDataModel model) {
157         chart.setChartDataModel(model);
158     }
159     
160     /** Sets the Map with all ChartRenderers. The keys
161      * have to be the z-coordinates of the ChartRenderers.
162      * @param renderer The Map of ChartRenderers.
163      */

164     public void setChartRenderer(Map JavaDoc renderer) {
165         chart.setChartRenderer(renderer);
166     }
167     
168     /** Sets the coordinate system for this chart,
169      * which can be null if the ChartRenderer
170      * doesn't need a coordinate system, e.g. if it's a
171      * PieChart.
172      * @param c The Coordinate System for the Chart.
173      */

174     public void setCoordSystem(CoordSystem c) {
175         chart.setCoordSystem(c);
176     }
177     
178     /** Sets the legend for this chart.
179      * @param l The Legend this Chart contains.
180      */

181     public void setLegend(Legend l) {
182         chart.setLegend(l);
183     }
184     
185     /** Sets the title for this chart.
186      * @param t This Chart's Title.
187      */

188     public void setTitle(Title t) {
189         chart.setTitle(t);
190     }
191     
192     /** Computes the preferred size of the ChartPanel.
193      * @return <code>new java.awt.Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE)</code>
194      */

195     public Dimension JavaDoc getPreferredSize() {
196         return new java.awt.Dimension JavaDoc(Integer.MAX_VALUE, Integer.MAX_VALUE);
197     }
198     
199     /** Paints the ChartPanel. Calls <code>chart.render((Graphics2D)graphics)</code>
200      * @param graphics the Graphics2D object to paint in
201      */

202     public void paint(Graphics JavaDoc graphics) {
203         chart.setBounds(new Rectangle JavaDoc(this.getWidth(), this.getHeight()));
204         chart.render((Graphics2D JavaDoc)graphics);
205     }
206     
207     /** Does the layout of the title, legend and coordinate system and
208      * calls the render method of all those including the ChartRenderers.
209      * @param g the <CODE>Graphics2D</CODE> object to paint in.
210      * Just calls paint(Graphics).
211      */

212     public void render(Graphics2D JavaDoc g) {
213         paint(g);
214     }
215     
216     // Variables declaration - do not modify//GEN-BEGIN:variables
217
// End of variables declaration//GEN-END:variables
218

219     
220 }
221
Popular Tags