how toget the Y value for a particular x value in JFreeChart

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
lakshmi
Posts: 10
Joined: Wed Mar 08, 2006 6:15 am

how toget the Y value for a particular x value in JFreeChart

Post by lakshmi » Thu Mar 30, 2006 7:14 am

hi,

i want to retreive the y value for the corresponding x-value.
i used
public double getYValue(int series, int item),

a method of XYDataset.
when i used that method, Nullpointer exception is thrown.
i cant intialise XYDataset,because it is an interface.
my x-axis is date and i want the return type of the function as double.
is there any method to get the y-value for the corresponding x-value.
can anyone please help me to solve the above problem.

gribas
Posts: 32
Joined: Thu Jan 26, 2006 5:34 pm

Post by gribas » Thu Mar 30, 2006 8:49 pm

First you'll have to get the correct XYDataset. Retrieve it from your plot:

Code: Select all

XYDataset dataset = chartPanel.getChart().getXYPlot().getDataset();
Iterate over the dataset till you find the X value you're looking for, then save its position and retrieve the desired Y value based on X's position. Be aware that you can have more then one series.

Code: Select all

        int itemCount = dataset.getItemCount(0);
        for (int k=0; k<itemCount; k++) {
            if (dataset.getX(0,k) == <something>)
                break;
        }
        double y = dataset.getYValue(0,k);

Locked