Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Pie Chart help

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Pie Chart help

    Hi forum,

    I'm new to the forum and to java, what I thought would be simple(and it probably is) is giving me trouble. I have a project where I need to draw a pie chart based on input. I have 99% of the project done, I'm just not sure on how to draw the pie chart. Here is my code for the chart panel. I tried doing it as a bar graph, I just don't know how to do the pie. Please help point me in the right direction


    import javax.swing.*;
    import java.awt.*;

    /**
    * Created by S1195552 on 4/7/14.
    */
    public class PanelChart extends JPanel {

    private double Boston;
    private double Miami;
    private double Dallas;
    private double StLouis;
    private double KansasCity;
    private double Seattle;
    private double LosAngeles;
    private double total;
    private Graphics g;

    public void paintComponent(Graphics g)
    {
    super.paintComponent(g);
    this.g = g;
    drawChart();

    }

    public void setPopulation(double boston, double miami, double dallas, double stLouis, double kansasCity, double seattle, double losAngeles) {
    Boston = boston;
    Miami = miami;
    Dallas = dallas;
    StLouis = stLouis;
    KansasCity = kansasCity;
    Seattle = seattle;
    LosAngeles = losAngeles;

    this.total = this.Boston +
    this.Miami +
    this.Dallas +
    this.StLouis +
    this.KansasCity +
    this.Seattle +
    this.LosAngeles;

    repaint();
    }

    public void drawChart()
    {
    int pctBoston = (int)(Boston/total*100);
    int pctMiami = (int)(Miami/total*100);
    int pctDallas = (int)(Dallas/total*100);
    int pctStLouis = (int)(StLouis/total*100);
    int pctKansasCity = (int)(KansasCity/total*100);
    int pctSeattle = (int)(Seattle/total*100);
    int pctLosAngeles = (int)(LosAngeles/total*100);

    int factor = 55;
    g.setColor(Color.magenta);
    //g.fillRect(25, factor * 1 - 15, pctBoston * 3, 10);
    g.fillOval(25, factor * 1 - 15, pctBoston * 3, 10);
    //g.drawOval();
    g.setColor(Color.yellow);
    g.fillRect(25,factor * 2-15,pctMiami*3,10);
    g.setColor(Color.gray);
    g.fillRect(25,factor * 3-15,pctDallas*3,10);
    g.setColor(Color.green);
    g.fillRect(25,factor * 4-15,pctStLouis*3,10);
    g.setColor(Color.red);
    g.fillRect(25,factor * 5-15,pctKansasCity*3,10);
    g.setColor(Color.red);
    g.fillRect(25,factor * 5-15,pctSeattle *3,10);

    g.setColor(Color.red);
    g.fillRect(25,factor * 5-15,pctLosAngeles*3,10);




    }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,295
    My Mood
    Mellow
    Thanks
    193
    Thanked 669 Times in 656 Posts

    Default Re: Pie Chart help

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly. What did you find when you searched the Internet for "java pie chart"?

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Pie Chart help

    I didnt find much that I recognize from class

    --- Update ---

    I'm currently trying to figure it out from the examples we did in class, but I'm having some trouble grasping some of it

    --- Update ---

    Here is the code properly formatted for the post
    import javax.swing.*;
    import java.awt.*;
     
    /**
    * Created by S1195552 on 4/7/14.
    */
    public class PanelChart extends JPanel {
     
    private double Boston;
    private double Miami;
    private double Dallas;
    private double StLouis;
    private double KansasCity;
    private double Seattle;
    private double LosAngeles;
    private double total;
    private Graphics g;
     
    public void paintComponent(Graphics g)
    {
    super.paintComponent(g);
    this.g = g;
    drawChart();
     
    }
     
    public void setPopulation(double boston, double miami, double dallas, double stLouis, double kansasCity, double seattle, double losAngeles) {
    Boston = boston;
    Miami = miami;
    Dallas = dallas;
    StLouis = stLouis;
    KansasCity = kansasCity;
    Seattle = seattle;
    LosAngeles = losAngeles;
     
    this.total = this.Boston +
    this.Miami +
    this.Dallas +
    this.StLouis +
    this.KansasCity +
    this.Seattle +
    this.LosAngeles;
     
    repaint();
    }
     
    public void drawChart()
    {
    int pctBoston = (int)(Boston/total*100);
    int pctMiami = (int)(Miami/total*100);
    int pctDallas = (int)(Dallas/total*100);
    int pctStLouis = (int)(StLouis/total*100);
    int pctKansasCity = (int)(KansasCity/total*100);
    int pctSeattle = (int)(Seattle/total*100);
    int pctLosAngeles = (int)(LosAngeles/total*100);
     
    int factor = 55;
    g.setColor(Color.magenta);
    //g.fillRect(25, factor * 1 - 15, pctBoston * 3, 10);
    g.fillOval(25, factor * 1 - 15, pctBoston * 3, 10);
    //g.drawOval();
    g.setColor(Color.yellow);
    g.fillRect(25,factor * 2-15,pctMiami*3,10);
    g.setColor(Color.gray);
    g.fillRect(25,factor * 3-15,pctDallas*3,10);
    g.setColor(Color.green);
    g.fillRect(25,factor * 4-15,pctStLouis*3,10);
    g.setColor(Color.red);
    g.fillRect(25,factor * 5-15,pctKansasCity*3,10);
    g.setColor(Color.red);
    g.fillRect(25,factor * 5-15,pctSeattle *3,10);
     
    g.setColor(Color.red);
    g.fillRect(25,factor * 5-15,pctLosAngeles*3,10);
     
     
     
     
    }
    }

Similar Threads

  1. Creating a pie chart
    By nat in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 8th, 2014, 05:41 PM
  2. Temperature Conversion Chart
    By cb12991 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 7th, 2012, 11:11 PM
  3. Need help with pie charts locations
    By Wise girl in forum Object Oriented Programming
    Replies: 10
    Last Post: June 13th, 2012, 03:28 PM
  4. Help with pie chart
    By Wise girl in forum Java Theory & Questions
    Replies: 12
    Last Post: June 2nd, 2012, 07:07 PM
  5. scatterplot chart
    By anikal in forum AWT / Java Swing
    Replies: 2
    Last Post: August 23rd, 2011, 11:47 PM

Tags for this Thread