title: JavaFX Area Charts and adding dynamic labels following cursor
body: <p>I'd like to to be able to add a label to a chart that follows the cursor. I've already seen several examples of this on stack overflow already that attach labels to the values/nodes of a series but I want to be able to do this for the whole of the area chart. My problem comes when I want to add a label (ultimately a floating box of values). I'm struggling to figure out how I can do this and maintain the layout of the scene. When I add the chart to the scene as the dedicated item the chart renders as expected. But when I add a label to the scene it is shared and the chart no longer fills the entire canvas</p>
  
  <pre><code>Group root = new Group();
  root.getChildren().addAll(sac,myLabel);
  Scene scene = new Scene(root, 800, 600); // Now the Chart shares the scene with the label
  </code></pre>
  
  <p>I want to add the label so I can use it as the mouse moves over a chart and then simply translate it to the cursor position. I've no problem with the mouse events just that I can't seem to share a scene with objects that are transient. I believe this is down to a lack of understanding on how to manage this particular situation. It's entirely likely the solution to this problem is to dynamically generate this label and not have it as a static scene object. Any help gratefully received.</p>
  
  <p>Full test case below (Java 8)</p>
  
  <pre><code>import javafx.application.Application;
  import javafx.scene.Group;
  import javafx.scene.Node;
  import javafx.scene.Scene;
  import javafx.scene.chart.NumberAxis;
  import javafx.scene.chart.StackedAreaChart;
  import javafx.scene.chart.XYChart;
  import javafx.scene.control.Label;
  import javafx.scene.input.MouseEvent;
  import javafx.stage.Stage;
  
  public class AreaChartTestCase extends Application {
  
      final NumberAxis xAxis = new NumberAxis(1, 31, 1);
      final NumberAxis yAxis = new NumberAxis();
      final StackedAreaChart&lt;Number, Number&gt; sac
              = new StackedAreaChart&lt;Number, Number&gt;(xAxis, yAxis);
      Label myLabel = new Label();
  
      @Override
      public void start(Stage stage) {
          stage.setTitle("Area Chart Sample");
          sac.setTitle("Temperature Monitoring (in Degrees C)");
          XYChart.Series&lt;Number, Number&gt; seriesApril
                  = new XYChart.Series&lt;Number, Number&gt;();
          seriesApril.setName("April");
          seriesApril.getData().add(new XYChart.Data(1, 4));
          seriesApril.getData().add(new XYChart.Data(3, 10));
          seriesApril.getData().add(new XYChart.Data(6, 15));
          seriesApril.getData().add(new XYChart.Data(9, 8));
          seriesApril.getData().add(new XYChart.Data(12, 5));
          seriesApril.getData().add(new XYChart.Data(15, 18));
          seriesApril.getData().add(new XYChart.Data(18, 15));
          seriesApril.getData().add(new XYChart.Data(21, 13));
          seriesApril.getData().add(new XYChart.Data(24, 19));
          seriesApril.getData().add(new XYChart.Data(27, 21));
          seriesApril.getData().add(new XYChart.Data(30, 21));
          XYChart.Series&lt;Number, Number&gt; seriesMay
                  = new XYChart.Series&lt;Number, Number&gt;();
          seriesMay.setName("May");
          seriesMay.getData().add(new XYChart.Data(1, 20));
          seriesMay.getData().add(new XYChart.Data(3, 15));
          seriesMay.getData().add(new XYChart.Data(6, 13));
          seriesMay.getData().add(new XYChart.Data(9, 12));
          seriesMay.getData().add(new XYChart.Data(12, 14));
          seriesMay.getData().add(new XYChart.Data(15, 18));
          seriesMay.getData().add(new XYChart.Data(18, 25));
          seriesMay.getData().add(new XYChart.Data(21, 25));
          seriesMay.getData().add(new XYChart.Data(24, 23));
          seriesMay.getData().add(new XYChart.Data(27, 26));
          seriesMay.getData().add(new XYChart.Data(31, 26));
  
          final Node chartPlotBackground = sac.lookup(".chart-plot-background");
          chartPlotBackground.setOnMouseEntered((MouseEvent mouseEvent) -&gt; {
              myLabel.setVisible(true);
          });
  
          chartPlotBackground.setOnMouseExited((MouseEvent mouseEvent) -&gt; {
              myLabel.setVisible(false);
          });
  
          chartPlotBackground.setOnMouseMoved((MouseEvent mouseEvent) -&gt; {
              myLabel.setText("Location : X = " + mouseEvent.getSceneX() + " : Y = "+ mouseEvent.getSceneY());
              myLabel.setTranslateX(mouseEvent.getSceneX());
              myLabel.setTranslateY(mouseEvent.getSceneY());
          });
  
          Group root = new Group();
          root.getChildren().addAll(sac, myLabel);
          Scene scene = new Scene(root, 800, 600); // Now the Chart shares the scene with the label
          sac.getData().addAll(seriesApril, seriesMay);
          stage.setScene(scene);
          stage.show();
  
      }
  
      public static void main(String[] args) {
          launch(args);
      }
  }
  </code></pre>