KickJava   Java API By Example, From Geeks To Geeks.

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


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  * StackedAreaXYChartDemo.java
24  * ---------------------------
25  * (C) Copyright 2003 by Richard Atkinson.
26  *
27  * Original Author: Richard Atkinson;
28  * Contributor(s): -;
29  *
30  * $Id: StackedAreaXYChartDemo.java,v 1.6 2003/11/28 10:57:33 mungady Exp $
31  *
32  * Changes:
33  * --------
34  * 27-Jul-2003 : Initial version (RA);
35  * 22-Sep-2003 : Updated to use new DefaultTableXYDataset (RA);
36  *
37  */

38 package org.jfree.chart.demo;
39
40 import java.awt.BasicStroke JavaDoc;
41 import java.awt.Color JavaDoc;
42 import java.awt.geom.Ellipse2D JavaDoc;
43 import java.text.NumberFormat JavaDoc;
44 import java.text.ParseException JavaDoc;
45 import java.text.SimpleDateFormat JavaDoc;
46 import java.util.Locale JavaDoc;
47
48 import org.jfree.chart.ChartPanel;
49 import org.jfree.chart.JFreeChart;
50 import org.jfree.chart.axis.DateAxis;
51 import org.jfree.chart.axis.NumberAxis;
52 import org.jfree.chart.labels.TimeSeriesToolTipGenerator;
53 import org.jfree.chart.plot.XYPlot;
54 import org.jfree.chart.renderer.AreaXYRenderer;
55 import org.jfree.chart.renderer.StackedAreaXYRenderer;
56 import org.jfree.data.DefaultTableXYDataset;
57 import org.jfree.data.TableXYDataset;
58 import org.jfree.data.XYSeries;
59 import org.jfree.ui.ApplicationFrame;
60 import org.jfree.ui.RefineryUtilities;
61
62 /**
63  * A simple demonstration application showing how to create a
64  * stacked area XY chart.
65  *
66  */

67 public class StackedAreaXYChartDemo extends ApplicationFrame {
68
69     /**
70      * Creates a new demo.
71      *
72      * @param title the frame title.
73      */

74     public StackedAreaXYChartDemo(String JavaDoc title) {
75
76         super(title);
77
78         SimpleDateFormat JavaDoc sdf = new SimpleDateFormat JavaDoc("dd-MMM-yyyy", Locale.UK);
79         XYSeries series1 = new XYSeries("Series 1", false);
80         XYSeries series2 = new XYSeries("Series 2", false);
81         XYSeries series3 = new XYSeries("Series 3", false);
82         try {
83             series1.add(sdf.parse("03-Jul-2003").getTime(), 115);
84             series1.add(sdf.parse("04-Jul-2003").getTime(), 120);
85             series1.add(sdf.parse("07-Jul-2003").getTime(), 125);
86             series1.add(sdf.parse("08-Jul-2003").getTime(), 160);
87             series1.add(sdf.parse("09-Jul-2003").getTime(), 175);
88             series1.add(sdf.parse("10-Jul-2003").getTime(), 140);
89             series1.add(sdf.parse("11-Jul-2003").getTime(), 145);
90             series1.add(sdf.parse("14-Jul-2003").getTime(), 150);
91             series1.add(sdf.parse("15-Jul-2003").getTime(), 155);
92             series1.add(sdf.parse("16-Jul-2003").getTime(), 160);
93             series1.add(sdf.parse("17-Jul-2003").getTime(), 165);
94             series1.add(sdf.parse("18-Jul-2003").getTime(), 170);
95
96             series2.add(sdf.parse("30-Jun-2003").getTime(), 50);
97             series2.add(sdf.parse("01-Jul-2003").getTime(), 60);
98             series2.add(sdf.parse("02-Jul-2003").getTime(), 70);
99             series2.add(sdf.parse("03-Jul-2003").getTime(), 80);
100             series2.add(sdf.parse("04-Jul-2003").getTime(), 90);
101             series2.add(sdf.parse("07-Jul-2003").getTime(), 100);
102             series2.add(sdf.parse("08-Jul-2003").getTime(), 110);
103             series2.add(sdf.parse("09-Jul-2003").getTime(), 120);
104             series2.add(sdf.parse("10-Jul-2003").getTime(), 130);
105             series2.add(sdf.parse("11-Jul-2003").getTime(), 140);
106             series2.add(sdf.parse("14-Jul-2003").getTime(), 150);
107             series2.add(sdf.parse("15-Jul-2003").getTime(), 160);
108             series2.add(sdf.parse("16-Jul-2003").getTime(), 170);
109             series2.add(sdf.parse("17-Jul-2003").getTime(), 180);
110             series2.add(sdf.parse("18-Jul-2003").getTime(), 190);
111
112             series3.add(sdf.parse("30-Jun-2003").getTime(), 100);
113             series3.add(sdf.parse("01-Jul-2003").getTime(), 120);
114             series3.add(sdf.parse("02-Jul-2003").getTime(), 110);
115             series3.add(sdf.parse("03-Jul-2003").getTime(), 120);
116             series3.add(sdf.parse("04-Jul-2003").getTime(), 130);
117             series3.add(sdf.parse("07-Jul-2003").getTime(), 135);
118             series3.add(sdf.parse("08-Jul-2003").getTime(), 140);
119             series3.add(sdf.parse("09-Jul-2003").getTime(), 155);
120             series3.add(sdf.parse("10-Jul-2003").getTime(), 130);
121             series3.add(sdf.parse("11-Jul-2003").getTime(), 135);
122             series3.add(sdf.parse("14-Jul-2003").getTime(), 140);
123             series3.add(sdf.parse("15-Jul-2003").getTime(), 165);
124             series3.add(sdf.parse("16-Jul-2003").getTime(), 170);
125             series3.add(sdf.parse("17-Jul-2003").getTime(), 165);
126             series3.add(sdf.parse("18-Jul-2003").getTime(), 140);
127         } catch (ParseException JavaDoc e) {
128             // Not likely, given that the dates are hard-coded
129
e.printStackTrace();
130         }
131         DefaultTableXYDataset dataset = new DefaultTableXYDataset(series1);
132         dataset.addSeries(series2);
133         dataset.addSeries(series3);
134
135         JFreeChart chart = createChart(dataset);
136
137         ChartPanel chartPanel = new ChartPanel(chart);
138         chartPanel.setPreferredSize(new java.awt.Dimension JavaDoc(500, 270));
139         setContentPane(chartPanel);
140
141     }
142
143     // ****************************************************************************
144
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
145
// * Please note that commercial support and documentation is available from: *
146
// * *
147
// * http://www.object-refinery.com/jfreechart/support.html *
148
// * *
149
// * This is not only a great service for developers, but is a VERY IMPORTANT *
150
// * source of funding for the JFreeChart project. Please support us so that *
151
// * we can continue developing free software. *
152
// ****************************************************************************
153

154     /**
155      * Creates a chart.
156      *
157      * @param dataset the dataset.
158      *
159      * @return A chart.
160      */

161     private JFreeChart createChart(TableXYDataset dataset) {
162
163         SimpleDateFormat JavaDoc sdf = new SimpleDateFormat JavaDoc("dd-MMM-yyyy", Locale.UK);
164         TimeSeriesToolTipGenerator ttg = new TimeSeriesToolTipGenerator(
165                 sdf, NumberFormat.getInstance());
166         DateAxis xAxis = new DateAxis("Domain (X)");
167         xAxis.setLowerMargin(0.0);
168         xAxis.setUpperMargin(0.0);
169
170         NumberAxis yAxis = new NumberAxis("Range (Y)");
171         yAxis.setAutoRangeIncludesZero(true);
172         StackedAreaXYRenderer renderer =
173                 new StackedAreaXYRenderer(AreaXYRenderer.AREA_AND_SHAPES, ttg, null);
174         renderer.setOutline(true);
175         renderer.setSeriesPaint(0, new Color JavaDoc(255, 255, 206));
176         renderer.setSeriesPaint(1, new Color JavaDoc(206, 230, 255));
177         renderer.setSeriesPaint(2, new Color JavaDoc(255, 230, 230));
178         renderer.setShapePaint(Color.gray);
179         renderer.setShapeStroke(new BasicStroke JavaDoc(0.5f));
180         renderer.setShape(new Ellipse2D.Double JavaDoc(-3, -3, 6, 6));
181         XYPlot plot = new XYPlot(dataset, xAxis, yAxis, renderer);
182
183         // Reconfigure Y-Axis so the auto-range knows that the data is stacked
184
yAxis.configure();
185
186         JFreeChart chart = new JFreeChart(null, JFreeChart.DEFAULT_TITLE_FONT, plot, true);
187
188         return chart;
189     }
190
191     /**
192      * Starting point for the demonstration application.
193      *
194      * @param args ignored.
195      */

196     public static void main(String JavaDoc[] args) {
197         StackedAreaXYChartDemo demo = new StackedAreaXYChartDemo("Stacked Area XY Chart Demo");
198         demo.pack();
199         RefineryUtilities.centerFrameOnScreen(demo);
200         demo.setVisible(true);
201     }
202
203 }
204
Popular Tags