0

I am using JFreeChart to have 2 datasets on the same graph. I am trying to do a comparison between how similar the points are on the graph and therefore I need access to the 2 datasets I put into the chart previously, but for some reason I can't seem to extract this information.

Here is how the data is input:

 private static IntervalXYDataset createDataset()
 {
     DefaultXYDataset completeDataset = new DefaultXYDataset();

     //populate with simulated data
     double[][] sim = new double[2][simData.size()];
     for(int i = 0; i < simData.size(); i++){
         sim[0][i] = simData.get(i).getOne();
         sim[1][i] = simData.get(i).getTwo();
     }

     //populated with known experimental data         
     double[][] exp = new double[2][expData.size()];
     for(int i = 0; i < expData.size(); i++){
         exp[0][i] = expData.get(i).getOne();
         exp[1][i] = expData.get(i).getTwo();
     }

     completeDataset.addSeries("Simulated", sim);
     completeDataset.addSeries("Experimental", exp);

     XYBarDataset dataset = new XYBarDataset(completeDataset, .1);

     return dataset; 
 }

The data is stored as a Pair initially, but this I know the data is input as the graph shows it correctly. How can I return the two series in this dataset?

1 Answer 1

2

If I understand correctly, you want to retrieve your 2 datasets from completeDataset?

As DefaultXYDataset has some limitations, you could try something like this as a workaround. I haven't tested it so it might need some tweaking :)

    completeDataset .addSeries("Simulated", sim); //"Simulated" will be at position 0
    completeDataset .addSeries("Experimental", exp); //"Experimental" will be at position 1

    //get series length
    int countSeries1 = completeDataset.getItemCount(0); //number of items in Simulated
    int countSeries2 = completeDataset.getItemCount(1); //number of items in Experimental

    double[][] series1 = new double[2][countSeries1];
    double[][] series2 = new double[2][countSeries2];

    //recreate Simulated series
    for(int i = 0; i < countSeries1; i++){

        series1[0][i] = completeDataset.getXValue(0, i);
        series1[1][i] = completeDataset.getYValue(0, i);

    }
    //recreate Experimental series
    for(int i = 0; i < countSeries2; i++){

        series2[0][i] = completeDataset.getXValue(1, i);
        series2[1][i] = completeDataset.getYValue(1, i);

    }
Sign up to request clarification or add additional context in comments.

3 Comments

it looks like completeDataset doesn't have the function getSeries() for some reason.
I've edited my answer. Hopefully this helps to retrieve your datasets. If it doesn't then it's worth taking a look at XYSeriesCollection for storing and retrieving your series.
I'd second Frank D's last comment…using XYSeries/XYSeriesCollection is going to make the code a lot more straightforward.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.