Jump to content
Changes to the Jaspersoft community edition download ×

Dynamic chart generation (JfreeChart)

2005 IR Help

Recommended Posts

By: mainsys - mainsys

Dynamic chart generation (JfreeChart)

2004-04-20 01:21

I need to generate dynamically a pie chart (with JfreeChart) in a subreport.


The master report use a DOMElementDatasource (source provided in the paches area http://sourceforge.net/tracker/index.php?func=detail&aid=734738&group_id=36382&atid=416705). To get an instance of the DOMElementDatasource, I pass an Element to the constructor. This Element is created by parsing a xml file with Xerces and by using the method getDocumentElement() of the generated DOM document.




My xml file :



<title>Sample Chart</title>


<label>category 1</label>




<label>category 2</label>




<label>category 3</label>







The call to jasper :


Document document = null;

try {

DocumentBuilderFactory factory =


DocumentBuilder builder = factory.newDocumentBuilder();

document = builder.parse(fileNameXml);

} catch (FactoryConfigurationError e) {

System.err.println("unable to get a document builder factory, " + e.getMessage());

} catch (ParserConfigurationException e) {

System.err.println("parser was unable to be configured, " + e.getMessage());

} catch (SAXException e) {

System.err.println("parsing error, " + e.getMessage());

} catch (IOException e) {

System.err.println("i/o error, " + e.getMessage());


JasperFillManager.fillReportToFile("report.jasper", null, (new JRDOMElementDataSource(document.getDocumentElement())));





To generate dynamically a chart, I pass to my subreport a subdatasource (based on my master datasource) using the following datasource expression :






In my subreport, I have defined two fields in the detail band to get access to field values (the category and the value) within a scriptlet. Then in the scriptlet of my subreport, I get these field values within the method afterDetailEval() and add them to a pie chart dataset. Finally, the chart is generated at report evaluation time by calling a specific method of the scriptlet, getChartImage().


Scriptlet source :


public class ChartScriptlet extends JRDefaultScriptlet



DefaultPieDataset data = new DefaultPieDataset();


public void afterDetailEval() throws JRScriptletException


String category = (String) this.getFieldValue("category");

String sValue = (String)this.getFieldValue("value");

BigDecimal value = new BigDecimal(sValue);

data.setValue(category, value);



public BufferedImage getChartImage() throws JRScriptletException


JFreeChart chart = ChartFactory.createPieChart("Sample Chart",






return chart.createBufferedImage(400, 400);







Everything is working fine but I've not figured out how to avoid hardcoding the title of the chart in my subreport scriptlet. In fact, the title of pie chart is specified in the datasource of my master report but I don't know how to get the title value from my master datasource and pass it to the subreport (i.e. as a parameter).


Thanks in advance for your help,


Bertrand GILLIS

Link to comment
Share on other sites

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...