How do I get data out?

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Craig Tataryn

How do I get data out?

Post by Craig Tataryn » Fri Apr 26, 2002 5:00 am

I've managed to get my data into a chart (it's a basic TimeSeries chart, y values are doubles and x values are dates). Given I have a reference to a the JFreeChart instance (no direct links to the series underlying), how do I:

1) Specify a date (x value) and get it's corresponding value (y value)from the chart?
2) Specify a value (y value) and get the closest date (x value) which from the chart?

Sorry, very new to charting, and to this tool. I have the registered documentation but I couldn't seem to find from it how I would accomplish the above given just a reference to a JFreeChart.

Thanks,

Craig.

David Gilbert

Re: How do I get data out?

Post by David Gilbert » Fri Apr 26, 2002 9:57 am

In my code, I try to keep a reference to the underlying dataset so I can use the complete range of methods it provides (most of the time I use TimeSeriesCollection).

But if you just have the JFreeChart instance, you can get access to the dataset with the getDataset() method which returns a Dataset. You'll need to cast the result to XYDataset (or CategoryDataset for bar charts) for it to be much use:

XYDataset myData = (XYDataset)myChart.getDataset();

Using only the methods in XYDataset, finding the y value corresponding to a particular date is going to require iterating through all the data items using:

public Number getXValue(int series, int item);
The value returned is coded as milliseconds since midnight GMT, 1-Jan-1970. When you find the item that matches, you can use:

public Number getYValue(int series, int item);
That's not terribly efficient, but it would work.

Finding the x-value corresponding to a given y-value is not as simple, since there could be duplicate y-values, so there isn't necessarily a one-to-one correspondence (unless you know your data has all unique y-values).
Regards,

DG.

Craig Tataryn

Re: How do I get data out?

Post by Craig Tataryn » Fri Apr 26, 2002 3:41 pm

Thank you David, I will try what you have suggested (and will probably just keep a reference to the underlying model for convenience).

Thanks,

Craig.

Locked