Sunday, 7 June 2015

JFreeChart Bar Charts

A bar chart is a chart, that displays information using rectangular bars of different heights.

ChartFactory class provides following constructors to create bar charts.

public static JFreeChart createBarChart(String title, String categoryAxisLabel, String valueAxisLabel, CategoryDataset dataset)

public static JFreeChart createBarChart(String title, String categoryAxisLabel, String valueAxisLabel, CategoryDataset dataset, PlotOrientation orientation, boolean legend, boolean tooltips, boolean urls)
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.data.category.DefaultCategoryDataset;

public class Main {
 public static void main(String args[]) {

  String title = "Multiple Pie chart demo";

  DefaultCategoryDataset dataset = new DefaultCategoryDataset();

  dataset.addValue(1, "Student heights", "Less than 130");
  dataset.addValue(4, "Student heights", "130 to 134");
  dataset.addValue(11, "Student heights", "135 to 139");
  dataset.addValue(3, "Student heights", "140 to 144");
  dataset.addValue(9, "Student heights", "144 to 148");

  /* create chart */
  JFreeChart chart = ChartFactory.createBarChart(title,
    "heights", "Number Of Students",  dataset);

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

 }
}


Output

Following example compares heights of students and teachers.

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.data.category.DefaultCategoryDataset;

public class Main {
 public static void main(String args[]) {

  String title = "Bar chart demp";

  DefaultCategoryDataset dataset = new DefaultCategoryDataset();

  dataset.addValue(1, "Student heights", "Less than 130");
  dataset.addValue(20, "Student heights", "130 to 134");
  dataset.addValue(30, "Student heights", "135 to 139");
  dataset.addValue(16, "Student heights", "140 to 144");
  dataset.addValue(9, "Student heights", "144 to 148");

  dataset.addValue(4, "Teacher heights", "Less than 130");
  dataset.addValue(3, "Teacher heights", "130 to 134");
  dataset.addValue(10, "Teacher heights", "135 to 139");
  dataset.addValue(5, "Teacher heights", "140 to 144");
  dataset.addValue(1, "Teacher heights", "144 to 148");

  /* create chart */
  JFreeChart chart = ChartFactory.createBarChart(title, "heights",
    "Number Of Persons", dataset);

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

 }
}


Output


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment