What is a Pie Chart?
A pie graph (or pie chart) is a
specialized graph used in
statistics. The independent
variable is plotted around a circle
in either a clockwise direction or a
counterclockwise direction. The
dependent variable (usually a
percentage) is rendered as an arc
whose measure is proportional to the
magnitude of the quantity. Each arc
is depicted by constructing radial
lines from its ends to the center of
the circle, creating a wedge-shaped
"slice." The independent variable
can attain a finite number of
discrete values (for example, five).
The dependent variable can attain
any value from zero to 100 percent.
[from www.whatis.com]
The applet above creates a Pie
chart based on integer inputs:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class Pie extends Applet
implements ActionListener
{
Label inputlabel;
TextField input1, input2, input3,
input4;
Button b1;
int number[], degrees[];
int sum, counter;
public void init()
{
sum=0;
counter=0;
number=new int[4];
degrees=new int[4];
inputlabel=
new Label("Enter four integers:");
input1=new TextField(5);
input2=new TextField(5);
input3=new TextField(5);
input4=new TextField(5);
b1=new Button("Draw Piechart");
add(inputlabel);
add(input1);
add(input2);
add(input3);
add(input4);
add(b1);
b1.addActionListener(this);
}
public void actionPerformed
(ActionEvent e)
{
number[0]=Integer.parseInt
(input1.getText());
number[1]=Integer.parseInt
(input2.getText());
number[2]=Integer.parseInt
(input3.getText());
number[3]=Integer.parseInt
(input4.getText());
sum=number[0]+number[1]
+number[2]+number[3];
degrees[0]=number[0]*360/sum;
degrees[1]=number[1]*360/sum;
degrees[2]=number[2]*360/sum;
degrees[3]=360-degrees[0]
-degrees[1]-degrees[2];
repaint();
}
public void paint(Graphics g)
{
g.setColor(Color.red);
g.fillArc
(40,60,200,200,0,degrees[0]);
g.setColor(Color.green);
g.fillArc(40,60,200,200,
degrees[0], degrees[1]);
g.setColor(Color.blue);
g.fillArc(40,60,200,200,
degrees[0]+degrees[1],
degrees[2]);
g.setColor(Color.yellow);
g.fillArc(40,60,200,200,
degrees[0]+degrees[1]+degrees[2],
degrees[3]);
}
}