JavaFX LineChart 不画圆?

标签 java javafx charts

这个图表显示了问题:

the problem

我有计算数据和绘制图表的 JavaFX 程序,但为什么点连接不正确?我尝试了很多东西,甚至创建了两个单独的系列,但它不起作用。

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

最佳答案

检查了类似的问题here ,其中解决方案取决于数据排序顺序。看着LineChart , SortingPolicy.NONE指定“数据应按照 XYChart.dataProperty() 中列表定义的顺序保留。”

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

根据您的平滑方法,您可能会遇到检查过的那种三次样条伪影 here ,也出现在 中.引用了一种使用贝塞尔曲线的方法 here .

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

关于JavaFX LineChart 不画圆?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72606268/

相关文章:

javascript - 如何在Javascript中分别调用两个同名方法

java - 无法在 Junit 中 Autowiring bean

java - 如何在 Android 的 RelativeLayout 中使用 ScrollView?

java - Gradle JavaFX - java.lang.NullPointerException : Location is required

reactjs - react 车速表或仪表。如何在 react 中创建标记的速度计?

javascript - Chart.js - 同一 Canvas 上的多个圆环图

java - Spring RestTemplate 处理异常

java - 我应该更喜欢 ThreadLocalRandom 而不是 ThreadLocal<Random>?

Javafx 和 Sun.glass 机器人由于使用线程而导致 IllegalStateException 问题

java - 当新阶段弹出时,我如何禁用初级阶段