Sunday 7 June 2015

JFreeChart Customizing bar spacing between bars

As you observe from previous posts, there is some space between bars. You can adjust it by using following snippet.

                  /* Get instance of CategoryPlot */
                  CategoryPlot plot = chart.getCategoryPlot();

                  /* Change Bar colors */
                  BarRenderer renderer = (BarRenderer) plot.getRenderer();

                  renderer.setItemMargin(0);
import java.awt.Color;

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.renderer.category.BarRenderer;
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);

  /* Get instance of CategoryPlot */
  CategoryPlot plot = chart.getCategoryPlot();

  /* Change Bar colors */
  BarRenderer renderer = (BarRenderer) plot.getRenderer();
  renderer.setSeriesPaint(0, Color.pink);
  renderer.setSeriesPaint(1, Color.gray);
  renderer.setDrawBarOutline(false);
  renderer.setItemMargin(0);

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