如何在 JavaFX 中更改 PieChart 图例的位置?
在饼图中,我们将数据值表示为圆的扇形。每个扇形都与其他扇形区分开来(通常是通过颜色)。在 JavaFX 中,您可以通过实例化 **javafx.scene.chart.PieChart** 类来创建饼图。
默认情况下,JavaFX 饼图包含扇形的标签和图例 - 一个带有颜色的条形,用于指定每个颜色表示的类别。

更改图例的位置 -
PieChart 类有一个名为 **legendSide** 的属性(继承自 Chart 类)。这指定了图表中图例的位置(左、右、上、下)。您可以使用 **setLegendSide()** 方法将值设置为此属性。此方法接受以下值之一作为参数 -
Side.BOTTOM
Side.TOP
Side.LEFT
Side.RIGHT
您可以通过调用 **setLegendSide()** 方法并传递适当的值作为参数来更改图表中图例的位置。
示例
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Side;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.chart.PieChart;
import javafx.scene.layout.StackPane;
public class PieChart_LegendPosition extends Application {
@Override
public void start(Stage stage) {
//Creating a Pie chart
PieChart pieChart = new PieChart();
//Setting data
ObservableList data = FXCollections.observableArrayList(
new PieChart.Data("Work", 10),
new PieChart.Data("Chores", 2),
new PieChart.Data("Sleep", 8),
new PieChart.Data("Others", 4));
pieChart.setData(data);
//Setting the legend on the left side of the chart
pieChart.setLegendSide(Side.LEFT);
//Creating a stack pane to hold the pie chart
StackPane pane = new StackPane(pieChart);
pane.setStyle("-fx-background-color: BEIGE");
//Setting the Scene
Scene scene = new Scene(pane, 595, 300);
stage.setTitle("Pie Chart");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}输出

广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP