本文概述
通常, 饼图是一种图形或图表, 其中圆的扇区用于表示整个信息的不同比例。扇区弧的角度根据该扇区代表的信息的百分比而变化。
在下图中, 显示了一个饼图, 其扇区代表一个人在其篮子中拥有的水果量。
在JavaFX中, 饼图由类javafx.scene.chart.PieChrt表示。我们需要实例化此类以创建pi-chart。
物产
下表描述了javafx.scene.chart.PieChart类的属性以及setter方法。
属性 | 描述 | 设置方法 |
---|---|---|
clockwise | 这是布尔类型的属性。它的真实值表示切片从起始角度开始顺时针放置。 | setClockWise(Boolean value) |
data | 这是一个可观察的List类型属性。它表示要在饼图中设置的数据。 PieChart.Data类用于处理对饼图切片的数据分配。 | setData(PieChart.Data value) |
labelLineLength | 这是一个双精度类型的属性。它表示从饼图外部到切片标签的线的长度。 | setLabelLineLength(double value) |
labelsVisible | 这是布尔类型的属性。它的真实值表示将绘制饼图标签。 | setLabelsVisible(boolean value) |
startAngle | 这是一个双精度类型的属性。它代表第一个饼图切片的角度。 | setStartAngle(double value) |
建设者
该类中有两个构造函数。
- public PieChart():创建一个空PieChart的新实例。
- public PieChart(ObservableList data):使用指定的数据切片创建饼图的新实例。
范例1:
package application;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Side;
import javafx.scene.Scene;
import javafx.scene.chart.PieChart;
import javafx.scene.chart.PieChart.Data;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class ChartTest extends Application{
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
//Instantiating the pie-chart class
PieChart piechart = new PieChart();
//setting the data of the pie chart.
piechart.setData(getChartData());
//Creating Layout
StackPane root = new StackPane();
//Adding pie-chart to the layout
root.getChildren().add(piechart);
//configuring scene
Scene scene = new Scene(root, 400, 400);
primaryStage.setScene(scene);
primaryStage.setTitle("PieChart Example");
primaryStage.show();
}
//creating getChartData method to set the chart data
private ObservableList<Data> getChartData() {
ObservableList<Data> list = FXCollections.observableArrayList();
list.addAll(new PieChart.Data("srcmini", 90), new PieChart.Data("Others", 10));
return list;
}
}
范例2:
在下面的示例中, 我们创建了一个饼图, 该饼图显示了所有计算机编程语言的流行程度。
package application;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Side;
import javafx.scene.Scene;
import javafx.scene.chart.PieChart;
import javafx.scene.chart.PieChart.Data;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class ChartTest extends Application{
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
//Instantiating PieChart class
PieChart piechart = new PieChart();
//Setting pieChart data
piechart.setData(getChartData());
piechart.setLegendSide(Side.LEFT);
piechart.setTitle("Computer Language Popularities");
piechart.setClockwise(false);
//Creating layout
StackPane root = new StackPane();
//Adding piechart to the layout
root.getChildren().add(piechart);
//Configuring Scene and stage object
Scene scene = new Scene(root, 800, 600);
primaryStage.setScene(scene);
primaryStage.setTitle("PieChart Example");
primaryStage.show();
}
//The method sets the data to the pie-chart.
private ObservableList<Data> getChartData() {
ObservableList<Data> list = FXCollections.observableArrayList();
list.addAll(new PieChart.Data("JavaScript", 30.8), new PieChart.Data("Ruby", 11.8), new PieChart.Data("Java", 10.8), new PieChart.Data("Python", 11.6), new PieChart.Data("PHP", 7.2), new PieChart.Data("Objective-C", 10.7), new PieChart.Data("C", 5.2), new PieChart.Data("C++", 4.3), new PieChart.Data("Go", 3.8), new PieChart.Data("CSS", 3.8)
);
return list;
}
}
评论前必须登录!
注册