JFreeChart polar chart

In this example, we will create a polar chart using JFreeChart library.

JFreeChart is a popular Java library for creating charts. JFreeChart allows to create a wide variety of both interactive and non-interactive charts. We can create line charts, bar charts, area charts, scatter charts, pie charts, Gantt charts, and various specialized charts such as wind charts or bubble charts.

JFreeChart Maven dependency

For our projects, we use the below Maven dependency:

<dependency>
    <groupId>org.jfree</groupId>
    <artifactId>jfreechart</artifactId>
    <version>1.5.0</version>
</dependency>

JFreeChart polar chart

A polar chart is a type of pie chart which is used to represent the multivariate data in the form of a two-dimensional chart of three or more quantitative variables represented on axes starting from the same point. The polar chart is also known as radar chart, web chart, spider chart, star chart, star plot etc.

Here is an example of creating a polar line chart using JFreeChart library.

package com.java.library.jfreechart;


import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class PolarLineChartExample extends JFrame {

   private static final long serialVersionUID = 1L;

   public PolarLineChartExample(String title) {
      super(title);
      
      // Create dataset
      XYDataset dataset = getXYDataset();
    
      // Create chart
      JFreeChart chart = ChartFactory.createPolarChart(
            "Polar Chart Example | https://www.sourcecodeexamples.net/", // Chart title
            dataset,
            true,
            true,
            false
            );

      ChartPanel panel = new ChartPanel(chart);
      panel.setMouseZoomable(false);
      setContentPane(panel);
   }

   private XYDataset getXYDataset() {
     
      XYSeriesCollection dataset = new XYSeriesCollection();

      XYSeries series1 = new XYSeries("Series");
      series1.add(20, 45);
      series1.add(145, 120);
      series1.add(90, 150);
      dataset.addSeries(series1);
      
      return dataset;
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(() -> {
         PolarLineChartExample example = new PolarLineChartExample("PolarLineChartExample");
         example.setSize(800, 400);
         example.setLocationRelativeTo(null);
         example.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
         example.setVisible(true);
      });
   }
}

Output



Comments