2

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.

rocksNwaves
  • 5,331
  • 4
  • 38
  • 77

2 Answers2

2

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!

rocksNwaves
  • 5,331
  • 4
  • 38
  • 77
1

As shown here, the collection's addSeries() method uses the name of the incoming XYSeries as the key. You can add multiple series to the collection as long as the names are unique. In this example, two series are named "Random" and "Added"; the latter series gets updated dynamically when you click the Add button.

image

As your program evolves,

  • You can update the chart as a value in a JSpinner changes, as shown here.

image

  • You can animate the progress of a calculation using a worker thread, as shown here.

image

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thanks for the good links. I think my question is a little more basic. As you can see, I have created my series(s) in a loop to add them to the dataset, but JFreeChart doesn't like that they are all the same name. I need a way to name them uniquely inside a loop. Perhaps some way to add a incremental number to the declaration? I've seen arrays mentioned, but I'm not sure how to accomplish that and keep the code simple. – rocksNwaves Oct 04 '16 at 14:18