KickJava   Java API By Example, From Geeks To Geeks.

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


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

48
49
50 package org.krysalis.jcharts.test;
51
52
53 import org.krysalis.jcharts.properties.*;
54 import org.krysalis.jcharts.properties.util.ChartStroke;
55 import org.krysalis.jcharts.properties.util.ChartFont;
56 import org.krysalis.jcharts.chartData.ScatterPlotDataSet;
57
58 import java.awt.*;
59
60
61 final public class TestDataGenerator
62 {
63     private final static Font[] ALL_FONTS=GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
64
65
66     /*****************************************************************************************
67      * Random font generator based on the available Fonts on this machine.
68      *
69      * @param minSize
70      * @param maxSize
71      * @return Font
72      ******************************************************************************************/

73     public static Font getRandomFont( double minSize, double maxSize )
74     {
75         Font font=ALL_FONTS[ (int) TestDataGenerator.getRandomNumber( ALL_FONTS.length ) ];
76         font=font.deriveFont( (float) TestDataGenerator.getRandomNumber( minSize, maxSize ) );
77         return font;
78     }
79
80
81     /*****************************************************************************************
82      * Random number generator.
83      *
84      * @param maxValue
85      * @return double
86      ******************************************************************************************/

87     public static double getRandomNumber( double maxValue )
88     {
89         return Math.random() * maxValue;
90     }
91
92
93     /*****************************************************************************************
94      * Random number generator in specified range.
95      *
96      * @param minValue
97      * @param maxValue
98      * @return double
99      ******************************************************************************************/

100     protected static double getRandomNumber( double minValue, double maxValue )
101     {
102         return ( minValue + ( Math.random() * ( maxValue - minValue ) ) );
103     }
104
105
106     /*****************************************************************************************
107      * Random number generator in specified range. Will include zeros a small percentage of
108      * the time for testing.
109      *
110      * @param minValue
111      * @param maxValue
112      * @return double
113      ******************************************************************************************/

114     protected static double getRandomNumberIncludingZero( double minValue, double maxValue )
115     {
116         if( ( TestDataGenerator.getRandomNumber( 1 ) > 0.8d ) )
117         {
118             return 0.0d;
119         }
120         else
121         {
122             return getRandomNumber( minValue, maxValue );
123         }
124     }
125
126     
127     /*****************************************************************************************
128      * Random numbers generator in specified range.
129      *
130      * @param numToGenerate the number of doubles to generate
131      * @param minValue
132      * @param maxValue
133      * @return double[]
134      ******************************************************************************************/

135     public static double[] getRandomNumbers( int numToGenerate, double minValue, double maxValue )
136     {
137         double[] data=new double[ numToGenerate ];
138         for( int i=0; i < numToGenerate; i++ )
139         {
140             data[ i ]=getRandomNumber( minValue, maxValue );
141         }
142         return data;
143     }
144
145
146     /*****************************************************************************************
147      * Random numbers generator in specified range.
148      *
149      * @param numberOfDataSets to generate
150      * @param numToGenerate the number of doubles to generate
151      * @param minValue
152      * @param maxValue
153      * @return double[]
154      ******************************************************************************************/

155     public static double[][] getRandomNumbers( int numberOfDataSets, int numToGenerate, double minValue, double maxValue )
156     {
157         double[][] data=new double[ numberOfDataSets ][ numToGenerate ];
158         for( int j=0; j < numberOfDataSets; j++ )
159         {
160             for( int i=0; i < numToGenerate; i++ )
161             {
162                 data[ j ][ i ]=getRandomNumberIncludingZero( minValue, maxValue );
163             }
164         }
165         return data;
166     }
167
168
169
170     /*****************************************************************************************
171      * Random numbers generator in specified range.
172      *
173      * @param numToGenerate the number of doubles to generate
174      * @param xMinValue
175      * @param xMaxValue
176      * @param yMinValue
177      * @param yMaxValue
178      * @return Point.Double[]
179      ******************************************************************************************/

180     public static Point.Double[] getRandomPoints( int numToGenerate,
181                                                                  double xMinValue,
182                                                                  double xMaxValue,
183                                                                  double yMinValue,
184                                                                  double yMaxValue )
185     {
186         Point.Double[] points= new Point.Double[ numToGenerate ];
187         for( int j=0; j < numToGenerate; j++ )
188         {
189             points[ j ]= ScatterPlotDataSet.createPoint2DDouble();
190             points[ j ].setLocation( getRandomNumber( xMinValue, xMaxValue ), getRandomNumber( yMinValue, yMaxValue ) );
191         }
192         return points;
193     }
194
195
196     /*****************************************************************************************
197      * Random Paint generator.
198      *
199      * @return Paint
200      ******************************************************************************************/

201     protected static Paint getRandomPaint()
202     {
203         if( getRandomNumber( 1 ) > 0.5 )
204         {
205             return getRandomColor();
206         }
207         else
208         {
209             float width=(float) TestDataGenerator.getRandomNumber( 10, 800 );
210             float height=(float) TestDataGenerator.getRandomNumber( 10, 600 );
211             float x=(float) TestDataGenerator.getRandomNumber( 0, 800 );
212             float y=(float) TestDataGenerator.getRandomNumber( 0, 600 );
213             return new GradientPaint( x, y, getRandomColor(), width, height, getRandomColor() );
214         }
215     }
216
217
218     /*****************************************************************************************
219      * Random Color generator.
220      *
221      * @return Paint[]
222      ******************************************************************************************/

223     public static Paint[] getRandomPaints( int numToCreate )
224     {
225         Paint paints[]=new Paint[ numToCreate ];
226         for( int i=0; i < numToCreate; i++ )
227         {
228             paints[ i ]=getRandomPaint();
229         }
230         return paints;
231     }
232
233
234     /*****************************************************************************************
235      * Random Color generator.
236      *
237      * @return Color
238      ******************************************************************************************/

239     protected static Color getRandomColor()
240     {
241         int transparency=(int) getRandomNumber( 100, 375 );
242         if( transparency > 255 )
243         {
244             transparency=255;
245         }
246
247         return new Color( (int) getRandomNumber( 255 ), (int) getRandomNumber( 255 ), (int) getRandomNumber( 255 ), transparency );
248     }
249
250
251     /*****************************************************************************************
252      * Random String generator.
253      *
254      * @param maxStringLength
255      * @param canBeNull
256      * @return String
257      ******************************************************************************************/

258     protected static String JavaDoc getRandomString( int maxStringLength, boolean canBeNull )
259     {
260         if( canBeNull )
261         {
262             if( TestDataGenerator.getRandomNumber( 10 ) <= 1 )
263             {
264                 return null;
265             }
266         }
267
268
269         int tempVal;
270
271         int stringLength=1 + (int) getRandomNumber( maxStringLength );
272         StringBuffer JavaDoc stringBuffer=new StringBuffer JavaDoc( stringLength );
273
274         while( stringLength-- > 0 )
275         {
276             tempVal=65 + (int) getRandomNumber( 58 );
277             while( tempVal > 90 && tempVal < 97 )
278             {
279                 tempVal=65 + (int) getRandomNumber( 58 );
280             }
281
282             stringBuffer.append( (char) tempVal );
283         }
284
285         return stringBuffer.toString();
286     }
287
288
289     /*****************************************************************************************
290      * Random String generator.
291      *
292      * @return String[]
293      ******************************************************************************************/

294     protected static String JavaDoc[] getRandomStrings( int numToCreate, int maxStringLength, boolean canBeNull )
295     {
296         if( canBeNull )
297         {
298             if( (int) TestDataGenerator.getRandomNumber( 10 ) <= 1 )
299             {
300                 return null;
301             }
302         }
303
304         String JavaDoc strings[]=new String JavaDoc[ numToCreate ];
305
306         for( int i=0; i < numToCreate; i++ )
307         {
308             strings[ i ]=getRandomString( maxStringLength, false );
309         }
310
311         return strings;
312     }
313
314
315     /******************************************************************************************
316      * Takes the passed AxisProperties and randomizes it.
317      *
318      * @param axisProperties
319      ******************************************************************************************/

320     protected static void randomizeAxisProperties( AxisProperties axisProperties )
321     {
322         DataAxisProperties dataAxisProperties;
323         LabelAxisProperties labelAxisProperties;
324         if( axisProperties.isPlotHorizontal() )
325         {
326             dataAxisProperties= (DataAxisProperties) axisProperties.getXAxisProperties();
327             labelAxisProperties= (LabelAxisProperties) axisProperties.getYAxisProperties();
328         }
329         else
330         {
331             dataAxisProperties= (DataAxisProperties) axisProperties.getYAxisProperties();
332             labelAxisProperties= (LabelAxisProperties) axisProperties.getXAxisProperties();
333         }
334
335         dataAxisProperties.setNumItems( (int) TestDataGenerator.getRandomNumber( 2, 15 ) );
336         dataAxisProperties.setRoundToNearest( (int) TestDataGenerator.getRandomNumber( -5, 3 ) );
337
338         dataAxisProperties.setUseDollarSigns( TestDataGenerator.getRandomNumber( 1 ) > 0.5d );
339         dataAxisProperties.setUseCommas( TestDataGenerator.getRandomNumber( 1 ) > 0.5d );
340
341         //axisProperties.setShowAxisTitle( AxisProperties.X_AXIS, TestDataGenerator.getRandomNumber( 1 ) > 0.5d );
342
//axisProperties.setShowAxisTitle( AxisProperties.Y_AXIS, TestDataGenerator.getRandomNumber( 1 ) > 0.5d );
343

344         //axisProperties.setShowGridLine( AxisProperties.X_AXIS, (int) TestDataGenerator.getRandomNumber( 3 ) );
345
//axisProperties.setShowGridLine( AxisProperties.X_AXIS, AxisProperties.GRID_LINES_ONLY_WITH_LABELS );
346
//axisProperties.setShowGridLine( AxisProperties.Y_AXIS, (int) TestDataGenerator.getRandomNumber( 3 ) );
347

348         dataAxisProperties.setShowEndBorder( TestDataGenerator.getRandomNumber( 1 ) > 0.5d );
349         labelAxisProperties.setShowEndBorder( TestDataGenerator.getRandomNumber( 1 ) > 0.5d );
350
351 // axisProperties.setShowTicks( AxisProperties.X_AXIS, (int) TestDataGenerator.getRandomNumber( 3 ) );
352
//axisProperties.setShowTicks( AxisProperties.X_AXIS, AxisProperties.TICKS_ONLY_WITH_LABELS );
353
//axisProperties.setShowTicks( AxisProperties.Y_AXIS, (int) TestDataGenerator.getRandomNumber( 3 ) );
354

355         //axisProperties.setShowZeroLine( TestDataGenerator.getRandomNumber( 1 ) > 0.5d );
356
//axisProperties.setZeroLinePaint( TestDataGenerator.getRandomPaint() );
357

358
359 // axisProperties.setScaleFont( TestDataGenerator.getRandomFont( 12.0, 15.0 ) );
360
//axisProperties.setScaleFontColor( TestDataGenerator.getRandomPaint() );
361

362         //axisProperties.setAxisTitleFont( TestDataGenerator.getRandomFont( 6.0, 20.0 ) );
363

364         axisProperties.getXAxisProperties().setAxisStroke( new ChartStroke( new BasicStroke( 1.5f ), TestDataGenerator.getRandomPaint() ) );
365         axisProperties.getYAxisProperties().setAxisStroke( new ChartStroke( new BasicStroke( 1.5f ), TestDataGenerator.getRandomPaint() ) );
366
367
368         //axisProperties.setBackgroundPaint( TestDataGenerator.getRandomPaint() );
369
}
370
371
372     /******************************************************************************************
373      * Takes the passed Legend and randomizes it.
374      *
375      * @param legendProperties
376      ******************************************************************************************/

377     protected static void randomizeLegend( LegendProperties legendProperties )
378     {
379         Font font;
380         int fontSize;
381
382         int numColumns=(int) TestDataGenerator.getRandomNumber( 1, 6 );
383         if( numColumns == 6 )
384         {
385             numColumns=LegendAreaProperties.COLUMNS_AS_MANY_AS_NEEDED;
386         }
387
388         legendProperties.setNumColumns( numColumns );
389         legendProperties.setPlacement( (int) TestDataGenerator.getRandomNumber( 4 ) );
390
391         fontSize=(int) TestDataGenerator.getRandomNumber( 6, 20 );
392         font=ALL_FONTS[ (int) TestDataGenerator.getRandomNumber( ALL_FONTS.length ) ];
393         font=font.deriveFont( (float) fontSize );
394         legendProperties.setChartFont( new ChartFont( font, TestDataGenerator.getRandomPaint() ) );
395
396
397         //---random between null and having a color.
398
if( (int) TestDataGenerator.getRandomNumber( 2 ) == 0 )
399         {
400             legendProperties.setBorderStroke( null );
401         }
402         else
403         {
404             legendProperties.setBorderStroke( ChartStroke.DEFAULT_LEGEND_OUTLINE );
405         }
406
407         //---random between null and having a color.
408
if( (int) TestDataGenerator.getRandomNumber( 2 ) == 0 )
409         {
410             legendProperties.setBackgroundPaint( null );
411         }
412         else
413         {
414             legendProperties.setBackgroundPaint( TestDataGenerator.getRandomPaint() );
415         }
416     }
417 }
Popular Tags