public Parent createContent() { final NumberAxis xAxis = new NumberAxis(); xAxis.setForceZeroInRange(false); final NumberAxis yAxis = new NumberAxis(-100, 100, 10); final ScatterChart<Number, Number> sc = new ScatterChart<>(xAxis, yAxis); // setup chart sc.getStylesheets() .add(LiveScatterChartApp.class.getResource("LiveScatterChart.css").toExternalForm()); sc.setTitle("Animated Sine Wave ScatterChart"); xAxis.setLabel("X Axis"); xAxis.setAnimated(false); yAxis.setLabel("Y Axis"); yAxis.setAutoRanging(false); // add starting data series = new ScatterChart.Series<>(); series.setName("Sine Wave"); series.getData().add(new ScatterChart.Data<Number, Number>(5d, 5d)); sc.getData().add(series); return sc; }
public LiveScatterChartApp() { // create animation Timeline timeline1 = new Timeline(); timeline1 .getKeyFrames() .add( new KeyFrame( Duration.millis(20), (ActionEvent actionEvent) -> { series .getData() .add( new XYChart.Data<Number, Number>( nextX, Math.sin(Math.toRadians(nextX)) * 100)); nextX += 10; })); timeline1.setCycleCount(200); Timeline timeline2 = new Timeline(); timeline2 .getKeyFrames() .add( new KeyFrame( Duration.millis(50), (ActionEvent actionEvent) -> { series .getData() .add( new XYChart.Data<Number, Number>( nextX, Math.sin(Math.toRadians(nextX)) * 100)); if (series.getData().size() > 54) { series.getData().remove(0); } nextX += 10; })); timeline2.setCycleCount(Animation.INDEFINITE); animation = new SequentialTransition(); animation.getChildren().addAll(timeline1, timeline2); }