001: /***********************************************************************************************
002: * File Info: $Id: TestDataGenerator.java,v 1.4 2004/05/26 01:58:43 nathaniel_auvil Exp $
003: * Copyright (C) 2000
004: * Author: Nathaniel G. Auvil
005: * Contributor(s):
006: *
007: * Copyright 2002 (C) Nathaniel G. Auvil. All Rights Reserved.
008: *
009: * Redistribution and use of this software and associated documentation
010: * ("Software"), with or without modification, are permitted provided
011: * that the following conditions are met:
012: *
013: * 1. Redistributions of source code must retain copyright
014: * statements and notices. Redistributions must also contain a
015: * copy of this document.
016: *
017: * 2. Redistributions in binary form must reproduce the
018: * above copyright notice, this list of conditions and the
019: * following disclaimer in the documentation and/or other
020: * materials provided with the distribution.
021: *
022: * 3. The name "jCharts" or "Nathaniel G. Auvil" must not be used to
023: * endorse or promote products derived from this Software without
024: * prior written permission of Nathaniel G. Auvil. For written
025: * permission, please contact nathaniel_auvil@users.sourceforge.net
026: *
027: * 4. Products derived from this Software may not be called "jCharts"
028: * nor may "jCharts" appear in their names without prior written
029: * permission of Nathaniel G. Auvil. jCharts is a registered
030: * trademark of Nathaniel G. Auvil.
031: *
032: * 5. Due credit should be given to the jCharts Project
033: * (http://jcharts.sourceforge.net/).
034: *
035: * THIS SOFTWARE IS PROVIDED BY Nathaniel G. Auvil AND CONTRIBUTORS
036: * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
037: * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
038: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
039: * jCharts OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
040: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
041: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
042: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
043: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
044: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
045: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
046: * OF THE POSSIBILITY OF SUCH DAMAGE.
047: ************************************************************************************************/package org.krysalis.jcharts.test;
048:
049: import org.krysalis.jcharts.properties.*;
050: import org.krysalis.jcharts.properties.util.ChartStroke;
051: import org.krysalis.jcharts.properties.util.ChartFont;
052: import org.krysalis.jcharts.chartData.ScatterPlotDataSet;
053:
054: import java.awt.*;
055:
056: final public class TestDataGenerator {
057: private final static Font[] ALL_FONTS = GraphicsEnvironment
058: .getLocalGraphicsEnvironment().getAllFonts();
059:
060: /*****************************************************************************************
061: * Random font generator based on the available Fonts on this machine.
062: *
063: * @param minSize
064: * @param maxSize
065: * @return Font
066: ******************************************************************************************/
067: public static Font getRandomFont(double minSize, double maxSize) {
068: Font font = ALL_FONTS[(int) TestDataGenerator
069: .getRandomNumber(ALL_FONTS.length)];
070: font = font.deriveFont((float) TestDataGenerator
071: .getRandomNumber(minSize, maxSize));
072: return font;
073: }
074:
075: /*****************************************************************************************
076: * Random number generator.
077: *
078: * @param maxValue
079: * @return double
080: ******************************************************************************************/
081: public static double getRandomNumber(double maxValue) {
082: return Math.random() * maxValue;
083: }
084:
085: /*****************************************************************************************
086: * Random number generator in specified range.
087: *
088: * @param minValue
089: * @param maxValue
090: * @return double
091: ******************************************************************************************/
092: protected static double getRandomNumber(double minValue,
093: double maxValue) {
094: return (minValue + (Math.random() * (maxValue - minValue)));
095: }
096:
097: /*****************************************************************************************
098: * Random number generator in specified range. Will include zeros a small percentage of
099: * the time for testing.
100: *
101: * @param minValue
102: * @param maxValue
103: * @return double
104: ******************************************************************************************/
105: protected static double getRandomNumberIncludingZero(
106: double minValue, double maxValue) {
107: if ((TestDataGenerator.getRandomNumber(1) > 0.8d)) {
108: return 0.0d;
109: } else {
110: return getRandomNumber(minValue, maxValue);
111: }
112: }
113:
114: /*****************************************************************************************
115: * Random numbers generator in specified range.
116: *
117: * @param numToGenerate the number of doubles to generate
118: * @param minValue
119: * @param maxValue
120: * @return double[]
121: ******************************************************************************************/
122: public static double[] getRandomNumbers(int numToGenerate,
123: double minValue, double maxValue) {
124: double[] data = new double[numToGenerate];
125: for (int i = 0; i < numToGenerate; i++) {
126: data[i] = getRandomNumber(minValue, maxValue);
127: }
128: return data;
129: }
130:
131: /*****************************************************************************************
132: * Random numbers generator in specified range.
133: *
134: * @param numberOfDataSets to generate
135: * @param numToGenerate the number of doubles to generate
136: * @param minValue
137: * @param maxValue
138: * @return double[]
139: ******************************************************************************************/
140: public static double[][] getRandomNumbers(int numberOfDataSets,
141: int numToGenerate, double minValue, double maxValue) {
142: double[][] data = new double[numberOfDataSets][numToGenerate];
143: for (int j = 0; j < numberOfDataSets; j++) {
144: for (int i = 0; i < numToGenerate; i++) {
145: data[j][i] = getRandomNumberIncludingZero(minValue,
146: maxValue);
147: }
148: }
149: return data;
150: }
151:
152: /*****************************************************************************************
153: * Random numbers generator in specified range.
154: *
155: * @param numToGenerate the number of doubles to generate
156: * @param xMinValue
157: * @param xMaxValue
158: * @param yMinValue
159: * @param yMaxValue
160: * @return Point.Double[]
161: ******************************************************************************************/
162: public static Point.Double[] getRandomPoints(int numToGenerate,
163: double xMinValue, double xMaxValue, double yMinValue,
164: double yMaxValue) {
165: Point.Double[] points = new Point.Double[numToGenerate];
166: for (int j = 0; j < numToGenerate; j++) {
167: points[j] = ScatterPlotDataSet.createPoint2DDouble();
168: points[j].setLocation(
169: getRandomNumber(xMinValue, xMaxValue),
170: getRandomNumber(yMinValue, yMaxValue));
171: }
172: return points;
173: }
174:
175: /*****************************************************************************************
176: * Random Paint generator.
177: *
178: * @return Paint
179: ******************************************************************************************/
180: protected static Paint getRandomPaint() {
181: if (getRandomNumber(1) > 0.5) {
182: return getRandomColor();
183: } else {
184: float width = (float) TestDataGenerator.getRandomNumber(10,
185: 800);
186: float height = (float) TestDataGenerator.getRandomNumber(
187: 10, 600);
188: float x = (float) TestDataGenerator.getRandomNumber(0, 800);
189: float y = (float) TestDataGenerator.getRandomNumber(0, 600);
190: return new GradientPaint(x, y, getRandomColor(), width,
191: height, getRandomColor());
192: }
193: }
194:
195: /*****************************************************************************************
196: * Random Color generator.
197: *
198: * @return Paint[]
199: ******************************************************************************************/
200: public static Paint[] getRandomPaints(int numToCreate) {
201: Paint paints[] = new Paint[numToCreate];
202: for (int i = 0; i < numToCreate; i++) {
203: paints[i] = getRandomPaint();
204: }
205: return paints;
206: }
207:
208: /*****************************************************************************************
209: * Random Color generator.
210: *
211: * @return Color
212: ******************************************************************************************/
213: protected static Color getRandomColor() {
214: int transparency = (int) getRandomNumber(100, 375);
215: if (transparency > 255) {
216: transparency = 255;
217: }
218:
219: return new Color((int) getRandomNumber(255),
220: (int) getRandomNumber(255), (int) getRandomNumber(255),
221: transparency);
222: }
223:
224: /*****************************************************************************************
225: * Random String generator.
226: *
227: * @param maxStringLength
228: * @param canBeNull
229: * @return String
230: ******************************************************************************************/
231: protected static String getRandomString(int maxStringLength,
232: boolean canBeNull) {
233: if (canBeNull) {
234: if (TestDataGenerator.getRandomNumber(10) <= 1) {
235: return null;
236: }
237: }
238:
239: int tempVal;
240:
241: int stringLength = 1 + (int) getRandomNumber(maxStringLength);
242: StringBuffer stringBuffer = new StringBuffer(stringLength);
243:
244: while (stringLength-- > 0) {
245: tempVal = 65 + (int) getRandomNumber(58);
246: while (tempVal > 90 && tempVal < 97) {
247: tempVal = 65 + (int) getRandomNumber(58);
248: }
249:
250: stringBuffer.append((char) tempVal);
251: }
252:
253: return stringBuffer.toString();
254: }
255:
256: /*****************************************************************************************
257: * Random String generator.
258: *
259: * @return String[]
260: ******************************************************************************************/
261: protected static String[] getRandomStrings(int numToCreate,
262: int maxStringLength, boolean canBeNull) {
263: if (canBeNull) {
264: if ((int) TestDataGenerator.getRandomNumber(10) <= 1) {
265: return null;
266: }
267: }
268:
269: String strings[] = new String[numToCreate];
270:
271: for (int i = 0; i < numToCreate; i++) {
272: strings[i] = getRandomString(maxStringLength, false);
273: }
274:
275: return strings;
276: }
277:
278: /******************************************************************************************
279: * Takes the passed AxisProperties and randomizes it.
280: *
281: * @param axisProperties
282: ******************************************************************************************/
283: protected static void randomizeAxisProperties(
284: AxisProperties axisProperties) {
285: DataAxisProperties dataAxisProperties;
286: LabelAxisProperties labelAxisProperties;
287: if (axisProperties.isPlotHorizontal()) {
288: dataAxisProperties = (DataAxisProperties) axisProperties
289: .getXAxisProperties();
290: labelAxisProperties = (LabelAxisProperties) axisProperties
291: .getYAxisProperties();
292: } else {
293: dataAxisProperties = (DataAxisProperties) axisProperties
294: .getYAxisProperties();
295: labelAxisProperties = (LabelAxisProperties) axisProperties
296: .getXAxisProperties();
297: }
298:
299: dataAxisProperties.setNumItems((int) TestDataGenerator
300: .getRandomNumber(2, 15));
301: dataAxisProperties.setRoundToNearest((int) TestDataGenerator
302: .getRandomNumber(-5, 3));
303:
304: dataAxisProperties.setUseDollarSigns(TestDataGenerator
305: .getRandomNumber(1) > 0.5d);
306: dataAxisProperties.setUseCommas(TestDataGenerator
307: .getRandomNumber(1) > 0.5d);
308:
309: //axisProperties.setShowAxisTitle( AxisProperties.X_AXIS, TestDataGenerator.getRandomNumber( 1 ) > 0.5d );
310: //axisProperties.setShowAxisTitle( AxisProperties.Y_AXIS, TestDataGenerator.getRandomNumber( 1 ) > 0.5d );
311:
312: //axisProperties.setShowGridLine( AxisProperties.X_AXIS, (int) TestDataGenerator.getRandomNumber( 3 ) );
313: //axisProperties.setShowGridLine( AxisProperties.X_AXIS, AxisProperties.GRID_LINES_ONLY_WITH_LABELS );
314: //axisProperties.setShowGridLine( AxisProperties.Y_AXIS, (int) TestDataGenerator.getRandomNumber( 3 ) );
315:
316: dataAxisProperties.setShowEndBorder(TestDataGenerator
317: .getRandomNumber(1) > 0.5d);
318: labelAxisProperties.setShowEndBorder(TestDataGenerator
319: .getRandomNumber(1) > 0.5d);
320:
321: // axisProperties.setShowTicks( AxisProperties.X_AXIS, (int) TestDataGenerator.getRandomNumber( 3 ) );
322: //axisProperties.setShowTicks( AxisProperties.X_AXIS, AxisProperties.TICKS_ONLY_WITH_LABELS );
323: //axisProperties.setShowTicks( AxisProperties.Y_AXIS, (int) TestDataGenerator.getRandomNumber( 3 ) );
324:
325: //axisProperties.setShowZeroLine( TestDataGenerator.getRandomNumber( 1 ) > 0.5d );
326: //axisProperties.setZeroLinePaint( TestDataGenerator.getRandomPaint() );
327:
328: // axisProperties.setScaleFont( TestDataGenerator.getRandomFont( 12.0, 15.0 ) );
329: //axisProperties.setScaleFontColor( TestDataGenerator.getRandomPaint() );
330:
331: //axisProperties.setAxisTitleFont( TestDataGenerator.getRandomFont( 6.0, 20.0 ) );
332:
333: axisProperties.getXAxisProperties().setAxisStroke(
334: new ChartStroke(new BasicStroke(1.5f),
335: TestDataGenerator.getRandomPaint()));
336: axisProperties.getYAxisProperties().setAxisStroke(
337: new ChartStroke(new BasicStroke(1.5f),
338: TestDataGenerator.getRandomPaint()));
339:
340: //axisProperties.setBackgroundPaint( TestDataGenerator.getRandomPaint() );
341: }
342:
343: /******************************************************************************************
344: * Takes the passed Legend and randomizes it.
345: *
346: * @param legendProperties
347: ******************************************************************************************/
348: protected static void randomizeLegend(
349: LegendProperties legendProperties) {
350: Font font;
351: int fontSize;
352:
353: int numColumns = (int) TestDataGenerator.getRandomNumber(1, 6);
354: if (numColumns == 6) {
355: numColumns = LegendAreaProperties.COLUMNS_AS_MANY_AS_NEEDED;
356: }
357:
358: legendProperties.setNumColumns(numColumns);
359: legendProperties.setPlacement((int) TestDataGenerator
360: .getRandomNumber(4));
361:
362: fontSize = (int) TestDataGenerator.getRandomNumber(6, 20);
363: font = ALL_FONTS[(int) TestDataGenerator
364: .getRandomNumber(ALL_FONTS.length)];
365: font = font.deriveFont((float) fontSize);
366: legendProperties.setChartFont(new ChartFont(font,
367: TestDataGenerator.getRandomPaint()));
368:
369: //---random between null and having a color.
370: if ((int) TestDataGenerator.getRandomNumber(2) == 0) {
371: legendProperties.setBorderStroke(null);
372: } else {
373: legendProperties
374: .setBorderStroke(ChartStroke.DEFAULT_LEGEND_OUTLINE);
375: }
376:
377: //---random between null and having a color.
378: if ((int) TestDataGenerator.getRandomNumber(2) == 0) {
379: legendProperties.setBackgroundPaint(null);
380: } else {
381: legendProperties.setBackgroundPaint(TestDataGenerator
382: .getRandomPaint());
383: }
384: }
385: }
|