Create 3D Pie Chart PDF Example iText JFreeChart Java Tutorial

In this post, we will provide a working Java Example code explaining how to create a 3D (3 Dimensional) Pie Chart using JFreeChart and stamp the chart into a PDF document using iText PDF API. This tutorial is similar to the one we described earlier to create a 2D Pie Chart in PDF file. The difference lies in the approach to create the chart, as we will be using a different method to generate the  3D chart. To generate a 3D Chart, the method createPieChart3D of class org.jfree.chart.ChartFactory will be used. The complete Java code for this example is provided below;
/* In this Tutorial, we will write some Java Code to create a 3D Pie chart and stamp the 3D Pie Chart into a PDF document */
/* This Tutorial uses JFreeChart and iText API for creating chart and generating PDF respectively */
import java.io.*;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import org.jfree.data.general.DefaultPieDataset; /* We will use DefaultPieDataset to define the data for the Pie Chart */
import org.jfree.chart.ChartFactory; 
import org.jfree.chart.JFreeChart;
public class PieChart3DExample {  
     public static void main(String[] args){
        try {
                /* To Create a 3D (3 Dimensional) Pie Chart using JFreeChart, we have to define the data range first */
                /* Total products by Brand Sold in the year 2008 */
                DefaultPieDataset my3DPieChartData = new DefaultPieDataset();                
                my3DPieChartData.setValue("Laptops", 1200);
                my3DPieChartData.setValue("HardDisks", 800);
                my3DPieChartData.setValue("Heater", 650);
                my3DPieChartData.setValue("iPAD", 1900);
                my3DPieChartData.setValue("iPhones", 1325);
                /* We have defined the data range for our pie chart, now we can create a 3D Pie Chart */
                /* This method returns a JFreeChart object back to us */                
                JFreeChart PieChart3D=ChartFactory.createPieChart3D("Programming - Pie Chart Example",my3DPieChartData,true,true,false);
                /* Stamping this Pie Chart inside a PDF document involves transforming this JFreeChart into 2D Graphics Object, which iText 
                can accept */                
                int width=540; /* Width of our chart */
                int height=380; /* Height of our chart */
                //Begin by Creating Document Object that will hold the 3D Pie Chart
                Document PieChart3D_PDF=new Document(new Rectangle(width,height));                 
                /* Create PDF Writer Object that will physically write the PDF file to File Output Stream */
                PdfWriter writer=PdfWriter.getInstance(PieChart3D_PDF,new FileOutputStream("Create_3D_Pie_Chart_Using_JFreeChart.pdf"));                
                PieChart3D_PDF.open(); //Open the Document for Writing
                /* Add some Metadata to identify document later */
                PieChart3D_PDF.addTitle("How to Add a 3D Pie Chart to PDF file using iText");
                PieChart3D_PDF.addAuthor("Thinktibits");                
                PieChart3D_PDF.addKeywords("iText,3D PieChart,JFreeChart,PDF,Example Tutorial");
                //To write new content to PDF access the direct content layer first
                PdfContentByte Add_Chart_Content= writer.getDirectContent();
                /* Create a template using the PdfContent Byte object */
                PdfTemplate template_Chart_Holder=Add_Chart_Content.createTemplate(width,height);
                /* Create a 2D graphics object and Rectangle object as before to write on the template */
                Graphics2D Graphics_Chart=template_Chart_Holder.createGraphics(width,height,new DefaultFontMapper());                
                Rectangle2D Chart_Region=new Rectangle2D.Double(0,0,540,380);
                /* Invoke the draw method passing the Graphics and Rectangle 2D object to draw the piechart */
                PieChart3D.draw(Graphics_Chart,Chart_Region);            
                Graphics_Chart.dispose();
                /* Add template to PdfContentByte and then to the PDF document */
                Add_Chart_Content.addTemplate(template_Chart_Holder,0,0);
                /* Close the Document, writer will create a 3D Pie chart inside the PDF document */
                PieChart3D_PDF.close();
        }
        catch (Exception i)
        {
            System.out.println(i);
        }
    }
}
This code produces a 3D Pie Chart inside a PDF document. A sample image that will be created as a chart is provided below;
JFreeChart iText 3D PDF Pie Chart Example
JFreeChart iText 3D PDF Pie Chart Example

2 comments:

  1. Hi,

    Is it not possible to create an exploded 3D Pie Chart with this method?

    /Umai

    ReplyDelete
  2. @Umai, No you cannot create an exploded 3D Pie Chart with this method

    ReplyDelete