本文介绍了尽管安装了JavaFX PieChart Tooltip但仍未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试在图表中为PieChart的每个切片显示一个工具提示,类似于chart.js:
I tried displaying a tooltip for each "slice" of a PieChart similar to this in chart.js:
我发现基本上试图在相同的框架。它有两个upvotes,同样似乎在其他(接受的)答案判断其他图表类型。但是,在没有显示工具提示的意义上,代码对我来说不起作用。
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.
显示问题的示例代码:
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!
推荐答案
我只是移动了
chart.getData().addAll(data);
以上
data.forEach(d -> {
Tooltip tip = new Tooltip();
tip.setText(d.getPieValue() + "");
Tooltip.install(d.getNode(), tip);
});
完整代码:
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);
}
}
这篇关于尽管安装了JavaFX PieChart Tooltip但仍未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!