KickJava   Java API By Example, From Geeks To Geeks.

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


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  * BarChartDemo5.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: BarChartDemo5.java,v 1.2 2003/11/28 10:57:36 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 11-Jun-2002 : Version 1 (DG);
35  * 25-Jun-2002 : Removed unnecessary imports (DG);
36  * 09-Oct-2002 : Added frame centering (DG);
37  * 14-Nov-2002 : Renamed HorizontalBarChartDemo --> HorizontalBarChartDemo2 (DG);
38  * 28-Nov-2003 : Renamed HorizontalBarChartDemo2 --> BarChartDemo5 (DG);
39  *
40  */

41
42 package org.jfree.chart.demo;
43
44 import java.awt.BasicStroke JavaDoc;
45 import java.awt.Color JavaDoc;
46 import java.awt.Font JavaDoc;
47
48 import org.jfree.chart.ChartFactory;
49 import org.jfree.chart.ChartPanel;
50 import org.jfree.chart.IntervalMarker;
51 import org.jfree.chart.JFreeChart;
52 import org.jfree.chart.axis.MarkerAxisBand;
53 import org.jfree.chart.axis.NumberAxis;
54 import org.jfree.chart.plot.CategoryPlot;
55 import org.jfree.chart.plot.PlotOrientation;
56 import org.jfree.data.CategoryDataset;
57 import org.jfree.data.DatasetUtilities;
58 import org.jfree.ui.ApplicationFrame;
59 import org.jfree.ui.RefineryUtilities;
60
61 /**
62  * A simple demonstration application showing how to create a horizontal bar chart.
63  *
64  * @author David Gilbert
65  */

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

73     public BarChartDemo5(String JavaDoc title) {
74
75         super(title);
76
77         // create a dataset...
78
double[][] data = new double[][] {
79             {1.0, 43.0, 35.0, 58.0, 54.0, 77.0, 71.0, 89.0},
80             {54.0, 75.0, 63.0, 83.0, 43.0, 46.0, 27.0, 13.0},
81             {41.0, 33.0, 22.0, 34.0, 62.0, 32.0, 42.0, 34.0}
82         };
83
84         CategoryDataset dataset = DatasetUtilities.createCategoryDataset(
85             "Series ",
86             "Factor ",
87             data
88         );
89
90         // create the chart...
91
JFreeChart chart = ChartFactory.createBarChart(
92             "Horizontal Bar Chart", // chart title
93
"Category", // domain axis label
94
"Score (%)", // range axis label
95
dataset, // data
96
PlotOrientation.HORIZONTAL,
97             true, // include legend
98
true,
99             false
100         );
101
102         // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
103

104         chart.setBackgroundPaint(Color.lightGray);
105
106         // get a reference to the plot for further customisation...
107
CategoryPlot plot = chart.getCategoryPlot();
108
109         plot.getRenderer().setSeriesPaint(0, new Color JavaDoc(0, 0, 255));
110         plot.getRenderer().setSeriesPaint(1, new Color JavaDoc(75, 75, 255));
111         plot.getRenderer().setSeriesPaint(2, new Color JavaDoc(150, 150, 255));
112
113         // change the auto tick unit selection to integer units only...
114
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
115         rangeAxis.setRange(0.0, 100.0);
116         rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
117
118         NumberAxis hna = rangeAxis;
119         MarkerAxisBand band = new MarkerAxisBand(hna, 2.0, 2.0, 2.0, 2.0,
120                                         new Font JavaDoc("SansSerif", Font.PLAIN, 9));
121
122         IntervalMarker m1 = new IntervalMarker(0.0, 33.0, "Low", Color.gray,
123                                                new BasicStroke JavaDoc(0.5f), Color.green, 0.75f);
124         IntervalMarker m2 = new IntervalMarker(33.0, 66.0, "Medium", Color.gray,
125                                                new BasicStroke JavaDoc(0.5f), Color.orange, 0.75f);
126         IntervalMarker m3 = new IntervalMarker(66.0, 100.0, "High", Color.gray,
127                                                new BasicStroke JavaDoc(0.5f), Color.red, 0.75f);
128         band.addMarker(m1);
129         band.addMarker(m2);
130         band.addMarker(m3);
131         hna.setMarkerBand(band);
132         // OPTIONAL CUSTOMISATION COMPLETED.
133

134         // add the chart to a panel...
135
ChartPanel chartPanel = new ChartPanel(chart);
136         chartPanel.setPreferredSize(new java.awt.Dimension JavaDoc(500, 270));
137         setContentPane(chartPanel);
138
139     }
140
141     // ****************************************************************************
142
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
143
// * Please note that commercial support and documentation is available from: *
144
// * *
145
// * http://www.object-refinery.com/jfreechart/support.html *
146
// * *
147
// * This is not only a great service for developers, but is a VERY IMPORTANT *
148
// * source of funding for the JFreeChart project. Please support us so that *
149
// * we can continue developing free software. *
150
// ****************************************************************************
151

152     /**
153      * Starting point for the demonstration application.
154      *
155      * @param args ignored.
156      */

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