[英]How to display multiple pie charts in JavaFX?
好的,所以我正在使用 JavaFX,我希望在一个窗口中显示多个饼图。 每个饼图代表大学学位的一个模块,将有 2 个切片,即在该模块中高于平均水平的学生数量和低于平均水平的学生数量。 我使用了两个哈希图,一个存储模块名称和表现良好的学生人数,另一个存储表现不佳的学生。
我的代码目前为一个模块显示一个馅饼。 我很接近但是我如何让所有的馅饼都显示出来?
我的代码:
public class PieChartSample extends Application {
static Map<String, Integer> studsabove = new HashMap<String, Integer>();
static Map<String, Integer> studsbelow = new HashMap<String, Integer>();
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("My First JavaFX App");
ArrayList<PieChart> pielist = new ArrayList<PieChart>();
for(Entry<String, Integer> mod: studsabove.entrySet()){
for(Entry<String, Integer> mod2: studsbelow.entrySet()){
PieChart pieChart = new PieChart();
PieChart.Data above = new PieChart.Data(mod.getKey(), mod.getValue());
PieChart.Data below = new PieChart.Data(mod2.getKey(), mod2.getValue());
pieChart.getData().add(above);
pieChart.getData().add(below);
pielist.add(pieChart);
}
}
for (PieChart pie: pielist){
VBox vbox = new VBox(pie);
}
//TODO: figure this shit out
Scene scene = new Scene(vbox, 400, 200);
primaryStage.setScene(scene);
primaryStage.setHeight(300);
primaryStage.setWidth(1200);
primaryStage.show();
}
public static void main(String[] args) {
PieChartSample.studsabove.put("CE201", 23);
PieChartSample.studsbelow.put("CE201", 67);
PieChartSample.studsabove.put("CE222", 20);
PieChartSample.studsbelow.put("CE222", 80);
PieChartSample.studsabove.put("CE233", 6);
PieChartSample.studsbelow.put("CE233", 94);
PieChartSample.studsabove.put("CE244", 56);
PieChartSample.studsbelow.put("CE244", 44);
Application.launch(args);
}
for (PieChart pie: pielist){
VBox vbox = new VBox(pie);
}
您正在为馅饼列表中的每个 PieChart 创建一个新的 VBox,其中仅包含您正在迭代的单个 PieChart。
不要在 For-Loop 中一遍又一遍地启动 VBox,而是在循环之前执行并添加每个 PieChart。 试试这个:
VBox vbox = new VBox();
for (PieChart pie: pielist){
vbox.getChildren().add(pie);
}
我不完全确定,但也可以使用一组孩子来启动 VBox,因此您也可以尝试以下操作:
VBox vbox = new VBox(pielist);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.