我想用群组创建StackedBarChart
,但我找不到任何办法。我拥有的是这样的:
public class TestStackedBarChart extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
Group root = new Group();
final CategoryAxis xAxis = new CategoryAxis();
final NumberAxis yAxis = new NumberAxis();
xAxis.setLabel("Month");
xAxis.setCategories(FXCollections.<String> observableArrayList(Arrays.asList(
"January",
"February",
"March")));
yAxis.setLabel("Value");
final StackedBarChart<String,Number> stackedBarChart = new StackedBarChart<>(xAxis,yAxis);
stackedBarChart.setTitle("StackedBarChart");
XYChart.Series<String,Number> series1 = new Series<>();
series1.setName("XYChart.Series 1");
series1.getData().add(new Data<String, Number>("January", Integer.valueOf(100)));
series1.getData().add(new Data<String, Number>("February", Integer.valueOf(200)));
series1.getData().add(new Data<String, Number>("March", Integer.valueOf(50)));
XYChart.Series<String,Number> series2 = new Series<>();
series2.setName("XYChart.Series 2");
series2.getData().add(new Data<String, Number>("January", Integer.valueOf(150)));
series2.getData().add(new Data<String, Number>("February", Integer.valueOf(100)));
series2.getData().add(new Data<String, Number>("March", Integer.valueOf(60)));
stackedBarChart.getData().add(series1);
stackedBarChart.getData().add(series2);
root.getChildren().addAll(stackedBarChart);
primaryStage.setScene(new Scene(root, 500, 400));
primaryStage.show();
}
}
这就是结果:
我想拥有的是这样的:
有没有办法在JavaFx中执行此操作?您也可以建议任何其他Chart-Type,但解决方案应该是JavaFX。
答案 0 :(得分:0)
在尝试堆积条形图一段时间后,我找到了一个可以接受的解决方案。无法对堆叠的条形节点进行分组,但可以按照分组条目并排放置的方式对它们进行排序。结果如下:
以下是我的表现:
public class TestStackedBarChart extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
Group root = new Group();
final CategoryAxis xAxis = new CategoryAxis();
final NumberAxis yAxis = new NumberAxis();
xAxis.setLabel("Month");
xAxis.setCategories(FXCollections.<String> observableArrayList(Arrays.asList(
"January(Plan)",
"January(Actual)",
"February(Plan)",
"February(Actual)",
"March(Plan)",
"March(Actual)")));
yAxis.setLabel("Value");
final StackedBarChart<String,Number> stackedBarChart = new StackedBarChart<>(xAxis,yAxis);
stackedBarChart.setTitle("StackedBarChart");
XYChart.Series<String,Number> series1 = new Series<>();
series1.setName("Category A");
series1.getData().add(new Data<>("January(Plan)", Integer.valueOf(100)));
series1.getData().add(new Data<>("February(Plan)", Integer.valueOf(200)));
series1.getData().add(new Data<>("March(Plan)", Integer.valueOf(50)));
XYChart.Series<String,Number> series2 = new Series<>();
series2.setName("Category B");
series2.getData().add(new Data<>("January(Plan)", Integer.valueOf(200)));
series2.getData().add(new Data<>("February(Plan)", Integer.valueOf(100)));
series2.getData().add(new Data<>("March(Plan)", Integer.valueOf(25)));
XYChart.Series<String,Number> series3 = new Series<>();
series3.setName("Actual Value");
series3.getData().add(new Data<String, Number>("January(Actual)", Integer.valueOf(250)));
series3.getData().add(new Data<String, Number>("February(Actual)", Integer.valueOf(275)));
series3.getData().add(new Data<String, Number>("March(Actual)", Integer.valueOf(60)));
stackedBarChart.getData().add(series1);
stackedBarChart.getData().add(series2);
stackedBarChart.getData().add(series3);
root.getChildren().addAll(stackedBarChart);
primaryStage.setScene(new Scene(root, 500, 400));
primaryStage.show();
}
}