背景:我正在向 XYSeries 資料集添加 x 和 y 值,以便渲染 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();
}
我的圖形用戶界面: 
uj5u.com熱心網友回復:
An根據賦予每個系列的鍵XYSeriesCollection來區分它包含的系列。在您的示例中,該鍵是, 。您將資料添加到系列的回圈嘗試每次通過回圈構建和添加系列。相反,在回圈之前創建系列,添加資料,然后在填充后添加完整系列:Comparable String"Object 1"
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);
此處顯示了一個完整的示例。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/457801.html
標籤:爪哇 摇摆 按钮 jfreechart
