Draw a bar chart in Java

By: Paawan Chaudhary Emailed: 1611 times Printed: 2079 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

This is a Java program to draw a bar chart for the table given below which shows annual result analysis of a school from period 2001-2005.these values may be placed in a HTML file as <param> attributes and then used in Applet for dislaying bar-chart.

Year	2001	2002	2003	2004	2005
Result %	80	90	100	100	98

 

import java.applet.*;
import java.awt.*;

/*
<applet code = "BarChart.class" width=400 height=400>

<param name="year0" value="2001">
<param name="year1" value="2002">
<param name="year2" value="2003">
<param name="year3" value="2004">
<param name="year4" value="2005">

<param name="result0" value="80">
<param name="result1" value="90">
<param name="result2" value="100">
<param name="result3" value="100">
<param name="result4" value="98">

</applet>
*/

public class BarChart extends Applet
{
	int n;
	String year[];
	int value[];

	public void init()
	{
		n = 5;
		year = new String[n];
		value = new int[n];

		year[0] = getParameter("year0");
		year[1] = getParameter("year1");
		year[2] = getParameter("year2");
		year[3] = getParameter("year3");
		year[4] = getParameter("year4");

		value[0] = Integer.parseInt(getParameter("result0"));
		value[1] = Integer.parseInt(getParameter("result1"));
		value[2] = Integer.parseInt(getParameter("result2"));
		value[3] = Integer.parseInt(getParameter("result3"));
		value[4] = Integer.parseInt(getParameter("result4"));

	}

	public void paint(Graphics g)
	{
		Font font = new Font("Arial",Font.BOLD,15);
		g.setFont(font);
		for(int i  = 0; i < n; i ++)
		{
			g.setColor(Color.BLUE);
			g.drawString(year[i], 20, i * 50 + 30);
			g.setColor(Color.RED);
			g.fillRect(70, i * 50 + 10, value[i], 40);
			g.drawString(String.valueOf(value[i]) + "%", 180, i * 50 + 35);
		}

		String msg = "Bar Chart from Year 2001 - 2005";
		g.setColor(Color.darkGray);

		font = new Font("Arial",Font.BOLD,20);
		g.setFont(font);
		g.drawString(msg, 50, 300);
	}
}

Java Home | All Java Tutorials | Latest Java Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(1)


1. View Comment

where is the main class .

View Tutorial          By: rishi at 2015-10-02 10:17:17

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Paawan Chaudhary
Count number of vowels, consonants and digits in a String in Java
Reverse a number in Java
Student marks calculation program in Java
Handling Fractions in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
MultiLevel Inheritance sample in Java
Multiple Inheritance sample in Java
Java program using Method Overriding
Java program to check if user input is an even number
Java program to print prime numbers using Thread
Java program to calculate volume of a Box
Java program to display numbers from 1 to 10 on Applet
Draw a bar chart in Java

More Tutorials in Java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Java File
Java String
Count number of vowels, consonants and digits in a String in Java
Reverse a number in Java
Student marks calculation program in Java
Handling Fractions in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
MultiLevel Inheritance sample in Java
Multiple Inheritance sample in Java
Java program using Method Overriding
Java program to check if user input is an even number

More Latest News
Most Viewed Articles (in Java )
Stack example in Java - push(), pop(), empty(), search()
Integer: byte, short, int, and long data types in Java
Count number of vowels, consonants and digits in a String in Java
Read from a COM port using Java program
Student marks calculation program in Java
indexOf( ) and lastIndexOf( ) in Java
Using One-Dimensional Arrays in Java
TCP Server and TCP Client in Java
How to Send SMS using Java Program (full code sample included)
FileReader and FileWriter example program in Java
XML and Java - Parsing XML using Java Tutorial
append() in Java
Multidimensional or arrays of arrays in Java
Recursion in java
Calculate gross salary in Java
Most Emailed Articles (in Java)
What is Java?
Read from a COM port using Java program
inheritance in Java
Booleans in java
How to Send SMS using Java Program (full code sample included)
Update contents of a file within a jar file
Using PrintWriter in Java
Recursion in java
The java Buzzwords
Handling Fractions in Java
package com.sun.speech.freetts does not exist.
Summary of Operators in Java
Use of return statement in Java
Converting Numbers to and from Strings using Java
Why java is important to the Internet