Home Chartgraphs Bar Chart
Questions:Ask|Latest

Related Tutorials


 
 

Share on Google+Share on Google+

Bar Chart

Advertisement
In this section we are providing you an example to create a Bar Chart.

Bar Chart

     

In this section we are providing you an example to create a Bar Chart.

Description of Program 

For creating a Bar chart we use the object of DefaultCategoryDataset. After creating the object of DefaultCategoryDataset we add some data in this object by invoking setValue() method. In this example you learn some more interactive features of JFreechart like set the background color of chart and adjust the color the title etc.

Description of Code

For defining a dataset for a Bar chart we have to create an object of DefaultCategoryDataset type :
  DefaultCategoryDataset dataset = new DefaultCategoryDataset();

After creating the instance of dataset then we have to add the data in the data set by invoking the method setValue()
  setValue(6, ?Marks?, ?Rahul?);
First argument specifies the total marks obtained by the student and the second argument specify what will appear in the legend to the meaning of the bar.

  JFreeChart chart = ChartFactory.createBarChart("BarChart using JFreeChart", "Student", "Marks", dataset, PlotOrientation.VERTICAL, true, true, false);
After added the data in dataset we create the Bar chart by invoking the createBarChart() method. This method is a static method of ChartFactory class and its returns the object of JFreeChart type.This method syntax is:

  Public static JFreeChart createBarChart(java.lang.String title, java.lang.String categoryAxisLabel, java.lang.String valueAxisLabel, CategoryDataset dataset, PlotOrientation orientation, boolean legend, boolean tooltips, boolean urls);

  chart.setBackgroundPaint(Color.yellow);
Above method is used to set the color of chart background

  chart.getTitle().setPaint(Color.blue); 
Above method is used to set the color of chart title

  CategoryPlot p = chart.getCategoryPlot(); 
Above method is used to get the object of the Plot for Bar Chart

  p.setRangeGridlinePaint(Color.red);
Above method is used to set the color of plot Gridlines

  ChartFrame frame1=new ChartFrame("Bar Chart",chart);
After this we create the object of ChartFrame. It used to display a chart.

Here is code of Program :

import org.jfree.chart.*;
import org.jfree.data.category.*;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.xy.*;
import org.jfree.data.*;
import org.jfree.chart.renderer.category.*;
import org.jfree.chart.plot.*;
import java.awt.*;

public class BarExample1{
public static void main(String arg[]){
  DefaultCategoryDataset dataset = new DefaultCategoryDataset();
  dataset.setValue(2"Marks""Rahul");
  dataset.setValue(7"Marks""Vinod");
  dataset.setValue(4"Marks""Deepak");
  dataset.setValue(9"Marks""Prashant");
  dataset.setValue(6"Marks""Chandan");
  JFreeChart chart = ChartFactory.createBarChart
  (
"BarChart using JFreeChart","Student""Marks", dataset, 
   PlotOrientation.VERTICAL, false,true, 
false);
  chart.setBackgroundPaint(Color.yellow);
  chart.getTitle().setPaint(Color.blue)
  CategoryPlot p = chart.getCategoryPlot()
  p.setRangeGridlinePaint(Color.red)
  ChartFrame frame1=new ChartFrame("Bar Chart",chart);
  frame1.setVisible(true);
  frame1.setSize(400,350);
  }
}

Output of the Program :

Download this Example

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 13, 2007

Related Tutorials

Ask Questions?    Discuss: Bar Chart   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Randon
September 13, 2011
data on x axis

Hello, how can I add data (Double value) in X axis, instead of String/Comparable. I am trying to bring up a graph with Double in X and Y axis. Thank you.
antonia
February 25, 2012
nice job

Thx for the great tutorial!! it's very helpful!!
Netaji Nikam
October 8, 2012
Bar chart generation calculation in java

please send this coding.
robo
November 22, 2012
How to implement the above in JSP

I am looking for the above bar chart to be implemented in jsp . I have gone through the only example that you provided in jsp. I want this chart to be displayed in jsp
Johny
September 14, 2013
Please mention the required jars

Please mention the required jars to execute this code in eclipse.
DMCA.com