001: /*
002: JOpenChart Java Charting Library and Toolkit
003: Copyright (C) 2001 Sebastian Müller
004: http://jopenchart.sourceforge.net
005:
006: This library is free software; you can redistribute it and/or
007: modify it under the terms of the GNU Lesser General Public
008: License as published by the Free Software Foundation; either
009: version 2.1 of the License, or (at your option) any later version.
010:
011: This library is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public
017: License along with this library; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019:
020: ChartPanel.java
021: Created on 6. September 2001, 14:10
022: */
023:
024: package de.progra.charting.swing;
025:
026: import javax.swing.JPanel;
027: import de.progra.charting.render.AbstractChartRenderer;
028: import de.progra.charting.event.*;
029: import de.progra.charting.Chart;
030: import de.progra.charting.Legend;
031: import de.progra.charting.Title;
032: import de.progra.charting.CoordSystem;
033: import de.progra.charting.DefaultChart;
034: import java.awt.geom.Rectangle2D;
035: import java.awt.Rectangle;
036: import java.awt.Graphics;
037: import java.awt.Graphics2D;
038: import java.awt.Dimension;
039: import java.util.Map;
040: import de.progra.charting.model.ChartDataModel;
041:
042: /**
043: * This Panel provides the possibility to include a Chart into a Swing
044: * Application. I choose not to make every Chart extend JComponent because
045: * of the overhead this would have meant. Instead, this class is an adaptor.
046: * It implements the Chart interface and contains a DefaultChart instance
047: * to which all Chart calls are promoted.
048: * @author mueller
049: */
050: public class ChartPanel extends JPanel implements Chart {
051:
052: /** The chart instance to which all method calls are promoted.*/
053: DefaultChart chart;
054:
055: /** Creates new ChartPanel */
056: private ChartPanel() {
057: }
058:
059: /** Creates a new ChartPanel with the given model
060: * and title string.
061: * @param model the ChartDataModel
062: * @param title the title String
063: */
064: public ChartPanel(ChartDataModel model, String title) {
065: this ();
066: chart = new DefaultChart(model, title);
067: }
068:
069: /** Creates a new ChartPanel with the given model
070: * and title string and a coordinate system.
071: * @param model the ChartDataModel
072: * @param title the title String
073: * @param coord the id of the coordinate system configuration
074: */
075: public ChartPanel(ChartDataModel model, String title, int coord) {
076: this ();
077: chart = new DefaultChart(model, title, coord);
078: }
079:
080: /** This method is write-protected by the IDE but isn't used at all.
081: */
082: private void initComponents() {//GEN-BEGIN:initComponents
083:
084: setLayout(new java.awt.BorderLayout());
085:
086: }//GEN-END:initComponents
087:
088: /** Adds a ChartRenderer with a specific z-coordinate.
089: * @param renderer the ChartRenderer
090: * @param z its z-coordinate.
091: */
092: public void addChartRenderer(AbstractChartRenderer renderer, int z) {
093: chart.addChartRenderer(renderer, z);
094: }
095:
096: /** Returns the Bounds for the ChartPanel.
097: * @return the bounds
098: */
099: public Rectangle 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 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 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 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 getPreferredSize() {
196: return new java.awt.Dimension(Integer.MAX_VALUE,
197: Integer.MAX_VALUE);
198: }
199:
200: /** Paints the ChartPanel. Calls <code>chart.render((Graphics2D)graphics)</code>
201: * @param graphics the Graphics2D object to paint in
202: */
203: public void paint(Graphics graphics) {
204: chart
205: .setBounds(new Rectangle(this .getWidth(), this
206: .getHeight()));
207: chart.render((Graphics2D) graphics);
208: }
209:
210: /** Does the layout of the title, legend and coordinate system and
211: * calls the render method of all those including the ChartRenderers.
212: * @param g the <CODE>Graphics2D</CODE> object to paint in.
213: * Just calls paint(Graphics).
214: */
215: public void render(Graphics2D g) {
216: paint(g);
217: }
218:
219: // Variables declaration - do not modify//GEN-BEGIN:variables
220: // End of variables declaration//GEN-END:variables
221:
222: }
|