title: Retrieve String values from a CategoryAxis in a javaFX Line Chart -CategoryAxis JavaFx 2.2.3 bug in .getValueForDisplay(double displayPosition)-
body: <p>I have a JavaFXline chart with months versus values. I'm trying to print in the console the month and the value from a Line chart every time I click the graph .The problem is that when I call the method <code>getValueForDisplay</code> in my <code>CategoryAxis</code> object, it returns <code>null</code>.<br>
  When I click (x,y):jan,3<br>
  I want to print:<br>
  <code>X value = jan Y value: 3</code><br>
  but the program is printing:<br>
  <code>X value = **null** Y value: 2.7796610169491527</code>  </p>
  
  <p>Another question;<br>
  2) Why when attempting to print the coordinates it seems to be not calibrated (if I click on 3 the program print <code>2.779661016949152</code>? You can notice this issue checking the values printed on the console.</p>
  
  <p>Here is the code I'm using:</p>
  
  <pre><code>public class LineChartTest extends Application {
  String xValue;
  Number yValue;
  private void init(Stage primaryStage) {
      Group root = new Group();
      primaryStage.setScene(new Scene(root));
      final CategoryAxis xAxis = new CategoryAxis();
  
      final NumberAxis yAxis = new NumberAxis("Values for Y-Axis", 0, 3, 1);
  
      yAxis.setUpperBound(5);
  
      ObservableList&lt;XYChart.Series&lt;String,Double&gt;&gt; lineChartData = FXCollections.observableArrayList(
          new LineChart.Series&lt;String,Double&gt;("en", FXCollections.observableArrayList(
              new XYChart.Data&lt;String,Double&gt;("jan", 1.0),
              new XYChart.Data&lt;String,Double&gt;("feb", 2.0),
              new XYChart.Data&lt;String,Double&gt;("mar", 3.0),
              new XYChart.Data&lt;String,Double&gt;("apr", 4.0),
              new XYChart.Data&lt;String,Double&gt;("may", 0.5)
          )),
          new LineChart.Series&lt;String,Double&gt;("to", FXCollections.observableArrayList(
              new XYChart.Data&lt;String,Double&gt;("jan", 1.6),
              new XYChart.Data&lt;String,Double&gt;("feb", 0.4),
              new XYChart.Data&lt;String,Double&gt;("mar", 2.9),
              new XYChart.Data&lt;String,Double&gt;("apr", 1.3),
              new XYChart.Data&lt;String,Double&gt;("may", 0.9)
          ))
      );
  
  
      //define a new line
      final Series&lt;String,Double&gt; otherSeries = new Series&lt;String,Double&gt;();
      //set points
      otherSeries.getData().add(new XYChart.Data("jan",3));
      otherSeries.getData().add(new XYChart.Data("feb",2));
      otherSeries.getData().add(new XYChart.Data("mar",4));
      //set name to the line
      otherSeries.setName("tre");
      //add the new line to the graph
      lineChartData.add(otherSeries);
      final LineChart chart = new LineChart(xAxis, yAxis, lineChartData);
  
      //set title to the figure
      chart.setTitle("Line Chart Test");
  
      root.getChildren().add(chart);
  
      chart.setOnMouseClicked(new EventHandler&lt;MouseEvent&gt;() {
  
            @Override
          public void handle(MouseEvent t) {
  
              //Why this seems to be not working properly?
              double sceneX = t.getSceneX();
              double sceneY = t.getSceneY();
  
              //gives the point of the graph
              xValue = xAxis.getValueForDisplay(sceneX);
              yValue = yAxis.getValueForDisplay(sceneY);
  
  
              System.out.println("sceneX-25: "+ sceneX);
              System.out.println("getDisplayPosition: " + xAxis.getValueForDisplay(sceneX));
  
              //Test
              System.out.println(" X value = " +  
              xValue + " Y value: " + yValue );  
  
              }
      });
  
  }
  
  @Override public void start(Stage primaryStage) throws Exception {
      init(primaryStage);
      primaryStage.show();
  
  
  }
  
  
      public static void main(String[] args) {
          launch(args);
      }
  }
  </code></pre>
  
  <p><b>Answer:</b></p>
  
  <p><i>Thanks to Sergey Grinev and Uluk Biy</i></p>
  
  <p>in the previous code you have to modified:</p>
  
  <p>Line chart code:</p>
  
  <pre><code>    chart.setOnMouseClicked(new EventHandler&lt;MouseEvent&gt;() {
  
  
            @Override
            public void handle(MouseEvent e) {
              Node chartPlotBackground = chart.lookup(".chart-plot-background");
              final double shiftX = xSceneShift(chartPlotBackground);
              final double shiftY = ySceneShift(chartPlotBackground);    
  
              double x = e.getSceneX() - shiftX;
              double y = e.getSceneY() - shiftY;
  
              xValue = xAxis.getValueForDisplay(x);
              yValue = yAxis.getValueForDisplay(y );
              System.out.println("shiftX = " + shiftX + " shiftY: " + shiftY);
              System.out.println("X value = "
                  + xValue + " \nY value: " + yValue);
            }
      });
  
  
  }
  //recursive calls    
  private double xSceneShift(Node node) {
  return node.getParent() == null ? 0 : node.getBoundsInParent().getMinX() +    xSceneShift(node.getParent());
  }
  
  private double ySceneShift(Node node) {
  return node.getParent() == null ? 0 : node.getBoundsInParent().getMinY() + ySceneShift(node.getParent());
  }
  
      @Override 
      public void start(Stage primaryStage) throws Exception {
      init(primaryStage);
      primaryStage.show();
  
  
  }
  
  
  public static void main(String[] args) {
      launch(args);
      }
  }
  </code></pre>
  
  <p>in the <code>CategoryAxis</code> source code you have to modified (look at the comments!):</p>
  
  <pre><code> @Override public String getValueForDisplay(double displayPosition) {
      if (getSide().equals(Side.TOP) || getSide().equals(Side.BOTTOM)) { // HORIZONTAL
          if (displayPosition &lt; 0 || displayPosition &gt; getHeight()) return null; //   &lt;-------------- WRONG SHOULD BE displayPosition &gt; getWidth()
          double d = (displayPosition - firstCategoryPos.get()) / categorySpacing.get();
          return toRealValue(d);
      } else { // VERTICAL
          if (displayPosition &lt; 0 || displayPosition &gt; getWidth()) return null; // &lt;-------------- WRONG SHOULD BE displayPosition &gt; getHeight()
          double d = (displayPosition - firstCategoryPos.get()) / (categorySpacing.get() * -1);
          return toRealValue(d);
      }
  } 
  </code></pre>
  
  <p>you can find the source code here:</p>
  
  <p><a href="http://hg.openjdk.java.net/openjfx/2.1/master/rt/file/5c3b3d524f07/javafx-ui-controls/src/javafx/scene/chart/CategoryAxis.java" rel="nofollow">http://hg.openjdk.java.net/openjfx/2.1/master/rt/file/5c3b3d524f07/javafx-ui-controls/src/javafx/scene/chart/CategoryAxis.java</a></p>