KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > demo > XYStepAreaChartDemo


1 /* ======================================
2  * JFreeChart : a free Java chart library
3  * ======================================
4  *
5  * Project Info: http://www.jfree.org/jfreechart/index.html
6  * Project Lead: David Gilbert (david.gilbert@object-refinery.com);
7  *
8  * (C) Copyright 2000-2003, by Object Refinery Limited and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it under the terms
11  * of the GNU Lesser General Public License as published by the Free Software Foundation;
12  * either version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License along with this
19  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  * ------------------------
23  * XYStepAreaChartDemo.java
24  * ------------------------
25  * (C) Copyright 2003 by Object Refinery Limited and Contributors.
26  *
27  * Original Author: Matthias Rose (for Ablay & Fodi GmbH);
28  * Contributor(s): David Gilbert (for Object Refinery Limited);
29  *
30  * $Id: XYStepAreaChartDemo.java,v 1.6 2003/11/28 10:57:36 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 26-Sep-2003 : Copied XYAreaChartDemo --> XYStepAreaChartDemo
35  * and adapted to test XYStepAreaRenderer (MR);
36  *
37  */

38
39 package org.jfree.chart.demo;
40
41 import java.awt.BasicStroke JavaDoc;
42 import java.awt.BorderLayout JavaDoc;
43 import java.awt.Color JavaDoc;
44 import java.awt.event.ActionEvent JavaDoc;
45 import java.awt.event.ActionListener JavaDoc;
46
47 import javax.swing.JCheckBox JavaDoc;
48 import javax.swing.JComboBox JavaDoc;
49 import javax.swing.JLabel JavaDoc;
50 import javax.swing.JPanel JavaDoc;
51 import javax.swing.JTextField JavaDoc;
52
53 import org.jfree.chart.ChartFactory;
54 import org.jfree.chart.ChartPanel;
55 import org.jfree.chart.JFreeChart;
56 import org.jfree.chart.plot.Plot;
57 import org.jfree.chart.plot.PlotOrientation;
58 import org.jfree.chart.plot.XYPlot;
59 import org.jfree.chart.renderer.XYStepAreaRenderer;
60 import org.jfree.data.XYDataset;
61 import org.jfree.data.XYSeries;
62 import org.jfree.data.XYSeriesCollection;
63 import org.jfree.ui.ApplicationFrame;
64 import org.jfree.ui.RefineryUtilities;
65
66 /**
67  * A simple demonstration application showing how to create a step area chart.
68  *
69  * @author Matthias Rose
70  */

71 public class XYStepAreaChartDemo extends ApplicationFrame implements ActionListener JavaDoc {
72
73     /** Vertical orientation. */
74     private static final String JavaDoc ORIENT_VERT = "Plot vertical";
75     
76     /** Horizontal orientation. */
77     private static final String JavaDoc ORIENT_HORIZ = "Plot horizontal";
78
79     /** Test data. */
80     private static final Object JavaDoc[][] TEST_DATA = {
81         // domain values, range values, may be null?
82
{new Integer JavaDoc(1), new Integer JavaDoc(500), Boolean.TRUE},
83         {new Integer JavaDoc(2), new Integer JavaDoc(694)},
84         {new Integer JavaDoc(3), new Integer JavaDoc(-734)},
85         {new Integer JavaDoc(4), new Integer JavaDoc(453)},
86         {new Integer JavaDoc(5), new Integer JavaDoc(500), Boolean.TRUE},
87         {new Integer JavaDoc(6), new Integer JavaDoc(200)},
88         {new Integer JavaDoc(7), new Integer JavaDoc(550), Boolean.TRUE},
89         {new Integer JavaDoc(8), new Integer JavaDoc(-150), Boolean.TRUE},
90         {new Integer JavaDoc(9), new Integer JavaDoc(232)},
91         {new Integer JavaDoc(10), new Integer JavaDoc(734)},
92         {new Integer JavaDoc(11), new Integer JavaDoc(400), Boolean.TRUE},
93     };
94
95     /** The chart panel. */
96     private ChartPanel chartPanel;
97     
98     /** The data series. */
99     private XYSeries xySeries;
100     
101     /** The null values checkbox. */
102     private JCheckBox JavaDoc nullValuesCheckBox;
103
104     /** The outline checkbox. */
105     private JCheckBox JavaDoc outlineCheckBox;
106     
107     /** The range base text field. */
108     private JTextField JavaDoc rangeBaseTextField;
109     
110     /** The orientation combobox. */
111     private JComboBox JavaDoc orientationComboBox;
112     
113     /**
114      * Creates a new demo.
115      *
116      * @param title the frame title.
117      */

118     public XYStepAreaChartDemo(String JavaDoc title) {
119
120         super(title);
121
122         xySeries = new XYSeries("Some data");
123         for (int i = 0; i < TEST_DATA.length; i++) {
124             xySeries.add((Integer JavaDoc) TEST_DATA[i][0], (Integer JavaDoc) TEST_DATA[i][1]);
125         }
126
127         XYSeriesCollection dataset = new XYSeriesCollection(xySeries);
128         
129         JFreeChart chart = createChart(dataset);
130         
131         chartPanel = new ChartPanel(chart);
132
133         // allow zooming
134
chartPanel.setHorizontalZoom(true);
135         chartPanel.setVerticalZoom(true);
136         
137         // size
138
chartPanel.setPreferredSize(new java.awt.Dimension JavaDoc(500, 270));
139
140         // make stroke more striking
141
Plot plot = chartPanel.getChart().getPlot();
142         plot.setOutlineStroke(new BasicStroke JavaDoc(2));
143         plot.setOutlinePaint(Color.magenta);
144
145         // add some components to make options changable
146
JPanel JavaDoc main = new JPanel JavaDoc(new BorderLayout JavaDoc());
147         JPanel JavaDoc optionsPanel = new JPanel JavaDoc();
148         
149         String JavaDoc[] options = {ORIENT_VERT, ORIENT_HORIZ};
150         orientationComboBox = new JComboBox JavaDoc(options);
151         orientationComboBox.addActionListener(this);
152         optionsPanel.add(orientationComboBox);
153
154         outlineCheckBox = new JCheckBox JavaDoc("Outline");
155         outlineCheckBox.addActionListener(this);
156         optionsPanel.add(outlineCheckBox);
157
158         optionsPanel.add(new JLabel JavaDoc("Base"));
159         rangeBaseTextField = new JTextField JavaDoc("0", 5);
160         rangeBaseTextField.addActionListener(this);
161         optionsPanel.add(rangeBaseTextField);
162
163         nullValuesCheckBox = new JCheckBox JavaDoc("NULL values");
164         nullValuesCheckBox.addActionListener(this);
165         optionsPanel.add(nullValuesCheckBox);
166
167         main.add(optionsPanel, BorderLayout.SOUTH);
168         main.add(chartPanel);
169         setContentPane(main);
170     }
171
172     // ****************************************************************************
173
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
174
// * Please note that commercial support and documentation is available from: *
175
// * *
176
// * http://www.object-refinery.com/jfreechart/support.html *
177
// * *
178
// * This is not only a great service for developers, but is a VERY IMPORTANT *
179
// * source of funding for the JFreeChart project. Please support us so that *
180
// * we can continue developing free software. *
181
// ****************************************************************************
182

183     /**
184      * Creates a chart.
185      *
186      * @param dataset the dataset.
187      *
188      * @return A chart.
189      */

190     private JFreeChart createChart(XYDataset dataset) {
191             
192         JFreeChart chart = ChartFactory.createXYStepAreaChart(
193             "XY Step Area Chart Demo",
194             "Domain (X)", "Range (Y)",
195             dataset,
196             PlotOrientation.VERTICAL,
197             true, // legend
198
true, // tool tips
199
false // URLs
200
);
201         
202         // color
203
XYPlot plot = chart.getXYPlot();
204         plot.getRenderer().setSeriesPaint(0, Color.green);
205
206         // fill shapes
207
XYStepAreaRenderer rend = (XYStepAreaRenderer) plot.getRenderer();
208         rend.setShapesFilled(true);
209
210         return chart;
211     }
212
213     /**
214      * Change options according to settings.
215      *
216      * @param evt the event.
217      */

218     public void actionPerformed(ActionEvent JavaDoc evt) {
219
220         Object JavaDoc source = evt.getSource();
221
222         if (source == nullValuesCheckBox) {
223             
224             boolean withNulls = nullValuesCheckBox.isSelected();
225             for (int i = 0; i < TEST_DATA.length; i++) {
226                 Integer JavaDoc yVal = (Integer JavaDoc) TEST_DATA[i][1];
227                 if (withNulls && TEST_DATA[i].length > 2) {
228                     yVal = null;
229                 }
230                 xySeries.getDataItem(i).setY(yVal);
231             }
232             
233         }
234         else if (source == outlineCheckBox) {
235
236             XYPlot plot = (XYPlot) chartPanel.getChart().getPlot();
237             ((XYStepAreaRenderer) plot.getRenderer()).setOutline(outlineCheckBox.isSelected());
238             
239         }
240         else if (source == rangeBaseTextField) {
241             
242             double val = Double.parseDouble(rangeBaseTextField.getText());
243             XYPlot plot = (XYPlot) chartPanel.getChart().getPlot();
244             XYStepAreaRenderer rend = (XYStepAreaRenderer) plot.getRenderer();
245             rend.setRangeBase(val);
246
247         }
248         else if (source == orientationComboBox) {
249
250             XYPlot plot = (XYPlot) chartPanel.getChart().getPlot();
251             if (orientationComboBox.getSelectedItem() == ORIENT_HORIZ) {
252                 plot.setOrientation(PlotOrientation.HORIZONTAL);
253             }
254             else if (orientationComboBox.getSelectedItem() == ORIENT_VERT) {
255                 plot.setOrientation(PlotOrientation.VERTICAL);
256             }
257         }
258
259         chartPanel.repaint();
260     }
261
262     /**
263      * Starting point for the demonstration application.
264      *
265      * @param args ignored.
266      */

267     public static void main(String JavaDoc[] args) {
268         XYStepAreaChartDemo demo = new XYStepAreaChartDemo("Step Area XY Chart Demo");
269         demo.pack();
270         RefineryUtilities.centerFrameOnScreen(demo);
271         demo.setVisible(true);
272     }
273 }
274
Popular Tags