Sunday, 27 April 2014

Creating Animated Pie Chart in Swing

The following program illustrates creating an animated pie chart the rotates smoothly.

You will need JFreeChart library for this. This contains a set of classes that simplify creating pie charts, graphs etc. You don't need to worry about all the jar files, classpath etc. There are only two jar files starting with jfreechart-1.0.17.jar and jcommon words. Here is the download url click on Download jfreechart-1.0.17.zip and in that file, you will find the lib folder. You will find those two jar files which you need to extract and then set them to your classpath. If you are using an IDE, add these jar files to your project. Here, I am just using Notepad.

PieChartExample.java



import javax.swing.*;
import java.awt.*;
import org.jfree.chart.*;
import org.jfree.chart.title.*;
import org.jfree.chart.plot.*;
import org.jfree.data.general.*;
import org.jfree.util.*;
import java.awt.event.*;
class PieChartExample extends JFrame
{
// A static variable represents pie chart angle
static int i=0;

    public PieChartExample()
    {
    setTitle("Pie chart example");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(400,400);
    setLayout(new GridBagLayout());
    setLocationRelativeTo(null);
    setVisible(true);

    // Create a dataset   
    DefaultPieDataset p=new DefaultPieDataset();

    // Ratio of fruits
    // Apple:Orange:Mango:Guava = 20:30:40:10
    p.setValue("Apple",20);
    p.setValue("Orange",30);
    p.setValue("Mango",40);
    p.setValue("Guava",10);

    // Create a 3D chart with the given title that appears above the chart
    JFreeChart chart=ChartFactory.createPieChart3D("Popular fruits this season",p);

    // Get a ui representation of the pie chart title so that you can customize
    // the font and padding
    TextTitle tt=new TextTitle("Popular fruits this season",new Font("Arial",Font.BOLD,14));

    // Space around the text and between its border
    // top, left, bottom, right
    tt.setPadding(5,5,5,5);
    chart.setTitle(tt);

    // Get the PiePlot object so that you can customize the label font
    final PiePlot3D plot = (PiePlot3D) chart.getPlot();
    plot.setLabelFont(new Font("Arial",Font.PLAIN,12));
   
    // Chart will be drawn on a panel
    ChartPanel panel=new ChartPanel(chart);
   
    // Set some preferred size
    panel.setPreferredSize(new Dimension(350,350));
   
    // Add the panel
    add(panel);

    // Create a timer for animation
    // Executed every 150ms   
    Timer t=new Timer(150,new ActionListener(){
        public void actionPerformed(ActionEvent ae)
        {
            // Set the start angle, this increases everytime
            // timer is executed
            plot.setStartAngle(i++);
        }
    });

    // Start the timer (animation starts!)
    t.start();
    }

    public static void main(String args[])
    {
    SwingUtilities.invokeLater(new Runnable(){
        public void run()
        {
        new PieChartExample();
        }
    });
    }
}

Hope you enjoy this, feel free to share this post.

No comments:

Post a Comment