Saturday, 6 June 2015

JFreeChart Create your first chart

Creating charts using JFreeChart is a three step process.

Step 1: Create data set, which is to be displayed on the chart.
                  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);

Step 2: Instantiate JFrameChart object, it is responsible for drawing charts.

JFreeChart chart = ChartFactory.createPieChart("Company Growth",
                                    dataset, true, true, false);

Step 3: Draw to output to some target like panel (swing), jsp etc.,
                  ChartFrame frame = new ChartFrame("First", chart);
                  frame.pack();
                  frame.setVisible(true);

Following is the Complete Application.
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("Company Growth",
    dataset, true, true, false);

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


Output




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment