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.
How do I get data out?
Re: How do I get data out?
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.
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.
Re: How do I get data out?
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.
Thanks,
Craig.