1

I tried displaying a tooltip for each "slice" of a PieChart similar to this in chart.js:

Tooltip example

I found this answer basically trying to achieve the same in the same framework. It has two upvotes and the same seemed to work on other chart types judging by other (accepted) answers. However the code would not really work for me in the sense that there was no tooltip showing up.

Example code showing the problem:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.PieChart;
import javafx.scene.control.Button;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;


public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {
        BorderPane pane = new BorderPane();

        PieChart chart = new PieChart();

        Button randomValues = new Button();
        randomValues.setOnAction(ae -> {
            chart.getData().clear();
            List<PieChart.Data> data = randomValues().stream().map(i -> new PieChart.Data("Test" + i % 10, i)).collect(Collectors.toList());
            data.forEach(d -> {
                Tooltip tip = new Tooltip();
                tip.setText(d.getPieValue() + "");
                Tooltip.install(d.getNode(), tip);
            });
            chart.getData().addAll(data);
        });

        pane.setCenter(chart);
        pane.setBottom(randomValues);
        primaryStage.setScene(new Scene(pane));
        primaryStage.show();
    }

    private List<Integer> randomValues() {
        return new Random().ints(5).boxed().collect(Collectors.toList());
    }

    public static void main(String[] args) {
        launch(args);
    }
}

I expected a popup similar to the picture but there is literally nothing showing up. Any help is much appreciated!

geisterfurz007
  • 5,292
  • 5
  • 33
  • 54
  • https://stackoverflow.com/questions/25892695/tooltip-on-line-chart-showing-date – SedJ601 Jan 19 '18 at 17:28
  • @Sedrick Thanks for the link. That links to the same question that is linked under the question I mentioned in my question and uses the same thing (Tooltip#install) which seems like it has no effect. – geisterfurz007 Jan 19 '18 at 17:30

1 Answers1

3

I simply moved

chart.getData().addAll(data);

above

data.forEach(d -> {
    Tooltip tip = new Tooltip();
    tip.setText(d.getPieValue() + "");
    Tooltip.install(d.getNode(), tip);
});

Complete code:

import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.PieChart;
import javafx.scene.control.Button;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class Main extends Application
{

    @Override
    public void start(Stage primaryStage)
    {
        BorderPane pane = new BorderPane();

        PieChart chart = new PieChart();

        Button randomValues = new Button();
        randomValues.setOnAction(ae -> {
            chart.getData().clear();

            List<PieChart.Data> data = randomValues().stream().map(i -> new PieChart.Data("Test" + i % 10, i)).collect(Collectors.toList());
            chart.getData().addAll(data);
            data.forEach(d -> {
                Tooltip tip = new Tooltip();
                tip.setText(d.getPieValue() + "");
                Tooltip.install(d.getNode(), tip);
            });

        });

        pane.setCenter(chart);
        pane.setBottom(randomValues);
        primaryStage.setScene(new Scene(pane));
        primaryStage.show();
    }

    private List<Integer> randomValues()
    {
        return new Random().ints(5).boxed().collect(Collectors.toList());
    }

    public static void main(String[] args)
    {
        launch(args);
    }
}
SedJ601
  • 12,173
  • 3
  • 41
  • 59
  • 1
    That was surprisingly easy! So the node has to be in the scenegraph before this works? Thanks a bunch! – geisterfurz007 Jan 19 '18 at 17:39
  • 1
    @geisterfurz007 The node isn't created until the data are added to the chart. (I'm surprised your original code didn't throw null pointer exceptions.) – James_D Jan 19 '18 at 18:21
  • And I just checked the implementation of it and if the node is null then the method will just return ^^ – geisterfurz007 Jan 20 '18 at 04:55