KickJava   Java API By Example, From Geeks To Geeks.

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


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  * BarChartDemo3.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: BarChartDemo3.java,v 1.3 2003/11/28 10:57:35 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 22-Aug-2002 : Version 1 (DG);
35  * 28-Jul-2003 : Updated custom renderer for API changes in 0.9.10 (DG);
36  * 11-Nov-2003 : Renamed BarChartDemo3.java (DG);
37  *
38  */

39
40 package org.jfree.chart.demo;
41
42 import java.awt.Color JavaDoc;
43 import java.awt.Paint JavaDoc;
44
45 import org.jfree.chart.ChartFactory;
46 import org.jfree.chart.ChartPanel;
47 import org.jfree.chart.JFreeChart;
48 import org.jfree.chart.axis.NumberAxis;
49 import org.jfree.chart.axis.ValueAxis;
50 import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
51 import org.jfree.chart.plot.CategoryPlot;
52 import org.jfree.chart.plot.PlotOrientation;
53 import org.jfree.chart.renderer.BarRenderer;
54 import org.jfree.chart.renderer.CategoryItemRenderer;
55 import org.jfree.chart.renderer.ItemLabelAnchor;
56 import org.jfree.chart.renderer.ItemLabelPosition;
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 import org.jfree.ui.TextAnchor;
62
63 /**
64  * A bar chart that uses a custom renderer to display different colors within a series.
65  * No legend is displayed because there is only one series but the colors are not consistent.
66  *
67  * @author David Gilbert
68  */

69 public class BarChartDemo3 extends ApplicationFrame {
70
71     /**
72      * A custom renderer that returns a different color for each item in a single series.
73      */

74     class CustomRenderer extends BarRenderer {
75
76         /** The colors. */
77         private Paint JavaDoc[] colors;
78
79         /**
80          * Creates a new renderer.
81          *
82          * @param colors the colors.
83          */

84         public CustomRenderer(Paint JavaDoc[] colors) {
85             this.colors = colors;
86         }
87
88         /**
89          * Returns the paint for an item. Overrides the default behaviour inherited from
90          * AbstractRenderer.
91          *
92          * @param row the series.
93          * @param column the category.
94          *
95          * @return The item color.
96          */

97         public Paint JavaDoc getItemPaint(int row, int column) {
98             return colors[column % colors.length];
99         }
100     }
101
102     /**
103      * Creates a new demo.
104      *
105      * @param title the frame title.
106      */

107     public BarChartDemo3(String JavaDoc title) {
108
109         super(title);
110
111         // create a dataset...
112
double[][] data = new double[][] {{4.0, 3.0, -2.0, 3.0, 6.0, 3.0, 4.0, 3.0}};
113
114         CategoryDataset dataset = DatasetUtilities.createCategoryDataset(
115             "Series ",
116             "Category ",
117             data
118         );
119
120         // create the chart...
121
JFreeChart chart = ChartFactory.createBarChart(
122             "Bar Chart Demo 3", // chart title
123
"Category", // domain axis label
124
"Value", // range axis label
125
dataset, // data
126
PlotOrientation.VERTICAL, // the plot orientation
127
false, // include legend
128
true,
129             false
130         );
131
132         // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
133

134         // set the background color for the chart...
135
chart.setBackgroundPaint(Color.lightGray);
136
137         // get a reference to the plot for further customisation...
138
CategoryPlot plot = chart.getCategoryPlot();
139         plot.setNoDataMessage("NO DATA!");
140         //plot.setBackgroundImage(JFreeChart.INFO.getLogo());
141

142         CategoryItemRenderer renderer = new CustomRenderer(
143             new Paint JavaDoc[] {Color.red, Color.blue, Color.green,
144                          Color.yellow, Color.orange, Color.cyan,
145                          Color.magenta, Color.blue});
146         renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
147         renderer.setItemLabelsVisible(true);
148         ItemLabelPosition p = new ItemLabelPosition(
149             ItemLabelAnchor.CENTER, TextAnchor.CENTER, TextAnchor.CENTER, 45.0);
150         renderer.setPositiveItemLabelPosition(p);
151         plot.setRenderer(renderer);
152
153
154         // change the margin at the top of the range axis...
155
ValueAxis rangeAxis = plot.getRangeAxis();
156         rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
157         rangeAxis.setLowerMargin(0.15);
158         rangeAxis.setUpperMargin(0.15);
159
160         // OPTIONAL CUSTOMISATION COMPLETED.
161

162         // add the chart to a panel...
163
ChartPanel chartPanel = new ChartPanel(chart);
164         chartPanel.setPreferredSize(new java.awt.Dimension JavaDoc(500, 270));
165         setContentPane(chartPanel);
166
167     }
168
169     // ****************************************************************************
170
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
171
// * Please note that commercial support and documentation is available from: *
172
// * *
173
// * http://www.object-refinery.com/jfreechart/support.html *
174
// * *
175
// * This is not only a great service for developers, but is a VERY IMPORTANT *
176
// * source of funding for the JFreeChart project. Please support us so that *
177
// * we can continue developing free software. *
178
// ****************************************************************************
179

180     /**
181      * Starting point for the demonstration application.
182      *
183      * @param args ignored.
184      */

185     public static void main(String JavaDoc[] args) {
186
187         BarChartDemo3 demo = new BarChartDemo3("Bar Chart Demo 3");
188         demo.pack();
189         RefineryUtilities.centerFrameOnScreen(demo);
190         demo.setVisible(true);
191
192     }
193
194 }
195
Popular Tags