KickJava   Java API By Example, From Geeks To Geeks.

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


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  * CombinedCategoryPlotDemo2.java
24  * ------------------------------
25  * (C) Copyright 2003, by Object Refinery Limited.
26  *
27  * Original Author: David Gilbert (for Object Refinery Limited).
28  * Contributor(s): -;
29  *
30  * $Id: CombinedCategoryPlotDemo2.java,v 1.9 2003/11/28 10:57:36 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 16-May-2003 : Version 1 (DG);
35  *
36  */

37
38 package org.jfree.chart.demo;
39
40 import java.awt.Font JavaDoc;
41
42 import org.jfree.chart.ChartPanel;
43 import org.jfree.chart.JFreeChart;
44 import org.jfree.chart.Legend;
45 import org.jfree.chart.axis.CategoryAxis;
46 import org.jfree.chart.axis.CategoryLabelPosition;
47 import org.jfree.chart.axis.NumberAxis;
48 import org.jfree.chart.axis.ValueAxis;
49 import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
50 import org.jfree.chart.plot.CategoryPlot;
51 import org.jfree.chart.plot.CombinedRangeCategoryPlot;
52 import org.jfree.chart.renderer.BarRenderer;
53 import org.jfree.chart.renderer.LineAndShapeRenderer;
54 import org.jfree.data.CategoryDataset;
55 import org.jfree.data.DefaultCategoryDataset;
56 import org.jfree.text.TextBlockAnchor;
57 import org.jfree.ui.ApplicationFrame;
58 import org.jfree.ui.RectangleAnchor;
59 import org.jfree.ui.RefineryUtilities;
60 import org.jfree.ui.TextAnchor;
61
62 /**
63  * A demo for the {@link CombinedRangeCategoryPlot} class.
64  *
65  * @author David Gilbert
66  */

67 public class CombinedCategoryPlotDemo2 extends ApplicationFrame {
68
69     /**
70      * Creates a new demo instance.
71      *
72      * @param title the frame title.
73      */

74     public CombinedCategoryPlotDemo2(String JavaDoc title) {
75
76         super(title);
77
78         // add the chart to a panel...
79
ChartPanel chartPanel = new ChartPanel(createChart());
80         chartPanel.setPreferredSize(new java.awt.Dimension JavaDoc(500, 270));
81         setContentPane(chartPanel);
82
83     }
84
85     /**
86      * Creates a dataset.
87      *
88      * @return A dataset.
89      */

90     public CategoryDataset createDataset1() {
91
92         DefaultCategoryDataset result = new DefaultCategoryDataset();
93
94         // row keys...
95
String JavaDoc series1 = "First";
96         String JavaDoc series2 = "Second";
97
98         // column keys...
99
String JavaDoc type1 = "Type 1";
100         String JavaDoc type2 = "Type 2";
101         String JavaDoc type3 = "Type 3";
102         String JavaDoc type4 = "Type 4";
103         String JavaDoc type5 = "Type 5";
104         String JavaDoc type6 = "Type 6";
105         String JavaDoc type7 = "Type 7";
106         String JavaDoc type8 = "Type 8";
107
108         result.addValue(1.0, series1, type1);
109         result.addValue(4.0, series1, type2);
110         result.addValue(3.0, series1, type3);
111         result.addValue(5.0, series1, type4);
112         result.addValue(5.0, series1, type5);
113         result.addValue(7.0, series1, type6);
114         result.addValue(7.0, series1, type7);
115         result.addValue(8.0, series1, type8);
116
117         result.addValue(5.0, series2, type1);
118         result.addValue(7.0, series2, type2);
119         result.addValue(6.0, series2, type3);
120         result.addValue(8.0, series2, type4);
121         result.addValue(4.0, series2, type5);
122         result.addValue(4.0, series2, type6);
123         result.addValue(2.0, series2, type7);
124         result.addValue(1.0, series2, type8);
125
126         return result;
127
128     }
129
130     /**
131      * Creates a dataset.
132      *
133      * @return A dataset.
134      */

135     public CategoryDataset createDataset2() {
136
137         DefaultCategoryDataset result = new DefaultCategoryDataset();
138
139         // row keys...
140
String JavaDoc series1 = "Third";
141         String JavaDoc series2 = "Fourth";
142
143         // column keys...
144
String JavaDoc sector1 = "Sector 1";
145         String JavaDoc sector2 = "Sector 2";
146         String JavaDoc sector3 = "Sector 3";
147         String JavaDoc sector4 = "Sector 4";
148
149         result.addValue(11.0, series1, sector1);
150         result.addValue(14.0, series1, sector2);
151         result.addValue(13.0, series1, sector3);
152         result.addValue(15.0, series1, sector4);
153
154         result.addValue(15.0, series2, sector1);
155         result.addValue(17.0, series2, sector2);
156         result.addValue(16.0, series2, sector3);
157         result.addValue(18.0, series2, sector4);
158
159         return result;
160
161     }
162
163     // ****************************************************************************
164
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
165
// * Please note that commercial support and documentation is available from: *
166
// * *
167
// * http://www.object-refinery.com/jfreechart/support.html *
168
// * *
169
// * This is not only a great service for developers, but is a VERY IMPORTANT *
170
// * source of funding for the JFreeChart project. Please support us so that *
171
// * we can continue developing free software. *
172
// ****************************************************************************
173

174     /**
175      * Creates a chart.
176      *
177      * @return A chart.
178      */

179     private JFreeChart createChart() {
180
181         CategoryDataset dataset1 = createDataset1();
182         CategoryAxis domainAxis1 = new CategoryAxis("Class 1");
183         LineAndShapeRenderer renderer1 = new LineAndShapeRenderer();
184         renderer1.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
185         CategoryPlot subplot1 = new CategoryPlot(dataset1, domainAxis1, null, renderer1);
186         subplot1.setDomainGridlinesVisible(true);
187         CategoryLabelPosition position1 = new CategoryLabelPosition(
188             RectangleAnchor.TOP, TextBlockAnchor.TOP_RIGHT, TextAnchor.TOP_RIGHT, -Math.PI / 8.0
189         );
190         domainAxis1.setBottomCategoryLabelPosition(position1);
191
192         CategoryDataset dataset2 = createDataset2();
193         CategoryAxis domainAxis2 = new CategoryAxis("Class 2");
194         BarRenderer renderer2 = new BarRenderer();
195         renderer2.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
196         CategoryPlot subplot2 = new CategoryPlot(dataset2, domainAxis2, null, renderer2);
197         subplot2.setDomainGridlinesVisible(true);
198         CategoryLabelPosition position2 = new CategoryLabelPosition(
199             RectangleAnchor.TOP, TextBlockAnchor.TOP_RIGHT, TextAnchor.TOP_RIGHT, -Math.PI / 8.0
200         );
201         domainAxis2.setBottomCategoryLabelPosition(position2);
202
203         ValueAxis rangeAxis = new NumberAxis("Value");
204         CombinedRangeCategoryPlot plot = new CombinedRangeCategoryPlot(rangeAxis);
205         plot.add(subplot1, 3);
206         plot.add(subplot2, 2);
207
208         JFreeChart result = new JFreeChart(
209             "Combined Range Category Plot Demo",
210             new Font JavaDoc("SansSerif", Font.BOLD, 12),
211             plot,
212             true
213         );
214         result.getLegend().setAnchor(Legend.SOUTH);
215         return result;
216
217     }
218
219     /**
220      * Starting point for the demonstration application.
221      *
222      * @param args ignored.
223      */

224     public static void main(String JavaDoc[] args) {
225
226         String JavaDoc title = "Combined Category Plot Demo 2";
227         CombinedCategoryPlotDemo2 demo = new CombinedCategoryPlotDemo2(title);
228         demo.pack();
229         RefineryUtilities.centerFrameOnScreen(demo);
230         demo.setVisible(true);
231
232     }
233
234 }
235
Popular Tags