Home > Software engineering >  Error: This dataset already contains a series with the key Object 1
Error: This dataset already contains a series with the key Object 1

Time:03-26

Background: I am adding x & y values to a XYSeries dataset so I can render a jfreechart. Each of the x and y values are read in from a text file and are doubles. I've set the x and y values equal to the array location of the text and printed them out to check results. They are currently printing the correct results.

The Problem: However, I'm having trouble adding the series to my XYSeriesCollection. So I created an XYSeries named series1 and added the two values in. Then when I go to add that series1 data to the XYSeriesCollection, it throws the: "This dataset already contains a series with the key Object 1" error.

The Question: What does this error mean, and how do I solve it?

Code:

    private void renderChartBtnActionPerformed(java.awt.event.ActionEvent evt) {                                               

        String manufacturer = "";
        
        XYSeriesCollection dcd = new XYSeriesCollection();
        
        String[] splitLine = new String[4];
        String[][] twoD_arr = new String[arraySize][4];
        int kount = 0;
        double dnum = 0;
        double dnum2 = 0;
        
         // Checks which radio button is selected and sets equal to manufacturer
        if (audiRB.isSelected()) {
            manufacturer = "Audi";
        }
        else if (volvoRB.isSelected()) {
            manufacturer = "Volvo";       
        }
        
        Iterator<String> itr = carData.iterator();
        while(itr.hasNext()){
            splitLine = itr.next().split(",");
            for(int i=0;i<=3;i  ){
                twoD_arr[kount][i] = splitLine[i];
               
            }
            kount = kount   1;
        }
        // Checks which data type is selected and adds data to series
        for(int i=0; i< twoD_arr.length; i  ){
            if (pctShare.isSelected() && (twoD_arr[i][1]).equals(manufacturer)) {
                XYSeries series1 = new XYSeries("Object 1");
                                
                dnum = Double.parseDouble(twoD_arr[i][3]);
                dnum2 = Double.parseDouble(twoD_arr[i][0]);
                series1.add(dnum, dnum2);
                System.out.println(dnum);
                System.out.println(dnum2);
                dcd.addSeries(series1);
            } 
            else if (qtySold.isSelected() && (twoD_arr[i][1]).equals(manufacturer)) {
//                inum = Integer.parseInt(twoD_arr[i][2]);
//                dcd.setValue(inum, "Quantity Sold", twoD_arr[i][0]);
//                dcd.setValue(dnum, dnum, dnum);
//                System.out.println(twoD_arr[i][0]   twoD_arr[i][2]);
                     
            }                
        }
        
         // User selection of chart type        
         if( LineRB.isSelected()){
            jchart = ChartFactory.createXYLineChart("New Car Sales in Norway", "Date", manufacturer, dcd);
        }
        else if(AreaRB.isSelected()){
            jchart = ChartFactory.createXYAreaChart("New Car Sales in Norway", "Date", manufacturer, dcd, PlotOrientation.VERTICAL, true, true, false);
        }
        else if(barRB.isSelected()){
            jchart = ChartFactory.createXYStepAreaChart("New Car Sales in Norway", "Date", manufacturer, dcd, PlotOrientation.VERTICAL, true, true, false);       
        }
        else {
           jchart = null; 
        }
        
        // Add chart to panel
        ChartPanel chartPanel = new ChartPanel(jchart);
        inner_chart_pnl.removeAll();
        inner_chart_pnl.add(chartPanel);
        inner_chart_pnl.updateUI(); 

    }      

My GUI: My GUI

CodePudding user response:

An XYSeriesCollection distinguishes among the series it contains based on the Comparable key given to each series. In your example, that key is the String, "Object 1". Your loop that adds data to series tries to construct and add the series each time through the loop. Instead, create the series before the loop, add the data, and then add the complete series after it is filled:

XYSeries series1 = new XYSeries("Object 1");
for(int i=0; i< twoD_arr.length; i  ) {
    //XYSeries series1 = new XYSeries("Object 1");
    …
    //dcd.addSeries(series1);
} 
dcd.addSeries(series1);

A complete example is shown here.

  • Related