001package bradleyross.opensource.jfreechart.helpers; 002import java.util.Date; 003import org.jfree.data.time.TimeSeries; 004import org.jfree.data.time.Second; 005/** 006 * Creates sets of sample data for testing of the JFreeChart product. 007 * @author Bradley Ross 008 * 009 */ 010@SuppressWarnings("deprecation") 011public class DataGenerator 012{ 013 /** 014 * Since all of the methods are static, the constructor should 015 * never be used. 016 */ 017 protected DataGenerator() 018 { ; } 019 /** 020 * Generate data according to a sine wave. 021 * @param name name for data set 022 * @param wavelength Wavelength of sine wave in milliseconds 023 * @param duration Duration for which series is to be generated in milliseconds 024 * @param interval Interval between data points in milliseconds - The value must be 025 * greater than or equal to one second 026 * @param magnitude maximum magnitude 027 * @param offset offset from 0 028 * @return TimeSeries containing data values 029 */ 030 public static TimeSeries generateSine(String name, long wavelength, long duration, long interval, 031 double magnitude, double offset) 032 { 033 return generateSine(name, wavelength, duration, interval, 034 magnitude, offset, new java.util.Date()); 035 } 036 /** 037 * Generate data according to a sine wave. 038 * @param name name o chart 039 * @param wavelength Wavelength of sine wave in milliseconds 040 * @param duration Duration for which series is to be generated in milliseconds 041 * @param interval Interval between data points in milliseconds - The value must be 042 * greater than or equal to one second 043 * @param offset offset of middle amplitude of waveform from zero 044 * @param startTime Starting time for plot 045 * @param magnitude maximum of wave 046 * @return TimeSeries containing data values 047 */ 048 public static TimeSeries generateSine(String name, long wavelength, long duration, long interval, 049 double magnitude, double offset, java.util.Date startTime) 050 { 051 TimeSeries working = new TimeSeries(name, Second.class ); 052 long now = startTime.getTime(); 053 for (long i = now; i < now + duration; i=i+interval) 054 { 055 working.add(new Second(new Date(i)), 056 magnitude*Math.sin(2.0d*Math.PI*(double) (i - now)/(double) wavelength) + offset); 057 } 058 return working; 059 } 060}