javajavafxgraphjfreechartjfreechart-fx

How would I change the axis labels when I change the values of the choice boxes


The below code plot a graph which values can be plotted by inputting values into a Spinner. I've added choice boxes next to these spinners, and I'd like it where I change the values of the choicebox so the axis labels change according to the choice boxes

public class ScatterAdd extends Application {
    private final XYSeries series = new XYSeries("Voltage");

    ChoiceBox<String> domainLabels = new ChoiceBox<>();
    ChoiceBox<String> rangeLabels = new ChoiceBox<>();

    private JFreeChart createChart() {
        XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(series);
        return ChartFactory.createScatterPlot("VI Characteristics", "Current", "Voltage", dataset);
    }

    @Override
    public void start(Stage stage) {
        domainLabels.getItems().addAll("Current", "Seconds");
        domainLabels.setValue("Current");

        rangeLabels.getItems().addAll("Voltage", "Metres");
        rangeLabels.setValue("Voltage");

        JFreeChart chart = createChart();
        domainLabels.getSelectionModel().selectedItemProperty().addListener((ov, s0, s1) -> {
            chart.getXYPlot().getDomainAxis().setLabel(s1);
        });
        rangeLabels.getSelectionModel().selectedItemProperty().addListener((ov, s0, s1) -> {
            chart.getXYPlot().getRangeAxis().setLabel(s1);
        });
        
        var xSpin = new Spinner<Double>(-10000.000, 10000.000, 0);
        xSpin.setEditable(true);
        xSpin.setPromptText("Xvalue");

        var ySpin = new Spinner<Double>(-10000.000, 10000.000, 0);
        ySpin.setEditable(true);
        ySpin.setPromptText("Yvalue");

        var button = new Button("Add");
        button.setOnAction(ae -> series.add(xSpin.getValue(), ySpin.getValue()));

        HBox xBox = new HBox();
        xBox.getChildren().addAll(domainLabels);

        HBox yBox = new HBox();
        yBox.getChildren().addAll(rangeLabels);

        var enter = new ToolBar(xBox, xSpin, yBox, ySpin, button);
        BorderPane.setAlignment(enter, Pos.CENTER);
        
        BorderPane root = new BorderPane();
        root.setCenter(new ChartViewer(createChart()));
        root.setBottom(enter);

        stage.setTitle("ScatterAdd");
        stage.setScene(new Scene(root, 640, 480));
        stage.show();

    }

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

}

Ive attempted to do this my self by adding a condition in the createchart method however i could'nt get this to work.


Solution

  • As shown here, a ChoiceBox<T> can listen to its selection model and respond to changes. As shown here, such a listener can alter the appearance of a chart's components as required.

    Given a pair of ChoiceBox<String> instances, the selection listeners update the axis labels as shown in the image below:

    ChoiceBox<String> domainLabels = new ChoiceBox<>();
    ChoiceBox<String> rangeLabels = new ChoiceBox<>();
    …
    JFreeChart chart = createChart();
    domainLabels.getSelectionModel().selectedItemProperty().addListener((ov, s0, s1) -> {
        chart.getXYPlot().getDomainAxis().setLabel(s1);
    });
    rangeLabels.getSelectionModel().selectedItemProperty().addListener((ov, s0, s1) -> {
        chart.getXYPlot().getRangeAxis().setLabel(s1);
    });
    

    Axis label image