📅  最后修改于: 2020-10-14 07:23:39             🧑  作者: Mango
在堆叠的条形图中,不同组的值以堆叠形式显示在矩形条上。这主要用于根据条上绘制的长度比较不同组的值。
下图比较了不同州的男女人口。在x轴上绘制状态,而在y轴上绘制种群。
在JavaFX中,类javafx.scene.chart.StackedBarChart表示堆积的条形图。我们需要实例化此类以生成StackedBarChart节点。
该类仅包含一个属性,下表及其setter方法对此属性进行了描述。
Property | Description | Setter Method |
---|---|---|
categoryGap | It is a double type property. It represents the gap between the categories of the bar. | setCategoryGap(double value) |
该类中有三个构造函数。
在下面的示例中,我们显示了一年中每个月两种编程语言的流行程度之间的比较。 x轴是类别轴,而y轴是数字轴。在X轴上,我们绘制了一年中的月份,而在Y轴上,我们绘制了读者数量。
package application;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.StackedBarChart;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;
public class StackedBarChartExample extends Application{
@Override
public void start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
//Configuring xaxis and yaxis
CategoryAxis xaxis = new CategoryAxis();
NumberAxis yaxis = new NumberAxis(1000,300000,1000);
xaxis.setLabel("Months");
yaxis.setLabel("Number of users");
//Configuring StackedBarChart
StackedBarChart sb = new StackedBarChart(xaxis,yaxis);
sb.setTitle("Popularity of Programming languages");
//Configuring series for java
XYChart.Series java = new XYChart.Series<>();
java.setName("java");
java.getData().add(new XYChart.Data<>("Jan",10000));
java.getData().add(new XYChart.Data<>("Jan",130000));
java.getData().add(new XYChart.Data<>("Feb",50000));
java.getData().add(new XYChart.Data<>("Mar",60300));
java.getData().add(new XYChart.Data<>("Apr",105600));
java.getData().add(new XYChart.Data<>("May",50600));
java.getData().add(new XYChart.Data<>("Jun",103000));
java.getData().add(new XYChart.Data<>("Jul",104500));
java.getData().add(new XYChart.Data<>("Aug",203000));
java.getData().add(new XYChart.Data<>("Sep",103400));
java.getData().add(new XYChart.Data<>("Oct",105600));
java.getData().add(new XYChart.Data<>("Nov",102400));
java.getData().add(new XYChart.Data<>("Dec",200000));
//Adding series java to the stackedbarchart
sb.getData().add(java);
//Configuring series python
XYChart.Series python = new XYChart.Series<>();
python.setName("python");
python.getData().add(new XYChart.Data<>("Jan",50000));
python.getData().add(new XYChart.Data<>("Jan",14300));
python.getData().add(new XYChart.Data<>("Feb",50400));
python.getData().add(new XYChart.Data<>("Mar",100500));
python.getData().add(new XYChart.Data<>("Apr",104000));
python.getData().add(new XYChart.Data<>("May",134000));
python.getData().add(new XYChart.Data<>("Jun",60000));
python.getData().add(new XYChart.Data<>("Jul",78000));
python.getData().add(new XYChart.Data<>("Aug",89000));
python.getData().add(new XYChart.Data<>("Sep",150000));
python.getData().add(new XYChart.Data<>("Oct",120000));
python.getData().add(new XYChart.Data<>("Nov",109450));
python.getData().add(new XYChart.Data<>("Dec",50450));
//adding python series to the stackedbarchart
sb.getData().add(python);
//Configuring group and Scene
Group root = new Group();
root.getChildren().add(sb);
Scene scene = new Scene(root,600,400);
primaryStage.setScene(scene);
primaryStage.setTitle("StackedBarChart Example");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}