Java Swing, a powerful GUI toolkit, offers a rich set of components for creating interactive and visually appealing applications. Among its many features, Swing's charting capabilities are particularly noteworthy, enabling developers to visualize data in a variety of ways. Let's explore how to create a simple Java Swing chart example, focusing on the JFreeChart library, which provides a wide range of chart types and customization options.

Before we dive into the code, ensure you have the JFreeChart library in your project's classpath. If you're using Maven, add the following dependency to your pom.xml file:

```xml
Setting Up the Chart
First, let's create a basic Java Swing application and set up the chart using JFreeChart.

Import the necessary classes and create a new JFrame with a JPanel to hold the 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 SwingChartExample extends JFrame { public SwingChartExample() { setTitle("Java Swing Chart Example"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(640, 480); setLocationRelativeTo(null); } } ```
Creating the Dataset

Next, create a dataset for the chart. In this example, we'll use a pie chart to display data from a DefaultPieDataset:
```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 Chart
Now, create the chart using the ChartFactory class and add it to a ChartPanel:

```java JFreeChart chart = ChartFactory.createPieChart("Swing Chart Example", dataset, true, true, true); ChartPanel chartPanel = new ChartPanel(chart); add(chartPanel); ```
Customizing the Chart
JFreeChart offers numerous customization options. Let's explore some of them.
Changing the Chart Type

You can easily switch between different chart types by using the appropriate create* method from the ChartFactory class. For example, to create a bar chart, use:
```java JFreeChart chart = ChartFactory.createBarChart("Swing Chart Example", "Category", "Value", dataset, PlotOrientation.VERTICAL, true, true, false); ```
Customizing the Plot



















To further customize the chart, you can access the plot and apply various styling options. Here's how to change the background color of the plot:
```java CategoryPlot plot = chart.getCategoryPlot(); plot.setBackgroundPaint(new Color(0xFFFFFF)); // Set background color to white ```
Adding a Legend
If you've disabled the legend in the ChartFactory method, you can add one manually using the LegendTitle class:
```java LegendTitle legend = new LegendTitle("Legend"); legend.setPosition(RectangleEdge.BOTTOM); chart.addLegend(legend); ```
With these customizations, you can create engaging and informative charts within your Java Swing applications. To learn more about JFreeChart's extensive feature set, consult the official documentation and API reference.
In this example, we've created a simple Java Swing chart using the JFreeChart library. By exploring the various chart types and customization options, you can bring your data to life and create visually appealing user interfaces. Happy coding!