KickJava   Java API By Example, From Geeks To Geeks.

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


1 /***********************************************************************************************
2  * File Info: $Id: AxisChartTestBase.java,v 1.3 2004/05/27 02:14:59 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
41 package org.krysalis.jcharts.test;
42
43
44 import org.krysalis.jcharts.axisChart.AxisChart;
45 import org.krysalis.jcharts.chartData.*;
46 import org.krysalis.jcharts.properties.*;
47 import org.krysalis.jcharts.types.ChartType;
48 import org.krysalis.jcharts.imageMap.ImageMap;
49
50 import java.awt.*;
51
52
53 /******************************************************************************************
54  *
55  *
56  *******************************************************************************************/

57 abstract class AxisChartTestBase
58 {
59
60     public AxisChartTestBase()
61     {
62         System.out.println( "Running: " + this.getClass().getName() );
63     }
64
65
66     abstract DataSeries getDataSeries() throws ChartDataException;
67     abstract boolean supportsImageMap();
68
69
70     /******************************************************************************************
71      * Test for LineChart
72      *
73      ******************************************************************************************/

74     static void axisChartTest( String JavaDoc name, AxisChartTestBase axisChartTestBase ) throws ChartDataException, PropertyException
75     {
76         LegendProperties legendProperties;
77         ChartProperties chartProperties;
78         AxisProperties axisProperties;
79         AxisChart axisChart;
80
81         int numTestsToRun = 10;
82         String JavaDoc fileName;
83
84         HTMLGenerator htmlGenerator = new HTMLGenerator( ChartTestDriver.OUTPUT_PATH + name + "Test.html" );
85
86         for( int i = 0; i < numTestsToRun; i++ )
87         {
88             DataSeries dataSeries= axisChartTestBase.getDataSeries();
89
90             boolean horizontalPlot= true; //( TestDataGenerator.getRandomNumber( 1 ) > 0.5d );
91
boolean createImageMap= true; //( TestDataGenerator.getRandomNumber( 1 ) > 0.5d );
92

93             axisProperties = new AxisProperties( horizontalPlot );
94             axisProperties.setXAxisLabelsAreVertical( ( TestDataGenerator.getRandomNumber( 1 ) > 0.5d ) );
95
96             TestDataGenerator.randomizeAxisProperties( axisProperties );
97             //axisProperties.setYAxisNumItems( 6 );
98

99             DataAxisProperties dataAxisProperties;
100             LabelAxisProperties labelAxisProperties;
101
102             if( horizontalPlot )
103             {
104                 dataAxisProperties = ( DataAxisProperties ) axisProperties.getXAxisProperties();
105                 labelAxisProperties = ( LabelAxisProperties ) axisProperties.getYAxisProperties();
106             }
107             else
108             {
109                 dataAxisProperties = ( DataAxisProperties ) axisProperties.getYAxisProperties();
110                 labelAxisProperties = ( LabelAxisProperties ) axisProperties.getXAxisProperties();
111             }
112
113             dataAxisProperties.setRoundToNearest( 1 );
114             //dataAxisProperties.setUserDefinedScale( 10, 500 );
115

116
117             //---if the labels are NULL, force to zero.
118
if( dataSeries.getNumberOfAxisLabels() == 0 )
119             {
120                 labelAxisProperties.setShowAxisLabels( false );
121             }
122             else
123             {
124                 labelAxisProperties.setShowAxisLabels( ( TestDataGenerator.getRandomNumber( 1 ) > 0.3d ) );
125             }
126
127
128             int width = ( int ) TestDataGenerator.getRandomNumber( 500, 1000 );
129             int height = ( int ) TestDataGenerator.getRandomNumber( 300, 800 );
130
131             legendProperties = new LegendProperties();
132             TestDataGenerator.randomizeLegend( legendProperties );
133             //legendProperties.setPlacement( LegendAreaProperties.RIGHT );
134

135             chartProperties = new ChartProperties();
136             //chartProperties.setBorderStroke( new BasicStroke( 1f ) );
137

138
139             axisChart = new AxisChart( dataSeries, chartProperties, axisProperties, legendProperties, width, height );
140
141
142             fileName = ChartTestDriver.OUTPUT_PATH + name + i + ChartTestDriver.EXTENSION;
143
144
145             ImageMap imageMap;
146             if( createImageMap && axisChartTestBase.supportsImageMap() )
147             {
148                 axisChart.renderWithImageMap();
149                 imageMap= axisChart.getImageMap();
150             }
151             else
152             {
153                 imageMap= null;
154             }
155
156             try
157             {
158                 ChartTestDriver.exportImage( axisChart, fileName );
159             }
160             catch( NullPointerException JavaDoc nullPointerException )
161             {
162                 nullPointerException.printStackTrace();
163                 System.out.println();
164             }
165
166             axisChart.toHTML( htmlGenerator, fileName, imageMap );
167             htmlGenerator.addLineBreak();
168         }
169
170         htmlGenerator.saveFile();
171     }
172
173
174     /*****************************************************************************************************
175      *
176      *
177      *****************************************************************************************************/

178     final DataSeries createDataSeries( int numberOfValuesToCreate )
179     {
180         String JavaDoc[] xAxisLabels= null;
181         if( ( TestDataGenerator.getRandomNumber( 1 ) > 0.3d ) )
182         {
183             xAxisLabels = TestDataGenerator.getRandomStrings( numberOfValuesToCreate, ( int ) TestDataGenerator.getRandomNumber( 10 ), false );
184         }
185         String JavaDoc xAxisTitle = TestDataGenerator.getRandomString( 15, true );
186         String JavaDoc yAxisTitle = TestDataGenerator.getRandomString( 15, true );
187
188         return new DataSeries( xAxisLabels, xAxisTitle, yAxisTitle, "This is a test title that is so freaking long is is going to wrap around the image for sure. lfksjg;ljs; dflgkjdfgsdgdg dsgdsgsdfg dsfgsdfgsdfgs dfgdsfgd" );
189     }
190
191
192     /*****************************************************************************************
193      * Generates a random MultiDataSet
194      *
195      * @param numberOfDataSets
196      * @param numberOfValuesToCreate the number of doubles to generate
197      * @param minValue
198      * @param maxValue
199      * @return AxisChartDataSet
200      ******************************************************************************************/

201     AxisChartDataSet createAxisChartDataSet( ChartType chartType,
202                                                                   ChartTypeProperties chartTypeProperties,
203                                                                   int numberOfDataSets,
204                                                                   int numberOfValuesToCreate,
205                                                                   int minValue,
206                                                                   int maxValue ) throws ChartDataException
207     {
208         double[][] data = TestDataGenerator.getRandomNumbers( numberOfDataSets, numberOfValuesToCreate, minValue, maxValue );
209         String JavaDoc[] legendLabels = TestDataGenerator.getRandomStrings( numberOfDataSets, 10, false );
210         Paint[] paints = TestDataGenerator.getRandomPaints( numberOfDataSets );
211
212
213 /*
214         //data[ 0 ][ 0 ]= Double.NaN;
215         data[ 0 ][ 1 ]= Double.NaN;
216
217
218         data[ 0 ][ 4 ]= Double.NaN;
219         data[ 0 ][ 6 ]= Double.NaN;
220
221         //data[ 0 ][ data[ 0 ].length - 1 ]= Double.NaN;
222         data[ 0 ][ data[ 0 ].length - 2 ]= Double.NaN;
223 */

224
225         return new AxisChartDataSet( data, legendLabels, paints, chartType, chartTypeProperties );
226     }
227 }
228
Popular Tags