In this section we are providing you an example to create a
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(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);
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.
Above method is used to set the color of plot Gridlines.
Above method is used to set the flag that controls the Gridlines are drawn or not against the domain axis.
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 :
Output of the Program :
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.