Search code examples
javanaming-conventionsjfreechart

JFreeChart multiple series issue


Good evening:

I am collecting data on number of function evaluations to be graphed using the two methods below. The first runs Romberg integration for a given n and m, n<=m. Within that method a global counter is incremented during each evaluation, and I want to graph the number of evaluations for a given R(n,m). The second receives the dataset and graphs it.

public static XYSeriesCollection functionDataCollecter(){
    counter = 0;

    //a single line on a chart
    XYSeries series; 

    //a collection of series
    XYSeriesCollection dataset = new XYSeriesCollection(); 

    for(int i=0;i<3;i++){
        //initiate new series
        series = new XYSeries("data");
        for(int k = i; k<9;k++){
            R_nm(k,i,0,(Math.PI)/2);
            series.add(k, counter);
        }
        //add series to dataset
        dataset.addSeries(series);   
    }

    return dataset;
}

public static void seriesPlotter(XYSeriesCollection dataset) {

    XYPlot myPlot = new XYPlot("m=2", "Math 521", "m<=n<9", "log_10(F(n,m))", dataset);
    myPlot.pack();
    myPlot.setVisible(true);

}

I am running into a problem. It seems that I have to name each series in the dataset uniquely. I get the error: "This dataset already contains a series with the key data at org.jfree.data.xy.XYSeriesCollection.addSeries(XYSeriesCollection.java:159)

I can't seem to find a simple way to append or increment my series variable names. Arrays confuse the crap out of me, and they are the only option I have found mentioned in my online searches.

I appreciate simple suggestions, as I am not a programmer and just learned the difference between a class and an object. I say this to help you frame your suggestions, if you care to help me out. Thanks in advance.


Solution

  • Ok, I just figured this out, and it was much more simple than I thought. The error message is not referring to some requirement for a unique declared name for each series. It is referring to the String argument:

    series = new XYSeries("data");
    

    Just update the argument in the loop, and that was enough to get three multi-colored plots:

    series = new XYSeries("data"+Integer.toString(i));
    

    I hope this helps someone down the road!