Area Chart

In this section we are providing you an example to create a Area Chart.

Area Chart

Area Chart

     

In this section we are providing you an example to create a Area Chart.

Description of Program

For creating a Area Chart we use the object of DefaultCategoryDataset. After creating the object of DefaultCategoryDataset we add some data in this object by invoking setValue() method. In this example you learn some more interactive features of JFreeChart like set the background color of chart and adjust the color the title etc.

Description of Code

For defining a dataset for a Area Chart we have to create an object of DefaultCategoryDataset type :
  DefaultCategoryDataset dataset = new DefaultCategoryDataset();
  
After creating the instance of dataset then we have to add the data in the data set by invoking the method setValue(). In this example we show more than one set of pictures for the same chart. This can done by the following modification :
    dataset.addValue(4.0, "Science", "Rahul");
  dataset.addValue(3.0, "Maths", "Rahul");
  dataset.addValue(5.0, "Science", "Vinod");
  dataset.addValue(2.0,"Maths", "Vinod");

First argument specifies the total marks obtained by the student and the second argument specifies what will appear in the legend to the meaning of the area chart.
  
  JFreeChart chart = ChartFactory.createAreaChart("Comparison between Students Marks","Students", "Marks ", dataset, PlotOrientation.VERTICAL, true, true, false);
After added the data in dataset we create the Area Chart by invoking the createAreaChart() method. This method is a static method of ChartFactory class and its returns the object of JFreeChart type.This method syntax is:
  Public static JFreeChart createAreaChart(java.lang.String title, java.lang.String categoryAxisLabel, java.lang.String valueAxisLabel, CategoryDataset dataset, PlotOrientation orientation, boolean legend, boolean tooltips, boolean urls);
  

  chart.setBackgroundPaint(Color.yellow);
Above method is used to set the background color of chart.
 
  chart.getTitle().setPaint(Color.blue); 
Above method is used to set the color of chart title.
  
  CategoryPlot p = chart.getCategoryPlot(); 
Above method is used to get the object of the Plot for Bar Chart.
 
  p.setForegroundAlpha(0.4f);
Above method is used to set the alpha-transparency for the plot. It takes the float type argument.

  p.setRangeGridlinePaint(Color.red);
Above method is used to set the color of plot Gridlines.
 
   CategoryItemRenderer renderer = p.getRenderer();
Above method is used to get the reference to the renderer for the plot.,
  
  renderer.setSeriesPaint(0,Color.red);
Above method is used set the color of the area chart.

  ChartFrame frame1=new ChartFrame("Bar Chart",chart);
After this we create the object of ChartFrame. It used to display a chart.
 
Here is a code of the Program :

import java.awt.*;
import java.io.*;
import org.jfree.chart.*;
import org.jfree.data.category.*;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.*;
import org.jfree.chart.renderer.category.*;
import org.jfree.chart.plot.*;

public class Area{
public static void main(String arg[]){
  DefaultCategoryDataset dataset = new DefaultCategoryDataset();
  dataset.addValue(4.0"Science""Rahul");
  dataset.addValue(3.0"Maths""Rahul");
  dataset.addValue(5.0"Science""Vinod");
  dataset.addValue(2.0,"Maths""Vinod");
  dataset.addValue(3.0"Science""Prashant");
  dataset.addValue(5.0"Maths""Prashant");
  dataset.addValue(6.0"Science""Tapan");
  dataset.addValue(2.0"Maths""Tapan");
  dataset.addValue(3.0,"Science""Santosh");
  dataset.addValue(5.0"Maths""Santosh");

  JFreeChart chart = ChartFactory.createAreaChart
   (
"Comparison between Students Marks","Students""Marks ",
    dataset, PlotOrientation.VERTICAL, true,true, 
false);
  chart.setBackgroundPaint(Color.yellow);
  chart.getTitle().setPaint(Color.blue)
  CategoryPlot p = chart.getCategoryPlot()
  p.setForegroundAlpha(0.7f);
  p.setRangeGridlinePaint(Color.red)
  p.setDomainGridlinesVisible(true);
  p.setDomainGridlinePaint(Color.black);
  CategoryItemRenderer renderer = p.getRenderer();
  renderer.setSeriesPaint(1, Color.red);
  renderer.setSeriesPaint(0, Color.green);
  ChartFrame frame1=new ChartFrame("Area Chart",chart);
  frame1.setVisible(true);
  frame1.setSize(300,300);
  }
}

Output of the Program :

Download this Program