Javafx 简明教程

JavaFX - Pie Chart

饼图是以不同颜色的圆形切片表示值的图表。这些切片都有标签,每个切片对应的值都显示在图表中。

A pie-chart is a representation of values as slices of a circle with different colors. These slices are labeled and the values corresponding to each slice is represented in the chart.

以下是显示各个公司某个时间点移动销量的饼图。

Following is a Pie Chart depicting the mobile sales of various companies at an instance.

mobilesales pie chart

Pie Chart in JavaFX

在 JavaFX 中,饼图由名为 PieChart 的类表示。这个类属于 javafx.scene.chart 包。

In JavaFX, a pie chart is represented by a class named PieChart. This class belongs to the package javafx.scene.chart.

通过实例化此类,可以在 JavaFX 中创建一个 PieChart 节点。

By instantiating this class, you can create a PieChart node in JavaFX.

此类有 5 个属性,如下所示:

This class has 5 properties which are as follows −

  1. clockwise − This is a Boolean Operator; on setting this operator true, the data slices in the pie charts will be arranged clockwise starting from the start angle of the pie chart.

  2. data − This represents an ObservableList object, which holds the data of the pie chart.

  3. labelLineLength − An integer operator representing the length of the lines connecting the labels and the slices of the pie chart.

  4. labelsVisible − This is a Boolean Operator; on setting this operator true, the labels for the pie charts will be drawn. By default, this operator is set to be true.

  5. startAngle − This is a double type operator, which represents the angle to start the first pie slice at.

要生成饼图,请准备一个 ObservableList 对象。在准备 ObservableList 对象后,将其作为参数传递给类 PieChart 的构造函数;或者,使用名为 setData() 的方法。

To generate a pie chart, Prepare an ObservableList object. After preparing the ObservableList object, pass it as an argument to the constructor of the class PieChart; Or, by using the method named setData().

Steps to Generate Pie Chart

要在 JavaFX 中生成 PieChart ,请按照以下步骤操作。

To generate a PieChart in JavaFX, follow the steps given below.

Step 1: Creating a Class

创建一个 Java 类并继承包 javafx.applicationApplication 类,并按如下方式实现此类的 start() 方法。

Create a Java class and inherit the Application class of the package javafx.application and implement the start() method of this class as follows.

public class ClassName extends Application {
   @Override
   public void start(Stage primaryStage) throws Exception {
   }
}

Step 2: Preparing the ObservableList Object

通过传递饼图数据准备接口 ObservableList 对象,如下所示:

Prepare an object of the interface ObservableList object by passing the data of the pie chart as shown below −

ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList(
   new PieChart.Data("Iphone 5S", 13),
   new PieChart.Data("Samsung Grand", 25),
   new PieChart.Data("MOTO G", 10),
   new PieChart.Data("Nokia Lumia", 22));

Step 3: Creating a PieChart Object

创建一个 PieChart ,方法是传递 ObservableList 对象,如下所示。

Create a PieChart by passing the ObservableList object as shown below.

//Creating a Pie chart
PieChart pieChart = new PieChart(pieChartData);

Step 4: Setting the Title of the Pie Chart

使用类 PieChartsetTitle() 方法设置饼图的标题。它属于包 javafx.scene.chart

Set the title of the Pie Chart using the setTitle() method of the class PieChart. This belongs to the package javafx.scene.chart

//Setting the title of the Pie chart
pieChart.setTitle("Mobile Sales");

Step 5: Setting the Slices Clockwise

顺时针设置饼图的切片。这是通过向类 PieChartsetClockwise() 方法传递布尔值 true 来完成的。它属于包 javafx.scene.chart

Set the slices of the Pie Charts clockwise. This is done by passing Boolean value true to the setClockwise() method of the class PieChart. This belongs to the package javafx.scene.chart

//setting the direction to arrange the data
pieChart.setClockwise(true);

Step 6: Set the Length of the Label Line

使用类 PieChartsetLabelLineLength() 方法设置标签线的长度,该类属于包 javafx.scene.chart ,如下所示:

Set the length of the label line using the setLabelLineLength() method of the class PieChart which belongs to the package javafx.scene.chart, as follows −

//Setting the length of the label line
pieChart.setLabelLineLength(50);

Step 7: Set the Labels Visible

通过将布尔值 true 传递给类 PieChart 的方法 setLabelsVisible() 来设置饼形图的标签为可见。它属于包 javafx.scene.chart

Set the labels of the pie chart to visible by passing the Boolean value true to the method setLabelsVisible() of the class PieChart. This belongs to the package javafx.scene.chart

//Setting the labels of the pie chart visible
pieChart.setLabelsVisible(true);

Step 8: Set the Start Angle of the Pie Chart

使用类 PieChartsetStartAngle() 方法设置饼形图的起始角度。它属于包 javafx.scene.chart

Set the Start angle of the pie chart using the setStartAngle() method of the class PieChart. This belongs to the package javafx.scene.chart

//Setting the start angle of the pie chart
pieChart.setStartAngle(180);

Step 9: Creating a Group Object

start() 方法中,通过实例化名为 Group 的类创建一个组对象。它属于包 javafx.scene 。将在上一步创建的 PieChart(节点)对象作为参数传递给 Group 类的构造函数。应按照以下方式进行此操作,以便将其添加到组中 −

In the start() method, create a group object by instantiating the class named Group. This belongs to the package javafx.scene. Pass the PieChart (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(piechart);

Step 10: Launching Application

最后,按照下面给出的步骤正确启动应用程序 −

Lastly, follow the given steps below to launch the application properly −

  1. 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.

  2. Then, set the title to the stage using the setTitle() method of the Stage class.

  3. Now, a Scene object is added to the stage using the setScene() method of the class named Stage.

  4. Display the contents of the scene using the method named show().

  5. Lastly, the application is launched with the help of the launch() method.

Example

下表描述了使用饼形图的移动式销售情况。下表列出了不同的移动品牌及其销售情况(每天的单位)。

The table given below depicts mobile sale with the help of a pie chart. The following table has a list of different mobile brands and their sale (units per day).

S.No

Mobile Brands

Sales (Units per day)

1

Iphone 5S

20

2

Samsung Grand

20

3

MOTO G

40

4

Nokia Lumia

10

以下是 Java 程序,它使用 JavaFX 生成了一个饼形图,描述了上述数据。将此代码保存在名为 PieChartExample.java 的文件中。

Following is a Java program which generates a pie chart, depicting the above data using JavaFX. Save this code in a file with the name PieChartExample.java.

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.chart.PieChart;

public class PieChartExample extends Application {
   @Override
   public void start(Stage stage) {
      //Preparing ObservbleList object
      ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList(
         new PieChart.Data("Iphone 5S", 13),
         new PieChart.Data("Samsung Grand", 25),
         new PieChart.Data("MOTO G", 10),
         new PieChart.Data("Nokia Lumia", 22));

      //Creating a Pie chart
      PieChart pieChart = new PieChart(pieChartData);

      //Setting the title of the Pie chart
      pieChart.setTitle("Mobile Sales");

      //setting the direction to arrange the data
      pieChart.setClockwise(true);

      //Setting the length of the label line
      pieChart.setLabelLineLength(50);

      //Setting the labels of the pie chart visible
      pieChart.setLabelsVisible(true);

      //Setting the start angle of the pie chart
      pieChart.setStartAngle(180);

      //Creating a Group object
      Group root = new Group(pieChart);

      //Creating a scene object
      Scene scene = new Scene(root, 600, 400);

      //Setting title to the Stage
      stage.setTitle("Pie 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 PieChartExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls PieChartExample

在执行时,上述程序会生成一个 JavaFX 窗口,显示一个饼形图,如下所示。

On executing, the above program generates a JavaFX window displaying a pie chart as shown below.

pie chart

Example

我们来看另一个示例,绘制一个 JavaFX 饼形图,说明月薪为 25,000 印度卢比的私有员工的每月支出。将文件保存在名为 PieChartEmployee.java 的文件下。

Let us see another example to draw a JavaFX pie chart illustrating monthly expenses of a private employee whose salary is 25,000 INR per month. Save the file under the name PieChartEmployee.java.

S.No

Necessities

Expenses

1

Rent

7500

2

Groceries

1000

3

Transport

1500

4

Savings

10000

5

Miscellaneous

5000

 import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.chart.PieChart;

public class PieChartEmployee extends Application {
   @Override
   public void start(Stage stage) {
      //Preparing ObservbleList object
      ObservableList pieChartData = FXCollections.observableArrayList(
         new PieChart.Data("Rent", 7500),
         new PieChart.Data("Groceries", 1000),
         new PieChart.Data("Transport", 1500),
         new PieChart.Data("Savings", 10000),
		 new PieChart.Data("Miscellaneous", 5000));

      //Creating a Pie chart
      PieChart pieChart = new PieChart(pieChartData);

      //Setting the title of the Pie chart
      pieChart.setTitle("Monthly Expenses");

      //setting the direction to arrange the data
      pieChart.setClockwise(true);

      //Setting the length of the label line
      pieChart.setLabelLineLength(50);

      //Setting the labels of the pie chart visible
      pieChart.setLabelsVisible(true);

      //Setting the start angle of the pie chart
      pieChart.setStartAngle(180);

      //Creating a Group object
      Group root = new Group(pieChart);

      //Creating a scene object
      Scene scene = new Scene(root, 600, 400);

      //Setting title to the Stage
      stage.setTitle("Pie 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 PieChartEmployee.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls PieChartEmployee

在执行时,上述程序会生成一个 JavaFX 窗口,显示一个饼形图,如下所示。

On executing, the above program generates a JavaFX window displaying a pie chart as shown below.

piechart expenses