【发布时间】:2021-12-07 17:27:13
【问题描述】:
我正在编写一个基本的Candlestick 图表类,其中烛台被创建为Regions
,并通过将它们的layoutX
和layoutY
值设置为相关轴的getDisplayPosition()
来绘制。
例如,要在 X 轴上绘制值为 3 的烛台,我这样做:
candlestick.setLayoutX(xAxis.getDisplayPosition(3));
当舞台调整大小或放大或缩小轴时,必须重置烛台的布局值,以便图表正确呈现。我目前正在通过ChangeListener
s 处理此事件以调整大小事件,Button.setOnAction()
s 以进行缩放。
但是,我宁愿将烛台的布局属性绑定到轴的显示位置,也不愿设置/重置布局值,但找不到 NumberAxis
的“displayPositionProperty”(或类似的)。
可以这样做吗?我将绑定到哪个NumberAxis
属性?即。
candlestick.layoutXProperty().bind(xAxis.WHICH_PROPERTY?);
另外,绑定属性会比重置布局位置更有效吗?有些图表可能有数千个烛台,但在我弄清楚如何编写绑定代码之前,我无法测试资源使用情况。
我已经尝试将烛台缩放到轴的比例,但不能使用这种方法,因为缩放 Region
会影响其边框宽度。对于某些类型的烛台,这可能会改变其含义。
我还玩过 Ensemble 烛台演示图表。这对我很有帮助,但对我的需求来说太简单了。
这是一个演示我的方法的 MVCE。任何重新绑定的指导将不胜感激。
我正在使用 OpenJFX 17。
package test023;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.chart.NumberAxis;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Test023 extends Application {
@Override
public void start(Stage stage) {
NumberAxis xAxis = new NumberAxis(0D, 10D, 1D);
Pane pChart = new Pane();
Pane pAxis = new Pane();
VBox vb = new VBox();
BorderPane bp = new BorderPane();
pChart.setPrefHeight(100D);
pAxis.getChildren().add(xAxis);
xAxis.prefWidthProperty().bind(pAxis.widthProperty());
xAxis.setAnimated(false);
vb.setPadding(new Insets(10D));
vb.getChildren().addAll(pChart, pAxis);
Region point = new Region();
point.setPrefSize(5D, 5D);
point.setStyle("-fx-background-color: black;");
pChart.getChildren().add(point);
//Plot the point in its initial position (value 3 on the axis)
double pointXValue = 3D;
plotPoint(point, pointXValue, xAxis);
//*****************************************************************
//Can the listeners and button.setOnActions be replaced by binding
//the point's layout value to the axis display position?
//*****************************************************************
//Handle resize events
pChart.widthProperty().addListener((obs, oldVal, newVal) -> {
plotPoint(point, pointXValue, xAxis);
});
stage.maximizedProperty().addListener((obs, oldVal, newVal) -> {
plotPoint(point, pointXValue, xAxis);
});
//Handle zooming (hard-coded upper and lower bounds for the
//sake of simplicity)
Button btnZoomIn = new Button("Zoom in");
btnZoomIn.setOnAction((event) -> {
xAxis.setLowerBound(2D);
xAxis.setUpperBound(8D);
xAxis.layout();
plotPoint(point, pointXValue, xAxis);
});
Button btnZoomOut = new Button("Zoom out");
btnZoomOut.setOnAction((event) -> {
xAxis.setLowerBound(0D);
xAxis.setUpperBound(10D);
xAxis.layout();
plotPoint(point, pointXValue, xAxis);
});
bp.setCenter(vb);
bp.setTop(new HBox(btnZoomIn, btnZoomOut));
stage.setScene(new Scene(bp));
stage.setTitle("Test bind layoutX");
stage.setWidth(400D);
stage.setHeight(200D);
stage.show();
}
private void plotPoint(Region region, double axisPos, NumberAxis axis) {
Platform.runLater(() -> {
double posX = axis.getDisplayPosition(axisPos);
region.setLayoutX(posX);
region.setLayoutY(80D);
});
}
public static void main(String args[]){
launch(args);
}
}
【问题讨论】:
-
另请参阅*.com/questions/38871202/… 了解有关此问题的规范方法。
-
感谢您的参考。我饶有兴趣地读了它,明白了你在说什么。我将尝试继承 Chart 类并覆盖 layoutPlotChildren() 方法。任何使添加趋势线和形状等内容变得更容易的事情都值得做。