JavaFX |饼图类
PieChart 类是 JavaFX 的一部分。 PieChart 类用于创建饼图。图表内容由基于 PieChart 上数据集的饼图填充。默认情况下,饼图的布局设置为顺时针。 PieChart 继承 Chart 类。
类的构造函数是:
- PieChart() :创建一个空的饼图实例。
- PieChart(ObservableList data) :使用给定数据创建饼图实例。
常用方法:
Methods | Explanation |
---|---|
getData() | returns the data of the pie chart |
getLabelLineLength() | returns the label length of the pie chart |
getLabelsVisible() | Indicates whether pie slice labels are drawn or not |
getStartAngle() | returns the start angle of the pie chart |
isClockwise() | returns whether the pie chart is clockwise or not |
setClockwise(boolean v) | sets the pie chart orientation to clockwise is true value is passed |
setData(ObservableList data) | Sets the value of the property data. |
setLabelLineLength(double v) | sets the label line length of pie chart . |
setLabelsVisible(boolean v) | Sets the value of the property labelsVisible. |
setStartAngle(double v) | sets the start angle of the the pie chart |
下面的程序将说明 PieChart 类的使用:
- 用一些指定数据创建饼图的Java程序:该程序创建饼图。将创建一个 PieChart.Data,该数据将作为可观察列表添加到饼图中。PieChart将在场景内创建,而场景又将托管在舞台内。函数setTitle()用于为舞台提供标题。然后创建一个组,并附加饼图。该组已连接到现场。最后调用show()方法显示最终结果。
// Java program to create a pie chart // with some specified data import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.chart.PieChart; import javafx.scene.layout.*; import javafx.event.ActionEvent; import javafx.scene.AmbientLight; import javafx.scene.shape.Sphere; import javafx.scene.control.*; import javafx.stage.Stage; import javafx.scene.Group; import javafx.scene.PerspectiveCamera; import javafx.scene.paint.Color; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.collections.FXCollections; public class pie_chart_1 extends Application { // launch the application public void start(Stage stage) { // set title for the stage stage.setTitle("Creating Pie Chart"); // piechart data PieChart.Data data[] = new PieChart.Data[5]; // string and integer data String status[] = {"Correct Answer", "Wrong Answer", "Compilation Error", "Runtime Error", "Others" }; int values[] = {20, 30, 10, 4, 2}; for (int i = 0; i < 5; i++) { data[i] = new PieChart.Data(status[i], values[i]); } // create a pie chart PieChart pie_chart = new PieChart(FXCollections.observableArrayList(data)); // create a Group Group group = new Group(pie_chart); // create a scene Scene scene = new Scene(group, 500, 300); // set the scene stage.setScene(scene); stage.show(); } // Main Method public static void main(String args[]) { // launch the application launch(args); } }
输出:
- Java程序创建一个带有一些指定数据的饼图,带有可见标签和定义的起始角度,并按逆时针方向排列:该程序创建一个 PieChart。将创建一个PieChart.Data作为可观察列表添加到饼图中。饼图将在场景内创建,而场景又将托管在舞台内。函数setTitle() 用于为舞台提供标题。然后创建一个组,并附加饼图。该组已连接到现场。最后调用show()方法显示最终结果。我们将使用setLabelLineLength()函数设置饼图的标签线长度,我们将分别使用setStartAngle()和setClockwise()函数设置起始角度和顺时针方向.我们可以使用 setLabelsVisible()函数使标签可见。
// Java program to create a pie chart with // some specified data, with visible labels // and a defined start angle, and ordered // in anticlockwise direction import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.chart.PieChart; import javafx.scene.layout.*; import javafx.event.ActionEvent; import javafx.scene.AmbientLight; import javafx.scene.shape.Sphere; import javafx.scene.control.*; import javafx.stage.Stage; import javafx.scene.Group; import javafx.scene.PerspectiveCamera; import javafx.scene.paint.Color; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.collections.FXCollections; public class pie_chart_2 extends Application { // launch the application public void start(Stage stage) { // set title for the stage stage.setTitle("Creating Pie Chart"); // piechart data PieChart.Data data[] = new PieChart.Data[5]; // string and integer data String status[] = {"Correct Answer", "Wrong Answer", "Compilation Error", "Runtime Error", "Others"}; int values[] = {20, 30, 10, 4, 2}; for (int i = 0; i < 5; i++) { data[i] = new PieChart.Data(status[i], values[i]); } // create a pie chart PieChart pie_chart = new PieChart(FXCollections.observableArrayList(data)); // set line length of label pie_chart.setLabelLineLength(10.0f); // make labels visible pie_chart.setLabelsVisible(true); // set start angle pie_chart.setStartAngle(20.0f); // set anticlockwise pie_chart.setClockwise(false); // create a Group Group group = new Group(pie_chart); // create a scene Scene scene = new Scene(group, 500, 500); // set the scene stage.setScene(scene); stage.show(); } // Main Method public static void main(String args[]) { // launch the application launch(args); } }
输出:
注意:上述程序可能无法在在线 IDE 中运行。请使用离线编译器。
参考: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/chart/PieChart.html