Click to See Complete Forum and Search --> : plotting pie and bar charts


kanakatam
07-10-2005, 11:04 AM
Hello floks,
I had to draw a pie chart and an equivalent bar chart with five numbers which i did and got the output as well. One little problem is that i need to display the bar horizontally. Right now i have got it to display vertically. I did play with the x and y coordinates but not able to make it display horizontally. Help appreciated,
Thanks,
kt. Here is my complete code
<code>
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;

public class Test extends JFrame
{

private int [] vals = {10, 20, 30, 20, 20};
private Color [] myColor = {Color.red, Color.blue, Color.yellow, Color.magenta, Color.cyan};
public Test()
{
super( "Pie Chart" );

setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
setSize( 700, 500 );
setVisible( true );

}

public void paint(Graphics g)
{
super.paint(g);
Graphics2D g2d = (Graphics2D) g;

int startAngle = 0;

for(int i = 0; i < vals.length; i ++)
{
int degreeIncrement = (int)((vals[i] / 100.0 )* 360) ;
g2d.setPaint(myColor[i]);
g2d.fill( new Arc2D.Double (
20, 50, 300, 300, startAngle, degreeIncrement, Arc2D.PIE ) );
startAngle += degreeIncrement;

}

int x = 350;
int y = 250;
int width = 20;
for(int i = 0; i < vals.length; i++)
{
int length = (int)((vals[i] / 100.0) * 300);
g2d.setPaint(myColor[i]);
g2d.fill(new Rectangle2D.Double(
x, y - length, width, length));

x = x + 10 + width;
}

}

public static void main(String args[])
{
Test pie = new Test();


}
}

</code>