如何在 JavaFX 中删除饼图的图例?
在饼图中,我们将数据值表示为圆形的切片。每个切片通过颜色与其他切片区分开来。在 JavaFX 中,你可以通过实例化javafx.scene.chart.PieChart类来创建饼图。
默认情况下,JavaFX 饼图包含切片的标签和一个图例,即一个用颜色表示每个颜色所代表的类别的条形图。

使图例不可见
PieChart类有一个名为legendVisible的属性(从 Chart 类继承)。它为布尔类型,你可以使用setLegendVisible()方法为其设置值。
默认情况下,legendVisible属性的值为 true,如果你将其设置为 false,则图例将不可见。
示例
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.chart.PieChart;
import javafx.scene.layout.StackPane;
public class PieChartExample 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);
//Making the legend invisible
pieChart.setLegendVisible(false);
//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);
}
}输出

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