KickJava   Java API By Example, From Geeks To Geeks.

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


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
36 package org.krysalis.jcharts.demo.userGuide;
37
38
39 import org.krysalis.jcharts.chartData.DataSeries;
40 import org.krysalis.jcharts.chartData.ChartDataException;
41 import org.krysalis.jcharts.chartData.AxisChartDataSet;
42 import org.krysalis.jcharts.axisChart.AxisChart;
43 import org.krysalis.jcharts.properties.AxisProperties;
44 import org.krysalis.jcharts.properties.AreaChartProperties;
45 import org.krysalis.jcharts.properties.ChartProperties;
46 import org.krysalis.jcharts.properties.LegendProperties;
47 import org.krysalis.jcharts.properties.PropertyException;
48 import org.krysalis.jcharts.properties.BarChartProperties;
49 import org.krysalis.jcharts.properties.DataAxisProperties;
50 import org.krysalis.jcharts.properties.AxisTypeProperties;
51 import org.krysalis.jcharts.properties.ChartTypeProperties;
52 import org.krysalis.jcharts.properties.util.ChartStroke;
53 import org.krysalis.jcharts.properties.util.ChartFont;
54 import org.krysalis.jcharts.types.ChartType;
55 import org.krysalis.jcharts.test.TestDataGenerator;
56
57 import java.awt.*;
58
59
60 /*************************************************************************************
61  *
62  * @author Nathaniel Auvil
63  * @version $Id: AxisChartsGuide.java,v 1.3 2003/07/05 13:24:35 nathaniel_auvil Exp $
64  ************************************************************************************/

65 public class AxisChartsGuide extends UserGuideBase
66 {
67     public static final int width = 500;
68     public static final int height = 320;
69
70
71     /*****************************************************************************************
72      *
73      *
74      ******************************************************************************************/

75     private AxisChart getChart( AxisProperties axisProperties ) throws ChartDataException
76     {
77         String JavaDoc[] xAxisLabels = {"1998", "1999", "2000", "2001", "2002", "2003", "2004"};
78         String JavaDoc xAxisTitle = "Years";
79         String JavaDoc yAxisTitle = "Problems";
80         String JavaDoc title = "Micro$oft at Work";
81         DataSeries dataSeries = new DataSeries( xAxisLabels, xAxisTitle, yAxisTitle, title );
82
83         dataSeries.addIAxisPlotDataSet( AxisChartsGuide.createAxisChartDataSet( ChartType.AREA, new AreaChartProperties(), 200, 5000 ) );
84
85         ChartProperties chartProperties = new ChartProperties();
86         LegendProperties legendProperties = new LegendProperties();
87
88         AxisChart axisChart = new AxisChart( dataSeries, chartProperties, axisProperties, legendProperties, AxisChartsGuide.width, AxisChartsGuide.height );
89
90         return axisChart;
91     }
92
93
94     /*****************************************************************************************
95      * Tests a 'real' data set and usage.
96      *
97      * @throws ChartDataException
98      ******************************************************************************************/

99     public void run() throws ChartDataException, PropertyException
100     {
101         this.horizontalPlots();
102
103
104         this.rounding();
105         this.userDefinedScale();
106
107         this.numberOfItemsOnYAxis();
108         this.gridLines();
109
110         this.dollarSigns();
111         this.commas();
112
113         this.axisBackgroundPaint();
114         this.axisTitles();
115
116         this.zeroLine();
117         this.scaleFont();
118
119         this.tickMarks();
120
121         this.verticalXAxisLabels();
122     }
123
124
125     /*****************************************************************************************/
126     private void horizontalPlots() throws ChartDataException
127     {
128         AxisProperties axisProperties = new AxisProperties( true );
129
130         double[][] data = {{3444d, 1506.3d, 2777d, 2550.345d, 659.667d, 950.6644d, 4500.3453d, 1200.67583d}};
131         String JavaDoc[] xAxisLabels = {"January", "Febuary", "March", "April", "May", "June", "July", "August"};
132         String JavaDoc[] legendLabels = {"New Bugs in Windows Per Month"};
133         Paint[] paints = {Color.blue};
134         String JavaDoc xAxisTitle = "Months";
135         String JavaDoc yAxisTitle = "Bugs";
136         String JavaDoc title = "Micro$oft At Work";
137
138         DataSeries dataSeries = new DataSeries( xAxisLabels, xAxisTitle, yAxisTitle, title );
139         AxisChartDataSet axisChartDataSet = new AxisChartDataSet( data, legendLabels, paints, ChartType.BAR, new BarChartProperties() );
140
141         dataSeries.addIAxisPlotDataSet( axisChartDataSet );
142
143         AxisChart axisChart = new AxisChart( dataSeries, new ChartProperties(), axisProperties, new LegendProperties(), width, height );
144         super.exportImage( axisChart, "horizontalPlots" );
145     }
146
147
148     /*****************************************************************************************/
149     private void rounding() throws ChartDataException
150     {
151         AxisProperties axisProperties = new AxisProperties();
152
153         DataAxisProperties dataAxisProperties = (DataAxisProperties) axisProperties.getYAxisProperties();
154         dataAxisProperties.setRoundToNearest( -2 );
155
156         super.exportImage( this.getChart( axisProperties ), "axisRounding" );
157     }
158
159
160     /*****************************************************************************************/
161     private void userDefinedScale() throws ChartDataException, PropertyException
162     {
163         AxisProperties axisProperties = new AxisProperties();
164
165         DataAxisProperties dataAxisProperties = (DataAxisProperties) axisProperties.getYAxisProperties();
166         dataAxisProperties.setUserDefinedScale( -2000, 850 );
167         dataAxisProperties.setRoundToNearest( 0 );
168
169         super.exportImage( this.getChart( axisProperties ), "userDefinedScale" );
170     }
171
172 //todo what about ScaleCalculator implementations?
173

174     /*****************************************************************************************/
175     private void numberOfItemsOnYAxis() throws ChartDataException
176     {
177         AxisProperties axisProperties = new AxisProperties();
178         DataAxisProperties dataAxisProperties = (DataAxisProperties) axisProperties.getYAxisProperties();
179         dataAxisProperties.setNumItems( 8 );
180
181         super.exportImage( this.getChart( axisProperties ), "numberOfItemsOnYAxis" );
182     }
183
184
185     /*****************************************************************************************/
186     private void gridLines() throws ChartDataException
187     {
188         AxisProperties axisProperties = new AxisProperties();
189
190         ChartStroke xAxisGridLines = new ChartStroke( new BasicStroke( 1.0f ), Color.red );
191         axisProperties.getXAxisProperties().setGridLineChartStroke( xAxisGridLines );
192         axisProperties.getXAxisProperties().setShowGridLines( AxisTypeProperties.GRID_LINES_ONLY_WITH_LABELS );
193
194         axisProperties.getYAxisProperties().setShowGridLines( AxisTypeProperties.GRID_LINES_NONE );
195
196
197         double[][] data = {{3444d, 1506.3d, 2777d, 2550.345d, 659.667d, 950.6644d, 4500.3453d, 1200.67583d, 3000.4354d, 1268.0001d, 2444.432d, 5003d}};
198         String JavaDoc[] xAxisLabels = {"January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
199         String JavaDoc[] legendLabels = {"New Bugs in Windows Per Month"};
200         Paint[] paints = {Color.blue};
201         String JavaDoc xAxisTitle = "Months";
202         String JavaDoc yAxisTitle = "Bugs";
203         String JavaDoc title = "Micro$oft At Work";
204
205         DataSeries dataSeries = new DataSeries( xAxisLabels, xAxisTitle, yAxisTitle, title );
206         AxisChartDataSet axisChartDataSet = new AxisChartDataSet( data, legendLabels, paints, ChartType.AREA, new AreaChartProperties() );
207
208         dataSeries.addIAxisPlotDataSet( axisChartDataSet );
209
210         AxisChart axisChart = new AxisChart( dataSeries, new ChartProperties(), axisProperties, new LegendProperties(), width, height );
211         super.exportImage( axisChart, "gridLines" );
212     }
213
214
215     /*****************************************************************************************/
216     private void dollarSigns() throws ChartDataException
217     {
218         AxisProperties axisProperties = new AxisProperties();
219         DataAxisProperties dataAxisProperties = (DataAxisProperties) axisProperties.getYAxisProperties();
220         dataAxisProperties.setUseDollarSigns( true );
221         super.exportImage( this.getChart( axisProperties ), "dollarSigns" );
222     }
223
224
225     /*****************************************************************************************/
226     private void commas() throws ChartDataException
227     {
228         AxisProperties axisProperties = new AxisProperties();
229         DataAxisProperties dataAxisProperties = (DataAxisProperties) axisProperties.getYAxisProperties();
230         dataAxisProperties.setUseCommas( false );
231         super.exportImage( this.getChart( axisProperties ), "commas" );
232     }
233
234
235     /*****************************************************************************************/
236     private void axisBackgroundPaint() throws ChartDataException
237     {
238         AxisProperties axisProperties = new AxisProperties();
239         axisProperties.setBackgroundPaint( Color.yellow );
240         super.exportImage( this.getChart( axisProperties ), "axisBackground" );
241     }
242
243
244     /*****************************************************************************************/
245     private void axisTitles() throws ChartDataException
246     {
247         double[][] data = {{3444d, 1506.3d, 2777d, 2550.345d, 659.667d, 950.6644d, 4500.3453d, 1200.67583d, 3000.4354d, 1268.0001d, 2444.432d, 5003d}};
248         String JavaDoc[] xAxisLabels = {"January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
249         String JavaDoc[] legendLabels = {"New Bugs in Windows Per Month"};
250         Paint[] paints = {Color.blue};
251         String JavaDoc xAxisTitle = "Months";
252         String JavaDoc yAxisTitle = "Bugs";
253         String JavaDoc title = "Micro$oft At Work";
254         AxisChartDataSet axisChartDataSet = new AxisChartDataSet( data, legendLabels, paints, ChartType.AREA, new AreaChartProperties() );
255
256
257         DataSeries dataSeries = new DataSeries( xAxisLabels, null, yAxisTitle, title );
258         dataSeries.addIAxisPlotDataSet( axisChartDataSet );
259
260         AxisChart axisChart = new AxisChart( dataSeries, new ChartProperties(), new AxisProperties(), new LegendProperties(), width, height );
261         super.exportImage( axisChart, "axisTitles" );
262
263         AxisProperties axisProperties = new AxisProperties();
264         ChartFont yAxisFont = new ChartFont( new Font( "Arial Narrow", Font.BOLD, 14 ), Color.blue );
265         axisProperties.getYAxisProperties().setAxisTitleChartFont( yAxisFont );
266
267         axisChart = new AxisChart( dataSeries, new ChartProperties(), axisProperties, new LegendProperties(), width, height );
268         super.exportImage( axisChart, "axisTitleFont" );
269     }
270
271
272     /*****************************************************************************************/
273     private void zeroLine() throws ChartDataException, PropertyException
274     {
275         AxisProperties axisProperties = new AxisProperties();
276
277         DataAxisProperties dataAxisProperties = (DataAxisProperties) axisProperties.getYAxisProperties();
278         dataAxisProperties.setUserDefinedScale( -2000, 1200 );
279         dataAxisProperties.setShowZeroLine( false );
280         super.exportImage( this.getChart( axisProperties ), "noZeroLine" );
281
282         dataAxisProperties.setShowZeroLine( true );
283
284         BasicStroke stroke = new BasicStroke( 1f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 5f, new float[]{5f, 5f, 10f, 5f}, 4f );
285         ChartStroke zeroLineChartStroke = new ChartStroke( stroke, Color.red );
286         dataAxisProperties.setZeroLineChartStroke( zeroLineChartStroke );
287         super.exportImage( this.getChart( axisProperties ), "zeroLinePaintStroke" );
288     }
289
290
291     /*****************************************************************************************/
292     private void scaleFont() throws ChartDataException
293     {
294         AxisProperties axisProperties = new AxisProperties();
295         ChartFont xScaleChartFont = new ChartFont( new Font( "Georgia Negreta cursiva", Font.PLAIN, 13 ), Color.blue );
296         axisProperties.getXAxisProperties().setScaleChartFont( xScaleChartFont );
297         super.exportImage( this.getChart( axisProperties ), "scaleFont" );
298     }
299
300
301     /*****************************************************************************************/
302     private void verticalXAxisLabels() throws ChartDataException
303     {
304         AxisProperties axisProperties = new AxisProperties();
305         axisProperties.setXAxisLabelsAreVertical( true );
306         super.exportImage( this.getChart( axisProperties ), "verticalXAxisLabels" );
307     }
308
309
310     /*****************************************************************************************/
311     private void tickMarks() throws ChartDataException
312     {
313         AxisProperties axisProperties = new AxisProperties();
314         axisProperties.getXAxisProperties().setShowTicks( AxisTypeProperties.TICKS_ONLY_WITH_LABELS );
315         super.exportImage( this.getChart( axisProperties ), "ticksWithLabels" );
316
317
318         axisProperties = new AxisProperties();
319         axisProperties.getXAxisProperties().setShowTicks( AxisTypeProperties.TICKS_NONE );
320         super.exportImage( this.getChart( axisProperties ), "noTicks" );
321
322
323         axisProperties = new AxisProperties();
324         ChartStroke xTicks = new ChartStroke( new BasicStroke( 1.0f ), Color.blue );
325         axisProperties.getXAxisProperties().setTickChartStroke( xTicks );
326
327         ChartStroke yTicks = new ChartStroke( new BasicStroke( 1.0f ), Color.green );
328         axisProperties.getYAxisProperties().setTickChartStroke( yTicks );
329
330         super.exportImage( this.getChart( axisProperties ), "tickColors" );
331
332
333         axisProperties = new AxisProperties();
334         ChartStroke xTickStroke = new ChartStroke( new BasicStroke( 2.0f ), Color.blue );
335         axisProperties.getXAxisProperties().setTickChartStroke( xTickStroke );
336         super.exportImage( this.getChart( axisProperties ), "tickStroke" );
337     }
338
339
340     /*****************************************************************************************************
341      *
342      *
343      *****************************************************************************************************/

344     public static final DataSeries createDataSeries()
345     {
346         String JavaDoc[] xAxisLabels = { "1998", "1999", "2000", "2001", "2002", "2003", "2004" };
347         String JavaDoc xAxisTitle = "Years";
348         String JavaDoc yAxisTitle = "Problems";
349         String JavaDoc title = null;
350
351         return new DataSeries( xAxisLabels, xAxisTitle, yAxisTitle, "Micro$oft At Work" );
352     }
353
354
355     /*****************************************************************************************
356      *
357      *
358      * @param minValue
359      * @param maxValue
360      * @return AxisChartDataSet
361      ******************************************************************************************/

362     public static final AxisChartDataSet createAxisChartDataSet( ChartType chartType,
363                                                                                      ChartTypeProperties chartTypeProperties,
364                                                                                      int numberOfDataSets,
365                                                                                      String JavaDoc[] legendLabels,
366                                                                                      int minValue,
367                                                                                      int maxValue ) throws ChartDataException
368     {
369         double[][] data = TestDataGenerator.getRandomNumbers( numberOfDataSets, 7, minValue, maxValue );
370         Paint[] paints = TestDataGenerator.getRandomPaints( numberOfDataSets );
371
372         return new AxisChartDataSet( data, legendLabels, paints, chartType, chartTypeProperties );
373     }
374
375
376     /*****************************************************************************************
377      *
378      *
379      * @param minValue
380      * @param maxValue
381      * @return AxisChartDataSet
382      ******************************************************************************************/

383     public static final AxisChartDataSet createAxisChartDataSet( ChartType chartType,
384                                                                                      ChartTypeProperties chartTypeProperties,
385                                                                                      int minValue,
386                                                                                      int maxValue ) throws ChartDataException
387     {
388         double[][] data = TestDataGenerator.getRandomNumbers( 1, 7, minValue, maxValue );
389         String JavaDoc[] legendLabels = { "Bugs" };
390         Paint[] paints = TestDataGenerator.getRandomPaints( 1 );
391
392         return new AxisChartDataSet( data, legendLabels, paints, chartType, chartTypeProperties );
393     }
394
395 }
396
397
398
399
400
401
Popular Tags