KickJava   Java API By Example, From Geeks To Geeks.

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


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  * AreaChartDemo.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: AreaChartDemo.java,v 1.19 2003/11/28 10:57:35 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 11-Jun-2002 : Version 1 (DG);
35  * 25-Jun-2002 : Removed unnecessary imports (DG);
36  * 10-Oct-2002 : Renamed AreaChartForCategoryDataDemo --> AreaChartDemo (DG);
37  * 05-Nov-2003 : Added category label position (DG);
38  *
39  */

40
41 package org.jfree.chart.demo;
42
43 import java.awt.Color JavaDoc;
44 import java.awt.Font 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.Spacer;
50 import org.jfree.chart.StandardLegend;
51 import org.jfree.chart.TextTitle;
52 import org.jfree.chart.axis.CategoryAxis;
53 import org.jfree.chart.axis.CategoryLabelPosition;
54 import org.jfree.chart.axis.NumberAxis;
55 import org.jfree.chart.plot.CategoryPlot;
56 import org.jfree.chart.plot.PlotOrientation;
57 import org.jfree.data.CategoryDataset;
58 import org.jfree.data.DatasetUtilities;
59 import org.jfree.text.TextBlockAnchor;
60 import org.jfree.ui.ApplicationFrame;
61 import org.jfree.ui.RectangleAnchor;
62 import org.jfree.ui.RefineryUtilities;
63 import org.jfree.ui.TextAnchor;
64
65 /**
66  * A simple demonstration application showing how to create an area chart using data from a
67  * {@link CategoryDataset}.
68  *
69  * @author David Gilbert
70  */

71 public class AreaChartDemo extends ApplicationFrame {
72
73     /**
74      * Creates a new demo application.
75      *
76      * @param title the frame title.
77      */

78     public AreaChartDemo(String JavaDoc title) {
79
80         super(title);
81
82         // create a dataset...
83
double[][] data = new double[][] {
84             {1.0, 4.0, 3.0, 5.0, 5.0, 7.0, 7.0, 8.0},
85             {5.0, 7.0, 6.0, 8.0, 4.0, 4.0, 2.0, 1.0},
86             {4.0, 3.0, 2.0, 3.0, 6.0, 3.0, 4.0, 3.0}
87         };
88
89         CategoryDataset dataset = DatasetUtilities.createCategoryDataset(
90             "Series ", "Type ", data
91         );
92
93         // create the chart...
94
JFreeChart chart = createChart(dataset);
95         
96         // add the chart to a panel...
97
ChartPanel chartPanel = new ChartPanel(chart);
98         chartPanel.setPreferredSize(new java.awt.Dimension JavaDoc(500, 270));
99         chartPanel.setEnforceFileExtensions(false);
100         
101         setContentPane(chartPanel);
102
103     }
104
105     // ****************************************************************************
106
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
107
// * Please note that commercial support and documentation is available from: *
108
// * *
109
// * http://www.object-refinery.com/jfreechart/support.html *
110
// * *
111
// * This is not only a great service for developers, but is a VERY IMPORTANT *
112
// * source of funding for the JFreeChart project. Please support us so that *
113
// * we can continue developing free software. *
114
// ****************************************************************************
115

116     /**
117      * Creates a chart.
118      *
119      * @param dataset the dataset.
120      *
121      * @return The chart.
122      */

123     private JFreeChart createChart(CategoryDataset dataset) {
124                 
125         JFreeChart chart = ChartFactory.createAreaChart(
126             "Area Chart", // chart title
127
"Category", // domain axis label
128
"Value", // range axis label
129
dataset, // data
130
PlotOrientation.VERTICAL,
131             true, // include legend
132
true, // tooltips
133
false // urls
134
);
135
136         // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
137

138         // set the background color for the chart...
139
StandardLegend legend = (StandardLegend) chart.getLegend();
140         legend.setAnchor(StandardLegend.SOUTH);
141
142         chart.setBackgroundPaint(Color.white);
143         TextTitle subtitle = new TextTitle("An area chart demonstration.");
144         subtitle.setFont(new Font JavaDoc("SansSerif", Font.PLAIN, 12));
145         chart.addSubtitle(subtitle);
146
147         CategoryPlot plot = chart.getCategoryPlot();
148         plot.setForegroundAlpha(0.5f);
149         
150         plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));
151         plot.setBackgroundPaint(Color.lightGray);
152         plot.setDomainGridlinesVisible(true);
153         plot.setDomainGridlinePaint(Color.white);
154         plot.setRangeGridlinesVisible(true);
155         plot.setRangeGridlinePaint(Color.white);
156         
157         CategoryAxis domainAxis = plot.getDomainAxis();
158         CategoryLabelPosition position = new CategoryLabelPosition(
159             RectangleAnchor.TOP, TextBlockAnchor.TOP_RIGHT, TextAnchor.TOP_RIGHT, -Math.PI / 8.0
160         );
161         domainAxis.setBottomCategoryLabelPosition(position);
162         domainAxis.setLowerMargin(0.0);
163         domainAxis.setUpperMargin(0.0);
164
165         NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
166         rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
167         rangeAxis.setLabelAngle(0 * Math.PI / 2.0);
168         // OPTIONAL CUSTOMISATION COMPLETED.
169

170         return chart;
171         
172     }
173     
174     /**
175      * Starting point for the demonstration application.
176      *
177      * @param args ignored.
178      */

179     public static void main(String JavaDoc[] args) {
180
181         AreaChartDemo demo = new AreaChartDemo("Area Chart Demo");
182         demo.pack();
183         RefineryUtilities.centerFrameOnScreen(demo);
184         demo.setVisible(true);
185
186     }
187
188 }
189
Popular Tags