Converting XYDataset to TimeSeries

Discussion about JFreeChart related to stockmarket charts.
Locked
allboox
Posts: 3
Joined: Thu Apr 08, 2004 9:17 am

Converting XYDataset to TimeSeries

Post by allboox » Thu Apr 08, 2004 9:39 am

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.

hongping
Posts: 14
Joined: Thu Jul 29, 2004 1:32 am

Post by hongping » Mon Aug 16, 2004 6:25 pm

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:

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.

Locked