Javafx 在图表上显示工具提示
我想在图表上移动鼠标指针时显示工具提示。我测试了这段代码:Javafx 在图表上显示工具提示,javafx,javafx-8,Javafx,Javafx 8,我想在图表上移动鼠标指针时显示工具提示。我测试了这段代码: private ObservableList<Data> pieChartdData = FXCollections.observableArrayList(); private PieChart chart = new PieChart(pieChartdData); public void pieChart(List<FSPartitions> obj) { for
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);
}
private observeList pieChartdData=FXCollections.observearraylist();
专用PieChart图表=新PieChart(PieChart数据);
公共表格(列表obj)
{
用于(FSJ1:obj)
{
字符串fsName=obj1.getFSName();
double-usedSize=obj1.getUsedSize();
用于(数据d:pieChartdData)
{
if(d.getName().equals(fsName))
{
d、 setPieValue(usedSize);
回来
}
}
}
用于(FSJ1:obj)
{
字符串fsName=obj1.getFSName();
double-usedSize=obj1.getUsedSize();
添加(新数据(fsName,usedSize));
}
Platform.runLater(()->
{
最终标签captioln=新标签(“”);
captilon.setTextFill(Color.DARKORANGE);
captioln.setStyle(“-fx字体:24 arial;”);
chart.getData().stream().forEach((数据)->
{
data.getNode().addEventHandler(MouseEvent.MOUSE_输入,
新的EventHandler()
{
@凌驾
公共无效句柄(MouseEvent e)
{
captioln.setTranslateX(e.getSceneX());
captioln.setTranslateY(e.getSceneY());
setText(String.valueOf(data.getPieValue())+“%”;
}
});
});
});
图表.setData(pieChartdData);
}
我添加了偶数Platform.runLater(())来测试这是否是JavaFX 8u40中的一个错误,但当我将鼠标移到PieChart上时,仍然没有工具提示。此代码在JavaFX任务中执行,因此可能存在已知问题。你有什么想法吗?尝试在每个节点中安装工具提示:
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 + "%"));
});
尝试在每个节点中安装工具提示:
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 + "%"));
});
您还可以尝试以下解决方案:
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 + "%");
});
}
您还可以尝试以下解决方案:
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 + "%");
});
}
非常相似的问题和公认的答案:非常相似的问题和公认的答案:这与其他答案有什么不同(除了稍微不同的文本)?这与其他答案有什么不同(除了稍微不同的文本)?