2015-06-19 79 views
0

我在javafx8中使用LineChart,我可以在圖中動態繪製系列圖併爲每個輸入繪製符號。但問題是我只想顯示當前節點的符號,而不是已經添加的節點。當前節點+ LineChart + JavaFX

我想:

linechart.setCreateSymbols(false); 

但是,這並不符合我的needs.It停止創建輸入符號。

任何人都可以告訴如何只顯示目前的符號不是添加的oness?

+0

通過「本節點「你的意思是最後一個添加? –

+0

@James_D是的。你說得對!它的最後一個補充 –

回答

0

只需跟蹤添加的最後一個數據點的節點。添加新點時,將前一個「最後一點」的可見性設置爲false。

你可以,如果你想成爲能夠更容易因素的代碼進行直接

import java.util.Random; 

import javafx.application.Application; 
import javafx.geometry.Insets; 
import javafx.geometry.Pos; 
import javafx.scene.Node; 
import javafx.scene.Scene; 
import javafx.scene.chart.LineChart; 
import javafx.scene.chart.NumberAxis; 
import javafx.scene.chart.XYChart.Data; 
import javafx.scene.chart.XYChart.Series; 
import javafx.scene.control.Button; 
import javafx.scene.layout.BorderPane; 
import javafx.stage.Stage; 

public class LineChartShowSymbolForLastPointOnly extends Application { 

    private Node lastPointAdded ; 

    @Override 
    public void start(Stage primaryStage) { 
     LineChart<Number, Number> chart = new LineChart<>(new NumberAxis(), new NumberAxis()); 
     Series<Number, Number> series = new Series<>(); 
     chart.getData().add(series); 
     chart.setAnimated(false); 

     Button addDataPointButton = new Button("Add data point"); 
     Random rng = new Random(); 
     addDataPointButton.setOnAction(e -> { 
      if (lastPointAdded != null) { 
       lastPointAdded.setVisible(false); 
      } 
      Data<Number, Number> data = new Data<>(series.getData().size(), rng.nextDouble()); 
      series.getData().add(data); 
      lastPointAdded = data.getNode(); 
     }); 

     BorderPane.setAlignment(addDataPointButton, Pos.CENTER); 
     BorderPane.setMargin(addDataPointButton, new Insets(10)); 
     BorderPane root = new BorderPane(chart, null, null, addDataPointButton, null); 

     primaryStage.setScene(new Scene(root, 800, 600)); 
     primaryStage.show(); 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 
} 

做到這一點,你可以用一個ObjectProperty並註冊一個監聽器:

import java.util.Random; 

import javafx.application.Application; 
import javafx.beans.property.ObjectProperty; 
import javafx.beans.property.SimpleObjectProperty; 
import javafx.geometry.Insets; 
import javafx.geometry.Pos; 
import javafx.scene.Node; 
import javafx.scene.Scene; 
import javafx.scene.chart.LineChart; 
import javafx.scene.chart.NumberAxis; 
import javafx.scene.chart.XYChart.Data; 
import javafx.scene.chart.XYChart.Series; 
import javafx.scene.control.Button; 
import javafx.scene.layout.BorderPane; 
import javafx.stage.Stage; 

public class LineChartShowSymbolForLastPointOnly extends Application { 

    @Override 
    public void start(Stage primaryStage) { 
     LineChart<Number, Number> chart = new LineChart<>(new NumberAxis(), new NumberAxis()); 
     Series<Number, Number> series = new Series<>(); 
     chart.getData().add(series); 
     chart.setAnimated(false); 


     ObjectProperty<Node> lastPointAdded = new SimpleObjectProperty<>(); 
     lastPointAdded.addListener((obs, oldNode, newNode) -> { 
      if (oldNode != null) { 
       oldNode.setVisible(false); 
      } 
     }); 

     Button addDataPointButton = new Button("Add data point"); 
     Random rng = new Random(); 
     addDataPointButton.setOnAction(e -> { 
      Data<Number, Number> data = new Data<>(series.getData().size(), rng.nextDouble()); 
      series.getData().add(data); 
      lastPointAdded.set(data.getNode()); 
     }); 

     BorderPane.setAlignment(addDataPointButton, Pos.CENTER); 
     BorderPane.setMargin(addDataPointButton, new Insets(10)); 
     BorderPane root = new BorderPane(chart, null, null, addDataPointButton, null); 

     Scene scene = new Scene(root, 800, 600); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 
}