我有一个带有实时更新的JavaFX折线图。每100毫秒添加一个新的数据点。该图表导致相对较高的CPU负载。使用setAnimated(false)
禁用动画已经大大提高了性能,但是在四核2,3 GHz Intel Core i7上CPU使用率仍为130%。
我的代码有问题,或者JavaFX折线图通常这么慢?
package test;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.scene.Scene;
import javafx.scene.chart.*;
import javafx.stage.Stage;
public class Main extends Application {
static XYChart.Series quoteSeries = new XYChart.Series();
@Override
public void start(Stage stage) {
final CategoryAxis xAxisLineChart = new CategoryAxis();
final NumberAxis yAxisLineChart = new NumberAxis();
final LineChart<String,Number> lineChart = new LineChart(xAxisLineChart,yAxisLineChart);
lineChart.getData().add(quoteSeries);
lineChart.setCreateSymbols(false);
lineChart.setAnimated(false);
Scene scene = new Scene(lineChart,800,600);
stage.setScene(scene);
stage.show();
new Thread(task).start();
}
// Adds each 100 ms a new data point to the chart
Task task = new Task() {
@Override
protected Integer call() throws Exception {
for (int i=0; i<1000; i++) {
final int q = i;
Thread.sleep(100);
Platform.runLater(new Runnable() {
@Override
public void run() {
XYChart.Data data = new XYChart.Data(""+q, q);
quoteSeries.getData().add(data);
}
});
}
return null;
}
};
public static void main(String[] args) {
launch(args);
}
}