JFreeChart - Polar chart example



JFreeChart - Polar chart example

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.

In this post, I will show you how to create a polar chart in a java program using the JFreeChart 1.0.19 library.

 

JAR dependencies

Add the following jars to build path of your project.

  • jfreechart-1.0.19.jar
  • jcommon-1.0.23.jar

 

Polar line chart example 

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

PolarLineChartExample.java

package com.boraji.tutorial.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;

/**
 * @author imssbora
 *
 */
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 | WWW.BORAJI.COM", // 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("Series1");
      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("Gantt Chart Example");
         example.setSize(800, 400);
         example.setLocationRelativeTo(null);
         example.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
         example.setVisible(true);
      });
   }
}

Output

JFreeChart - Gantt chart example | BORAJI.COM

 

Polar area chart example 

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

PolarAreaChartExample.java

package com.boraji.tutorial.jfreechart;

import java.awt.AlphaComposite;

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.chart.plot.PolarPlot;
import org.jfree.chart.renderer.DefaultPolarItemRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
 * @author imssbora
 *
 */
public class PolarAreaChartExample extends JFrame {

   private static final long serialVersionUID = 1L;

   public PolarAreaChartExample(String title) {
      super(title);
      
      // Create dataset
      XYDataset dataset = getXYDataset();
    
      // Create chart
      JFreeChart chart = ChartFactory.createPolarChart(
            "Polar Chart Example | WWW.BORAJI.COM", // Chart title
            dataset,
            true,
            true,
            false
            );

      PolarPlot plot = (PolarPlot) chart.getPlot();
      DefaultPolarItemRenderer renderer = (DefaultPolarItemRenderer) plot.getRenderer();
      renderer.setFillComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.9f));
      renderer.setSeriesFilled(0, true);
      
      ChartPanel panel = new ChartPanel(chart);
      panel.setMouseZoomable(false);
      setContentPane(panel);
   }

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

      XYSeries series1 = new XYSeries("Series1");
      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(() -> {
         PolarAreaChartExample example = new PolarAreaChartExample("Gantt Chart Example");
         example.setSize(800, 400);
         example.setLocationRelativeTo(null);
         example.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
         example.setVisible(true);
      });
   }
}

Output

JFreeChart - Gantt chart example | BORAJI.COM