2015-11-18 67 views
2

我一直想弄清楚如何從LineChart中刪除一條已繪製的線。我試圖用我想要的系列重新創建場景,但那不起作用。我已經做了一些故障排除,發現該系列實際上已經改變並且減少爲空,但是即使該系列是空的,該行仍然存在。然後,一旦您將系列更改爲新數字,它會嘗試將點連接到不應該存在的舊點。如果有人能幫助我,這將非常感激。如何從LineChart中刪除線?

public class GraphSection { 
NumberAxis xAxis; 
NumberAxis yAxis; 
LineChart<Number, Number> lineChart; 
Scene scene; 
XYChart.Series series1, series2, series3, series4; 




public GraphSection(){ 
    xAxis = new NumberAxis(); 
    yAxis = new NumberAxis(); 
    lineChart = new LineChart<Number,Number>(xAxis,yAxis); 
    lineChart.setTitle("Test Graph"); 
    lineChart.setCreateSymbols(false); 
    lineChart.setLegendVisible(false); 

    series1 = new XYChart.Series(); 
    series2 = new XYChart.Series(); 
    series3 = new XYChart.Series(); 
    series4 = new XYChart.Series(); 

    lineChart.getData().addAll(series1, series2, series3, series4); 
    scene = new Scene(lineChart,400,300); 


} 
public Scene getGraph(){ 
    return this.scene; 

} 
public void addPoints(int n){ 

    if(n == 1){ 
     //Test Points 
     series1.getData().add(new XYChart.Data(1,50)); 
     series1.getData().add(new XYChart.Data(2,60)); 
     series1.getData().add(new XYChart.Data(3,70)); 
     series1.getData().add(new XYChart.Data(4,80)); 
     series1.getData().add(new XYChart.Data(5,90)); 

    } 
    else if(n == 2){ 
     //More Test Points 
     series1.getData().add(new XYChart.Data(1,70)); 
     series1.getData().add(new XYChart.Data(2,80)); 
     series1.getData().add(new XYChart.Data(3,90)); 
     series1.getData().add(new XYChart.Data(4,100)); 
     series1.getData().add(new XYChart.Data(5,110)); 
    } 
    else if(n == 3){ 

    } 
    else if(n == 4){ 

    } 

} 

public void removePoints(int n){ 
    Series s; 
    LineChart l = this.lineChart; 
    if(n == 1){ 
     s = this.series1; 
     s.getData().clear(); 
     l.removeSeriesFromDisplay(s); //This is the method I would guess I need but 
             //it is "not visible" and thus produces 
             // an error when I try to compile. 
    } 
    else if(n==2){ 
     series2 = this.series2; 
    } 
    else if(n==3){ 
     series3 = this.series3; 
    } 
    else if(n==4){ 
     series4 = this.series4; 
    } 


} 

} 

編輯:因此,儘管.remove()將努力得到它刪除它也拋出IllegalStateException異常圖中的線。這雖然很醜,但不會導致程序崩潰,但是,當我將數據點添加回系列時,它不會繪製新的點。因此,一個可能的解決方案是動態地將一系列添加到LineChart中。如果有人知道如何做到這一點,那可能會解決我的問題。

編輯:我已經改變.addPoints(2);給series1添加點,我在下面添加圖片(在remove()之後添加只是給出一個空白圖表)。我還包括我從中調用類的代碼。

//Bottom 
    JFXPanel bottom = new JFXPanel(); 
    final GraphSection gs = new GraphSection(); 
    bottom.setScene(gs.getGraph()); 
    gs.addPoints(1); 

    //ActionListeners 
    testButton1.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 
      gs.removePoints(1); 
      System.out.println(gs.series1.getData()); //Used for debugging 
     } 
    }); 

    testButton2.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 
      gs.addPoints(2); 
      System.out.println(gs.series1.getData()); 
     } 
    }); 

Start with .addPoints(1); This is what happens with just .getData().clear();

+0

'lineChart.getData()。remove(series1)'? –

+0

給了一個鏡頭,併產生了IllegalStateException – CVarg

+0

好。其實它會給出這個錯誤,但它確實會刪除這條線......呵呵。任何方式使它不會拋出錯誤? – CVarg

回答

0

是,這個問題實際上試圖一個Swing的JFrame內使用JFXPanel。這意味着illegalStateException被彈出。通過修改代碼以在JavaFX階段運行,這允許remove()工作,然後您可以使用lineChart.getData()。add(Series);輕鬆地動態添加新系列。您可以將Swing組件與SwingNodes合併,儘管我的建議是將您的GUI重新編碼到所有JavaFX中。