Hi,
I have a function that returns an XYDataset (Date and Price), and I need to convert it to a TimeSeries, as I'm finding that TimeSeries format is more versatile. Any suggestions would be very much appreciated.
Converting XYDataset to TimeSeries
If you XYDataset has more than one series, then you could extract each series as a TimeSeries and then aggregate them together as a TimeSeriesCollection.
I am assuming that the Date is stored as a value representing Time in Milliseconds.
You could try the following:
Hope this helps.
I am assuming that the Date is stored as a value representing Time in Milliseconds.
You could try the following:
Code: Select all
XYDataset dataset = yourClass.getDataset();
int numSeries = dataset.getSeriesCount();
TimeSeriesCollection c = new TimeSeriesCollection();
// loop through each series
for (int i = 0; i < numSeries; i++) {
// Get the name
String seriesName = dataset.getSeriesName(i);
TimeSeries s = new TimeSeries(seriesName, Second.class);
// Get the values
for (int j=0; j< dataset.getItemCount(i); j++){
double dateValue = dataset.getX (i, j);
double priceValue = dataset.getY (i, j);
Date dateTime = new Date(dateValue.longValue());
s.add (new Second (dateTime), priceValue);
}
c.addSeries(s);
}
// you can use c and the TimeSeries objects contained within it
Hope this helps.