Sunday 7 June 2015

JFreeChart Change the orientation Of Bar chart

You can specify the orientation of bar chart to horizontal/vertical. You can do this in two ways.

Way 1: By using the constructor of class ChartFactory.

public static JFreeChart createBarChart(String title, String categoryAxisLabel, String valueAxisLabel, CategoryDataset dataset, PlotOrientation orientation, boolean legend, boolean tooltips, boolean urls)

Way 2: By using the CategoryPlot object.

CategoryPlot plot = chart.getCategoryPlot();
plot.setOrientation(PlotOrientation.HORIZONTAL);

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
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);

  CategoryPlot plot = chart.getCategoryPlot();
  plot.setOrientation(PlotOrientation.HORIZONTAL);

  /* 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