Jasper Report (iReport) Pie Dataset from main dataset

datasetireportjasper-reports

I want to use a pie chart per group in Jasper iReport, but I don't know how to create a dataset per group.

Assume I have a table as follows:

Table users
---------
category - user - status

Then I have a the following SQL

select * from users order by category

My display has a simple group band of category with detail showing each user and their status. Ideally I would have each category group header show a pie chart with all the distinct statuses and their counts within that group. I'm at a loss as how to do this in iReport.

If I knew all the categories ahead of time I could just create a dataset for each one using the following:

select status, count(user) from user where category = <current category> group by status

I'm attempting to do this using a JRMapCollectionDataSource that resets each group and pulls the data from the rows in the group using a variable, but I'm not very familiar with the whole reset group and increment paradigm.

Any help?

Best Answer

You can solve this issue without programming following this steps:

  1. sort data by category field and by user field (add sorting via iReport or adding ORDER BY clause to the query in case having deal with jdbc datasource);
  2. add two groups: the 1st group - for category field and the 2nd group - for user field (via iReport). Note: It is not necessarily to add GROUP BY clause to the query;
  3. add Pie Chart component to the Group Header band for the category field;
  4. set evaluationTime and evaluationGroup properties for this pieChart.

Here is working sample for demonstrating how to build Pie Chart. In this sample I've used the csv file as datasource.

The chart shows the quantity of users for every category. I used variable usersCount for counting users in every group, but it is was possible to use built-in variable $V{userGroup_COUNT}.

Here is my jrxml file:

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport ...>
    <field name="category" class="java.lang.Integer"/>
    <field name="user" class="java.lang.String"/>
    <field name="status" class="java.lang.String"/>
    <sortField name="category"/>
    <sortField name="user"/>
    <variable name="usersCount" class="java.lang.Integer" resetType="Group" resetGroup="userGroup" calculation="Count">
        <variableExpression><![CDATA[$F{user}]]></variableExpression>
        <initialValueExpression><![CDATA[0]]></initialValueExpression>
    </variable>
    <group name="categoryGroup">
        <groupExpression><![CDATA[$F{category}]]></groupExpression>
        <groupHeader>
            <band height="135">
                <pieChart>
                    <chart evaluationTime="Group" evaluationGroup="categoryGroup">
                        <reportElement x="158" y="0" width="200" height="135"/>
                        <chartTitle/>
                        <chartSubtitle/>
                        <chartLegend/>
                    </chart>
                    <pieDataset>
                        <dataset resetType="Group" resetGroup="categoryGroup" incrementType="Group" incrementGroup="userGroup"/>
                        <keyExpression><![CDATA[$F{user}]]></keyExpression>
                        <valueExpression><![CDATA[$V{usersCount}]]></valueExpression>
                        <labelExpression><![CDATA[String.valueOf($V{usersCount})]]></labelExpression>
                    </pieDataset>
                    <piePlot>
                        <plot/>
                        <itemLabel/>
                    </piePlot>
                </pieChart>
                <textField>
                    <reportElement x="0" y="0" width="100" height="20"/>
                    <textElement/>
                    <textFieldExpression><![CDATA["Category: " + $F{category}]]></textFieldExpression>
                </textField>
            </band>
        </groupHeader>
    </group>
    <group name="userGroup">
        <groupExpression><![CDATA[$F{user}]]></groupExpression>
    </group>
    <title>
        <band height="79" splitType="Stretch"/>
    </title>
</jasperReport>

Here is my csv file (data file - datasource):

category,user,status
1,user1,WORK
1,user1,HOLIDAY
1,user2,SICK
2,user1,WORK
2,user2,WORK
2,user2,HOLIDAY
2,user3,HOLIDAY
2,user4,SICK
3,user1,HOLIDAY
3,user3,HOLIDAY
3,user5,HOLIDAY
4,user1,WORK
4,user1,HOLIDAY
4,user2,SICK
4,user2,WORK
4,user2,SICK
4,user3,HOLIDAY
4,user4,HOLIDAY
4,user5,HOLIDAY

The result will be (via iReport preview):

The result

Related Topic