title: JavaFX get coords x,y points from LineChart
body: <p>I have some problem with LineChart. My code:</p>
  
  <pre><code>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&lt;Number, Number&gt; 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&lt;Number, Number&gt;(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&lt;Integer, Integer&gt;(1, 23));
          series.getData().add(new XYChart.Data&lt;Integer, Integer&gt;(2, 14));
          series.getData().add(new XYChart.Data&lt;Integer, Integer&gt;(3, 15));
          series.getData().add(new XYChart.Data&lt;Integer, Integer&gt;(4, 24));
          series.getData().add(new XYChart.Data&lt;Integer, Integer&gt;(5, 34));
          series.getData().add(new XYChart.Data&lt;Integer, Integer&gt;(6, 36));
          series.getData().add(new XYChart.Data&lt;Integer, Integer&gt;(7, 22));
          series.getData().add(new XYChart.Data&lt;Integer, Integer&gt;(8, 45));
          series.getData().add(new XYChart.Data&lt;Integer, Integer&gt;(9, 43));
          series.getData().add(new XYChart.Data&lt;Integer, Integer&gt;(10, 17));
          series.getData().add(new XYChart.Data&lt;Integer, Integer&gt;(11, 29));
          series.getData().add(new XYChart.Data&lt;Integer, Integer&gt;(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&lt;ActionEvent&gt;() {
  
              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);
      }
  }
  </code></pre>
  
  <p>When I click Button, I need to get coordinates x,y points from lineChart. In debug mode , i can read coords from lineChart->children->list->backingList->elementData->Chart$1->children->list->backlist->XYChart$1 -> children -> list -> backingList -> elementData -> Path[3].
  But I don't know how can write code to get coordinates ?</p>
  
  <p>----My Solve----</p>
  
  <pre><code>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&lt;Number, Number&gt; series = new XYChart.Series&lt;&gt;();
      private NumberAxis yAxis;
      private NumberAxis xAxis;
      private double yShift;
      private double xShift;
      Pane pane;
      private void Coords() {
          int index = 0;
          for (Data&lt;Number, Number&gt; 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&lt;Number, Number&gt; chart = new LineChart&lt;&gt;(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&lt;MouseEvent&gt;() {
              @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();
      }
  }
  </code></pre>