KickJava   Java API By Example, From Geeks To Geeks.

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


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  * OverlaidBarChartDemo.java
24  * -------------------------
25  * (C) Copyright 2002, 2003, by Object Refinery Limited.
26  *
27  * Original Author: David Gilbert (for Object Refinery Limited);
28  * Contributor(s): -;
29  *
30  * $Id: OverlaidBarChartDemo.java,v 1.20 2003/11/28 10:57:34 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 20-Sep-2002 : Version 1 (DG);
35  * 11-Oct-2002 : Added tooltips, modified series colors, centered frame on screen (DG);
36  * 11-Feb-2003 : Fixed bug where category labels were not showing on the axis (DG);
37  * 08-Sep-2003 : Bug fix (DG);
38  *
39  */

40
41 package org.jfree.chart.demo;
42
43 import org.jfree.chart.ChartPanel;
44 import org.jfree.chart.JFreeChart;
45 import org.jfree.chart.StandardLegend;
46 import org.jfree.chart.axis.CategoryAxis;
47 import org.jfree.chart.axis.NumberAxis;
48 import org.jfree.chart.axis.ValueAxis;
49 import org.jfree.chart.labels.CategoryItemLabelGenerator;
50 import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
51 import org.jfree.chart.plot.CategoryPlot;
52 import org.jfree.chart.plot.DatasetRenderingOrder;
53 import org.jfree.chart.plot.PlotOrientation;
54 import org.jfree.chart.renderer.BarRenderer;
55 import org.jfree.chart.renderer.CategoryItemRenderer;
56 import org.jfree.chart.renderer.LineAndShapeRenderer;
57 import org.jfree.data.CategoryDataset;
58 import org.jfree.data.DatasetUtilities;
59 import org.jfree.ui.ApplicationFrame;
60 import org.jfree.ui.RefineryUtilities;
61
62 /**
63  * A simple demonstration application showing how to create a bar chart overlaid
64  * with a line chart. From version 0.9.10 onwards, this no longer uses the
65  * <code>OverlaidCategoryPlot</code> class as this has been removed. Instrad, multiple datasets
66  * and renderers are added to the standard {@link CategoryPlot} class.
67  *
68  * @author David Gilbert
69  */

70 public class OverlaidBarChartDemo extends ApplicationFrame {
71
72     /**
73      * Default constructor.
74      *
75      * @param title the frame title.
76      */

77     public OverlaidBarChartDemo(String JavaDoc title) {
78
79         super(title);
80
81         // create the first dataset...
82
double[][] data1 = new double[][] {
83             {1.0, 4.0, 3.0, 5.0, 5.0, 7.0, 7.0, 8.0},
84             {5.0, 7.0, 6.0, 8.0, 4.0, 4.0, 2.0, 1.0}
85         };
86         CategoryDataset dataset1 = DatasetUtilities.createCategoryDataset(
87             "S", "Category ", data1
88         );
89
90         // create the first plot...
91
CategoryItemLabelGenerator tooltips = new StandardCategoryItemLabelGenerator();
92         CategoryItemRenderer renderer = new BarRenderer();
93         renderer.setItemLabelGenerator(tooltips);
94         CategoryPlot plot = new CategoryPlot();
95         plot.setDataset(dataset1);
96         plot.setRenderer(new BarRenderer());
97         
98         plot.setDomainAxis(new CategoryAxis("Category"));
99         plot.setRangeAxis(new NumberAxis("Value"));
100
101         plot.setOrientation(PlotOrientation.VERTICAL);
102         plot.setRangeGridlinesVisible(true);
103         plot.setDomainGridlinesVisible(true);
104
105         ValueAxis rangeAxis2 = new NumberAxis("Axis 2");
106         plot.setSecondaryRangeAxis(0, rangeAxis2);
107
108         // create the second dataset and renderer...
109
double[][] data2 = new double[][] {
110             {9.0, 7.0, 2.0, 6.0, 6.0, 9.0, 5.0, 4.0}
111         };
112         CategoryDataset dataset2 = DatasetUtilities.createCategoryDataset("T", "Category", data2);
113         CategoryItemRenderer renderer2 = new LineAndShapeRenderer();
114         plot.setSecondaryDataset(0, dataset2);
115         plot.setSecondaryRenderer(0, renderer2);
116
117         // create the third dataset and renderer...
118
double[][] data3 = new double[][] {
119             {94.0, 75.0, 22.0, 74.0, 83.0, 9.0, 23.0, 98.0}
120         };
121         CategoryDataset dataset3 = DatasetUtilities.createCategoryDataset("R", "Category", data3);
122         plot.setSecondaryDataset(1, dataset3);
123         plot.setSecondaryRenderer(1, new LineAndShapeRenderer());
124         plot.mapSecondaryDatasetToRangeAxis(1, new Integer JavaDoc(0));
125
126         plot.setDatasetRenderingOrder(DatasetRenderingOrder.REVERSE);
127         JFreeChart chart = new JFreeChart(plot);
128         chart.setTitle("Overlaid Bar Chart");
129         chart.setLegend(new StandardLegend(chart));
130
131         // add the chart to a panel...
132
ChartPanel chartPanel = new ChartPanel(chart);
133         chartPanel.setPreferredSize(new java.awt.Dimension JavaDoc(500, 270));
134         setContentPane(chartPanel);
135
136     }
137
138     // ****************************************************************************
139
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
140
// * Please note that commercial support and documentation is available from: *
141
// * *
142
// * http://www.object-refinery.com/jfreechart/support.html *
143
// * *
144
// * This is not only a great service for developers, but is a VERY IMPORTANT *
145
// * source of funding for the JFreeChart project. Please support us so that *
146
// * we can continue developing free software. *
147
// ****************************************************************************
148

149     /**
150      * Starting point for the demonstration application.
151      *
152      * @param args ignored.
153      */

154     public static void main(String JavaDoc[] args) {
155
156         OverlaidBarChartDemo demo = new OverlaidBarChartDemo("Overlaid Bar Chart Demo");
157         demo.pack();
158         RefineryUtilities.centerFrameOnScreen(demo);
159         demo.setVisible(true);
160
161     }
162
163 }
164
Popular Tags