KickJava   Java API By Example, From Geeks To Geeks.

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


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  * LineChartDemo1.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: LineChartDemo1.java,v 1.17 2003/11/28 10:57:36 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 08-Apr-2002 : Version 1 (DG);
35  * 30-May-2002 : Modified to display values on the chart (DG);
36  * 25-Jun-2002 : Removed redundant import (DG);
37  * 11-Oct-2002 : Fixed errors reported by Checkstyle (DG);
38  *
39  */

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

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

73     public LineChartDemo1(String JavaDoc title) {
74
75         super(title);
76
77         CategoryDataset dataset = createDataset();
78         JFreeChart chart = createChart(dataset);
79         
80         // add the chart to a panel...
81
ChartPanel chartPanel = new ChartPanel(chart);
82         chartPanel.setPreferredSize(new java.awt.Dimension JavaDoc(500, 270));
83         setContentPane(chartPanel);
84
85     }
86
87     /**
88      * Creates a sample dataset.
89      *
90      * @return The dataset.
91      */

92     private CategoryDataset createDataset() {
93         
94         // row keys...
95
String JavaDoc series1 = "First";
96         String JavaDoc series2 = "Second";
97         String JavaDoc series3 = "Third";
98
99         // column keys...
100
String JavaDoc type1 = "Type 1";
101         String JavaDoc type2 = "Type 2";
102         String JavaDoc type3 = "Type 3";
103         String JavaDoc type4 = "Type 4";
104         String JavaDoc type5 = "Type 5";
105         String JavaDoc type6 = "Type 6";
106         String JavaDoc type7 = "Type 7";
107         String JavaDoc type8 = "Type 8";
108
109         // create the dataset...
110
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
111
112         dataset.addValue(1.0, series1, type1);
113         dataset.addValue(4.0, series1, type2);
114         dataset.addValue(3.0, series1, type3);
115         dataset.addValue(5.0, series1, type4);
116         dataset.addValue(5.0, series1, type5);
117         dataset.addValue(7.0, series1, type6);
118         dataset.addValue(7.0, series1, type7);
119         dataset.addValue(8.0, series1, type8);
120
121         dataset.addValue(5.0, series2, type1);
122         dataset.addValue(7.0, series2, type2);
123         dataset.addValue(6.0, series2, type3);
124         dataset.addValue(8.0, series2, type4);
125         dataset.addValue(4.0, series2, type5);
126         dataset.addValue(4.0, series2, type6);
127         dataset.addValue(2.0, series2, type7);
128         dataset.addValue(1.0, series2, type8);
129
130         dataset.addValue(4.0, series3, type1);
131         dataset.addValue(3.0, series3, type2);
132         dataset.addValue(2.0, series3, type3);
133         dataset.addValue(3.0, series3, type4);
134         dataset.addValue(6.0, series3, type5);
135         dataset.addValue(3.0, series3, type6);
136         dataset.addValue(4.0, series3, type7);
137         dataset.addValue(3.0, series3, type8);
138
139         return dataset;
140                 
141     }
142     
143     /**
144      * Creates a sample chart.
145      *
146      * @param dataset a dataset.
147      *
148      * @return The chart.
149      */

150     private JFreeChart createChart(CategoryDataset dataset) {
151         
152         // create the chart...
153
JFreeChart chart = ChartFactory.createLineChart(
154             "Line Chart Demo 1", // chart title
155
"Type", // domain axis label
156
"Value", // range axis label
157
dataset, // data
158
PlotOrientation.VERTICAL, // orientation
159
true, // include legend
160
true, // tooltips
161
false // urls
162
);
163
164         // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
165
StandardLegend legend = (StandardLegend) chart.getLegend();
166         legend.setDisplaySeriesShapes(true);
167         legend.setDisplaySeriesLines(true);
168
169         chart.setBackgroundPaint(new Color JavaDoc(0xCC, 0xCC, 0xFF));
170
171         CategoryPlot plot = chart.getCategoryPlot();
172
173         // customise the range axis...
174
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
175         rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
176         rangeAxis.setAutoRangeIncludesZero(true);
177         rangeAxis.setUpperMargin(0.20);
178         rangeAxis.setLabelAngle(Math.PI / 2.0);
179
180         // ****************************************************************************
181
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
182
// * Please note that commercial support and documentation is available from: *
183
// * *
184
// * http://www.object-refinery.com/jfreechart/support.html *
185
// * *
186
// * This is not only a great service for developers, but is a VERY IMPORTANT *
187
// * source of funding for the JFreeChart project. Please support us so that *
188
// * we can continue developing free software. *
189
// ****************************************************************************
190

191         // customise the renderer...
192
LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
193         renderer.setDrawShapes(true);
194
195         renderer.setSeriesStroke(
196             0, new BasicStroke JavaDoc(2.0f,
197                                BasicStroke.CAP_ROUND,
198                                BasicStroke.JOIN_ROUND,
199                                1.0f,
200                                new float[] {10.0f, 6.0f},
201                                0.0f)
202         );
203         renderer.setSeriesStroke(
204             1, new BasicStroke JavaDoc(2.0f,
205                                BasicStroke.CAP_ROUND,
206                                BasicStroke.JOIN_ROUND,
207                                1.0f,
208                                new float[] {6.0f, 6.0f},
209                                0.0f)
210         );
211         renderer.setSeriesStroke(
212             2, new BasicStroke JavaDoc(2.0f,
213                                BasicStroke.CAP_ROUND,
214                                BasicStroke.JOIN_ROUND,
215                                1.0f,
216                                new float[] {2.0f, 6.0f},
217                                0.0f)
218         );
219
220         renderer.setItemLabelsVisible(true);
221         renderer.setPositiveItemLabelPosition(new ItemLabelPosition());
222         renderer.setNegativeItemLabelPosition(new ItemLabelPosition());
223         // OPTIONAL CUSTOMISATION COMPLETED.
224

225         return chart;
226     }
227     
228     /**
229      * Starting point for the demonstration application.
230      *
231      * @param args ignored.
232      */

233     public static void main(String JavaDoc[] args) {
234
235         LineChartDemo1 demo = new LineChartDemo1("Line Chart Demo");
236         demo.pack();
237         RefineryUtilities.centerFrameOnScreen(demo);
238         demo.setVisible(true);
239
240     }
241
242 }
243
Popular Tags