从JFreeChart获取多个数据集

时间:2014-06-24 07:27:27

标签: java jfreechart

我正在使用JFreeChart在同一个图表上有2个数据集。我试图比较点在图表上的相似程度,因此我需要访问我之前放入图表的2个数据集,但出于某种原因,我似乎无法提取这些信息。

以下是输入数据的方式:

 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; 
 }

数据最初存储为一对,但我知道数据是在图形正确显示时输入的。如何在此数据集中返回两个系列?

1 个答案:

答案 0 :(得分:2)

如果我理解正确,您想从completeDataset检索您的2个数据集?

由于DefaultXYDataset有一些限制,您可以尝试这样的解决方法。我没有测试过,所以可能需要一些调整:)

    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);

    }