Javafx 简明教程
JavaFX - Bar Chart
条形图用于使用矩形条来表示分组数据。这些条的长度描绘了这些值。条形图中的条可以垂直或水平绘制。
A bar chart is used to represent grouped data using rectangular bars. The length of these bars depicts the values. The bars in the bar chart can be plotted vertically or horizontally.
下面是一个条形图,比较了各种汽车品牌。
Following is a bar chart, comparing various car brands.
Bar Chart in JavaFX
在 JavaFX 中,条形图由名为 BarChart 的类表示。此类属于 javafx.scene.chart 包。通过实例化此类,您可以在 JavaFX 中创建一个 BarChart 节点。
In JavaFX, a Bar chart is represented by a class named BarChart. This class belongs to the package javafx.scene.chart. By instantiating this class, you can create an BarChart node in JavaFX.
要在 JavaFX 中生成条形图,请遵循以下步骤。
To generate a bar chart in JavaFX, follow the steps given below.
Step 1: Defining the Axis
在 Application 类的 start() 方法中,定义条形图的 X 和 Y 轴,并为它们设置标签。在我们的示例中,X 轴表示比较的类别,y 轴表示分数。
In the start() method of Application class, define the X and Y axis of the bar chart and set labels to them. In our example, X axis represents the category of comparison and the y axis represents the score.
public class ClassName extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
//Defining the x axis
CategoryAxis xAxis = new CategoryAxis();
xAxis.setCategories(FXCollections.<String>observableArrayList(Arrays.asList(
"Speed", "User rating", "Milage", "Safety")));
xAxis.setLabel("category");
//Defining the y axis
NumberAxis yAxis = new NumberAxis();
yAxis.setLabel("score");
}
}
Step 2: Creating the Bar Chart
通过实例化 javafx.scene.chart 包中名为 BarChart 的类,创建一个折线图。在此类的构造函数中,传递表示在前一步中创建的 X 和 Y 轴的对象。
Create a line chart by instantiating the class named BarChart of the package javafx.scene.chart. To the constructor of this class, pass the objects representing the X and Y axis created in the previous step.
//Creating the Bar chart
BarChart<String, Number> barChart = new BarChart<>(xAxis, yAxis);
barChart.setTitle("Comparison between various cars");
Step 3: Preparing the Data
实例化 XYChart.Series 类并将数据(系列、x 和 y 坐标)添加到此类的可观察列表中,如下所示 −
Instantiate the XYChart.Series class and add the data (a series of, x and y coordinates) to the Observable list of this class as follows −
//Prepare XYChart.Series objects by setting data
XYChart.Series<String, Number> series1 = new XYChart.Series<>();
series1.setName("Fiat");
series1.getData().add(new XYChart.Data<>("Speed", 1.0));
series1.getData().add(new XYChart.Data<>("User rating", 3.0));
series1.getData().add(new XYChart.Data<>("Milage", 5.0));
series1.getData().add(new XYChart.Data<>("Safety", 5.0));
XYChart.Series<String, Number> series2 = new XYChart.Series<>();
series2.setName("Audi");
series2.getData().add(new XYChart.Data<>("Speed", 5.0));
series2.getData().add(new XYChart.Data<>("User rating", 6.0));
series2.getData().add(new XYChart.Data<>("Milage", 10.0));
series2.getData().add(new XYChart.Data<>("Safety", 4.0));
XYChart.Series<String, Number> series3 = new XYChart.Series<>();
series3.setName("Ford");
series3.getData().add(new XYChart.Data<>("Speed", 4.0));
series3.getData().add(new XYChart.Data<>("User rating", 2.0));
series3.getData().add(new XYChart.Data<>("Milage", 3.0));
series3.getData().add(new XYChart.Data<>("Safety", 6.0));
Step 4: Add Data to the Bar Chart
将上一步中准备的数据系列添加到条形图,如下所示 −
Add the data series prepared in the previous step to the bar chart as follows −
//Setting the data to bar chart
barChart.getData().addAll(series1, series2, series3);
Step 5: Creating a Group Object
在 start() 方法中,创建一个群组对象,通过实例化名为 Group 的类实现。这属于包 javafx.scene 。
In the start() method, create a group object by instantiating the class named Group. This belongs to the package javafx.scene.
将前一步创建的 BarChart(节点)对象作为参数传递给 Group 类的构造函数。应该执行此操作以将其添加到组,如下所示:
Pass the BarChart (node) object, created in the previous step as a parameter to the constructor of the Group class. This should be done in order to add it to the group as follows −
Group root = new Group(barChart);
Step 6: Launching Application
最后,按照下面给出的步骤正确启动应用程序 −
Lastly, follow the given steps below to launch the application properly −
-
Firstly, instantiate the class named Scene by passing the Group object as a parameter value to its constructor. To this constructor, you can also pass dimensions of the application screen as optional parameters.
-
Then, set the title to the stage using the setTitle() method of the Stage class.
-
Now, a Scene object is added to the stage using the setScene() method of the class named Stage.
-
Display the contents of the scene using the method named show().
-
Lastly, the application is launched with the help of the launch() method.
Example
以下示例借助条形图描述了各种汽车统计数据。以下是一些汽车品牌及其不同的特征列表,我们将使用条形图显示这些列表:
The following example depicts various car statistics with the help of a bar chart. Following is a list of car brands along with their different characteristics, which we will show using a bar chart −
Car |
Speed |
User Rating |
Millage |
Safety |
Fiat |
1.0 |
3.0 |
5.0 |
5.0 |
Audi |
5.0 |
6.0 |
10.0 |
4.0 |
Ford |
4.0 |
2.0 |
3.0 |
6.0 |
以下是使用 JavaFX 生成条形图的 Java 程序,该程序描述了上述数据。
Following is a Java program which generates a bar chart, depicting the above data using JavaFX.
将此代码保存在名为 BarChartExample.java 的文件中。
Save this code in a file with the name BarChartExample.java.
import java.util.Arrays;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.stage.Stage;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
public class BarChartExample extends Application {
@Override
public void start(Stage stage) {
//Defining the axes
CategoryAxis xAxis = new CategoryAxis();
xAxis.setCategories(FXCollections.<String>
observableArrayList(Arrays.asList("Speed", "User rating", "Milage", "Safety")));
xAxis.setLabel("category");
NumberAxis yAxis = new NumberAxis();
yAxis.setLabel("score");
//Creating the Bar chart
BarChart<String, Number> barChart = new BarChart<>(xAxis, yAxis);
barChart.setTitle("Comparison between various cars");
//Prepare XYChart.Series objects by setting data
XYChart.Series<String, Number> series1 = new XYChart.Series<>();
series1.setName("Fiat");
series1.getData().add(new XYChart.Data<>("Speed", 1.0));
series1.getData().add(new XYChart.Data<>("User rating", 3.0));
series1.getData().add(new XYChart.Data<>("Milage", 5.0));
series1.getData().add(new XYChart.Data<>("Safety", 5.0));
XYChart.Series<String, Number> series2 = new XYChart.Series<>();
series2.setName("Audi");
series2.getData().add(new XYChart.Data<>("Speed", 5.0));
series2.getData().add(new XYChart.Data<>("User rating", 6.0));
series2.getData().add(new XYChart.Data<>("Milage", 10.0));
series2.getData().add(new XYChart.Data<>("Safety", 4.0));
XYChart.Series<String, Number> series3 = new XYChart.Series<>();
series3.setName("Ford");
series3.getData().add(new XYChart.Data<>("Speed", 4.0));
series3.getData().add(new XYChart.Data<>("User rating", 2.0));
series3.getData().add(new XYChart.Data<>("Milage", 3.0));
series3.getData().add(new XYChart.Data<>("Safety", 6.0));
//Setting the data to bar chart
barChart.getData().addAll(series1, series2, series3);
//Creating a Group object
Group root = new Group(barChart);
//Creating a scene object
Scene scene = new Scene(root, 600, 400);
//Setting title to the Stage
stage.setTitle("Bar Chart");
//Adding scene to the stage
stage.setScene(scene);
//Displaying the contents of the stage
stage.show();
}
public static void main(String args[]){
launch(args);
}
}
使用以下命令,从命令提示符编译并执行已保存的 java 文件。
Compile and execute the saved java file from the command prompt using the following commands.
javac --module-path %PATH_TO_FX% --add-modules javafx.controls BarChartExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls BarChartExample
执行后,上述程序会生成一个 JavaFX 窗口,显示如下所示的区域图。
On executing, the above program generates a JavaFX window displaying an area chart as shown below.
Example
下表描绘了 John 和 Jane 在一周内食用的水果数量。
The following table depicts the number of fruits consumed by John and Jane in a week.
Day of the Week |
Fruits consumed by John |
Fruits consumed by Jane |
Monday |
3 |
1 |
Tuesday |
4 |
3 |
Wednesday |
3 |
4 |
Thursday |
5 |
3 |
Friday |
4 |
3 |
Saturday |
10 |
5 |
Sunday |
12 |
4 |
以下是使用 JavaFX 生成条形图的 Java 程序,该程序描述了上述数据。
Following is a Java program which generates an bar chart, depicting the above data using JavaFX.
将此代码保存在名为 BarChartFruits.java 的文件中。
Save this code in a file with the name BarChartFruits.java.
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.stage.Stage;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
public class BarChartFruits extends Application {
@Override
public void start(Stage stage) {
//Defining the X axis
CategoryAxis xAxis = new CategoryAxis();
//defining the y Axis
NumberAxis yAxis = new NumberAxis(0, 15, 2.5);
yAxis.setLabel("Fruit units");
//Creating the Area chart
BarChart<String, Number> barChart = new BarChart(xAxis, yAxis);
barChart.setTitle("Average fruit consumption during one week");
//Prepare XYChart.Series objects by setting data
XYChart.Series series1 = new XYChart.Series();
series1.setName("John");
series1.getData().add(new XYChart.Data("Monday", 3));
series1.getData().add(new XYChart.Data("Tuesday", 4));
series1.getData().add(new XYChart.Data("Wednesday", 3));
series1.getData().add(new XYChart.Data("Thursday", 5));
series1.getData().add(new XYChart.Data("Friday", 4));
series1.getData().add(new XYChart.Data("Saturday", 10));
series1.getData().add(new XYChart.Data("Sunday", 12));
XYChart.Series series2 = new XYChart.Series();
series2.setName("Jane");
series2.getData().add(new XYChart.Data("Monday", 1));
series2.getData().add(new XYChart.Data("Tuesday", 3));
series2.getData().add(new XYChart.Data("Wednesday", 4));
series2.getData().add(new XYChart.Data("Thursday", 3));
series2.getData().add(new XYChart.Data("Friday", 3));
series2.getData().add(new XYChart.Data("Saturday", 5));
series2.getData().add(new XYChart.Data("Sunday", 4));
//Setting the XYChart.Series objects to area chart
barChart.getData().addAll(series1,series2);
//Creating a Group object
Group root = new Group(barChart);
//Creating a scene object
Scene scene = new Scene(root, 600, 400);
//Setting title to the Stage
stage.setTitle("Bar Chart");
//Adding scene to the stage
stage.setScene(scene);
//Displaying the contents of the stage
stage.show();
}
public static void main(String args[]){
launch(args);
}
}
使用以下命令,从命令提示符编译并执行已保存的 java 文件。
Compile and execute the saved java file from the command prompt using the following commands.
javac --module-path %PATH_TO_FX% --add-modules javafx.controls BarChartFruits.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls BarChartFruits
执行后,上述程序会生成一个 JavaFX 窗口,显示如下所示的区域图。
On executing, the above program generates a JavaFX window displaying an area chart as shown below.