Peter Penzov
Peter Penzov

Reputation: 1754

Display ToolTip on PieChart

I want to display Tooltip when I move mouse pointer on PieChart. I tested this code:

private ObservableList<Data> pieChartdData = FXCollections.observableArrayList();
    private PieChart chart = new PieChart(pieChartdData);

    public void pieChart(List<FSPartitions> obj)
    {
        for (FSPartitions obj1 : obj)
        {
            String fsName = obj1.getFSName();
            double usedSize = obj1.getUsedSize();

            for (Data d : pieChartdData)
            {
                if (d.getName().equals(fsName))
                {
                    d.setPieValue(usedSize);
                    return;
                }
            }
        }

        for (FSPartitions obj1 : obj)
        {
            String fsName = obj1.getFSName();
            double usedSize = obj1.getUsedSize();

            pieChartdData.add(new Data(fsName, usedSize));
        }

        Platform.runLater(() ->
        {
            final Label captioln = new Label("");
            captioln.setTextFill(Color.DARKORANGE);
            captioln.setStyle("-fx-font: 24 arial;");

            chart.getData().stream().forEach((data) ->
            {
                data.getNode().addEventHandler(MouseEvent.MOUSE_ENTERED,
                    new EventHandler<MouseEvent>()
                    {
                        @Override
                        public void handle(MouseEvent e)
                        {
                            captioln.setTranslateX(e.getSceneX());
                            captioln.setTranslateY(e.getSceneY());
                            captioln.setText(String.valueOf(data.getPieValue()) + "%");
                        }
                    });
            });
        });

        chart.setData(pieChartdData);
    }

I added even Platform.runLater(() to test is this a bug in JavaFX 8u40 but still there is no ToolTip when I move the mouse over the PieChart. This code is executed in JavaFX Task so maybe there is a known issue. Do you have some idea?

Upvotes: 2

Views: 2180

Answers (2)

Warren Nocos
Warren Nocos

Reputation: 1292

Try to install the Tooltip in every node:

chart.getData().forEach(data -> {
    Tooltip tooltip = new Tooltip();
    tooltip.setText(data.getPieValue() + "%");
    Tooltip.install(data.getNode(), tooltip);
    data.pieValueProperty()
        .addListener((observable, oldValue, newValue) -> 
             tooltip.setText(newValue + "%"));
});

Upvotes: 4

Kingstone Job
Kingstone Job

Reputation: 9

You can also try the following solution:

for(PieChart.Data data: pieChart.getData()){
        Tooltip tooltip = new Tooltip(data.getName() + ": " + data.getPieValue() +"%");
        Tooltip.install(data.getNode(), tooltip);
        
        //respond to change in value
        data.pieValueProperty().addListener((observable, oldPieValue, newPieValue)->{
            tooltip.setText(data.getName() + ": " + newPieValue + "%");
        });
    }

Upvotes: 0

Related Questions