KickJava   Java API By Example, From Geeks To Geeks.

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


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  * DualAxisDemo5.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: DualAxisDemo5.java,v 1.3 2003/11/28 10:57:34 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 19-Sep-2003 : 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.PlotOrientation;
51 import org.jfree.chart.renderer.BarRenderer;
52 import org.jfree.data.CategoryDataset;
53 import org.jfree.data.DefaultCategoryDataset;
54 import org.jfree.ui.ApplicationFrame;
55 import org.jfree.ui.RefineryUtilities;
56
57 /**
58  * A simple demonstration application showing how to create a dual axis chart based on data
59  * from two {@link CategoryDataset} instances.
60  *
61  * @author David Gilbert
62  */

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

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

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

126     /**
127      * Creates a sample dataset.
128      *
129      * @return The dataset.
130      */

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

177     private CategoryDataset createDataset2() {
178
179         // row keys...
180
String JavaDoc series1 = "Third";
181         String JavaDoc series2 = "Fourth";
182
183         // column keys...
184
String JavaDoc category1 = "Category 1";
185         String JavaDoc category2 = "Category 2";
186         String JavaDoc category3 = "Category 3";
187         String JavaDoc category4 = "Category 4";
188         String JavaDoc category5 = "Category 5";
189         String JavaDoc category6 = "Category 6";
190         String JavaDoc category7 = "Category 7";
191         String JavaDoc category8 = "Category 8";
192
193         // create the dataset...
194
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
195
196         dataset.addValue(null, series1, category1);
197         dataset.addValue(null, series1, category2);
198         dataset.addValue(null, series1, category3);
199         dataset.addValue(null, series1, category4);
200         dataset.addValue(null, series1, category5);
201         dataset.addValue(null, series1, category6);
202         dataset.addValue(null, series1, category7);
203         dataset.addValue(null, series1, category8);
204
205         dataset.addValue(75.0, series2, category1);
206         dataset.addValue(87.0, series2, category2);
207         dataset.addValue(96.0, series2, category3);
208         dataset.addValue(68.0, series2, category4);
209         dataset.addValue(84.0, series2, category5);
210         dataset.addValue(94.0, series2, category6);
211         dataset.addValue(82.0, series2, category7);
212         dataset.addValue(71.0, series2, category8);
213
214         return dataset;
215
216     }
217
218     /**
219      * Starting point for the demonstration application.
220      *
221      * @param args ignored.
222      */

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