JFreeChart Pie Chart Example Java Tutorial

In the previous blog post, we described how to create a pie chart using JFreeChart and stamp the Pie Chart into a PDF document using iText Java library. After I completed the last post, I got fascinated towards the different options available in JFreeChart to beautify my pie chart. And the result, JFreeChart does not let you go disappointed and it provides quite a lot of configuration options for your Pie Chart. So, we will stay away from stamping chart objects in PDF document for a while, and take a look at some of the configuration options available for our chart, when created using JFreeChart. We will try and cover as many options as possible with working Java Code examples and finally provide a tutorial that would explain how to embed these chart objects into a PDF document. As a beginner example, we will discuss how to change the color of our Pie Chart with an example.

This tutorial writes the chart as a JPG file for us and explains how to change the colors of Pie Chart in JFreechart. Follow the complete commented version of Java code which will act as a step by step guide for the reader;
/* This example tutorial demonstrates how to draw and change colors to a Pie chart Using JFreeChart Java API */
import java.io.*;
import org.jfree.data.general.DefaultPieDataset; /* This class is required to define the data for our Pie Chart */
import org.jfree.chart.ChartFactory; /* This class creates the Pie Chart for us */
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot; /* To change pie chart colors */
import org.jfree.chart.ChartUtilities; /* To write the Chart as JPG image file */
import java.awt.Color; /* To specify the color coordinates for the pie chart */
public class ColouredPieChartExample {  
     public static void main(String[] args){
        try {
                /* A data range is required to create a Pie Chart in JFreeChart. So, we will begin with defining a data range using the code below */
                DefaultPieDataset myColoredPieChart = new DefaultPieDataset();
                /* Define Values for the Pie Chart - Programming Languages Percentage Difficulty */
                myColoredPieChart.setValue("Java", 12.9);
                myColoredPieChart.setValue("C++", 37.9);
                myColoredPieChart.setValue("C", 86.5);
                myColoredPieChart.setValue("VB", 80.5);
                myColoredPieChart.setValue("Shell Script", 19.5);
                /* With the dataset defined for Pie Chart, we can invoke a method in ChartFactory object to create Pie Chart and Return a JFreeChart object*/
                /* This method returns a JFreeChart object back to us */                
                JFreeChart myColoredChart=ChartFactory.createPieChart("Programming - Colored Pie Chart Example",myColoredPieChart,true,true,false);                
                /* Once we have got the JFreeChart object, it is time to change the default colors for the Pie Chart */
                /* PiePlot class helps to change the colors for us, defined in org.jfree.chart.plot.PiePlot */
                /* getPlot method of JFreeChart class returns the PiePlot object back to us */                
                PiePlot ColorConfigurator = (PiePlot)myColoredChart.getPlot();
                /* We can now use setSectionPaint method to change the color of our chart */
                ColorConfigurator.setSectionPaint("Java", new Color(160, 160, 255));
                ColorConfigurator.setSectionPaint("C++", Color.ORANGE);
                ColorConfigurator.setSectionPaint("C", Color.GREEN);
                ColorConfigurator.setSectionPaint("VB", Color.PINK);
                ColorConfigurator.setSectionPaint("Shell Script", Color.YELLOW);
                /* We will now write the chart as a JPEG file. TO do this we will use the ChartUtilities class. org.jfree.chart.ChartUtilities */                
                int width=640; /* Width of the image */
                int height=480; /* Height of the image */
                /* Note on Chart Quality Factor : java.lang.IllegalArgumentException: The 'quality' must be in the range 0.0f to 1.0f */
                float quality=1; /* Quality factor */
                File PieChart=new File("my_pie_chart.jpg");
                /* Convert JFreeChart to JPEG File Using Code below */
                ChartUtilities.saveChartAsJPEG(PieChart, quality, myColoredChart,width,height); 
        }
        catch (Exception i)
        {
            System.out.println(i);
        }
    }
}
Now, when you run this example code you get a nice colorful pie chart in the output as a JPG file. A sample output of the chart is shown below;

JFreeChart - Pie Chart Color - Example Tutorial
JFreeChart - Pie Chart Color - Tutorial
Now, you have to note that you can push this JFreeChart object into a PDF file as described in our earlier tutorials.Using java.awt.Color gives you a lot of color options for your chart. Go ahead and color your pie charts..[ A PDF integration of this chart will be provided in a later tutorial ]

No comments:

Post a Comment