Program to make Barchart
import javax.swing.JFrame;
import java.awt.Graphics;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JPanel;
class bc extends JPanel
{
String [] x={"2004","2005","2006","2007","2008"};//plot varaibles Converted this to String for convenience
int [] y={50,60,68,79,90,100};//Data to be plotted
Color clr;
int Hspace,Xpos;
int Xrect,Yrect;
public bc()
{
//default constructor
}
public bc(int [] data,String [] caption)
{
y=data;
x=caption;
}
public void setValues(int [] data,String [] caption)
{
y=data;
x=caption;
}
public void set(Color c)
{
clr=c;
}
public Color get()
{
return clr;
}
public Color reset()
{
clr=Color.RED;
return clr;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawLine(40,10,40,410);
g.drawLine(40,410,570,410);
g.setFont(new Font("serif",Font.BOLD+Font.ITALIC,14));
set(Color.RED);
g.setColor(get());
g.drawString("100%",15,20);
g.drawString("75%",15,120);
g.drawString("50%",15,220);
g.drawString("25%",15,320);
Hspace=(530/x.length);//570-40
Xpos=45;
for(int i=0;i
{
// g.drawString((new Integer(x[i])).toString(),Xpos,430);
g.drawString(x[i],Xpos,430);
Yrect=4*(100-y[i])+10;//Scaling factor
g.drawRect(Xpos+10,Yrect,15,410-Yrect);
g.setColor(Color.GREEN);
g.fillRect(Xpos+15,Yrect+10,5,400-Yrect);
Xpos+=Hspace;
g.setColor(reset());
}
}
}
class barchartp
{
public static void main(String args[])
{
bc b=new bc();
JFrame jr=new JFrame("Barchart program");
jr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jr.add(b);
jr.setSize(600,600);
jr.setLocationRelativeTo(null);
jr.setVisible(true);
}
}
OUTPUT:
Read More ->>