KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > krysalis > jcharts > test > ChartTestDriver


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

39
40 package org.krysalis.jcharts.test;
41
42
43 import org.krysalis.jcharts.Chart;
44 import org.krysalis.jcharts.properties.PropertyException;
45 import org.krysalis.jcharts.chartData.ChartDataException;
46 import org.krysalis.jcharts.encoders.*;
47
48 import java.io.*;
49
50
51 /******************************************************************************************
52  * This file provides examples of how to create all the different chart types provided by
53  * this package.
54  *
55  *******************************************************************************************/

56 public final class ChartTestDriver
57 {
58     private final static String JavaDoc SVG = ".svg";
59     private final static String JavaDoc PNG = ".png";
60     private final static String JavaDoc JPEG = ".jpg";
61     private final static String JavaDoc JPEG_LEGACY = ".jpeg";
62     final static String JavaDoc OUTPUT_PATH = "";
63
64     final static String JavaDoc EXTENSION = PNG;
65
66
67     /*****************************************************************************************
68      * Main method so can run as command line.
69      *
70      * @param args command line arguements.
71      *****************************************************************************************/

72     public static void main( String JavaDoc[] args ) throws ChartDataException, PropertyException
73     {
74         StopWatch stopWatch = new StopWatch();
75         stopWatch.start();
76
77       AxisChartTestBase.axisChartTest( "barChart", new BarTestDriver() );
78
79 /*
80         AxisChartTestBase.axisChartTest( "stockChart", new StockTestDriver() );
81
82         AxisChartTestBase.axisChartTest( "areaChart", new AreaTestDriver() );
83
84         AxisChartTestBase.axisChartTest( "barChart", new BarTestDriver() );
85         AxisChartTestBase.axisChartTest( "stackedBarChart", new StackedBarTestDriver() );
86         AxisChartTestBase.axisChartTest( "clusteredBarChart", new ClusteredBarTestDriver() );
87 */

88
89 // AxisChartTestBase.axisChartTest( "comboChart", new ComboTestDriver() );
90

91
92 /*
93         PieTestDriver.test();
94
95
96         AxisChartTestBase.axisChartTest( "pointChart", new PointTestDriver() );
97
98         AxisChartTestBase.axisChartTest( "lineChart", new LineTestDriver() );
99
100 */

101 // AxisChartTestBase.axisChartTest( "scatterChart", new ScatterPlotTestDriver() );
102

103
104         //ChartTestDriver.testAxisScale();
105

106
107         stopWatch.stop();
108         System.out.println( stopWatch );
109     }
110
111
112     private static void testAxisScale()
113     {
114
115         double yMax = -15;
116         double yMin = -130;
117
118         // In the following line, note that Math.log is actually Natural Logarithm.
119
// log base a of b = ln b / ln a => log base 10 of x = ln 10 / ln x
120
double yDelta = Math.pow( 10.0, Math.round( Math.log( yMax - yMin ) / Math.log( 10 ) ) );
121         double yStart = yMin - (yMin % yDelta);
122         double yEnd = yMax - (yMax % yDelta) + yDelta;
123
124         System.out.println( "yDelta= " + yDelta + " yStart= " + yStart + " yEnd= " + yEnd );
125
126
127         // Count the number of segments this gives us. Shoot for 20 segments or so.
128
int segments = ( int ) ((yEnd - yStart) / yDelta);
129
130         if( segments <= 2 )
131         {
132             // we need 10 times this many
133
yDelta = yDelta / 10.0;
134         }
135         else if( segments <= 5 )
136         {
137             // we need 4 times this many
138
yDelta = yDelta / 4.0;
139         }
140         else if( segments <= 10 )
141         {
142             yDelta = yDelta / 2.0;
143         }
144
145         // Recalc start and end to match with new delta.
146
yStart = yMin - (yMin % yDelta);
147         yEnd = yMax - (yMax % yDelta) + yDelta;
148         segments = ( int ) ((yEnd - yStart) / yDelta);
149
150 /*
151
152         axisProperties = new AxisProperties(yStart, yDelta);
153         axisProperties.setYAxisNumItems(segments);
154 */

155
156
157     }
158
159
160     /******************************************************************************************
161      * Utility method to write the image to file so I do not have to deal with file i/o
162      * every time I write a test.
163      *
164      * @param chart verything that is renderable extends this class.
165      * @param fileName what to name the file
166      * @throws ChartDataException
167      * @throws PropertyException
168      ******************************************************************************************/

169     static void exportImage( Chart chart, String JavaDoc fileName ) throws ChartDataException, PropertyException
170     {
171         try
172         {
173             FileOutputStream fileOutputStream = new FileOutputStream( fileName );
174
175             if( EXTENSION.equals( SVG ) )
176             {
177                 SVGEncoder.encode( chart, fileOutputStream );
178             }
179             else if( EXTENSION.equals( PNG ) )
180             {
181                 PNGEncoder.encode( chart, fileOutputStream );
182             }
183             else if( EXTENSION.equals( JPEG ) )
184             {
185                 JPEGEncoder.encode( chart, 1.0f, fileOutputStream );
186             }
187             else if( EXTENSION.equals( JPEG_LEGACY ) )
188             {
189                 JPEGEncoder13.encode( chart, 1.0f, fileOutputStream );
190             }
191             else
192             {
193                 System.out.println( "unknown file type to encode: " + EXTENSION );
194             }
195
196             fileOutputStream.flush();
197             fileOutputStream.close();
198         }
199         catch( FileNotFoundException fileNotFoundException )
200         {
201             fileNotFoundException.printStackTrace();
202         }
203         catch( IOException ioException )
204         {
205             ioException.printStackTrace();
206         }
207     }
208
209
210 }
211
Popular Tags