Present node +LineChart + JavaFX

1k Views Asked by At

I am using LineChart in the javafx8 and I am able to get the series plotted dynamically in the graph and the symbols plotted for every input. But the problem is I just want to show the symbol for the present node and not for the nodes which are already added.

I tried:

linechart.setCreateSymbols(false);

But that does not fulfill my needs.It stopped creating symbols for inputs.

Can anyone tell How to show only the present symbol not the added oness????

1

There are 1 best solutions below

0
On BEST ANSWER

Just keep track of the node for the last data point added. When a new point is added, set the visibility of the previous "last point" to false.

You can do this directly with

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);
    }
}

If you want to be able to factor the code out more readily, you might use an ObjectProperty and register a listener:

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);
    }
}