How to change the position of the legend in a PieChart in JavaFX?

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.

By default, a JavaFX pie chart contains labels of the slices and a legend − a bar with colors specifying the category represented by each color.

Changing the position of the Legend −

The PieChart class has a property named legendSide (inherited from the Chart class). This specifies the position of the legend in the chart (left, right, top-bottom). You can set the value to this property using the setLegendSide() method. This method accepts one of the following values as a parameter −

  • Side.BOTTOM

  • Side.TOP

  • Side.LEFT

  • Side.RIGHT

You can change the position of the legend in a chart, by invoking the setLegendSide() method by passing the appropriate value as a parameter.


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 {
   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));
      //Setting the legend on the left side of the chart
      //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");
   public static void main(String args[]){


Updated on: 20-May-2020


Kickstart Your Career

Get certified by completing the course

Get Started