Drawing Simple Pie Chart

To draw pie chart with 5 sectors. The program calculates the total of all the input given by user and find the angle in degrees to draw each sectors.

When the program first loads, chart based on default values is painted by the program. User can edit the values in the text field and presses draw button to redraw the chart. Each time the draw button is pressed, new set of angle is calculated and repainted.



I have attached the the sources for this program. It is simple and needs upgrades.

Pie.java

This class executed the program and initializes the components JTextField and JPanel to draw the chart.


package coolApp.graphics;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Pie {
JTextField a,b,c,d,e;
JButton btnDraw;
double total;

JPanel contentPane;
Pie(){
JFrame frame = new JFrame("Pie - CoolApps");
frame.setSize(500,500);
contentPane = new JPanel();
contentPane.setLayout(new BorderLayout());
frame.add(contentPane);

initComponent();
frame.setVisible(true);
}

void initComponent(){
JPanel inputPanel = new JPanel();

inputPanel.add(new JLabel("a"));
a = new JTextField("3",5);
inputPanel.add(a);
inputPanel.add(new JLabel("b"));
b = new JTextField("2",5);
inputPanel.add(b);
inputPanel.add(new JLabel("c"));
c = new JTextField("4",5);
inputPanel.add(c);
inputPanel.add(new JLabel("d"));
d = new JTextField("7",5);
inputPanel.add(d);
inputPanel.add(new JLabel("e"));
e = new JTextField("5",5);
inputPanel.add(e);
PieChart ppanel = new PieChart();
btnDraw = new JButton("Draw");
btnDraw.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String aa = a.getText();
String bb = b.getText();
String cc = c.getText();
String dd = d.getText();
String ee = Pie.this.e.getText();

ppanel.setValues(aa,bb,cc,dd,ee);
}
});
inputPanel.add(btnDraw);
contentPane.add(inputPanel, BorderLayout.NORTH);
contentPane.add(ppanel,BorderLayout.CENTER);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Pie();
}
}

PieChart.java

This class is the drawing area for the chart. It has a method called setValues() to update the values for each sector.


package coolApp.graphics;
import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JPanel;

public class PieChart extends JPanel{

double a,b,c,d,e;
double total;
PieChart(){
a = 3;
b = 2;
c = 4;
d = 7;
e = 5;
total = a+b+c+d+e;
this.setBackground(Color.white);
}
public void setValues(String a, String b, String c, String d, String e) {
this.a = Integer.parseInt(a);
this.b = Integer.parseInt(b);
this.c = Integer.parseInt(c);
this.d = Integer.parseInt(d);
this.e = Integer.parseInt(e);
this.total = this.a + this.b + this.c + this.d + this.e;
repaint();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);

int pa, pb, pc, pd, pe;
int sa, sb, sc, sd, se;

pa = (int) Math.ceil(((a / total ) * 360));
pb = (int) Math.ceil(((b / total ) * 360));
pc = (int) Math.ceil(((c / total ) * 360));
pd = (int) Math.ceil(((d / total ) * 360));
pe = (int) Math.ceil(((e / total ) * 360));

sa = 0;
sb = sa + pa;
sc = sb + pb;
sd = sc + pc;
se = sd + pd;

g.setColor(Color.gray);
g.fillArc(100, 50, 300, 300, sa, pa);
g.setColor(Color.red);
g.fillArc(100, 50, 300, 300, sb, pb);
g.setColor(Color.green);
g.fillArc(100, 50, 300, 300, sc, pc);
g.setColor(Color.blue);
g.fillArc(100, 50, 300, 300, sd, pd);
g.setColor(Color.orange);
g.fillArc(100, 50, 300, 300, se, pe);
}
}


Discussion is welcome. Please email me at civmook@gmail.com for any assistance...

Comments

Popular posts from this blog

VB.net connecting to SQL Server

VB.net