Pie Chart Issue

Will not make more than 2 slices of the pie

Page 1 of 1

1 Replies - 1839 Views - Last Post: 04 May 2010 - 10:18 PM Rate Topic: -----

#1 croog24  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 39
  • Joined: 04-February 10

Pie Chart Issue

Posted 04 May 2010 - 09:36 PM

In this project, I've created a class to take user input on multiple university names, their student populations, and a color for each slice. The input is then stored in a few arrays and output into a pie chart graphic in a JFrame. My issue is no matter how many universities I put, the pie chart only shows two of the slices. Any ideas?


The main method class:
package testcs;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import java.awt.Color;
public class Main {

    public static void main(String[] args) 
    {
        JFrame win=new JFrame();
        win.setSize(600,600);
        win.setTitle("Pie Chart");
        win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        int num=0;
        String school="";
        double students=0.0;
        int r, g, b=0;
        
        num=Integer.parseInt(JOptionPane.showInputDialog("Enter your number " +
                "of universities: "));
                
        String[] schools=new String[num];
        double[] population=new double[num];
        Color[] colors=new Color[num];
        int count=0;
        while(count==0)
        {
        for(int i=0; i<num; i++)
            {
                school=JOptionPane.showInputDialog("Enter a university: ");

                students=Double.parseDouble(JOptionPane.showInputDialog("How " +
                        "many students does "+school+" have?"));
                
                r=Integer.parseInt(JOptionPane.showInputDialog("How much red " +
                        "is in "+school+"'s color?"));
                g=Integer.parseInt(JOptionPane.showInputDialog("How much " +
                        "green is in "+school+"'s color?"));
                b=Integer.parseInt(JOptionPane.showInputDialog("How much blue" +
                        " is in "+school+"'s color?"));
                
                Color c=new Color(r, g, B)/>;
                
                schools[i]=school;
                population[i]=students;
                colors[i]=c;
            }
            PieChart p=new PieChart(schools, population, colors);
            PieComponent pc=new PieComponent(p);
            win.add(pc);
            win.setVisible(true);
            break;
        }
    }
}    



The PieChart class
package testcs;

import java.awt.geom.Arc2D;
import java.awt.Color;
import java.awt.Graphics2D;

public class PieChart
{
    private String[] uni_name;
    private double[] students;
    private Color color[];

    //Constructors
    public PieChart(String[] u, double[] s, Color[] c)
    {
        this.uni_name=u;
        this.students=s;
        this.color=c;
    }

    //methods
    public double total(double t)
    {
        double temp=0.0;
        for(int i=0; i<students.length;i++)
        {
            temp=students[i];
            if(i+1<students.length)
                t=temp+students[i+1];
        }
        return t;
    }

    public double average(double a)
    {
        double t=0;
        double temp=0.0;
        for(int i=0; i<=students.length;i++)
        {
            temp=students[i];
            if(i+1<=students.length)
                t=temp+students[i+1];
        }
        a=t/students.length;
        return a;
    }

    public String smallestUniversity()
    {
        String min="";
        for(int i=0; i<uni_name.length; i++)
        {
            if(i==0)
                min=uni_name[0];
            else
            {
                if(students[i+1]<uni_name.length)
                    if(students[i]<students[i+1])
                        min=uni_name[i];
                    else
                        min=uni_name[i+1];
            }
        }
        return min;
    }

    public String largestUniversity()
    {
        String max="";
        for(int i=0; i<uni_name.length; i++)
        {
            if(i==0)
                max=uni_name[0];
            else
            {
                if(students[i+1]<uni_name.length)
                    if(students[i]>students[i+1])
                        max=uni_name[i];
                    else
                        max=uni_name[i+1];
            }
        }
        return max;
   }

   public void drawChart(Graphics2D g, int x, int y)
   {
    // Get total value of all slices
    double temp=0.0;
    double total=0.0;
        for(int i=0; i<students.length;i++)
        {
            temp=students[i];
            if(i+1<students.length)
                total=temp+students[i+1];
        }

    // Draw each pie slice
    double curValue = 0.0;
    int startAngle = 0;
    for (int i=0; i<students.length; i++)
    {
        // Compute the start and stop angles
        startAngle = (int)(curValue * 360 / total);
        int arcAngle = (int)(students[i] * 360 / total);

        // Set the color and draw a filled arc
        g.setColor(color[i]);
        g.fill(new Arc2D.Double(200, 300, 300, 300, startAngle,
                arcAngle, Arc2D.PIE));

        curValue += students[i];
    }
 }
}



The JComponent
package testcs;

import javax.swing.JComponent;
import java.awt.Graphics;
import java.awt.Graphics2D;

public class PieComponent extends JComponent
{
    private PieChart chart;

    public PieComponent(PieChart s)
    {
        chart=s;
    }

    public void paintComponent(Graphics g)
    {
        Graphics2D g2=(Graphics2D)g;
        chart.drawChart(g2, 100, 200);
    }
}



Thank you!

This post has been edited by croog24: 04 May 2010 - 09:38 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Pie Chart Issue

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4422
  • View blog
  • Posts: 12,289
  • Joined: 18-April 07

Re: Pie Chart Issue

Posted 04 May 2010 - 10:18 PM

In your drawChart method, simply make your for loop...

for(int i=0; i<students.length;i++)
{
     total += students[i];
}



The idea is that you simply want to find the overall student total. The way you were doing it before you were not properly counting up the total of all students. If I enter three universities and make their student counts 10, 20 and 30 the result is 60 students. Your original loop had it adding up to only 50. The last two slices. Thus your first slice was not being counted.

Once you fix this, things should start working for you. Enjoy! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1