Home Java Example Java Swing Draw Pie Chart
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Draw Pie Chart

Advertisement
This Java Pie Chart example is going to show you how to draw Pie Charts in Java.

Draw Pie Chart

     

This Java Pie Chart example is going to show you how to draw Pie Charts in Java.

This Java Pie Chart example is drawing a pie chart to show the percentage of  marks obtained in particular session. In this example we have used the object of JFrame class that calls the PieChartPanel class. The Font class represents the font 'Book Antiqua'. 

The method getPercentInstance() of NumberFormat class returns the percentage format for the current default locale. The ComponenetAdapter class is an abstract adapter class  which is called to receive the component events. The BufferedImage class is called by image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB) which defines the image of pie chart The method Math.min(width,height)  returns the smaller of two into values. The Ellipse2D.Double class is called to draw the circle shape. 

Following code draws the leading edge of pie:

g2.draw(new Line2D.Double(cp, cq, p, q))

The angle is defined by:

phi = (marks[j]/total) * 2 * Math.PI

Following code marks the center on centerline:

p = cp + (9*pie/24) * Math.cos(theta + phi/2);
q = cq + (9*pie/24) * Math.sin(theta + phi/2);

 FontRenderContext class is used to measure the text.  

Following code shows the percent values outside the circle:

s = nf.format(data[j]/total);
textWidth = (float)font.getStringBounds(s, frc).getWidth();
lm = font.getLineMetrics(s, frc);
sx = (float)(x - textWidth/2);
sy = (float)(y + lm.getAscent()/2);
g2.drawString(s, sx, sy);
theta += phi;

Finally, graphics2d.drawImage(image, 0, 0, this) draw the image of Pie Chart.

Here is the code of PieChartExample.java

import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.text.*;
import javax.swing.*;
 
public class PieChartExample
{
    public static void main(String[] args)
    {
        JFrame f = new JFrame("Pie Chart");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(new PieChartPanel());
        f.setSize(350,300);
        f.setLocation(300,300);
        f.setVisible(true);
    }
}
 class PieChartPanel extends JPanel
{
    BufferedImage image;
    final int PAD = 30;
    Font font;
    NumberFormat numberFormat;
    
  public PieChartPanel()
    {
        font = new Font("Book Antiqua", Font.BOLD, 20);
        numberFormat = NumberFormat.getPercentInstance();
        addMouseListener(new Visibility(this));
        addComponentListener(new ComponentAdapter()
        {});
    }
 protected void paintComponent(Graphics graphics)
    {
        super.paintComponent(graphics);
        Graphics2D graphics2d = (Graphics2D)graphics;
        graphics2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);
        createChartImage();
        graphics2d.drawImage(image, 0, 0, this);
    }
    private void createChartImage()
    {
        int[] marks = { 98, 76, 90, 85, 75 };
        int width = getWidth();
        int height = getHeight();
        int cp = width/2;
        int cq = height/2;
        image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = image.createGraphics();
        g2.setPaint(Color.blue);
        g2.fillRect(0, 0, width, height);
        g2.setPaint(Color.black);
        int pie = Math.min(width,height) - 2*PAD;
        g2.draw(new Ellipse2D.Double(cp - pie/2, cq - pie/2, pie, pie));
        double total = 0;
        for(int j = 0; j < marks.length; j++)
            total += marks[j];
        double theta = 0, phi = 0;
        double p, q;
        for(int j = 0; j < marks.length; j++)
        {
            p = cp + (pie/2) * Math.cos(theta);
            q = cq + (pie/2) * Math.sin(theta);
            g2.draw(new Line2D.Double(cp, cq, p, q));
            phi = (marks[j]/total) * 2 * Math.PI;
            p = cp + (9*pie/24) * Math.cos(theta + phi/2);
            q = cq + (9*pie/24) * Math.sin(theta + phi/2);
            g2.setFont(font);
            String st = String.valueOf(marks[j]);
            FontRenderContext frc = g2.getFontRenderContext();
            float textWidth = (float)font.getStringBounds(st, frc).getWidth();
            LineMetrics lm = font.getLineMetrics(st, frc);
            float sp = (float)(p - textWidth/2);
            float sq = (float)(q + lm.getAscent()/2);
            g2.drawString(st, sp, sq);
            p = cp + (pie/2 + 4*PAD/5) * Math.cos(theta + phi/2);
            q = cq + (pie/2 + 4*PAD/5) * Math.sin(theta+ phi/2);
            st = numberFormat.format(marks[j]/total);
            textWidth = (float)font.getStringBounds(st, frc).getWidth();
            lm = font.getLineMetrics(st, frc);
            sp = (float)(p - textWidth/2);
            sq = (float)(q + lm.getAscent()/2);
            g2.drawString(st, sp, sq);
            theta += phi;
        }
        g2.dispose();
    }
 public void toggleVisibility()
    {
        repaint();
    }
}
class Visibility extends MouseAdapter
{
    PieChartPanel piechart;
public Visibility(PieChartPanel pc)
    {
        piechart = pc;
    }
 public void mousePressed(MouseEvent event)
    {
        if(event.getClickCount() > 1)
            piechart.toggleVisibility();
    }
}


Output will be displayed as:


Download Source Code

 

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 15, 2008

Related Tutorials

Ask Questions?    Discuss: Draw Pie Chart   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Devilz...
November 15, 2011
Pie Chart in java

I am a new java beginner only. i am interested in using this pie chart to show my report in my assignment. However, i still not understand about how the source code running in producing the pie chart...starting at the line "int pie = Math.min(width,height) - 2*PAD;" and until the last line..Can you explain to me? Please. Your helping are much appreciated.
shahla.shona
December 25, 2011
java

java site pie chart is good
DMCA.com