多读书多实践,勤思考善领悟

JavaFX Pie Chart(饼图)

本文于1662天之前发表,文中内容可能已经过时。

饼图是将值表示为具有不同颜色的圆的切片。标记这些切片,并且在图表中表示与每个切片对应的值。

以下是一张饼图,描绘了一个实例中各公司的移动销售情况。

Mobilesales饼图

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

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

这个类有5个属性如下 -

  • clockwise - 这是一个布尔运算符; 将此运算符设置为true时,饼图中的数据切片将从饼图的起始角开始顺时针排列。
  • data - 这表示一个ObservableList对象,它保存饼图的数据。
  • labelLineLength - 一个整数运算符,表示连接标签和饼图切片的线条的长度。
  • labelsVisible - 这是一个布尔运算符; 将此运算符设置为true时,将绘制饼图的标签。默认情况下,此运算符设置为true。
  • startAngle - 这是一个double类型的运算符,表示在第一个饼图切片处开始的角度。

要生成饼图,请准备ObservableList对象,如以下代码块所示 -

1
2
3
4
5
6
//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));

在准备ObservableList对象之后,将它作为参数传递给类PieChart的构造函数,如下所示 -

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

或者,通过使用指定的方法使用setData()命名的类的饼图包命名的javafx.scene.chart

1
pieChart.setData(pieChartData);

生成饼图的步骤

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

第1步:创建一个类

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

1
2
3
4
5
public class ClassName extends Application {  
@Override
public void start(Stage primaryStage) throws Exception {
}
}

第2步:准备ObservableList对象

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

1
2
3
4
5
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));

第3步:创建PieChart对象

通过传递ObservableList对象来创建PieChart,如下所示。

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

第4步:设置饼图的标题

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

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

第5步:顺时针设置切片

顺时针设置饼图的切片。这是通过布尔值真到完成setClockwise()之类的方法饼图。这属于包javafx.scene.chart -

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

第6步:设置标签线的长度

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

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

第7步:设置标签可见

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

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

步骤8:设置饼图的起始角度

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

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

第9步:创建组对象

start()方法中,通过实例化名为Group的类来创建组对象。这属于包javafx.scene

将在上一步中创建的PieChart(节点)对象作为参数传递给Group类的构造函数。这样做是为了将其添加到组中,如下所示 -

1
Group root = new Group(piechart);

第10步:创建场景对象

通过实例化名Scene的类来创建一个Scene,该类属于包javafx.scene。在此类中,传递上一步中创建的Group对象(root)。

除了根对象之外,您还可以传递两个表示屏幕高度和宽度的双参数,以及Group类的对象,如下所示。

1
Scene scene = new Scene(group ,600, 300);

第11步:设置舞台的标题

您可以使用Stage类的setTitle()方法将标题设置为舞台。所述primaryStage是Stage对象,它被传递给场景类作为参数的启动方法。

使用primaryStage对象,将场景标题设置为Sample Application,如下所示。

1
primaryStage.setTitle("Sample Application");

第12步:将场景添加到舞台

您可以使用名为Stage的类的方法setScene()将Scene对象添加到舞台。使用此方法添加在前面步骤中准备的Scene对象,如下所示。

1
primaryStage.setScene(scene);

步骤13:显示舞台的内容

显示场景的使用命名的方法的内容显示()的的阶段类,如下所示。

1
primaryStage.show();

第14步:启动应用程序

通过从main方法调用Application类的静态方法launch()来启动JavaFX应用程序,如下所示。

1
2
3
public static void main(String args[]){   
launch(args);
}

下面的表格在饼图的帮助下描述了移动销售。下表列出了不同的移动品牌及其销售(每天的单位)。

序号 移动品牌 销售额(每日单位)
1 IPhone 5S 20
2 三星大 20
3 摩托车 40
4 诺基亚Lumia 10

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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文件。

1
2
javac PieChartExample.java 
java PieChartExample

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

饼形图