javafx如何实时绘制图表

时间:2017-04-02 10:37:46

标签: java javafx

我试图实时绘制图表,以创建弹丸轨迹的图示,并模拟它在空中的移动方式。但是,当我尝试这样做时,图表上没有绘制任何内容,但不会抛出任何错误。

    String seriesName = " ";
    fireButton.setOnAction(e -> {
        double initialVelValue = initialVelSlider.getValue();
        double angleValue = angleSlider.getValue();
        double gravityValue = gravitySlider.getValue();
        // Define series
        XYChart.Series series = new XYChart.Series();
        series.setName(seriesName);     // Title of the series
        graph.setLegendVisible(false);
        long startTime = System.nanoTime();
        new AnimationTimer() {
            @Override
            public void handle(long now) {
                double elapsedTime = (now - startTime)/1000000000d;
                updateGraph(graph, initialVelValue, angleValue, gravityValue, 0, 
                        projectileCalculations.getTotalDisplacement(initialVelValue, angleValue, gravityValue),
                        " ", elapsedTime);

                System.out.println(elapsedTime);
                if(elapsedTime > projectileCalculations.getTotalTime(initialVelValue, angleValue, gravityValue)) {
                    this.stop();
                }
            }
        }.start();

public void updateGraph(LineChart chart, double initialVel, double angle, double gravity, double start, double stop, String seriesName, double elapsedTime){
    double step = (stop-start) / 100; // Divide the visible area into 1000 parts

    int vMax = 0;
    double vDisplacement = projectileCalculations.getVDisplacement(initialVel, angle, gravity, elapsedTime);
    double hDisplacement = projectileCalculations.getHDisplacement(initialVel, angle, elapsedTime);
    vMax = (int) (vDisplacement > vMax ? Math.ceil(vDisplacement) : vMax);



    XYChart.Series series;
    boolean newChart = false;
    if (chart.getData().size() > 0) {
        newChart = true;
        series = (XYChart.Series) chart.getData().get(0);
    } else {
        series = new Series();
    }

    series.getData().clear();
    series.getData().add(new XYChart.Data(hDisplacement, vDisplacement));


    ((NumberAxis) chart.getYAxis()).setAutoRanging(false); // Alow manual choosing of range
    ((NumberAxis) chart.getYAxis()).setLowerBound(0); // Set start and stop of axis
    ((NumberAxis) chart.getYAxis()).setUpperBound(vMax);


    if (newChart) {
        chart.getData().add(series); // Add data series to line chart
    }
}           

1 个答案:

答案 0 :(得分:0)

我不明白您正在尝试做什么,但由于chart.getData()开始为空newChart为空,因此系统数据不会添加任何系列。

我会简化这个只与1系列合作,首先解决超过1系列的绘画问题。