gpt4 book ai didi

Java Fx - 将功能放大到实时图表

转载 作者:行者123 更新时间:2023-12-02 11:37:01 26 4
gpt4 key购买 nike

我想通过鼠标拖动或缩放按钮实现实时折线图的放大功能(图表每 50 毫秒移动一次)。我想放大特定区域..但是我面临的问题是每次图形中的(x,y)坐标值发生变化时。有什么办法可以放大这个图表吗?

JavaFx 示例代码在这里..

    import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.animation.AnimationTimer;
import javafx.animation.Timeline;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart.Data;
import javafx.scene.chart.XYChart.Series;
import javafx.stage.Stage;



public class MainApp extends Application {

private static final int MAX_DATA_POINTS = 100;

private Series series;
private int xSeriesData = 100;
private ConcurrentLinkedQueue<Number> dataQ = new ConcurrentLinkedQueue<Number>();
private ExecutorService executor;
private AddToQueue addToQueue;
private Timeline timeline2;
private NumberAxis xAxis;

private void init(Stage primaryStage) throws Exception {

xAxis = new NumberAxis(0, MAX_DATA_POINTS, MAX_DATA_POINTS / 100);
xAxis.setForceZeroInRange(false);
xAxis.setAutoRanging(false);

NumberAxis yAxis = new NumberAxis();
yAxis.setAutoRanging(true);

//-- Chart
final LineChart<Number, Number> sc = new LineChart<Number, Number>(xAxis, yAxis) {
// Override to remove symbols on each data point
@Override
protected void dataItemAdded(Series<Number, Number> series, int itemIndex, Data<Number, Number> item) {

}
};
sc.setAnimated(false);
sc.setId("liveAreaChart");
sc.setTitle("RealTime Area Chart");

//-- Chart Series
series = new LineChart.Series<Number, Number>();
series.setName("Area Chart Series");
sc.getData().add(series);

primaryStage.setScene(new Scene(sc));
}

@Override
public void start(Stage primaryStage) throws Exception {

init(primaryStage);
primaryStage.show();

executor = Executors.newCachedThreadPool();

addToQueue = new AddToQueue();

executor.execute(addToQueue);

//-- Prepare Timeline
prepareTimeline();

}

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

private class AddToQueue implements Runnable {

public void run() {
try {

dataQ.add(Math.random());

Thread.sleep(50);

executor.execute(this);

} catch (InterruptedException ex) {

Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

//-- Timeline gets called in the JavaFX Main thread
private void prepareTimeline() {
// Every frame to take any data from queue and add to chart

new AnimationTimer() {

@Override
public void handle(long now) {

addDataToSeries();

}
}.start();

}

private void addDataToSeries() {

for (int i = 0; i < 20; i++) {

if (dataQ.isEmpty()) {

break;
}

series.getData().add(new LineChart.Data(xSeriesData++, dataQ.remove()));

}
// remove points to keep us at no more than MAX_DATA_POINTS
if (series.getData().size() > MAX_DATA_POINTS) {
series.getData().remove(0, series.getData().size() - MAX_DATA_POINTS);
}

// update
xAxis.setLowerBound(xSeriesData - MAX_DATA_POINTS);
int m = xSeriesData - MAX_DATA_POINTS;

xAxis.setUpperBound(xSeriesData - 1);
int j = xSeriesData - 1;

}

}

我使用了 https://github.com/kerner1000/javafx-chart-zooming/blob/master/src/main/java/com/github/javafx/charts/zooming/ZoomManager.java 中的缩放管理器代码但鼠标释放功能未调用。

提前致谢:)

最佳答案

因为原生图表不提供缩放、平移等功能,所以我使用plotly.js 图表实现了一个库。你可以去github看看,它是开源的。也许这会有所帮助

https://github.com/jasrodis/javafx-dataviewer/

关于Java Fx - 将功能放大到实时图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48861064/

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