JFreeChart - XY Step Area Chart example


A XY Step Area Chart is much like XY step line chart except that the area under them is shaded. The Step Area Charts can be used to highlight the increase or decrease in values over time.

In this post, we will show you how to create a XY Step Area Chart using the JFreeChart 1.0.19 library.

 

The following example demonstrates how to create a simple XY Step Area Chart using the JFreeChart.

XYStepAreaChartExample.java

package com.boraji.tutorial.jfreechart;

import java.awt.Color;

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.XYPlot;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

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

   private static final long serialVersionUID = 1L;

   public XYStepAreaChartExample(String title) {
      super(title);
      // Create dataset
      XYDataset dataset = createDataset();
      // Create chart
      JFreeChart chart = ChartFactory.createXYStepAreaChart(
            "XY Step Area Chart | WWW.BORAJI.COM", // Chart Title
            "X-Axis", // X-Axis Label
            "Y-Axis", // Y-Axis Label
            dataset);
      
      XYPlot plot=(XYPlot)chart.getPlot();
      plot.setBackgroundPaint(new Color(229, 150, 97, 60));
      
      ChartPanel panel = new ChartPanel(chart);
      setContentPane(panel);
   }

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

      XYSeries series1 = new XYSeries("Series1");
      series1.add(2, 4);
      series1.add(3, 6);
      series1.add(5, 2);
      series1.add(8, 5);
      series1.add(1, 8);
      
      XYSeries series2 = new XYSeries("Series2");
      series2.add(5, 6);
      series2.add(9, 5);
      series2.add(10, 9);
      series2.add(18, 11);
      series2.add(15, 18);
      
      // Add series to dataset
      dataset.addSeries(series1);
      dataset.addSeries(series2);
      return dataset;
   }

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

Output

JFreeChart - XY Step Area Chart | BORAJI.COM