How to Draw a Pie Chart in Java

Written by kevin walker
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Draw a Pie Chart in Java
Make pie charts like this programmatically with Java. (pencil pie chart and calculator image by patrimonio designs from Fotolia.com)

Though the JavaFX language contains a tool for the easy creation of pie charts, ordinary Java provides no such easy functionality. Instead, you will need to design your own class for the creation of pie charts using the Java language. The key to the task will be the Graphics2D class built into the standard Java AWT library.

Skill level:
Moderate

Other People Are Reading

Instructions

  1. 1

    Create a new class. If you are using an Integrated Development Environment (IDE), just click "File," "New Class." Otherwise, open Notepad or your text editor of choice and save the file as "PieChart.java."

  2. 2

    Import the libraries you will need into your program from the Java standard library by pasting the following:

    import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.geom.Rectangle2D; import java.util.ArrayList; import javax.swing.JFrame;

  3. 3

    Paste the following to create the PieChart class:

    /* * Draws a pie chart. / public class PieChart extends JFrame{

    ArrayList<PieSlice> slices = new ArrayList<PieSlice>();
    
    Rectangle2D rect;
    
    public void add Slice(double value, Color colour)
    
    public void draw(Graphics2D g, Rectangle2D area)
    
    public void init() ;
    
    @Override
    public void paint(Graphics g)
    
    
    
    public static void main(String[] args)
    
    /**
     * This represents the data for a slice of the pie.
     */
    private class PieSlice
    

    }

    The rest of the tutorial will cover filling in the code for each method described in the above code.

  4. 4

    Implement the PieSlice private class. This will hold the data for each "slice" of the pie chart. Paste the following code:

    private class PieSlice {
        double value;
        Color colour;
    
        /**
         * The slice of the pie.
         * @param value The size of the slice.
         * @param colour The colour desired for the slice.
         */
        public PieSlice(double value, Color colour) {
            this.color = colour;
            this.value = value;
        }
    
    }
    
  5. 5

    Implement the add Slice method by pasting the following code:

    public void add Slice(double value, Color colour) {
        slices.add(new PieSlice(value, colour));
    }
    

    This just creates a new PieSlice and adds it to the slice list ArrayList.

  6. 6

    Implement the init method by pasting the following:

    public void init() {

        this.setSize(800,600);
        this.setVisible(true);
        rect = new Rectangle(100, 100, 600, 480);
    
    };
    
  7. 7

    Implement the draw method by pasting the following code:

    public void draw(Graphics2D g, Rectangle2D area) {
        double pie Size = 0;
        for (PieSlice slice : slices) {
            pie Size += slice.value;
        }
        double current Value = 0;
        int start Angle = 0;
        int arc Angle = 0;
        for (PieSlice slice : slices) {
            start Angle = (int)(current Value * 360 / pie Size);
            arc Angle = (int)(slice.value * 360 / pie Size);
    
            // If this is the last slice, do this to ensure there is no gaps.
            if (slice.equals(slices.get(slices.size()-1))) {
                arc Angle = 360 - start Angle;
            }
    
           g.setColor(slice.color);
           g.fillArc((int)area.getMinX(), (int)area.getMinY(), (int)area.getWidth(), (int)area.getHeight(), start Angle, arc Angle);
           current Value += slice.value;
        }
    
    }
    

    This is the workhorse of the program. It goes through each slice of the pie chart and draws a coloured arc corresponding to its percentage of the pie.

  8. 8

    Implement the paint method that the PieChart class will get from the JFrame superclass. All it does is call the draw method every time a refresh is needed:

    @Override
    public void paint(Graphics g) {
        draw((Graphics2D) this.getGraphics(), rect);
    }
    
  9. 9

    Create a main method to test the class and ensure it works:

    public static void main(String[] args) { PieChart chart = new PieChart(); chart.init();

        chart.addSlice(10, Color.RED);
        chart.addSlice(7, Color.BLUE);
        chart.addSlice(3, Color.GREEN);
        chart.addSlice(15, Color.ORANGE);
    
    }
    

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.