OKay so I'm using JavaFX and i wish to display multiple pie charts in one window. 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. 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.
Don't initiate the VBox over and over again in a For-Loop instead do it before the loop and add every 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 = new VBox(pielist);