Java Swing is a powerful GUI toolkit for creating desktop applications. Among its many features, Swing provides a rich set of components for data visualization, including the JFreeChart library for creating charts and graphs. In this article, we'll explore how to create a pie chart using Java Swing and JFreeChart.

Free Custom Pie Chart Tool
Free Custom Pie Chart Tool

Before we dive into the code, let's ensure you have the necessary libraries. You'll need to add the JFreeChart library to your project. If you're using Maven, add this to your pom.xml file:

the pie chart shows that there are many different types of people
the pie chart shows that there are many different types of people

```xml org.jfree jfreechart 1.0.19 ```

Setting Up the Environment

First, import the necessary classes and set up the JFrame for your application:

Set of pie charts infographic by siraanamwong Vectors & Illustrations  with Unlimited Downloads - Yayimages
Set of pie charts infographic by siraanamwong Vectors & Illustrations with Unlimited Downloads - Yayimages

```java import javax.swing.*; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.data.general.DefaultPieDataset; public class PieChartExample extends JFrame { public PieChartExample() { super("Pie Chart Example"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(640, 480); } } ```

Creating the Pie Dataset

The first step in creating a pie chart is to create a dataset. JFreeChart provides the DefaultPieDataset class for this purpose:

dynamic charts visualisation
dynamic charts visualisation

```java DefaultPieDataset dataset = new DefaultPieDataset(); dataset.setValue("Category 1", new Double(45.0)); dataset.setValue("Category 2", new Double(25.0)); dataset.setValue("Category 3", new Double(30.0)); ```

Creating the Pie Chart

Next, use the ChartFactory to create the chart. You can customize the chart's title, legend, and tooltips:

```java JFreeChart chart = ChartFactory.createPieChart("Pie Chart Example", dataset, true, true, true); ```

Displaying the Chart

How to Draw Charts Using JavaScript and HTML5 Canvas | Envato Tuts+
How to Draw Charts Using JavaScript and HTML5 Canvas | Envato Tuts+

Now that you have your chart, you can display it in a JFrame using a ChartPanel:

```java ChartPanel chartPanel = new ChartPanel(chart); add(chartPanel); setVisible(true); ```

Here's the complete code for a simple pie chart:

```java import javax.swing.*; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.data.general.DefaultPieDataset; public class PieChartExample extends JFrame { public PieChartExample() { super("Pie Chart Example"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(640, 480); DefaultPieDataset dataset = new DefaultPieDataset(); dataset.setValue("Category 1", new Double(45.0)); dataset.setValue("Category 2", new Double(25.0)); dataset.setValue("Category 3", new Double(30.0)); JFreeChart chart = ChartFactory.createPieChart("Pie Chart Example", dataset, true, true, true); ChartPanel chartPanel = new ChartPanel(chart); add(chartPanel); } public static void main(String[] args) { SwingUtilities.invokeLater(() -> new PieChartExample().setVisible(true)); } } ```

This example creates a simple pie chart with three categories. You can customize the chart further by changing the dataset values, colors, and other properties. Happy coding!

Pie Charts: Types, Advantages, Examples, and More | EdrawMax
Pie Charts: Types, Advantages, Examples, and More | EdrawMax
Pie Chart with Legend - amCharts
Pie Chart with Legend - amCharts
Create a Pie Chart of Pandas Series Values
Create a Pie Chart of Pandas Series Values
the pie chart shows that there are many different types of people
the pie chart shows that there are many different types of people
a pie chart with the names and colors for james bond's six chic - takings
a pie chart with the names and colors for james bond's six chic - takings
Basic Pie Charts
Basic Pie Charts
How to Make an Expanding Donut Chart in Tableau
How to Make an Expanding Donut Chart in Tableau
Introducing pie charts - Statistics (Handling Data) in Year 6 by URBrainy.com
Introducing pie charts - Statistics (Handling Data) in Year 6 by URBrainy.com
CSS Pie Chart Code with Example
CSS Pie Chart Code with Example
Pie Chart Resources | Spire Maths
Pie Chart Resources | Spire Maths
Ⅰ. What is a pie chart?
Ⅰ. What is a pie chart?
Chart > Pie - Slide Members
Chart > Pie - Slide Members
Interpreting Pie Charts worksheet
Interpreting Pie Charts worksheet
a pie chart that shows the number of people in each region
a pie chart that shows the number of people in each region
Pie chart free download 2025
Pie chart free download 2025
It’s time we learn to design a proper pie chart
It’s time we learn to design a proper pie chart
Pie Chart With Shadow For PowerPoint & Google Slides
Pie Chart With Shadow For PowerPoint & Google Slides
Chapter 9 Pie Chart | Basic R Guide for NSC Statistics
Chapter 9 Pie Chart | Basic R Guide for NSC Statistics
Chart.js
Chart.js
a pie chart with the words video - cable digital media players in different colors and sizes
a pie chart with the words video - cable digital media players in different colors and sizes