获取散点图的坐标 XYChart.Data JavaFX
Get coordinates of a scatter chart XYChart.Data JavaFX
我正在尝试获取一系列散点图的每个数据(点)的坐标。
public class Main extends Application {
@Override public void start(Stage stage) {
stage.setTitle("Scatter Chart Issue");
final NumberAxis xAxis = new NumberAxis(0, 10, 1);
final NumberAxis yAxis = new NumberAxis(-100, 500, 100);
final ScatterChart<Number,Number> sc = new
ScatterChart<Number,Number>(xAxis,yAxis);
xAxis.setLabel("Time");
yAxis.setLabel("Random data");
sc.setTitle("Chart");
XYChart.Series series1 = new XYChart.Series();
series1.setName("Series1");
series1.getData().add(new XYChart.Data(4.2, 193.2));
series1.getData().add(new XYChart.Data(2.8, 33.6));
series1.getData().add(new XYChart.Data(6.2, 24.8));
sc.getData().add(series1);
for(XYChart.Data<Number, Number> dot : sc.getData().get(0).getData()) {
double xCoordinate = dot.getNode().localToScene(dot.getNode().getBoundsInLocal()).getMinX();
double yCoordinate = dot.getNode().localToScene(dot.getNode().getBoundsInLocal()).getMinY();
System.out.println(xCoordinate + ", " + yCoordinate);
}
Scene scene = new Scene(sc, 500, 400);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
这是我得到的,似乎该系列的所有点都具有相同的坐标,这是错误的:
因此我的问题是:如何从一系列散点图中获取每个数据的坐标?
谢谢。
编辑:完成工作 class 添加
在 stage.show 之后放置 for 循环:
You're asking for the coordinates before layout has occurred (so the coordinates of all the nodes have not been set).
Scene scene = new Scene(sc, 500, 400);
stage.setScene(scene);
stage.show();
for(XYChart.Data<Number, Number> dot : sc.getData().get(0).getData()) {
double xCoordinate = dot.getNode().localToScene(dot.getNode().getBoundsInLocal()).getMinX();
double yCoordinate = dot.getNode().localToScene(dot.getNode().getBoundsInLocal()).getMinY();
System.out.println(xCoordinate + ", " + yCoordinate);
}
我正在尝试获取一系列散点图的每个数据(点)的坐标。
public class Main extends Application {
@Override public void start(Stage stage) {
stage.setTitle("Scatter Chart Issue");
final NumberAxis xAxis = new NumberAxis(0, 10, 1);
final NumberAxis yAxis = new NumberAxis(-100, 500, 100);
final ScatterChart<Number,Number> sc = new
ScatterChart<Number,Number>(xAxis,yAxis);
xAxis.setLabel("Time");
yAxis.setLabel("Random data");
sc.setTitle("Chart");
XYChart.Series series1 = new XYChart.Series();
series1.setName("Series1");
series1.getData().add(new XYChart.Data(4.2, 193.2));
series1.getData().add(new XYChart.Data(2.8, 33.6));
series1.getData().add(new XYChart.Data(6.2, 24.8));
sc.getData().add(series1);
for(XYChart.Data<Number, Number> dot : sc.getData().get(0).getData()) {
double xCoordinate = dot.getNode().localToScene(dot.getNode().getBoundsInLocal()).getMinX();
double yCoordinate = dot.getNode().localToScene(dot.getNode().getBoundsInLocal()).getMinY();
System.out.println(xCoordinate + ", " + yCoordinate);
}
Scene scene = new Scene(sc, 500, 400);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
这是我得到的,似乎该系列的所有点都具有相同的坐标,这是错误的:
因此我的问题是:如何从一系列散点图中获取每个数据的坐标? 谢谢。
编辑:完成工作 class 添加
在 stage.show 之后放置 for 循环:
You're asking for the coordinates before layout has occurred (so the coordinates of all the nodes have not been set).
Scene scene = new Scene(sc, 500, 400);
stage.setScene(scene);
stage.show();
for(XYChart.Data<Number, Number> dot : sc.getData().get(0).getData()) {
double xCoordinate = dot.getNode().localToScene(dot.getNode().getBoundsInLocal()).getMinX();
double yCoordinate = dot.getNode().localToScene(dot.getNode().getBoundsInLocal()).getMinY();
System.out.println(xCoordinate + ", " + yCoordinate);
}