In this JavaFX example, we will see how to create a Pie Chart using JavaFX.
A pie chart is a circular chart that is divided into slices to illustrate numerical proportions.
JavaFX Pie Chart Example
The example uses a pie chart to show the market share of web servers:package sample;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.chart.PieChart;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage stage) {
initUI(stage);
}
private void initUI(Stage stage) {
HBox root = new HBox();
Scene scene = new Scene(root, 450, 330);
ObservableList<PieChart.Data> pieChartData
= FXCollections.observableArrayList(
new PieChart.Data("Apache", 52),
new PieChart.Data("Nginx", 31),
new PieChart.Data("IIS", 12),
new PieChart.Data("LiteSpeed", 2),
new PieChart.Data("Google server", 1),
new PieChart.Data("Others", 2));
PieChart pieChart = new PieChart(pieChartData);
pieChart.setTitle("Web servers market share (2022)");
root.getChildren().add(pieChart);
stage.setTitle("PieChart");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Output:
Related JavaFX Examples
- JavaFX GridPane Example
- JavaFX ColorPicker Example
- JavaFX DatePicker Example
- JavaFX MenuBar Example
- JavaFX Radio Button Example
- JavaFX TabPane Example
- JavaFX Accordion Example
- JavaFX Login Form Validation Example
- JavaFX Form Validation - Registration Form Validation Example
- JavaFX Line Chart Example
- JavaFX Area Chart Example
- JavaFX Scatter Chart Example
- JavaFX Bar Chart Example
- JavaFX Pie Chart Example
- JavaFX Select and Multi-Select Example
- JavaFX Check Box Example
- Java Calculator Project
Comments
Post a Comment
Leave Comment