gpt4 book ai didi

javafx-2 - 具有 65000 个数据点的 JavaFX LineChart 的性能问题

转载 作者:行者123 更新时间:2023-12-04 12:30:59 24 4
gpt4 key购买 nike

构建描述的 LineChart 需要 JavaFX 15 分钟,这对我的任务不起作用。

使用旧的 Swing 和 jFreeChart 的类似实现需要 1.5 秒来构建图表。

但我仍然想实现一个 JavaFX。

这是我的代码:

public class FXMLController implements Initializable {

@FXML
private Label statusbar;
@FXML
public LineChart lineChart;
@FXML
public Button connect;
@FXML
public MenuItem options;
@FXML
public NumberAxis xAxis;
@FXML
NumberAxis yAxis;

@FXML
private void connect(ActionEvent event) {

}
public static FileChooser fileChooser = new FileChooser();
public static String path;
public static XYChart.Series<Integer, Integer> dataSeries = new XYChart.Series<Integer, Integer>();
public static int y = 0;
public static XYChart.Data<Integer, Integer> data;


@FXML
private void open(ActionEvent event) {
fileChooser.setTitle("Open Resource File");
fileChooser.getExtensionFilters().addAll(
new ExtensionFilter("Text Files", "*.txt"),
new ExtensionFilter("Image Files", "*.png", "*.jpg", "*.gif"),
new ExtensionFilter("Audio Files", "*.wav", "*.mp3", "*.aac"),
new ExtensionFilter("All Files", "*.*"));
File selectedFile = fileChooser.showOpenDialog(new Stage());
if (selectedFile != null) {
path = selectedFile.getAbsolutePath();
System.out.println(path);
try {
ReadingFromFile.readFile(path);

} catch (IOException ex) {
Logger.getLogger(FXMLController.class.getName()).log(Level.SEVERE, null, ex);
}
}

}

@FXML
private void close(ActionEvent event) {

}

@FXML
private void getconnect(ActionEvent event) {

}

@Override
public void initialize(URL url, ResourceBundle rb) {
xAxis.setLabel("Tick");
xAxis.setTickUnit(100);
yAxis.setLabel("Signal");
xAxis.setForceZeroInRange(false);
lineChart.setLegendVisible(false);
lineChart.setCreateSymbols(false);
lineChart.setAnimated(false);
lineChart.getData().add(dataSeries);
}
}

并从文件中读取:
public class ReadingFromFile extends FXMLController {

public static String s = null;
public static String[] str;
public static int parseInt;

public static void readFile(String filename)
throws IOException {

BufferedReader br = new BufferedReader(new FileReader(filename));
try {
StringBuilder sb = new StringBuilder();
String line = br.readLine();

while (line != null) {
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();

System.out.println(line);
try {
str = line.split(" ");
for (int i = 0; i < str.length; i = i + 2) {
s = str[i + 1] + str[i];
parseInt = Integer.parseInt(s, 16);
javafx.application.Platform.runLater(new Runnable() {
@Override
public void run() {

data = new XYChart.Data<Integer, Integer>(y, parseInt);
//data.setNode(new HoveredThresholdNode(0, second, ""));
dataSeries.getData().add(data);
y++;
}

});
}
} catch (java.lang.NullPointerException ex) {
System.out.println("тут ноль!!!");

}

}

} finally {

br.close();
}

}

}

最佳答案

我遇到了类似的问题,每隔几秒钟向 LineChart 添加 100,000 个点。我们使用 Ramer–Douglas–Peucker algorithm 解决了它,这会在用户不注意的情况下减少线上的点数。我在 JTS topology suite 中找到了一个现成的实现在 LGPL 许可下。

这是我的测试代码。

public class ChartUpdate extends Application {

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

@Override
public void start(Stage stage) {

NumberAxis xAxis = new NumberAxis(0, 50_000, 5000);
xAxis.setAutoRanging(false);
NumberAxis yAxis = new NumberAxis(-1, 1, 25);
yAxis.setAutoRanging(false);
LineChart<Number, Number> graph = new LineChart<>(xAxis, yAxis);
graph.setAnimated(false);
graph.setCreateSymbols(false);
graph.setLegendVisible(false);
Series<Number, Number> series = new Series<>();
stage.setScene(new Scene(graph));

GeometryFactory gf = new GeometryFactory();

long t0 = System.nanoTime();
Coordinate[] coordinates = new Coordinate[100_000];
for (int i = 0; i < coordinates.length; i++) {
coordinates[i] = new Coordinate(i, Math.sin(Math.toRadians(i / 100)));
}
Geometry geom = new LineString(new CoordinateArraySequence(coordinates), gf);
Geometry simplified = DouglasPeuckerSimplifier.simplify(geom, 0.00001);
List<Data<Number, Number>> update = new ArrayList<Data<Number, Number>>();
for (Coordinate each : simplified.getCoordinates()) {
update.add(new Data<>(each.x, each.y));
}
long t1 = System.nanoTime();

System.out.println(String.format("Reduces points from %d to %d in %.1f ms", coordinates.length, update.size(),
(t1 - t0) / 1e6));
ObservableList<Data<Number, Number>> list = FXCollections.observableArrayList(update);
series.setData(list);
graph.getData().add(series);

stage.show();

}
}

关于javafx-2 - 具有 65000 个数据点的 JavaFX LineChart 的性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28850211/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com