我遇到LineChart的问题。我的代码:
package test;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.chart.Chart;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Path;
import javafx.scene.shape.Shape;
import javafx.stage.Stage;
public class SampleLineChart extends Application {
public LineChart<Number, Number> lineChart;
public Button button = new Button();
public XYChart.Series series;
@Override public void start(Stage stage) {
stage.setTitle("Line Chart Sample");
// defining the axes
final NumberAxis xAxis = new NumberAxis();
final NumberAxis yAxis = new NumberAxis();
xAxis.setLabel("Number of Month");
// creating the chart
lineChart = new LineChart<Number, Number>(xAxis, yAxis);
lineChart.setTitle("Stock Monitoring, 2010");
// defining a series
series = new XYChart.Series();
series.setName("My portfolio");
// populating the series with data
series.getData().add(new XYChart.Data<Integer, Integer>(1, 23));
series.getData().add(new XYChart.Data<Integer, Integer>(2, 14));
series.getData().add(new XYChart.Data<Integer, Integer>(3, 15));
series.getData().add(new XYChart.Data<Integer, Integer>(4, 24));
series.getData().add(new XYChart.Data<Integer, Integer>(5, 34));
series.getData().add(new XYChart.Data<Integer, Integer>(6, 36));
series.getData().add(new XYChart.Data<Integer, Integer>(7, 22));
series.getData().add(new XYChart.Data<Integer, Integer>(8, 45));
series.getData().add(new XYChart.Data<Integer, Integer>(9, 43));
series.getData().add(new XYChart.Data<Integer, Integer>(10, 17));
series.getData().add(new XYChart.Data<Integer, Integer>(11, 29));
series.getData().add(new XYChart.Data<Integer, Integer>(12, 25));
lineChart.setCreateSymbols(false);
Pane pane = new Pane();
HBox hbox = new HBox();
hbox.getChildren().add(lineChart);
button = new Button("Naciśnij");
hbox.getChildren().add(button);
button.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent arg0) {
System.out.println("Get cords");
}
});
pane.getChildren().add(hbox);
Scene scene = new Scene(pane, 800, 600);
lineChart.getData().add(series);
Shape shape = lineChart.getShape();
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
当我点击Button时,我需要从lineChart获取坐标x,y点。在调试模式下,我可以读取lineChart-&gt; children-&gt; list-&gt; backingList-&gt; elementData-&gt;图表$ 1-&gt; children-&gt; list-&gt; backlist-&gt; XYChart $ 1 - &gt的coords ;儿童 - &gt;列表 - &gt; backingList - &gt; elementData - &gt;路径[3]。 但我不知道如何编写代码来获取坐标?
----我的解决----
package test;
import java.util.Random;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Bounds;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.chart.XYChart.Data;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class SampleLineChart extends Application {
private XYChart.Series<Number, Number> series = new XYChart.Series<>();
private NumberAxis yAxis;
private NumberAxis xAxis;
private double yShift;
private double xShift;
Pane pane;
private void Coords() {
int index = 0;
for (Data<Number, Number> value : series.getData()) {
System.out.println("Coord #" + index);
double x=xShift + xAxis.getDisplayPosition(value.getXValue().doubleValue());
double y=yShift + yAxis.getDisplayPosition(value.getYValue().doubleValue());
System.out.println(x);
System.out.println(y);
index++;
}
}
@Override public void start(Stage stage) {
LineChart<Number, Number> chart = new LineChart<>(xAxis = new NumberAxis(0, 100, 10), yAxis = new NumberAxis(0, 100, 10));
series.getData().add(new XYChart.Data(0, 0));
series.getData().add(new XYChart.Data(1, 2));
series.getData().add(new XYChart.Data(2, 14));
series.getData().add(new XYChart.Data(4, 18));
series.getData().add(new XYChart.Data(10, 20));
chart.getData().addAll(series);
pane = new Pane();
pane.getChildren().addAll(chart);
Scene scene = new Scene(pane);
// add new value on mouseclick for testing
chart.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent t) {
series.getData().add(new XYChart.Data(series.getData().size() * 10, 30 + 50 * new Random().nextDouble()));
Coords();
}
});
stage.setScene(scene);
stage.show();
// find chart area Node
Node chartArea = chart.lookup(".chart-plot-background");
Bounds chartAreaBounds = chartArea.localToScene(chartArea.getBoundsInLocal());
// remember scene position of chart area
yShift = chartAreaBounds.getMinY();
xShift = chartAreaBounds.getMinX();
// set x parameters of the valueMarker to chart area bounds
Coords();
}
public static void main(String[] args) {
launch();
}
}