Share on Google+Share on Google+

Bar Chart in Java

In this series of Java Graphic examples, we are going to show you how to draw simple bar chart in Java.

Bar Chart in Java


In this series of Java Graphic examples, we are going to show you how to draw simple bar chart in Java. A bar chart (bar graph) is a way of comparing two or more values using rectangular bars of lengths. The bars can be horizontally or vertically oriented. We are providing you an example which shows the Bar chart in Java.

To draw a bar chart, the variables minvalue, maxvalue of double type are define. The width of the bar is determined by clientWidth/value.length. The FontMetrics class defines a font metrics object which encapsulates information about the rendering of a particular font on a particular screen. The Font class represents font. The font 'Book Antiqua' is defined in the constructor of Font class

The method getAscent() determines the font ascent of the font described by the FontMetrics object. The font ascent is the distance from the font's baseline to the top of most alphanumeric characters. The method getDescent()  determines the font descent. The method getHeight() gets the standard height of a line of text in the font. 

Following code describes the scale of bar chart:

double scale = (clientHeight - top - bottom) / (maxValue - minValue);

Here is the code of

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

  public class SimpleBarChart extends JPanel {
  private double[] value;
  private String[] languages;
  private String title;

  public SimpleBarChart(double[] val, String[] lang, String t) {
  languages = lang;
  value = val;
  title = t;
  public void paintComponent(Graphics graphics) {
  if (value == null || value.length == 0)
  double minValue = 0;
  double maxValue = 0;
  for (int i = 0; i < value.length; i++) {
  if (minValue > value[i])
  minValue = value[i];
  if (maxValue < value[i])
  maxValue = value[i];
  Dimension dim = getSize();
  int clientWidth = dim.width;
  int clientHeight = dim.height;
  int barWidth = clientWidth / value.length;
  Font titleFont = new Font("Book Antiqua", Font.BOLD, 15);
  FontMetrics titleFontMetrics = graphics.getFontMetrics(titleFont);
  Font labelFont = new Font("Book Antiqua", Font.PLAIN, 10);
  FontMetrics labelFontMetrics = graphics.getFontMetrics(labelFont);
  int titleWidth = titleFontMetrics.stringWidth(title);
  int q = titleFontMetrics.getAscent();
  int p = (clientWidth - titleWidth) / 2;
  graphics.drawString(title, p, q);
  int top = titleFontMetrics.getHeight();
  int bottom = labelFontMetrics.getHeight();
  if (maxValue == minValue)
  double scale = (clientHeight - top - bottom) / (maxValue - minValue);
  q = clientHeight - labelFontMetrics.getDescent();
  for (int j = 0; j < value.length; j++) {
  int valueP = j * barWidth + 1;
  int valueQ = top;
  int height = (int) (value[j] * scale);
  if (value[j] >= 0)
  valueQ += (int) ((maxValue - value[j]) * scale);
  else {
  valueQ += (int) (maxValue * scale);
  height = -height;
  graphics.fillRect(valueP, valueQ, barWidth - 2, height);
  graphics.drawRect(valueP, valueQ, barWidth - 2, height);
  int labelWidth = labelFontMetrics.stringWidth(languages[j]);
  p = j * barWidth + (barWidth - labelWidth) / 2;
  graphics.drawString(languages[j], p, q);
 public static void main(String[] args) {
  JFrame frame = new JFrame();
  double[] value= new double[5];
  String[] languages = new String[5];
  value[0] = 1;
  languages[0] = "Visual Basic";

  value[1] = 2;
  languages[1] = "PHP";

  value[2] = 3;
  languages[2] = "C++";

  value[3] = 4;
  languages[3] = "C";

  value[4] = 5;
  languages[4] = "Java";
  frame.getContentPane().add(new SimpleBarChart(value, languages,
"Programming Languages"));

  WindowListener winListener = new WindowAdapter() {
  public void windowClosing(WindowEvent event) {

Output will be displayed as:

Download Source Code




Posted on: December 15, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

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.

Discuss: Bar Chart in Java   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
Aman Kumar Singh
June 14, 2012
Thanks man

Thanks dude you helped me out a lot.