KickJava   Java API By Example, From Geeks To Geeks.

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


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  * LineChartDemo2.java
24  * -------------------
25  * (C) Copyright 2002, 2003, by Object Refinery Limited and Contributors.
26  *
27  * Original Author: David Gilbert (for Object Refinery Limited);
28  * Contributor(s): -;
29  *
30  * $Id: LineChartDemo2.java,v 1.10 2003/11/28 10:57:35 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 08-Apr-2002 : Version 1 (DG);
35  * 10-Nov-2003 : Added axis offsets and changed colors (DG);
36  *
37  */

38
39 package org.jfree.chart.demo;
40
41 import java.awt.Color JavaDoc;
42
43 import org.jfree.chart.ChartFactory;
44 import org.jfree.chart.ChartPanel;
45 import org.jfree.chart.JFreeChart;
46 import org.jfree.chart.Spacer;
47 import org.jfree.chart.StandardLegend;
48 import org.jfree.chart.axis.NumberAxis;
49 import org.jfree.chart.plot.PlotOrientation;
50 import org.jfree.chart.plot.XYPlot;
51 import org.jfree.chart.renderer.StandardXYItemRenderer;
52 import org.jfree.data.XYDataset;
53 import org.jfree.data.XYSeries;
54 import org.jfree.data.XYSeriesCollection;
55 import org.jfree.ui.ApplicationFrame;
56 import org.jfree.ui.RefineryUtilities;
57
58 /**
59  * A simple demonstration application showing how to create a line chart using data from an
60  * {@link XYDataset}.
61  *
62  * @author David Gilbert
63  */

64 public class LineChartDemo2 extends ApplicationFrame {
65
66     /**
67      * Creates a new demo.
68      *
69      * @param title the frame title.
70      */

71     public LineChartDemo2(String JavaDoc title) {
72
73         super(title);
74
75         XYDataset dataset = createDataset();
76         JFreeChart chart = createChart(dataset);
77         ChartPanel chartPanel = new ChartPanel(chart);
78         chartPanel.setPreferredSize(new java.awt.Dimension JavaDoc(500, 270));
79         setContentPane(chartPanel);
80
81     }
82     
83     /**
84      * Creates a sample dataset.
85      *
86      * @return a sample dataset.
87      */

88     private XYDataset createDataset() {
89         
90         XYSeries series1 = new XYSeries("First");
91         series1.add(1.0, 1.0);
92         series1.add(2.0, 4.0);
93         series1.add(3.0, 3.0);
94         series1.add(4.0, 5.0);
95         series1.add(5.0, 5.0);
96         series1.add(6.0, 7.0);
97         series1.add(7.0, 7.0);
98         series1.add(8.0, 8.0);
99
100         XYSeries series2 = new XYSeries("Second");
101         series2.add(1.0, 5.0);
102         series2.add(2.0, 7.0);
103         series2.add(3.0, 6.0);
104         series2.add(4.0, 8.0);
105         series2.add(5.0, 4.0);
106         series2.add(6.0, 4.0);
107         series2.add(7.0, 2.0);
108         series2.add(8.0, 1.0);
109
110         XYSeries series3 = new XYSeries("Third");
111         series3.add(3.0, 4.0);
112         series3.add(4.0, 3.0);
113         series3.add(5.0, 2.0);
114         series3.add(6.0, 3.0);
115         series3.add(7.0, 6.0);
116         series3.add(8.0, 3.0);
117         series3.add(9.0, 4.0);
118         series3.add(10.0, 3.0);
119
120         XYSeriesCollection dataset = new XYSeriesCollection();
121         dataset.addSeries(series1);
122         dataset.addSeries(series2);
123         dataset.addSeries(series3);
124                 
125         return dataset;
126         
127     }
128     
129     /**
130      * Creates a chart.
131      *
132      * @param dataset the data for the chart.
133      *
134      * @return a chart.
135      */

136     private JFreeChart createChart(XYDataset dataset) {
137         
138         // create the chart...
139
JFreeChart chart = ChartFactory.createXYLineChart(
140             "Line Chart Demo 2", // chart title
141
"X", // x axis label
142
"Y", // y axis label
143
dataset, // data
144
PlotOrientation.VERTICAL,
145             true, // include legend
146
true, // tooltips
147
false // urls
148
);
149
150         // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
151
chart.setBackgroundPaint(Color.white);
152
153         StandardLegend legend = (StandardLegend) chart.getLegend();
154         legend.setDisplaySeriesShapes(true);
155         
156         // get a reference to the plot for further customisation...
157
XYPlot plot = chart.getXYPlot();
158         plot.setBackgroundPaint(Color.lightGray);
159         plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));
160         plot.setDomainGridlinePaint(Color.white);
161         plot.setRangeGridlinePaint(Color.white);
162         
163         StandardXYItemRenderer renderer = (StandardXYItemRenderer) plot.getRenderer();
164         renderer.setPlotShapes(true);
165         renderer.setShapesFilled(true);
166
167         // change the auto tick unit selection to integer units only...
168
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
169         rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
170         // OPTIONAL CUSTOMISATION COMPLETED.
171

172         return chart;
173         
174     }
175
176     // ****************************************************************************
177
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
178
// * Please note that commercial support and documentation is available from: *
179
// * *
180
// * http://www.object-refinery.com/jfreechart/support.html *
181
// * *
182
// * This is not only a great service for developers, but is a VERY IMPORTANT *
183
// * source of funding for the JFreeChart project. Please support us so that *
184
// * we can continue developing free software. *
185
// ****************************************************************************
186

187     /**
188      * Starting point for the demonstration application.
189      *
190      * @param args ignored.
191      */

192     public static void main(String JavaDoc[] args) {
193
194         LineChartDemo2 demo = new LineChartDemo2("Line Chart Demo 2");
195         demo.pack();
196         RefineryUtilities.centerFrameOnScreen(demo);
197         demo.setVisible(true);
198
199     }
200
201 }
202
Popular Tags