KickJava   Java API By Example, From Geeks To Geeks.

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


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  * DualAxisDemo.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: DualAxisDemo.java,v 1.12 2003/11/28 10:57:35 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 19-Nov-2002 : Version 1 (DG);
35  *
36  */

37
38 package org.jfree.chart.demo;
39
40 import java.awt.Color JavaDoc;
41
42 import org.jfree.chart.ChartFactory;
43 import org.jfree.chart.ChartPanel;
44 import org.jfree.chart.JFreeChart;
45 import org.jfree.chart.Legend;
46 import org.jfree.chart.axis.AxisLocation;
47 import org.jfree.chart.axis.NumberAxis;
48 import org.jfree.chart.axis.ValueAxis;
49 import org.jfree.chart.plot.CategoryPlot;
50 import org.jfree.chart.plot.DatasetRenderingOrder;
51 import org.jfree.chart.plot.PlotOrientation;
52 import org.jfree.chart.renderer.LineAndShapeRenderer;
53 import org.jfree.data.CategoryDataset;
54 import org.jfree.data.DefaultCategoryDataset;
55 import org.jfree.ui.ApplicationFrame;
56 import org.jfree.ui.RefineryUtilities;
57
58 /**
59  * A simple demonstration application showing how to create a dual axis chart based on data
60  * from two {@link CategoryDataset} instances.
61  *
62  * @author David Gilbert
63  */

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

71     public DualAxisDemo(String JavaDoc title) {
72
73         super(title);
74
75         CategoryDataset dataset1 = createDataset1();
76
77         // create the chart...
78
JFreeChart chart = ChartFactory.createBarChart(
79             "Dual Axis Chart", // chart title
80
"Category", // domain axis label
81
"Value", // range axis label
82
dataset1, // data
83
PlotOrientation.VERTICAL,
84             true, // include legend
85
true, // tooltips?
86
false // URL generator? Not required...
87
);
88
89         // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
90
chart.setBackgroundPaint(Color.white);
91         chart.getLegend().setAnchor(Legend.SOUTH);
92
93         // get a reference to the plot for further customisation...
94
CategoryPlot plot = chart.getCategoryPlot();
95         plot.setBackgroundPaint(new Color JavaDoc(0xEE, 0xEE, 0xFF));
96         plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
97
98         CategoryDataset dataset2 = createDataset2();
99         plot.setSecondaryDataset(0, dataset2);
100         plot.mapSecondaryDatasetToRangeAxis(0, new Integer JavaDoc(0));
101
102         ValueAxis axis2 = new NumberAxis("Secondary");
103         plot.setSecondaryRangeAxis(0, axis2);
104
105         LineAndShapeRenderer renderer2 = new LineAndShapeRenderer();
106         plot.setSecondaryRenderer(0, renderer2);
107         plot.setDatasetRenderingOrder(DatasetRenderingOrder.REVERSE);
108         // OPTIONAL CUSTOMISATION COMPLETED.
109

110         // add the chart to a panel...
111
ChartPanel chartPanel = new ChartPanel(chart);
112         chartPanel.setPreferredSize(new java.awt.Dimension JavaDoc(500, 270));
113         setContentPane(chartPanel);
114
115     }
116
117     // ****************************************************************************
118
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
119
// * Please note that commercial support and documentation is available from: *
120
// * *
121
// * http://www.object-refinery.com/jfreechart/support.html *
122
// * *
123
// * This is not only a great service for developers, but is a VERY IMPORTANT *
124
// * source of funding for the JFreeChart project. Please support us so that *
125
// * we can continue developing free software. *
126
// ****************************************************************************
127

128     /**
129      * Creates a sample dataset.
130      *
131      * @return The dataset.
132      */

133     private CategoryDataset createDataset1() {
134
135         // row keys...
136
String JavaDoc series1 = "First";
137         String JavaDoc series2 = "Second";
138         String JavaDoc series3 = "Third";
139
140         // column keys...
141
String JavaDoc category1 = "Category 1";
142         String JavaDoc category2 = "Category 2";
143         String JavaDoc category3 = "Category 3";
144         String JavaDoc category4 = "Category 4";
145         String JavaDoc category5 = "Category 5";
146         String JavaDoc category6 = "Category 6";
147         String JavaDoc category7 = "Category 7";
148         String JavaDoc category8 = "Category 8";
149
150         // create the dataset...
151
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
152
153         dataset.addValue(1.0, series1, category1);
154         dataset.addValue(4.0, series1, category2);
155         dataset.addValue(3.0, series1, category3);
156         dataset.addValue(5.0, series1, category4);
157         dataset.addValue(5.0, series1, category5);
158         dataset.addValue(7.0, series1, category6);
159         dataset.addValue(7.0, series1, category7);
160         dataset.addValue(8.0, series1, category8);
161
162         dataset.addValue(5.0, series2, category1);
163         dataset.addValue(7.0, series2, category2);
164         dataset.addValue(6.0, series2, category3);
165         dataset.addValue(8.0, series2, category4);
166         dataset.addValue(4.0, series2, category5);
167         dataset.addValue(4.0, series2, category6);
168         dataset.addValue(2.0, series2, category7);
169         dataset.addValue(1.0, series2, category8);
170
171         dataset.addValue(4.0, series3, category1);
172         dataset.addValue(3.0, series3, category2);
173         dataset.addValue(2.0, series3, category3);
174         dataset.addValue(3.0, series3, category4);
175         dataset.addValue(6.0, series3, category5);
176         dataset.addValue(3.0, series3, category6);
177         dataset.addValue(4.0, series3, category7);
178         dataset.addValue(3.0, series3, category8);
179
180         return dataset;
181
182     }
183
184     /**
185      * Creates a sample dataset.
186      *
187      * @return The dataset.
188      */

189     private CategoryDataset createDataset2() {
190
191         // row keys...
192
String JavaDoc series1 = "Fourth";
193
194         // column keys...
195
String JavaDoc category1 = "Category 1";
196         String JavaDoc category2 = "Category 2";
197         String JavaDoc category3 = "Category 3";
198         String JavaDoc category4 = "Category 4";
199         String JavaDoc category5 = "Category 5";
200         String JavaDoc category6 = "Category 6";
201         String JavaDoc category7 = "Category 7";
202         String JavaDoc category8 = "Category 8";
203
204         // create the dataset...
205
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
206
207         dataset.addValue(15.0, series1, category1);
208         dataset.addValue(24.0, series1, category2);
209         dataset.addValue(31.0, series1, category3);
210         dataset.addValue(25.0, series1, category4);
211         dataset.addValue(56.0, series1, category5);
212         dataset.addValue(37.0, series1, category6);
213         dataset.addValue(77.0, series1, category7);
214         dataset.addValue(18.0, series1, category8);
215
216         return dataset;
217
218     }
219
220     /**
221      * Starting point for the demonstration application.
222      *
223      * @param args ignored.
224      */

225     public static void main(String JavaDoc[] args) {
226
227         DualAxisDemo demo = new DualAxisDemo("Dual Axis Demo");
228         demo.pack();
229         RefineryUtilities.centerFrameOnScreen(demo);
230         demo.setVisible(true);
231
232     }
233
234 }
235
Popular Tags