How to make the labels of a JavaFX Pie chart invisible?


In the pie chart, we represent the data values as slices of a circle. Each slice is differentiated from other (typically by color). In JavaFX, you can create a pie chart by instantiating the javafx.scene.chart.PieChart class.

Making Labels Invisible

Each slice is associated with a label. (name of the slice as value) By default, these labels are visible. This class has a property named labels visible specifying whether to display the labels in the pie chart or not. You can set the value to this property using the setLabelsVisible() method.

To make the labels of the current pie chart invisible you need to invoke this method by passing the boolean value false as a parameter.

Example

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 {
   public void start(Stage stage) {
      //Creating a Pie chart
      PieChart pieChart = new PieChart();
      //Setting data
      ObservableList<PieChart.Data> 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 other properties
      pieChart.setTitle("Activities");
      pieChart.setClockwise(true);
      pieChart.setLabelLineLength(10);
      pieChart.setLabelsVisible(true);
      pieChart.setStartAngle(360);
      //Creating a stack pane to hold the pie chart
      StackPane pane = new StackPane(pieChart);
      //Setting the Scene
      Scene scene = new Scene(pane, 600, 350);
      stage.setTitle("Pie chart");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

Output

Updated on: 20-May-2020

355 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements