Convert XML to Pie Chart JFreeChart Java Example

In our previous tutorial, we explained how to convert XML to a Bar Chart using JFreeChart Java API. At the end of the example, we mentioned that it is also possible to create a pie chart from input XML using JFreechart. However, we provided only a sample input XML and did not that further. In this example tutorial , we will go a step further and cover how to create a pie chart from XML data in JFreeChart. I assume you are familiar with some basics around JFreeChart, I won't be doing a line by line explanation of the code. You can still refer to the related post section in this tutorial if you are keen to know what each line of code does. We will be using PieDataset class to accept the input data for our chart over a XML data.

XML Data to Pie Chart - Input


The input data that we want to convert to pie chart is provided below:
<?xml version="1.0" encoding="UTF-8"?>
<PieDataset>
  <Item>
    <Key>Italy</Key>
    <Value>67</Value>
  </Item>
  <Item>
    <Key>India</Key>
    <Value>88</Value>
  </Item>
  <Item>
    <Key>China</Key>
    <Value>55</Value>
  </Item>
  <Item>
    <Key>Australia</Key>
    <Value>95</Value>
  </Item>
</PieDataset>

We can pass this data to readPieDatasetFromXML method in the class DatasetReader. This method can accept our XML data as a file or through the InputStream. For this example, we will accept the input through a File. (java.io.File)

Create Pie Chart with XML Data


Once we have obtained the XML into a dataset object, generating a Pie Chart from this output would be a very simple job and will follow the earlier tutorial. Without further explanations, the fully working Java code that converts XML data to Pie Chart using JFreeChart is provided below
import java.io.*;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.chart.ChartFactory; 
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.JFreeChart; 
import org.jfree.chart.ChartUtilities; 
import org.jfree.data.xml.DatasetReader;
import java.io.InputStream;
public class PieChartXMLExample {  
        
        public static void main(String[] args) throws Exception{
                
                DefaultPieDataset my_pie_chart_dataset = new DefaultPieDataset();
                File my_file=new File("xml_pie_chart_input.xml");               
                my_pie_chart_dataset = (DefaultPieDataset) DatasetReader.readPieDatasetFromXML(my_file);
                JFreeChart PieChartObject=ChartFactory.createPieChart("Country Vs Count - Pie Chart Example",my_pie_chart_dataset,true,true,false);             
                int width=640; 
                int height=480;   
                float quality=1; /* Quality factor */
                File BarChart=new File("XML2PieChart.png");              
                ChartUtilities.saveChartAsJPEG(BarChart, quality, PieChartObject,width,height); 
                }               
        }       

XML to Pie Chart - JFreeChart - Output Example


The output chart produced out of the sample code above is provided below:

XML to Pie Chart Java Example
JFreeChart - XML to Pie Chart - Java Example
As explained earlier, you can also convert an InputStream to Pie Chart using the same example but passing InputStream object instead of File. That completes our Pie Chart example in JFreeChart. If you are stuck, or have a question, post it in the comments section below, we will have a look.

1 comment:

  1. Hi,

    Excellent example. I got the following exception initially.

    Exception in thread "main" java.lang.NoClassDefFoundError: org/jfree/util/Public
    Cloneable
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at PieChartXMLExample.main(PieChartXMLExample.java:13)
    Caused by: java.lang.ClassNotFoundException: org.jfree.util.PublicCloneable
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 13 more


    I then found that I'm missing jcommon-1.0.17.jar in my CLASSPATH. After I did this and executed, the code worked like a charm! You solved my day. Thank you.

    ReplyDelete