Saturday, 6 June 2015

JFreeChart : Pie charts


A pie chart is a specialized circular graph used in statistics, it is divided into slices to illustrate numerical proportion.

Simple way to create Pie Charts
You can create a Pie chart by using ChatFactory class. ChatFactory class provides following factory methods to create Pie chart.
public static JFreeChart createPieChart(String title, PieDataset dataset);

public static JFreeChart createPieChart(String title, PieDataset dataset,
   boolean legend, boolean tooltips, Locale locale);

public static JFreeChart createPieChart(String title, PieDataset dataset,
   boolean legend, boolean tooltips, boolean urls);

public static JFreeChart createPieChart(String title, PieDataset dataset,
   PieDataset previousDataset, int percentDiffForMaxScale,
   boolean greenForIncrease, boolean legend, boolean tooltips,
   Locale locale, boolean subTitle, boolean showDifference);


public static JFreeChart createPieChart(String title, PieDataset dataset,
   PieDataset previousDataset, int percentDiffForMaxScale,
   boolean greenForIncrease, boolean legend, boolean tooltips,
   boolean urls, boolean subTitle, boolean showDifference);

Parameter details
Parameter
Description
title
Specifies chart title
dataset
Input for the chart.
legend
Boolean flag specifies whether a legend is required or not.
tooltips
If tooltips set to true, then tooltips are enabled on chart, else not.
locale
Specifies the locale
previousDataSet
The dataset for the last run, this will be used to compare each key in the dataset
percentDiffForMaxScale
Integer value, goes from bright red/green to black, percentDiffForMaxScale indicate the change required to reach top scale.
subtitle
If set to true, then it displays a sub title with color scheme
showDifference
create a new dataset that will show the % difference between the two datasets.
urls
Configure chart to generate URLs



Example1
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;

public class Main {
 public static void main(String args[]) {
  /* Create dataset */
  DefaultPieDataset dataset = new DefaultPieDataset();
  dataset.setValue("2004-2005", 58);
  dataset.setValue("2005-2006", 41);
  dataset.setValue("2006-2007", 85.3);
  dataset.setValue("2007-2008", 81);

  /* create chart */
  JFreeChart chart = ChartFactory.createPieChart("Simple Piechart",
    dataset);

  /* create and display chart on frame */
  ChartFrame frame = new ChartFrame("First", chart);
  frame.pack();
  frame.setVisible(true);
 }
}


By default, legend and tooltip are enabled for this constructor.





Prevoius                                                 Next                                                 Home

No comments:

Post a Comment