Demonxus
Demonxus

Reputation: 64

JavaFX LineChart not drawing circle?

This chart shows the problem:

the problem

I have JavaFX program that calculates data and draws a chart, but why points are not connected properly? I have tried many things, even creating two separate series, but it doesn't work.

public void createScatterChart(){
    final NumberAxis xAxis = new NumberAxis();
    final NumberAxis yAxis = new NumberAxis();

    final SmoothedChart<Number,Number> smoothedChart = new SmoothedChart<>(xAxis, yAxis);

    XYChart.Series series1 = new XYChart.Series();
    XYChart.Series series2 = new XYChart.Series();
    XYChart.Series series3 = new XYChart.Series();

    for(int i = 0 ; i < this.r.size() ; i ++)
    {
        series1.getData().add(new XYChart.Data(this.r.get(i) * Math.cos(Math.toRadians(this.nodes.get(i))),this.r.get(i) * Math.sin(Math.toRadians(this.nodes.get(i)))));
        //series2.getData().add(new XYChart.Data(this.r.get(i) * Math.cos(Math.toRadians(this.nodes.get(i) * this.xArray[i][0])),this.r.get(i) * Math.sin(Math.toRadians(this.nodes.get(i) * this.xArray[i][0]))));
    }


    smoothedChart.getData().add(series1);
    smoothedChart.getData().add(series2);
    Stage stage = new Stage();
    Scene scene  = new Scene(smoothedChart,800,600);

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

Upvotes: 3

Views: 286

Answers (1)

trashgod
trashgod

Reputation: 205785

A similar problem is examined here, in which the solution hinges on the data sort order. Looking at LineChart, SortingPolicy.NONE specifies "The data should be left in the order defined by the list in XYChart.dataProperty()."

I had to change chart from my SmoothChart to standard LineChart.

Depending on your approach to smoothing, you may encounter the kind of cubic spline artifacts examined here, which also occurs in . An approach using Bézier curves is adduced here.

As tested using synthetic data:

NONE X_AXIS

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
 * @see https://stackoverflow.com/a/72607616/230513
 * @see https://stackoverflow.com/a/2510048/230513
 */
public class ChartTest extends Application {

    private static final int N = 32;

    @Override
    public void start(Stage stage) {
        var xAxis = new NumberAxis();
        var yAxis = new NumberAxis();
        var series = new XYChart.Series();
        series.setName("Data");
        for (int i = 0; i <= N; i++) {
            var t = 2 * Math.PI * i / N;
            var x = Math.cos(t);
            var y = Math.sin(t);
            series.getData().add(new XYChart.Data(x, y));
        }
        var chart = new LineChart<Number, Number>(xAxis, yAxis);
        chart.getData().add(series);
        ObservableList<LineChart.SortingPolicy> policies
            = FXCollections.observableArrayList(LineChart.SortingPolicy.values());
        var policy = new ChoiceBox<LineChart.SortingPolicy>(policies);
        policy.setTooltip(new Tooltip("Choose a data sorting policy."));
        policy.getSelectionModel().select(chart.getAxisSortingPolicy());
        chart.axisSortingPolicyProperty().bind(policy.valueProperty());
        Pane root = new StackPane(chart, policy);
        StackPane.setAlignment(policy, Pos.TOP_RIGHT);
        stage.setScene(new Scene(root));
        stage.show();
    }

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

Upvotes: 4

Related Questions