gpt4 book ai didi

java - 如何强制 XYChart 刷新

转载 作者:太空宇宙 更新时间:2023-11-04 10:34:17 27 4
gpt4 key购买 nike

我有两个图表绘制在同一个堆栈 Pane 上。

它们有共同的 x 轴和 y 轴,我想让它们可滚动。

这是我编码的内容:

import javafx.application.Application;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
* Demonstrates how to draw layers of XYCharts.
* https://forums.oracle.com/forums/thread.jspa?threadID=2435995 "Using StackPane to layer more different type charts"
*/
public class LayeredXyChartsSample extends Application {

private XYChart.Series<Number, Number> lineSeries1;
private XYChart.Series<Number, Number> lineSeries2;

private NumberAxis xAxis;
private NumberAxis yAxis;

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

@Override
public void start(Stage stage) {
initSeries();

// Close the application when the window is closed
stage.setOnCloseRequest(t -> {
Platform.exit();
System.exit(0);
});

stage.setScene(
new Scene(
layerCharts(
createLineChart1(),
createLineChart2()
)
)
);
stage.show();
}

@SuppressWarnings("unchecked")
private void initSeries() {
lineSeries1 = new XYChart.Series(
FXCollections.observableArrayList(
new XYChart.Data(1, 2),
new XYChart.Data(2, 10),
new XYChart.Data(3, 8),
new XYChart.Data(4, 4),
new XYChart.Data(5, 7),
new XYChart.Data(6, 5),
new XYChart.Data(7, 4),
new XYChart.Data(8, 8),
new XYChart.Data(9, 16.5),
new XYChart.Data(10, 13.9),
new XYChart.Data(11, 17),
new XYChart.Data(12, 10)
)
);

lineSeries2 = new XYChart.Series(
FXCollections.observableArrayList(
new XYChart.Data(1, 1),
new XYChart.Data(2, 2),
new XYChart.Data(3, 1.5),
new XYChart.Data(4, 3),
new XYChart.Data(5, 2.5),
new XYChart.Data(6, 5),
new XYChart.Data(7, 4),
new XYChart.Data(8, 8),
new XYChart.Data(9, 6.5),
new XYChart.Data(10, 13),
new XYChart.Data(11, 10),
new XYChart.Data(12, 20)
)
);

createXaxis();
createYaxis();
}

private void createYaxis() {
yAxis = new NumberAxis();
yAxis.setAutoRanging(false);
yAxis.setPrefWidth(35);
yAxis.setMinorTickCount(10);
yAxis.setLowerBound(0);
yAxis.setUpperBound(20);
}

private void createXaxis() {
xAxis = new NumberAxis();
xAxis.setAutoRanging(false);
xAxis.setLowerBound(-10);
xAxis.setUpperBound(22);
}

@SuppressWarnings("unchecked")
private LineChart<Number, Number> createLineChart1() {
final LineChart<Number, Number> chart = new LineChart<>(xAxis, yAxis);
setDefaultChartProperties(chart);
chart.setCreateSymbols(false);
chart.getData().addAll(lineSeries1);
return chart;
}

@SuppressWarnings("unchecked")
private LineChart<Number, Number> createLineChart2() {
final LineChart<Number, Number> chart = new LineChart<>(xAxis, yAxis);
setDefaultChartProperties(chart);
chart.setCreateSymbols(false);
chart.getData().addAll(lineSeries2);
return chart;
}

private void setDefaultChartProperties(final XYChart<Number, Number> chart) {
chart.setLegendVisible(false);
chart.setAnimated(false);
}

@SafeVarargs
private final StackPane layerCharts(final XYChart<Number, Number>... charts) {
for (int i = 1; i < charts.length; i++) {
configureOverlayChart(charts[i]);
}

StackPane stackpane = new StackPane();
stackpane.getChildren().addAll(charts);

stackpane.setOnScroll(event -> {
double deltaX = event.getDeltaX();

if (deltaX != 0) {
xAxis.setUpperBound(xAxis.getUpperBound() + deltaX / 10);
xAxis.setLowerBound(xAxis.getLowerBound() + deltaX / 10);
}
});

return stackpane;
}

private void configureOverlayChart(final XYChart<Number, Number> chart) {
chart.setAlternativeRowFillVisible(false);
chart.setAlternativeColumnFillVisible(false);
chart.setHorizontalGridLinesVisible(false);
chart.setVerticalGridLinesVisible(false);
chart.getXAxis().setVisible(false);
chart.getYAxis().setVisible(false);

chart.getStylesheets().addAll(getClass().getResource("/overlay-chart.css").toExternalForm());
}
}

当我用侧面的触控板滚动时,两个图表中只有一个被刷新...

怎么会呢?我也希望更新第二个图表,但我在 XYChart 类中找不到可以调用来强制刷新的方法。所有似乎执行此操作的方法都是私有(private)的...

编辑: 我没有在堆栈 Pane 上设置 onScroll 监听器,而是尝试在两个图表上设置它。但似乎只有顶部布局收到通知...所以我有相同的结果:只有一个图表翻译...

编辑2:我知道在这个例子中我可以只创建一个包含两个系列的图表,而不是堆叠它们,这样就可以了。但实际上,我使用的堆叠图表是不同类型的(折线图和自定义图表),因此我确实需要将它们堆叠在堆栈 Pane 中。

最佳答案

正如 @James_D 所建议的(顺便说一句,非常感谢),解决方案是使用多个 x 轴并分别更新每个轴:

import javafx.application.Application;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

import java.util.ArrayList;
import java.util.List;

/**
* Demonstrates how to draw layers of XYCharts.
* https://forums.oracle.com/forums/thread.jspa?threadID=2435995 "Using StackPane to layer more different type charts"
*/
public class LayeredXyChartsSample extends Application {

private XYChart.Series<Number, Number> lineSeries1;
private XYChart.Series<Number, Number> lineSeries2;

private List<NumberAxis> xAxes = new ArrayList<>();
private NumberAxis yAxis;

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

@Override
public void start(Stage stage) {
initSeries();

// Close the application when the window is closed
stage.setOnCloseRequest(t -> {
Platform.exit();
System.exit(0);
});

stage.setScene(
new Scene(
layerCharts(
createLineChart1(),
createLineChart2()
)
)
);
stage.show();
}

@SuppressWarnings("unchecked")
private void initSeries() {
lineSeries1 = new XYChart.Series(
FXCollections.observableArrayList(
new XYChart.Data(1, 2),
new XYChart.Data(2, 10),
new XYChart.Data(3, 8),
new XYChart.Data(4, 4),
new XYChart.Data(5, 7),
new XYChart.Data(6, 5),
new XYChart.Data(7, 4),
new XYChart.Data(8, 8),
new XYChart.Data(9, 16.5),
new XYChart.Data(10, 13.9),
new XYChart.Data(11, 17),
new XYChart.Data(12, 10)
)
);

lineSeries2 = new XYChart.Series(
FXCollections.observableArrayList(
new XYChart.Data(1, 1),
new XYChart.Data(2, 2),
new XYChart.Data(3, 1.5),
new XYChart.Data(4, 3),
new XYChart.Data(5, 2.5),
new XYChart.Data(6, 5),
new XYChart.Data(7, 4),
new XYChart.Data(8, 8),
new XYChart.Data(9, 6.5),
new XYChart.Data(10, 13),
new XYChart.Data(11, 10),
new XYChart.Data(12, 20)
)
);

createYaxis();
}

private void createYaxis() {
yAxis = new NumberAxis();
yAxis.setAutoRanging(false);
yAxis.setPrefWidth(35);
yAxis.setMinorTickCount(10);
yAxis.setLowerBound(0);
yAxis.setUpperBound(20);
}

private NumberAxis createXaxis() {
NumberAxis xAxis = new NumberAxis();
xAxis.setAutoRanging(false);
xAxis.setLowerBound(-10);
xAxis.setUpperBound(22);
xAxes.add(xAxis);

return xAxis;
}

@SuppressWarnings("unchecked")
private LineChart<Number, Number> createLineChart1() {
final LineChart<Number, Number> chart = new LineChart<>(createXaxis(), yAxis);
setDefaultChartProperties(chart);
chart.setCreateSymbols(false);
chart.getData().addAll(lineSeries1);
return chart;
}

@SuppressWarnings("unchecked")
private LineChart<Number, Number> createLineChart2() {
final LineChart<Number, Number> chart = new LineChart<>(createXaxis(), yAxis);
setDefaultChartProperties(chart);
chart.setCreateSymbols(false);
chart.getData().addAll(lineSeries2);
return chart;
}

private void setDefaultChartProperties(final XYChart<Number, Number> chart) {
chart.setLegendVisible(false);
chart.setAnimated(false);
}

@SafeVarargs
private final StackPane layerCharts(final XYChart<Number, Number>... charts) {
for (int i = 1; i < charts.length; i++) {
configureOverlayChart(charts[i]);
}

StackPane stackpane = new StackPane();
stackpane.getChildren().addAll(charts);

stackpane.setOnScroll(event -> {
double deltaX = event.getDeltaX();
if (deltaX != 0) {
for (NumberAxis xAxis : xAxes) {
xAxis.setUpperBound(xAxis.getUpperBound() - deltaX / 10);
xAxis.setLowerBound(xAxis.getLowerBound() - deltaX / 10);
}
}
});

return stackpane;
}

private void configureOverlayChart(final XYChart<Number, Number> chart) {
chart.setAlternativeRowFillVisible(false);
chart.setAlternativeColumnFillVisible(false);
chart.setHorizontalGridLinesVisible(false);
chart.setVerticalGridLinesVisible(false);
chart.getXAxis().setVisible(false);
chart.getYAxis().setVisible(false);

chart.getStylesheets().addAll(getClass().getResource("/overlay-chart.css").toExternalForm());
}
}

关于java - 如何强制 XYChart 刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49661524/

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