KickJava   Java API By Example, From Geeks To Geeks.

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


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  * LayeredBarChartDemo1.java
24  * -------------------------
25  * (C) Copyright 2003, by Arnaud Lelievre and Contributors.
26  *
27  * Original Author: Arnaud Lelievre (for Garden);
28  * Contributor(s): David Gilbert (for Object Refinery Limited);
29  *
30  * $Id: LayeredBarChartDemo1.java,v 1.5 2003/11/28 10:57:36 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 28-Aug-2003 : Version 1 (AL);
35  * 11-Nov-2003 : Minor changes (DG);
36  *
37  */

38
39 package org.jfree.chart.demo;
40
41 import java.awt.Color JavaDoc;
42
43 import org.jfree.chart.ChartPanel;
44 import org.jfree.chart.JFreeChart;
45 import org.jfree.chart.axis.CategoryAxis;
46 import org.jfree.chart.axis.NumberAxis;
47 import org.jfree.chart.axis.ValueAxis;
48 import org.jfree.chart.plot.CategoryPlot;
49 import org.jfree.chart.renderer.LayeredBarRenderer;
50 import org.jfree.data.CategoryDataset;
51 import org.jfree.data.DatasetUtilities;
52 import org.jfree.ui.ApplicationFrame;
53 import org.jfree.ui.RefineryUtilities;
54
55 /**
56  * A simple demonstration application showing how to create a superimposed horizontal bar chart.
57  *
58  * @author Arnaud Lelievre
59  */

60 public class LayeredBarChartDemo1 extends ApplicationFrame {
61
62     /**
63      * Creates a new demo instance.
64      *
65      * @param title the frame title.
66      */

67     public LayeredBarChartDemo1(String JavaDoc title) {
68
69         super(title);
70
71         CategoryDataset dataset = createDataset();
72         JFreeChart chart = createChart(dataset);
73         ChartPanel chartPanel = new ChartPanel(chart);
74         chartPanel.setPreferredSize(new java.awt.Dimension JavaDoc(500, 270));
75         setContentPane(chartPanel);
76
77     }
78
79     // ****************************************************************************
80
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
81
// * Please note that commercial support and documentation is available from: *
82
// * *
83
// * http://www.object-refinery.com/jfreechart/support.html *
84
// * *
85
// * This is not only a great service for developers, but is a VERY IMPORTANT *
86
// * source of funding for the JFreeChart project. Please support us so that *
87
// * we can continue developing free software. *
88
// ****************************************************************************
89

90     /**
91      * Returns a sample dataset.
92      *
93      * @return a sample dataset.
94      */

95     private CategoryDataset createDataset() {
96
97         // create a dataset...
98
double[][] data = new double[][] {
99             {41.0, 33.0, 22.0, 64.0, 42.0, 62.0, 22.0, 14.0},
100             {55.0, 63.0, 55.0, 48.0, 54.0, 37.0, 41.0, 39.0},
101             {57.0, 75.0, 43.0, 33.0, 63.0, 46.0, 57.0, 33.0}
102         };
103
104         return DatasetUtilities.createCategoryDataset("Series ", "Factor ", data);
105         
106     }
107     
108     /**
109      * Creates a chart for the specified dataset.
110      *
111      * @param dataset the dataset.
112      *
113      * @return a chart.
114      */

115     private JFreeChart createChart(CategoryDataset dataset) {
116
117         CategoryAxis categoryAxis = new CategoryAxis("Category");
118         ValueAxis valueAxis = new NumberAxis("Score (%)");
119
120
121         CategoryPlot plot = new CategoryPlot(dataset,
122                                             categoryAxis,
123                                             valueAxis,
124                                             new LayeredBarRenderer());
125         
126         plot.setOrientation(org.jfree.chart.plot.PlotOrientation.HORIZONTAL);
127         JFreeChart chart = new JFreeChart("Layered Bar Chart",
128                                           JFreeChart.DEFAULT_TITLE_FONT, plot, true);
129
130         // set the background color for the chart...
131
chart.setBackgroundPaint(Color.lightGray);
132
133         LayeredBarRenderer renderer = (LayeredBarRenderer) plot.getRenderer();
134
135         // we can set each series bar width individually or let the renderer manage a standard view.
136
// the width is set in percentage, where 1.0 is the maximum (100%).
137
renderer.setSeriesBarWidth(0, 1.0);
138         renderer.setSeriesBarWidth(1, 0.7);
139         renderer.setSeriesBarWidth(2, 0.4);
140
141         renderer.setItemMargin(0.01);
142         CategoryAxis domainAxis = plot.getDomainAxis();
143         domainAxis.setCategoryMargin(0.25);
144         domainAxis.setUpperMargin(0.05);
145         domainAxis.setLowerMargin(0.05);
146         
147         return chart;
148         
149     }
150     
151     /**
152      * Starting point for the demonstration application.
153      *
154      * @param args ignored.
155      */

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