WaterFall Chart

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

WaterFall Chart

WaterFall Chart

     

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

Description of Program :

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

Description of Code :

For defining a dataset for WaterFall 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 addValue().
  addValue(3.0, ?Salary?, ?Rahul?);
First argument specifies the salary of a employee and the second argument specify what will appear in the legend to the meaning of the Waterfall chart.

  JFreeChart chart = ChartFactory.createWaterfallChart("Comparison between Employee", "Employee", "Salary", dataset, PlotOrientation.VERTICAL, true, true, false);
After added the data in dataset we create the WaterFall chart by invoking the createWaterfallChart() 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 createWaterfallChart(java.lang.String title, java.lang.String categoryAxisLabel, java.lang.String valueAxisLabel, CategoryDataset dataset, PlotOrientation orientation, boolean legend, boolean tooltips, boolean urls);

  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 Waterfall Chart. GetCategoryPlot() is method of the JFreeChart class and it returns the object of CategoryPlot type.

  p.setRangeGridlinePaint(Color.red);
Above method is used to set the color of plot Gridlines.

  p.setDomainGridlinesVisible(true);
Above method is used to set the flag that controls the Gridlines are drawn or not against the domain axis.

  p.setDomainGridlinePaint(Color.black);
Above method is used to set the color of Gridlines against the domain axis. 

  ChartFrame frame1=new ChartFrame("WaterFall 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 org.jfree.chart.*;
import org.jfree.data.category.*;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.xy.*;
import org.jfree.data.*;
import org.jfree.chart.renderer.category.*;
import org.jfree.chart.plot.*;

public class Waterfall{
public static void main(String arg[]){
  DefaultCategoryDataset dataset = new DefaultCategoryDataset();
  dataset.addValue(3.0"Salary""Rahul");
  dataset.addValue(3.0"Salary""Prashant");
  dataset.addValue(2.0"Salary""Chandan");
  dataset.addValue(2.0"Salary""Vinod");
  dataset.addValue(10.0"Salary""Total");
  JFreeChart chart = ChartFactory.createWaterfallChart
  (
"Comparison between Employees","Employee""Salary"
  dataset, PlotOrientation.VERTICAL, true,true, 
false);
  chart.getTitle().setPaint(Color.blue)
  CategoryPlot p = chart.getCategoryPlot()
  p.setRangeGridlinePaint(Color.red)
  p.setDomainGridlinesVisible(true);
  p.setDomainGridlinePaint(Color.black);
  ChartFrame frame1=new ChartFrame("WaterFall Chart",chart);
  frame1.setVisible(true);
  frame1.setSize(400,350);
  }
}

Output of the Program :

Download this Program