Convert XML to Chart JFreeChart Java Example

In the previous post, we explained how to create a bar chart over a JDBC connection with database. Upon completion of the post, I got a few questions about creating bar chart from a XML file. I thought I would explain this over a tutorial and in this example, let us look how to use JFreechart Java API to create bar chart with the input coming from XML file.

JFreeChart library has built in methods to accept XML inputs over a file for both category based charts and pie charts. However, to generate chart out of XML content, you need to make sure that the input XML is in a format that can be accepted by the API. You can convert the input XML to a PieDataset object or a CategoryDataset object. The PieDataset object is useful to create Pie Charts and the CategoryDataset object can be used for other type of charts. This example, we will focus on CategoryDataset object.

XML Data to Bar Chart - Input


A sample input XML that you can use for the example is provided below:

<?xml version="1.0" encoding="UTF-8"?>
<CategoryDataset>
  <Series name = "Marks in Various Subjects">
    <Item>
      <Key>English</Key>
      <Value>15.4</Value>
    </Item>
    <Item>
      <Key>Maths</Key>
      <Value>12.7</Value>
    </Item>
    <Item>
      <Key>Physics</Key>
      <Value>5.7</Value>
    </Item>
    <Item>
      <Key>Biology</Key>
      <Value>9.1</Value>
    </Item>
  </Series>
</CategoryDataset>

Create bar chart with XML data

Once you have your input data in the format provided above, generating bar chart is very easy. You need to use the method readCategoryDatasetFromXML to read the input data. This can be a file or inputstream. We will provide an example for a File based reading in this tutorial. You can also take your input as a string and convert it to input stream to create the chart.  The method converts the XML into a DefaultCategoryDataset object. Once this is done, you can pass this to createbarchart method and create a logical chart object, which can be written to a file as JPEG or PNG image. (Refer to our earlier tutorials for a detailed explanation on this)

The complete Java code to convert XML data to chart using JFreeChart is provided below:

import java.io.*;
import org.jfree.data.category.DefaultCategoryDataset; 
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 BarChartXMLExample {  
        
        public static void main(String[] args) throws Exception{
                
                DefaultCategoryDataset my_bar_chart_dataset = new DefaultCategoryDataset();
                File my_file=new File("xml_chart_input.xml");           
                my_bar_chart_dataset = (DefaultCategoryDataset) DatasetReader.readCategoryDatasetFromXML(my_file);
                JFreeChart BarChartObject=ChartFactory.createBarChart("Subject Vs Marks - Bar Chart","Subject","Marks",my_bar_chart_dataset,PlotOrientation.VERTICAL,true,true,false);                  
                int width=640; 
                int height=480;                
                File BarChart=new File("XML2Chart.png");              
                ChartUtilities.saveChartAsPNG(BarChart,BarChartObject,width,height); 
                }               
        }

This example requires the following JAR files: jfreechart-1.0.14.jar and jcommon-1.0.17.jar. The chart output produced by this example is given below:

XML to Chart - JFreeChart - Output Example

Java Bar Chart from XML input
JFreeChart - XML to Chart Example

XML to Pie Chart in Java


If you want to create a Pie Chart from an input XML you need to use readPieDatasetFromXML method and pass XML data as a File or InputStream. This way you can create a PieDataset and use this as an input to create the chart. Check our website where it explains how to create PieChart in JFreechart from PieDataset object.

The input XML that you need to supply to create a Pie Chart is different to the XML provided above. Use the input provided here as an example

<?xml version="1.0" encoding="UTF-8"?>
<PieDataset>
  <Item>
    <Key>Maths</Key>
    <Value>74</Value>
  </Item>
  <Item>
    <Key>Physics</Key>
    <Value>35</Value>
  </Item>
  <Item>
    <Key>Chemistry</Key>
    <Value>67</Value>
  </Item>
  <Item>
    <Key>Biology</Key>
    <Value>91</Value>
  </Item>
</PieDataset>

That completes this part of the tutorial. Hope you find it how easy to convert XML to charts in Java JfreeChart. If you are stuck, or finding any exceptions, post it in the comments section of this blog. We will have a look and resolve it.

2 comments:

  1. hi, your explanations are very clear. Helped me getting hands on with JFreeChart. However I am stucked on using XML file externally. I would like to import via a JFileChooser but I seem to get the way to handle pass and make it an URL to get resource from wrong.

    Any explanations/examples would be much appreciated! Thanks and keep up with nice tutorials:)

    ReplyDelete
  2. hi, i want to create chart without using any jars from xml file data.

    Any example/explanations would be much appreciated!

    thanks in advance

    ReplyDelete