Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
Example
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);
}
}
Output
