KickJava   Java API By Example, From Geeks To Geeks.

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


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  * DualAxisDemo3.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: DualAxisDemo3.java,v 1.11 2003/11/28 10:57:36 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.PlotOrientation;
51 import org.jfree.chart.renderer.CategoryItemRenderer;
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 DualAxisDemo3 extends ApplicationFrame {
65
66     /**
67      * Creates a new demo instance.
68      *
69      * @param title the frame title.
70      */

71     public DualAxisDemo3(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.HORIZONTAL,
84             true, // include legend
85
true,
86             false
87         );
88
89         // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
90

91         // set the background color for the chart...
92
chart.setBackgroundPaint(new Color JavaDoc(0xCC, 0xFF, 0xCC));
93         chart.getLegend().setAnchor(Legend.WEST);
94
95         // get a reference to the plot for further customisation...
96
CategoryPlot plot = chart.getCategoryPlot();
97         plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
98         CategoryDataset dataset2 = createDataset2();
99         ValueAxis axis2 = new NumberAxis("Secondary");
100         plot.setSecondaryRangeAxis(0, axis2);
101         plot.setSecondaryDataset(0, dataset2);
102         plot.mapSecondaryDatasetToRangeAxis(0, new Integer JavaDoc(0));
103         CategoryItemRenderer renderer2 = new LineAndShapeRenderer();
104         plot.setSecondaryRenderer(0, renderer2);
105
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      * Creates a sample dataset.
117      *
118      * @return The dataset.
119      */

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

176     private CategoryDataset createDataset2() {
177
178         // row keys...
179
String JavaDoc series1 = "Fourth";
180
181         // column keys...
182
String JavaDoc category1 = "Category 1";
183         String JavaDoc category2 = "Category 2";
184         String JavaDoc category3 = "Category 3";
185         String JavaDoc category4 = "Category 4";
186         String JavaDoc category5 = "Category 5";
187         String JavaDoc category6 = "Category 6";
188         String JavaDoc category7 = "Category 7";
189         String JavaDoc category8 = "Category 8";
190
191         // create the dataset...
192
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
193
194         dataset.addValue(15.0, series1, category1);
195         dataset.addValue(24.0, series1, category2);
196         dataset.addValue(31.0, series1, category3);
197         dataset.addValue(25.0, series1, category4);
198         dataset.addValue(56.0, series1, category5);
199         dataset.addValue(37.0, series1, category6);
200         dataset.addValue(77.0, series1, category7);
201         dataset.addValue(18.0, series1, category8);
202
203         return dataset;
204
205     }
206
207     // ****************************************************************************
208
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
209
// * Please note that commercial support and documentation is available from: *
210
// * *
211
// * http://www.object-refinery.com/jfreechart/support.html *
212
// * *
213
// * This is not only a great service for developers, but is a VERY IMPORTANT *
214
// * source of funding for the JFreeChart project. Please support us so that *
215
// * we can continue developing free software. *
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         DualAxisDemo3 demo = new DualAxisDemo3("Dual Axis Demo 3");
226         demo.pack();
227         RefineryUtilities.centerFrameOnScreen(demo);
228         demo.setVisible(true);
229
230     }
231
232 }
233
Popular Tags