JavaFX real-time LineChart with time axis

前端 未结 2 2190
甜味超标
甜味超标 2021-02-20 05:03

I\'m trying to plot real-time graph, with time axis, but I have found the LineChart constructor only has the signature.

LineChart(Axis xAxi         


        
2条回答
  •  悲&欢浪女
    2021-02-20 05:37

    Download Ensemble sample from http://www.oracle.com/technetwork/java/javafx/samples/index.html

    There are several examples in it for dynamic charts, e.g. "Advanced Stock Line Chart". You can take a look at their source code directly in the application.

    enter image description here

    To show time on axis you can use string and DateFormatter:

        BarChart chart = new BarChart<>(new CategoryAxis(), new NumberAxis());
    
        final XYChart.Series series1 = new XYChart.Series<>();
        chart.getData().addAll(series1);
    
        SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
        Date date = new Date();
        for (int i = 0; i <= 10; i += 1) {
            date.setTime(date.getTime() + i * 11111);
            series1.getData().add(new XYChart.Data(dateFormat.format(date), Math.random() * 500));
        }
    

提交回复
热议问题