Hello,
I'm trying to plot a moving average overlaid over a time series. In order to do this I plot the time series and then I call MovingAveragePlotFitAlgorithm getY(int series, java.lang.Number x) to provide the Y values for the overlaid moving average plot.
My x value on the original moving average chart is of type 'Hour'. I've tried to use a long value converted to a Number for the second argument in 'getY', but when I do this, the X values for the moving average line don't match the X values for the regular time series (i.e. because one is of type 'Hour' and the other is of type 'Number').
Also, I'm not certain what to provide for the first argument in getY(int series, java.lang.Number x).
A snippet of the code to plot the time series is included below (see Snippet1). Also, a snippet of the code which attempts to plot the moving average is at the bottom of this message (see Snippet2)
Thanks, and your guidance is appreciated.
Anita
----------------------------------------------------------------------------------------------
<b>Snippet1:</b>
for (int k=0;k<hits.size();k++) {
// Y axis value
temp_int=(Integer)hits.get(k);
// Set up the x axis values
columns[k]==(Timestamp)dates.get(k);
timeseries.add(new Hour(new java.util.Date(temp_date.getTime())), temp_int.intValue());
}
// Add non-smooth data to the plot
TimeSeriesCollection data = new TimeSeriesCollection(timeseries);
XYPlot subplot1 = new XYPlot(data, null, null, new StandardXYItemRenderer());
plot.add(subplot1);
----------------------------------------------------------------------------------------------
<b>Snippet2:</b>
// Get the moving average
moving_av = new MovingAveragePlotFitAlgorithm();
moving_av.setXYDataset((XYDataset) data);
moving_av.setPeriod(sample_size);
for (int k=0;k<hits.size();k++) {
Timestamp ts = (Timestamp)columns[k];
long ts_long = ts.getTime();
Number ts_number = new Long(ts_long);
// This function will return the yvalue given an x value
Number yval = moving_av.getY(0, ts_number);
timeseries_smooth.add(new Hour(new java.util.Date(ts.getTime())), yval);
}
// Add moving average data to the plot
TimeSeriesCollection data_smooth = new TimeSeriesCollection(timeseries_smooth);
XYPlot subplot2 = new XYPlot(data_smooth, null, null, new StandardXYItemRenderer());
plot.add(subplot2);