KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

72     public DualAxisDemo4(String JavaDoc title) {
73
74         super(title);
75
76         CategoryDataset dataset1 = createDataset1();
77
78         // create the chart...
79
JFreeChart chart = ChartFactory.createBarChart3D(
80             "Dual Axis Chart", // chart title
81
"Category", // domain axis label
82
"Value", // range axis label
83
dataset1, // data
84
PlotOrientation.VERTICAL,
85             true, // include legend
86
true,
87             false
88         );
89
90         // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
91

92         // set the background color for the chart...
93
chart.setBackgroundPaint(new Color JavaDoc(0xCC, 0xFF, 0xCC));
94         chart.getLegend().setAnchor(Legend.SOUTH);
95
96         // get a reference to the plot for further customisation...
97
CategoryPlot plot = chart.getCategoryPlot();
98         plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
99         plot.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT);
100         CategoryItemRenderer renderer1 = plot.getRenderer();
101         renderer1.setSeriesPaint(0, Color.red);
102         renderer1.setSeriesPaint(1, Color.yellow);
103         renderer1.setSeriesPaint(2, Color.green);
104         CategoryDataset dataset2 = createDataset2();
105         ValueAxis axis2 = new NumberAxis3D("Secondary");
106         plot.setSecondaryRangeAxis(0, axis2);
107         plot.setSecondaryDataset(0, dataset2);
108         plot.mapSecondaryDatasetToRangeAxis(0, new Integer JavaDoc(0));
109         CategoryItemRenderer renderer2 = new LineAndShapeRenderer();
110         renderer2.setSeriesPaint(0, Color.blue);
111         plot.setSecondaryRenderer(0, renderer2);
112
113         plot.setDatasetRenderingOrder(DatasetRenderingOrder.REVERSE);
114         // OPTIONAL CUSTOMISATION COMPLETED.
115

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

134     /**
135      * Creates a sample dataset.
136      *
137      * @return The dataset.
138      */

139     private CategoryDataset createDataset1() {
140
141         // row keys...
142
String JavaDoc series1 = "First";
143         String JavaDoc series2 = "Second";
144         String JavaDoc series3 = "Third";
145
146         // column keys...
147
String JavaDoc category1 = "Category 1";
148         String JavaDoc category2 = "Category 2";
149         String JavaDoc category3 = "Category 3";
150         String JavaDoc category4 = "Category 4";
151         String JavaDoc category5 = "Category 5";
152
153         // create the dataset...
154
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
155
156         dataset.addValue(1.0, series1, category1);
157         dataset.addValue(4.0, series1, category2);
158         dataset.addValue(3.0, series1, category3);
159         dataset.addValue(5.0, series1, category4);
160         dataset.addValue(5.0, series1, category5);
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
168         dataset.addValue(4.0, series3, category1);
169         dataset.addValue(3.0, series3, category2);
170         dataset.addValue(2.0, series3, category3);
171         dataset.addValue(3.0, series3, category4);
172         dataset.addValue(6.0, series3, category5);
173
174         return dataset;
175
176     }
177
178     /**
179      * Creates a sample dataset.
180      *
181      * @return The dataset.
182      */

183     private CategoryDataset createDataset2() {
184
185         // row keys...
186
String JavaDoc series1 = "Fourth";
187
188         // column keys...
189
String JavaDoc category1 = "Category 1";
190         String JavaDoc category2 = "Category 2";
191         String JavaDoc category3 = "Category 3";
192         String JavaDoc category4 = "Category 4";
193         String JavaDoc category5 = "Category 5";
194
195         // create the dataset...
196
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
197
198         dataset.addValue(15.0, series1, category1);
199         dataset.addValue(24.0, series1, category2);
200         dataset.addValue(31.0, series1, category3);
201         dataset.addValue(25.0, series1, category4);
202         dataset.addValue(56.0, series1, category5);
203
204         return dataset;
205
206     }
207
208     /**
209      * Starting point for the demonstration application.
210      *
211      * @param args ignored.
212      */

213     public static void main(String JavaDoc[] args) {
214
215         DualAxisDemo4 demo = new DualAxisDemo4("Dual Axis Demo 4");
216         demo.pack();
217         RefineryUtilities.centerFrameOnScreen(demo);
218         demo.setVisible(true);
219
220     }
221
222 }
223
Popular Tags