4 Replies Latest reply: Nov 19, 2012 4:42 PM by 894573 RSS

    Having tooltip trouble

    894573
      I'm having some tootip issues. The scenario is I'm returning some data from an EJB (which is working). You're supposed hover over a mark on a scatter chart, and it gives you some information about that mark. I can't get it to work. Maybe I'm being too simple about the way I've coded it? Anyway, here's my code:
      ("array" comes from the EJB, and each entry in that array is another array)
      
              XYChart.Series<Number, Number> normalSeries = new XYChart.Series<>();
              XYChart.Series<Number, Number> negativeOutlierSeries = new XYChart.Series<>();
              XYChart.Series<Number, Number> positiveOutlierSeries = new XYChart.Series<>();
      
              for (int i = 0; i < array.size(); i++) {
                  ArrayList arr = (ArrayList) array.get(i);
                  // intercept, slope
                  XYChart.Data data = new XYChart.Data(arr.get(0), arr.get(1));
                  System.out.println("tooltip: " + arr.get(3));
                  // tooltip 
                  Tooltip.install(data.getNode(), new Tooltip(arr.get(3).toString()));
                  // check outlier
                  if (Integer.parseInt(arr.get(2).toString()) == 0) {
                      normalSeries.getData().add(data);
                  // if the outlier isn't zero, check slope
                  } else if (Float.parseFloat(arr.get(1).toString()) < 0) {
                      negativeOutlierSeries.getData().add(data);
                  } else {
                      positiveOutlierSeries.getData().add(data);
                  }
              }
              sc.getData().addAll(normalSeries, negativeOutlierSeries, positiveOutlierSeries);
              sc.setLegendVisible(false);
      The chart displays okay, but when I hover over one of the marks, it doesn't show me anything. I must be doing something wrong, but what?

      Thanks!
        • 1. Re: Having tooltip trouble
          James_D
          I'm guessing, but I think the node property gets set on the data object after the data is added to the chart (maybe even not until the chart becomes part of the scene). This thread Display Tooltips on charts values? has various options for setting tooltips on nodes on chart data.
          • 2. Re: Having tooltip trouble
            894573
            Well... I tried doing a little bit of what you mentioned (the link). It still doesn't work, and I'm perplexed.

            Code:
                    final ScatterChart sc = new ScatterChart(xAxis, yAxis) {
                        @Override
                        protected void layoutPlotChildren() {
                            super.layoutPlotChildren();
                            for (Iterator it = getPlotChildren().iterator(); it.hasNext();) {
                                Object mark = it.next();
                                if (mark instanceof XYChart.Data) {
                                    XYChart.Data data = (XYChart.Data) mark;
                                    Tooltip.install((Node) mark, new Tooltip(data.getExtraValue().toString()));
                                }
                            }
                        }
                    };
            (I shoved the tooltip in the extra value.) <sigh> I'm just confused. Any other ideas?

            Edited by: user_891570 on Nov 18, 2012 9:01 PM
            • 3. Re: Having tooltip trouble
              James_D
              The other mechanism in that thread seems to work ok:
              import java.util.Random;
              
              import javafx.application.Application;
              import javafx.beans.value.ChangeListener;
              import javafx.beans.value.ObservableValue;
              import javafx.scene.Node;
              import javafx.scene.Scene;
              import javafx.scene.chart.NumberAxis;
              import javafx.scene.chart.ScatterChart;
              import javafx.scene.control.Tooltip;
              import javafx.scene.layout.BorderPane;
              import javafx.stage.Stage;
              
              import static javafx.scene.chart.XYChart.*;
              
              public class ScatterChartTest extends Application {
              
                @Override
                public void start(Stage primaryStage) throws Exception {
                  BorderPane root = new BorderPane();
                  NumberAxis xaxis = new NumberAxis();
                  NumberAxis yaxis = new NumberAxis();
                  ScatterChart<Number, Number> scatterPlot = new ScatterChart<>(xaxis, yaxis);
                  Series<Number, Number> series = new Series<>();
                  series.setName("Random Data");
                  Random rng = new Random();
                  for (int i=0; i<10; i++) {
                    final double x = rng.nextDouble();
                    final double y = rng.nextDouble();
                    final Data<Number, Number> datum = new Data<Number, Number>(x, y, String.format("x=%.3f%ny=%.3f",x,y));
                    series.getData().add(datum);
                    
                    // When node is installed on datum, register a Tooltip on it:
                    datum.nodeProperty().addListener(new ChangeListener<Node>() {
                      @Override
                      public void changed(ObservableValue<? extends Node> observable,
                          Node oldValue, Node newValue) {
                        if (newValue != null) {
                          Tooltip.install(newValue, new Tooltip(datum.getExtraValue().toString()));
                          datum.nodeProperty().removeListener(this);
                        }
                      }
                    });
                    
                  }
                  scatterPlot.getData().add(series);
                  root.setCenter(scatterPlot);
                  primaryStage.setScene(new Scene(root, 600, 600));
                  primaryStage.show();
                }
              
              
                public static void main(String[] args) {launch(args);}
              
              }
              • 4. Re: Having tooltip trouble
                894573
                Oh, thank you! That did it. I guess I just don't understand JavaFX too well.