Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JasperReport Pie Chart Example

I am brand new to JasperReports and am slowly fighting my way through the basics. I have a situation where I do not want to fill a pie chart with DB-driven data (through a so-called datasource). I want to supply all the information necessary to fill the pie chart from a Java hashmap passed into the JasperFillManager at runtime.

This would include parameters to label the slices in the pie chart, set their colors, and define their weights/values (size of the slices). So, at some point in my Java code, I would be writing something like:

HashMap<String,Object> jrParams = new HashMap<String,Object>();

jpParams.put("slice_1_label", "Red Team");
jpParams.put("slice_1_color", Color.RED);
jpParams.put("slice_1_value", 67.0);
jpParams.put("slice_2_label", "Blue Team");
jpParams.put("slice_2_color", Color.BLUE);
jpParams.put("slice_2_value", 33.0);

// ... some other code

JasperFillManager.fillReport(jasperDesign, jrParams);

The goal I am trying to achieve here would be to have a pie chart with 2 slices; a red "Red Team" slice taking up 67% of the pie, and a blue "Blue Team" slice takig up 33%.

I now need help "connecting the dots" between my hashmap and the JRXML/JasperDesign.

Can someone either show me (or just help guide me) towards what sort of <pieChart> JRXML I would need to write in order to have my jrParam hashmap fill the pie chart with runtime parameters? I have made a best-attempt below but am just struggling on making total sense of it all.

<pieChart>
    <chart isShowLegend="true">
        <reportElement x="10" y="10" width="300" height="300"/>
        <chartTitle>
            <titleExpression><![CDATA[My First JR Pie Chart]]></titleExpression>
        </chartTitle>
    </chart>
    <pieDataset>

        <!-- Here is where I believe I need to put my two slices; not sure how -->

    </pieDataset>
    <piePlot>
        <plot backcolor="#8BA870"/>
        <itemLabel color="#000000"/>
    </piePlot>
</pieChart>

Thanks in advance for any help/clarification!

like image 814
IAmYourFaja Avatar asked Nov 10 '11 17:11

IAmYourFaja


1 Answers

zharvey,

Since you are pretty new to JasperReport i am hoping you are using the iReport Designer tool. Design a pie chart from one of the sample templates that the iReport designer has and then try to study the generated JRXML. There is a very easy sample you can look at when going to iReport->Help->Samples->Charts

My second tip for you is to use java beans or POJOs as a datasource (simply based on the code you posted). I am looking at your sample code where you are creating a Map of String label, color and values. Wouldn't be nice and easy if you had a plain old java bean class with all these three properties as instance variable? It would be easy for your report as well, because you can create a collection of bean objects which can easily be accessed and used via the JRBeanDataSource. I feel it will make your code a little cleaner and easier to maintain. Regards!

like image 104
Mechkov Avatar answered Sep 25 '22 21:09

Mechkov