简体   繁体   English

如何在 JavaFX 中显示多个饼图?

[英]How to display multiple pie charts in JavaFX?

OKay so I'm using JavaFX and i wish to display multiple pie charts in one window.好的,所以我正在使用 JavaFX,我希望在一个窗口中显示多个饼图。 Each pie will represent one module of a degree at university and there will be 2 slices, the amount of students that got above their average in that module and the amount who got below.每个饼图代表大学学位的一个模块,将有 2 个切片,即在该模块中高于平均水平的学生数量和低于平均水平的学生数量。 I am using two hashmaps, one to store the module names and number of students who performed well and the other for those that didn't.我使用了两个哈希图,一个存储模块名称和表现良好的学生人数,另一个存储表现不佳的学生。

My code currently displays one pie for one module.我的代码目前为一个模块显示一个馅饼。 I'm close but how do I get all the pies to show?我很接近但是我如何让所有的馅饼都显示出来?

My Code:我的代码:

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

There you are creating for every PieChart in your pielist a new VBox just containing the single PieChart you're iterating over.您正在为馅饼列表中的每个 PieChart 创建一个新的 VBox,其中仅包含您正在迭代的单个 PieChart。

Don't initiate the VBox over and over again in a For-Loop instead do it before the loop and add every PieChart.不要在 For-Loop 中一遍又一遍地启动 VBox,而是在循环之前执行并添加每个 PieChart。 Try this instead:试试这个:

VBox vbox = new VBox();
for (PieChart pie: pielist){
    vbox.getChildren().add(pie);
}

I'm not totally sure but it might also be possible to initiate a VBox with a Collection of children so you can also try this:我不完全确定,但也可以使用一组孩子来启动 VBox,因此您也可以尝试以下操作:

VBox vbox = new VBox(pielist);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM