KickJava   Java API By Example, From Geeks To Geeks.

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


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  * LineChartDemo5.java
24  * -------------------
25  * (C) Copyright 2003 by Object Refinery Limited and Contributors.
26  *
27  * Original Author: David Gilbert (for Object Refinery Limited);
28  * Contributor(s): -;
29  *
30  * $Id: LineChartDemo5.java,v 1.13 2003/11/28 10:57:34 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 23-Apr-2003 : Version 1 (DG);
35  *
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.Polygon JavaDoc;
43 import java.awt.Shape JavaDoc;
44 import java.awt.geom.Rectangle2D 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.DefaultDrawingSupplier;
53 import org.jfree.chart.plot.DrawingSupplier;
54 import org.jfree.chart.plot.PlotOrientation;
55 import org.jfree.chart.renderer.LineAndShapeRenderer;
56 import org.jfree.data.DefaultCategoryDataset;
57 import org.jfree.ui.ApplicationFrame;
58 import org.jfree.ui.RefineryUtilities;
59
60 /**
61  * A line chart demo showing the use of a custom drawing supplier.
62  *
63  * @author David Gilbert
64  */

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

72     public LineChartDemo5(String JavaDoc title) {
73
74         super(title);
75
76         // row keys...
77
String JavaDoc series1 = "First";
78         String JavaDoc series2 = "Second";
79         String JavaDoc series3 = "Third";
80
81         // column keys...
82
String JavaDoc type1 = "Type 1";
83         String JavaDoc type2 = "Type 2";
84         String JavaDoc type3 = "Type 3";
85         String JavaDoc type4 = "Type 4";
86         String JavaDoc type5 = "Type 5";
87         String JavaDoc type6 = "Type 6";
88         String JavaDoc type7 = "Type 7";
89         String JavaDoc type8 = "Type 8";
90
91         // create the dataset...
92
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
93
94         dataset.addValue(1.0, series1, type1);
95         dataset.addValue(4.0, series1, type2);
96         dataset.addValue(3.0, series1, type3);
97         dataset.addValue(5.0, series1, type4);
98         dataset.addValue(5.0, series1, type5);
99         dataset.addValue(7.0, series1, type6);
100         dataset.addValue(7.0, series1, type7);
101         dataset.addValue(8.0, series1, type8);
102
103         dataset.addValue(5.0, series2, type1);
104         dataset.addValue(7.0, series2, type2);
105         dataset.addValue(6.0, series2, type3);
106         dataset.addValue(8.0, series2, type4);
107         dataset.addValue(4.0, series2, type5);
108         dataset.addValue(4.0, series2, type6);
109         dataset.addValue(2.0, series2, type7);
110         dataset.addValue(1.0, series2, type8);
111
112         dataset.addValue(4.0, series3, type1);
113         dataset.addValue(3.0, series3, type2);
114         dataset.addValue(2.0, series3, type3);
115         dataset.addValue(3.0, series3, type4);
116         dataset.addValue(6.0, series3, type5);
117         dataset.addValue(3.0, series3, type6);
118         dataset.addValue(4.0, series3, type7);
119         dataset.addValue(3.0, series3, type8);
120
121         // create the chart...
122
JFreeChart chart = ChartFactory.createLineChart(
123             "Line Chart Demo 5", // chart title
124
"Type", // domain axis label
125
"Value", // range axis label
126
dataset, // data
127
PlotOrientation.VERTICAL, // orientation
128
true, // include legend
129
true, // tooltips
130
false // urls
131
);
132
133         // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
134
StandardLegend legend = (StandardLegend) chart.getLegend();
135         legend.setDisplaySeriesShapes(true);
136
137         Shape JavaDoc[] shapes = new Shape JavaDoc[3];
138         int[] xpoints;
139         int[] ypoints;
140
141         // right-pointing triangle
142
xpoints = new int[] {-3, 3, -3};
143         ypoints = new int[] {-3, 0, 3};
144         shapes[0] = new Polygon JavaDoc(xpoints, ypoints, 3);
145
146         // vertical rectangle
147
shapes[1] = new Rectangle2D.Double JavaDoc(-2, -3, 3, 6);
148
149         // left-pointing triangle
150
xpoints = new int[] {-3, 3, 3};
151         ypoints = new int[] {0, -3, 3};
152         shapes[2] = new Polygon JavaDoc(xpoints, ypoints, 3);
153
154         DrawingSupplier supplier = new DefaultDrawingSupplier(
155             DefaultDrawingSupplier.DEFAULT_PAINT_SEQUENCE,
156             DefaultDrawingSupplier.DEFAULT_OUTLINE_PAINT_SEQUENCE,
157             DefaultDrawingSupplier.DEFAULT_STROKE_SEQUENCE,
158             DefaultDrawingSupplier.DEFAULT_OUTLINE_STROKE_SEQUENCE,
159             shapes
160         );
161         CategoryPlot plot = chart.getCategoryPlot();
162         plot.setDrawingSupplier(supplier);
163
164         chart.setBackgroundPaint(Color.yellow);
165
166         // set the stroke for each series...
167
plot.getRenderer().setSeriesStroke(0, new BasicStroke JavaDoc(2.0f,
168                                                               BasicStroke.CAP_ROUND,
169                                                               BasicStroke.JOIN_ROUND,
170                                                               1.0f,
171                                                               new float[] {10.0f, 6.0f},
172                                                               0.0f));
173         plot.getRenderer().setSeriesStroke(1, new BasicStroke JavaDoc(2.0f,
174                                                               BasicStroke.CAP_ROUND,
175                                                               BasicStroke.JOIN_ROUND,
176                                                               1.0f,
177                                                               new float[] {6.0f, 6.0f},
178                                                               0.0f));
179         plot.getRenderer().setSeriesStroke(2, new BasicStroke JavaDoc(2.0f,
180                                                               BasicStroke.CAP_ROUND,
181                                                               BasicStroke.JOIN_ROUND,
182                                                               1.0f,
183                                                               new float[] {2.0f, 6.0f},
184                                                               0.0f));
185
186         // customise the renderer...
187
LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
188         renderer.setDrawShapes(true);
189         renderer.setItemLabelsVisible(Boolean.TRUE);
190
191         // customise the range axis...
192
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
193         rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
194         rangeAxis.setAutoRangeIncludesZero(false);
195         rangeAxis.setUpperMargin(0.12);
196
197         // OPTIONAL CUSTOMISATION COMPLETED.
198

199         // add the chart to a panel...
200
ChartPanel chartPanel = new ChartPanel(chart);
201         chartPanel.setPreferredSize(new java.awt.Dimension JavaDoc(500, 270));
202         setContentPane(chartPanel);
203
204     }
205
206     // ****************************************************************************
207
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
208
// * Please note that commercial support and documentation is available from: *
209
// * *
210
// * http://www.object-refinery.com/jfreechart/support.html *
211
// * *
212
// * This is not only a great service for developers, but is a VERY IMPORTANT *
213
// * source of funding for the JFreeChart project. Please support us so that *
214
// * we can continue developing free software. *
215
// ****************************************************************************
216

217    /**
218      * Starting point for the demonstration application.
219      *
220      * @param args ignored.
221      */

222     public static void main(String JavaDoc[] args) {
223
224         LineChartDemo5 demo = new LineChartDemo5("Line Chart Demo 5");
225         demo.pack();
226         RefineryUtilities.centerFrameOnScreen(demo);
227         demo.setVisible(true);
228
229     }
230
231 }
232
Popular Tags