本文介绍了在PieChart上显示工具提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在PieChart上移动鼠标指针时显示工具提示。我测试了这段代码:

  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);
返回;
}
}
}

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);
}

我添加了甚至Platform.runLater(()来测试这是一个错误JavaFX 8u40但是当我将鼠标移到PieChart上时仍然没有ToolTip。这段代码在JavaFX Task中执行,所以可能存在一个已知问题。你有什么想法吗?

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


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?

解决方案

Try to install tooltip in every node:

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

这篇关于在PieChart上显示工具提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 14:53