2

I am learning javafx for creating GUI. I want to implement a simple line chart of X-Y NumberAxis. As per the property of line chart, the lines connecting the data points never intersect.

For example - Suppose for data points

 series.getData().add(new XYChart.Data(1.5,1.5));
 series.getData().add(new XYChart.Data(2.5,4.5));
 series.getData().add(new XYChart.Data(4.5,2.5));
 series.getData().add(new XYChart.Data(1.0,2.0));
 series.getData().add(new XYChart.Data(3.0,3.0));`

The output for these points in line chart is - line chart](http://[![enter image description here]1)

where as it should be as the following edited image of scatter chart output of same points -enter image description here

Is there a way to change this behavior of line chart? OR connect the dots of scatter chart?

Note that I am going to add points dynamically and lower bound, upper bound of both the axis are going to vary continuously.

code_poetry
  • 333
  • 1
  • 6
  • 16

1 Answers1

3

Switch the sorting policy off for the chart.

lineChart.setAxisSortingPolicy(LineChart.SortingPolicy.NONE);

When I use this, I get this beautiful child's scribble.

mess

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;

public class LineChartSample extends Application {

    @Override public void start(Stage stage) {
        final NumberAxis xAxis = new NumberAxis();
        final NumberAxis yAxis = new NumberAxis();

        final LineChart<Number,Number> lineChart =
                new LineChart<>(xAxis, yAxis);

        XYChart.Series<Number, Number> series = new XYChart.Series<>();
        series.getData().addAll(
                new XYChart.Data<>(4, 24),
                new XYChart.Data<>(1, 23),
                new XYChart.Data<>(6, 36),
                new XYChart.Data<>(8, 45),
                new XYChart.Data<>(2, 14),
                new XYChart.Data<>(9, 43),
                new XYChart.Data<>(7, 22),
                new XYChart.Data<>(12, 25),
                new XYChart.Data<>(10, 17),
                new XYChart.Data<>(5, 34),
                new XYChart.Data<>(3, 15),
                new XYChart.Data<>(11, 29)
        );
        lineChart.setAxisSortingPolicy(LineChart.SortingPolicy.NONE);

        Scene scene  = new Scene(lineChart,800,600);
        lineChart.getData().add(series);
        lineChart.setLegendVisible(false);

        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
jewelsea
  • 150,031
  • 14
  • 366
  • 406