Error: This dataset already contains a series with the key Object 1
Error: This dataset already contains a series with the key Object 1
背景: 我正在将 x 和 y 值添加到 XYSeries 数据集,以便我可以呈现 jfreechart。每个 x 和 y 值都是从文本文件中读入的,并且是双精度值。我已将 x 和 y 值设置为等于文本的数组位置并将它们打印出来以检查结果。他们目前正在打印正确的结果。
问题: 但是,我无法将系列添加到我的 XYSeriesCollection。所以我创建了一个名为 series1 的 XYSeries 并添加了两个值。然后当我将该 series1 数据添加到 XYSeriesCollection 时,它会抛出:“此数据集已包含具有键对象 1 的系列”错误。
问题:这个错误是什么意思,我该如何解决?
代码:
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();
}
我的图形界面:
一个XYSeriesCollection
根据给每个系列的Comparable
键区分它包含的系列。在您的示例中,key 是 String
、"Object 1"
。将数据添加到系列的循环尝试通过循环每次 构造和添加系列。相反,在循环 before 之前创建系列,添加数据,然后 then 添加完整的系列 after已满:
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);
已显示完整示例 here。
背景: 我正在将 x 和 y 值添加到 XYSeries 数据集,以便我可以呈现 jfreechart。每个 x 和 y 值都是从文本文件中读入的,并且是双精度值。我已将 x 和 y 值设置为等于文本的数组位置并将它们打印出来以检查结果。他们目前正在打印正确的结果。
问题: 但是,我无法将系列添加到我的 XYSeriesCollection。所以我创建了一个名为 series1 的 XYSeries 并添加了两个值。然后当我将该 series1 数据添加到 XYSeriesCollection 时,它会抛出:“此数据集已包含具有键对象 1 的系列”错误。
问题:这个错误是什么意思,我该如何解决?
代码:
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();
}
我的图形界面:
一个XYSeriesCollection
根据给每个系列的Comparable
键区分它包含的系列。在您的示例中,key 是 String
、"Object 1"
。将数据添加到系列的循环尝试通过循环每次 构造和添加系列。相反,在循环 before 之前创建系列,添加数据,然后 then 添加完整的系列 after已满:
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);
已显示完整示例 here。