KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > krysalis > jcharts > demo > userGuide > BarChartsGuide


1 /***********************************************************************************************
2  * Copyright 2002 (C) Nathaniel G. Auvil. All Rights Reserved.
3  *
4  * Redistribution and use of this software and associated documentation ("Software"), with or
5  * without modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain copyright statements and notices.
8  * Redistributions must also contain a copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
11  * conditions and the following disclaimer in the documentation and/or other materials
12  * provided with the distribution.
13  *
14  * 3. The name "jCharts" or "Nathaniel G. Auvil" must not be used to endorse or promote
15  * products derived from this Software without prior written permission of Nathaniel G.
16  * Auvil. For written permission, please contact nathaniel_auvil@users.sourceforge.net
17  *
18  * 4. Products derived from this Software may not be called "jCharts" nor may "jCharts" appear
19  * in their names without prior written permission of Nathaniel G. Auvil. jCharts is a
20  * registered trademark of Nathaniel G. Auvil.
21  *
22  * 5. Due credit should be given to the jCharts Project (http://jcharts.sourceforge.net/).
23  *
24  * THIS SOFTWARE IS PROVIDED BY Nathaniel G. Auvil AND CONTRIBUTORS ``AS IS'' AND ANY
25  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
27  * jCharts OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
32  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
33  ************************************************************************************************/

34
35 package org.krysalis.jcharts.demo.userGuide;
36
37
38 import org.krysalis.jcharts.chartData.ChartDataException;
39 import org.krysalis.jcharts.chartData.DataSeries;
40 import org.krysalis.jcharts.chartData.AxisChartDataSet;
41 import org.krysalis.jcharts.axisChart.AxisChart;
42 import org.krysalis.jcharts.axisChart.customRenderers.axisValue.renderers.ValueLabelPosition;
43 import org.krysalis.jcharts.axisChart.customRenderers.axisValue.renderers.ValueLabelRenderer;
44 import org.krysalis.jcharts.axisChart.customRenderers.axisValue.renderers.BackgroundRenderer;
45 import org.krysalis.jcharts.properties.BarChartProperties;
46 import org.krysalis.jcharts.properties.LegendProperties;
47 import org.krysalis.jcharts.properties.AxisProperties;
48 import org.krysalis.jcharts.properties.ChartProperties;
49 import org.krysalis.jcharts.properties.ClusteredBarChartProperties;
50 import org.krysalis.jcharts.properties.StackedBarChartProperties;
51 import org.krysalis.jcharts.properties.util.ChartStroke;
52 import org.krysalis.jcharts.types.ChartType;
53 import org.krysalis.jcharts.test.TestDataGenerator;
54
55 import java.awt.*;
56
57
58 /*************************************************************************************
59  *
60  * @author Nathaniel Auvil
61  * @version $Id: BarChartsGuide.java,v 1.2 2003/06/25 01:40:15 nathaniel_auvil Exp $
62  ************************************************************************************/

63 public class BarChartsGuide extends AxisChartsGuide
64 {
65
66     /*****************************************************************************************
67     * Tests a 'real' data set and usage.
68     *
69     * @throws ChartDataException
70     ******************************************************************************************/

71     public void run() throws ChartDataException
72     {
73         this.barChart();
74         this.stackedBars();
75         this.clusteredBars();
76
77         this.barLabels();
78         this.barWidths();
79         this.barOutlines();
80         this.barBackground();
81     }
82
83
84     /*****************************************************************************************
85     *
86     *
87     ******************************************************************************************/

88     private AxisChart getBarChart( BarChartProperties barChartProperties ) throws ChartDataException
89     {
90         String JavaDoc[] xAxisLabels= { "1998", "1999", "2000", "2001", "2002", "2003", "2004" };
91         String JavaDoc xAxisTitle= "Years";
92         String JavaDoc yAxisTitle= "Problems";
93         String JavaDoc title= "Micro$oft at Work";
94         DataSeries dataSeries = new DataSeries( xAxisLabels, xAxisTitle, yAxisTitle, title );
95
96       double[][] data= new double[][]{ { 250, 45, -36, 66, 145, 80, 55 } };
97         String JavaDoc[] legendLabels= { "Bugs" };
98         Paint[] paints= TestDataGenerator.getRandomPaints( 1 );
99
100         AxisChartDataSet axisChartDataSet= new AxisChartDataSet( data, legendLabels, paints, ChartType.BAR, barChartProperties );
101
102         dataSeries.addIAxisPlotDataSet( axisChartDataSet );
103
104         ChartProperties chartProperties= new ChartProperties();
105         AxisProperties axisProperties= new AxisProperties();
106         LegendProperties legendProperties= new LegendProperties();
107
108         AxisChart axisChart= new AxisChart( dataSeries, chartProperties, axisProperties, legendProperties, AxisChartsGuide.width, AxisChartsGuide.height );
109
110         return axisChart;
111     }
112
113
114     /*****************************************************************************************
115     *
116     *
117     ******************************************************************************************
118     private AxisChart getMultiBarCharts( ChartType chartType, BarChartProperties barChartProperties ) throws ChartDataException
119     {
120         String[] xAxisLabels= { "1998", "1999", "2000", "2001", "2002", "2003", "2004" };
121         String xAxisTitle= "Years";
122         String yAxisTitle= "Problems";
123         String title= "Micro$oft at Work";
124         DataSeries dataSeries = new DataSeries( xAxisLabels, xAxisTitle, yAxisTitle, title );
125
126
127         double[][] data= new double[][]{ { } };
128         String[] legendLabels= { "Bugs", "Security Holes", "Backdoors" };
129         Paint[] paints= TestDataGenerator.getRandomPaints( 3 );
130
131         AxisChartDataSet axisChartDataSet= new AxisChartDataSet( data, legendLabels, paints, chartType, barChartProperties );
132
133         dataSeries.addIAxisPlotDataSet( axisChartDataSet );
134
135         ChartProperties chartProperties= new ChartProperties();
136         AxisProperties axisProperties= new AxisProperties();
137         LegendProperties legendProperties= new LegendProperties();
138
139         AxisChart axisChart= new AxisChart( dataSeries, chartProperties, axisProperties, legendProperties, AxisCharts.width, AxisCharts.height );
140
141         return axisChart;
142     }
143
144
145
146     /******************************************************************************************/

147     private void barChart() throws ChartDataException
148     {
149         String JavaDoc[] xAxisLabels= { "1998", "1999", "2000", "2001", "2002", "2003", "2004" };
150         String JavaDoc xAxisTitle= "Years";
151         String JavaDoc yAxisTitle= "Problems";
152         String JavaDoc title= "Micro$oft at Work";
153         DataSeries dataSeries = new DataSeries( xAxisLabels, xAxisTitle, yAxisTitle, title );
154
155       double[][] data= new double[][]{ { 250, 45, -36, 66, 145, 80, 55 } };
156         String JavaDoc[] legendLabels= { "Bugs" };
157         Paint[] paints= new Paint[]{ Color.blue.darker() };
158         BarChartProperties barChartProperties= new BarChartProperties();
159         AxisChartDataSet axisChartDataSet= new AxisChartDataSet( data, legendLabels, paints, ChartType.BAR, barChartProperties );
160         dataSeries.addIAxisPlotDataSet( axisChartDataSet );
161
162         ChartProperties chartProperties= new ChartProperties();
163         AxisProperties axisProperties= new AxisProperties();
164         LegendProperties legendProperties= new LegendProperties();
165         AxisChart axisChart= new AxisChart( dataSeries, chartProperties, axisProperties, legendProperties, AxisChartsGuide.width, AxisChartsGuide.height );
166
167         super.exportImage( axisChart, "barChart" );
168     }
169
170
171     /******************************************************************************************/
172     private void barLabels() throws ChartDataException
173     {
174         String JavaDoc[] xAxisLabels= { "1998", "1999", "2000", "2001", "2002", "2003", "2004" };
175         String JavaDoc xAxisTitle= "Years";
176         String JavaDoc yAxisTitle= "Problems";
177         String JavaDoc title= "Micro$oft at Work";
178         DataSeries dataSeries = new DataSeries( xAxisLabels, xAxisTitle, yAxisTitle, title );
179
180       double[][] data= new double[][]{ { 250, 45, -36, 66, 145, 80, 55 } };
181         String JavaDoc[] legendLabels= { "Bugs" };
182         Paint[] paints= new Paint[]{ Color.yellow };
183         BarChartProperties barChartProperties= new BarChartProperties();
184
185         ValueLabelRenderer valueLabelRenderer = new ValueLabelRenderer( false, false, true, -1 );
186         valueLabelRenderer.setValueLabelPosition( ValueLabelPosition.AT_TOP );
187         valueLabelRenderer.useVerticalLabels( false );
188         barChartProperties.addPostRenderEventListener( valueLabelRenderer );
189
190
191         AxisChartDataSet axisChartDataSet= new AxisChartDataSet( data, legendLabels, paints, ChartType.BAR, barChartProperties );
192         dataSeries.addIAxisPlotDataSet( axisChartDataSet );
193
194         ChartProperties chartProperties= new ChartProperties();
195         AxisProperties axisProperties= new AxisProperties( true );
196         LegendProperties legendProperties= new LegendProperties();
197         AxisChart axisChart= new AxisChart( dataSeries, chartProperties, axisProperties, legendProperties, AxisChartsGuide.width, AxisChartsGuide.height );
198
199         super.exportImage( axisChart, "barLabels" );
200     }
201
202
203
204     /******************************************************************************************/
205     private void barBackground() throws ChartDataException
206     {
207         String JavaDoc[] xAxisLabels= { "1998", "1999", "2000", "2001", "2002", "2003", "2004" };
208         String JavaDoc xAxisTitle= "Years";
209         String JavaDoc yAxisTitle= "Problems";
210         String JavaDoc title= "Micro$oft at Work";
211         DataSeries dataSeries = new DataSeries( xAxisLabels, xAxisTitle, yAxisTitle, title );
212
213       double[][] data= new double[][]{ { 250, 45, -36, 66, 145, 80, 55 } };
214         String JavaDoc[] legendLabels= { "Bugs" };
215         Paint[] paints= new Paint[]{ Color.blue.darker() };
216         BarChartProperties barChartProperties= new BarChartProperties();
217
218         //---paints over the background of every other bar area.
219
BackgroundRenderer backgroundRenderer = new BackgroundRenderer( new Color( 20, 20, 20, 50 ) );
220         barChartProperties.addPreRenderEventListener( backgroundRenderer );
221
222         AxisChartDataSet axisChartDataSet= new AxisChartDataSet( data, legendLabels, paints, ChartType.BAR, barChartProperties );
223         dataSeries.addIAxisPlotDataSet( axisChartDataSet );
224
225         ChartProperties chartProperties= new ChartProperties();
226         AxisProperties axisProperties= new AxisProperties();
227         LegendProperties legendProperties= new LegendProperties();
228         AxisChart axisChart= new AxisChart( dataSeries, chartProperties, axisProperties, legendProperties, AxisChartsGuide.width, AxisChartsGuide.height );
229
230         super.exportImage( axisChart, "barBackground" );
231     }
232
233
234     /**********************************************************************************************/
235     private void stackedBars() throws ChartDataException
236     {
237         String JavaDoc[] xAxisLabels= { "1998", "1999", "2000", "2001", "2002", "2003", "2004" };
238         String JavaDoc xAxisTitle= "Years";
239         String JavaDoc yAxisTitle= "Problems";
240         String JavaDoc title= "Micro$oft at Work";
241         DataSeries dataSeries = new DataSeries( xAxisLabels, xAxisTitle, yAxisTitle, title );
242
243         double[][] data= new double[][]{ { 250, 45, 36, 66, 145, 80, 55 }, { 150, 15, 6, 62, 54, 10, 84 }, { 250, 45, 36, 66, 145, 80, 55 } };
244         String JavaDoc[] legendLabels= { "Bugs", "Security Holes", "Backdoors" };
245         Paint[] paints= TestDataGenerator.getRandomPaints( 3 );
246         StackedBarChartProperties stackedBarChartProperties= new StackedBarChartProperties();
247         AxisChartDataSet axisChartDataSet= new AxisChartDataSet( data, legendLabels, paints, ChartType.BAR_STACKED, stackedBarChartProperties );
248         dataSeries.addIAxisPlotDataSet( axisChartDataSet );
249
250         ChartProperties chartProperties= new ChartProperties();
251         AxisProperties axisProperties= new AxisProperties();
252         LegendProperties legendProperties= new LegendProperties();
253         AxisChart axisChart= new AxisChart( dataSeries, chartProperties, axisProperties, legendProperties, AxisChartsGuide.width, AxisChartsGuide.height );
254
255         super.exportImage( axisChart, "stackedBarChart" );
256     }
257
258
259     /**********************************************************************************************/
260     private void clusteredBars() throws ChartDataException
261     {
262         String JavaDoc[] xAxisLabels= { "1998", "1999", "2000", "2001", "2002", "2003", "2004" };
263         String JavaDoc xAxisTitle= "Years";
264         String JavaDoc yAxisTitle= "Problems";
265         String JavaDoc title= "Micro$oft at Work";
266         DataSeries dataSeries = new DataSeries( xAxisLabels, xAxisTitle, yAxisTitle, title );
267
268         double[][] data= new double[][]{ { 250, 45, -36, 66, 145, 80, 55 }, { 150, 15, 6, 62, -54, 10, 84 }, { 20, 145, 36, 6, 45, 18, 5 } };
269         String JavaDoc[] legendLabels= { "Bugs", "Security Holes", "Backdoors" };
270         Paint[] paints= TestDataGenerator.getRandomPaints( 3 );
271         ClusteredBarChartProperties clusteredBarChartProperties= new ClusteredBarChartProperties();
272         AxisChartDataSet axisChartDataSet= new AxisChartDataSet( data, legendLabels, paints, ChartType.BAR_CLUSTERED, clusteredBarChartProperties );
273         dataSeries.addIAxisPlotDataSet( axisChartDataSet );
274
275         ChartProperties chartProperties= new ChartProperties();
276         AxisProperties axisProperties= new AxisProperties();
277         LegendProperties legendProperties= new LegendProperties();
278         AxisChart axisChart= new AxisChart( dataSeries, chartProperties, axisProperties, legendProperties, AxisChartsGuide.width, AxisChartsGuide.height );
279
280         super.exportImage( axisChart, "clusteredBarChart" );
281     }
282
283
284
285     /******************************************************************************************/
286     private void barWidths() throws ChartDataException
287     {
288         BarChartProperties barChartProperties= new BarChartProperties();
289         barChartProperties.setWidthPercentage( 1f );
290
291         AxisChart axisChart= this.getBarChart( barChartProperties );
292
293         super.exportImage( axisChart, "barChartWidths" );
294     }
295
296     /******************************************************************************************/
297     private void barOutlines() throws ChartDataException
298     {
299         BarChartProperties barChartProperties= new BarChartProperties();
300         barChartProperties.setShowOutlinesFlag( true );
301         ChartStroke outlineChartStroke= new ChartStroke( new BasicStroke( 2.0f ), Color.red );
302         barChartProperties.setBarOutlineStroke( outlineChartStroke );
303
304         AxisChart axisChart= this.getBarChart( barChartProperties );
305
306         super.exportImage( axisChart, "barChartOutlines" );
307     }
308
309
310
311 }
312
313
Popular Tags