How to set the slices of a JavaFX PieChart in anti-clockwise direction?

JavaFXObject Oriented ProgrammingProgramming

The pie chart is a data a circular statistical graphic, where the data values are represented as the 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.

Changing the direction of the slices

The angle at which the first slice of a PieChart is started is known as start angle and you can set this value using the setStartAngle() method (by default, 0).

The PieChart class provides a property (boolean) known as clockWise specifying whether the slices of the current PieChart are in a clockwise direction from the start angle (true − clockwise, false − anti-clockwise). By default, the value of this property is true. You can change it using the setClockWise() method.

i.e. if you want to set the slices of a PieChart in the anti-clockwise direction you need to invoke the setClockWise() method, bypassing false as an argument.

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 PieChart_AntiClockWise 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 slices anti-clockwise
      pieChart.setClockwise(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);
   }
}

Output

raja
Published on 20-May-2020 11:33:23
Advertisements