Search code examples
jasper-reportspie-chart

Pie chart is showing wrong value for key


I am developing a program that create a report with data of diferents persons stored in a database. 

The report is created fine, but the chart isn't correct. I'm trying to show the distribution of the professions. 

This is my report:

enter image description here

As you can see, all the professions have the same count. It's wrong because Cantante and Profesor appear two times.

I define the chart in iReport of the following way:

enter image description here

How to fix it?


Solution

  • Looks like you are using net.sf.jasperreports.chart.pie.ignore.duplicated.key property but forgot to sort data at dataset.

    If you are using groups at JasperReports you should always remember about data sorting.

    Looks like all you need is to sort data by profession field.

    Example built with Jaspersoft Studio 7.1.0

    Datasource

    Using simple csv datasource is enough for example.

    The content of my professions.csv file is very simple. The first row is going for fields names.

    name,age,profession
    Miguel,25,Professor
    Toni,22,Engineer
    Josefa,34,Singer
    Andres,27,Mechanic
    Jony,41,Professor
    Lola,33,Singer
    

    The name of dataadapter at JSS for my example will be professions.csv.

    The report template, jrxml file

    I used main dataset at pie chart for showing information.

    <?xml version="1.0" encoding="UTF-8"?>
    <jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="NotSortedDataChart" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="09483b77-da8e-4dbb-a87d-ae46c27df140">
        <property name="com.jaspersoft.studio.data.defaultdataadapter" value="professions.csv"/>
        <property name="net.sf.jasperreports.chart.pie.ignore.duplicated.key" value="true"/>
        <field name="name" class="java.lang.String"/>
        <field name="age" class="java.lang.String"/>
        <field name="profession" class="java.lang.String"/>
        <sortField name="profession"/>
        <group name="ProfessionGroup">
            <groupExpression><![CDATA[$F{profession}]]></groupExpression>
        </group>
        <columnHeader>
            <band height="15" splitType="Stretch">
                <staticText>
                    <reportElement x="0" y="0" width="120" height="15" uuid="edda065f-1a67-4f94-a2e9-1fc0042e4d32"/>
                    <textElement textAlignment="Left" verticalAlignment="Middle">
                        <font isBold="true"/>
                    </textElement>
                    <text><![CDATA[Name]]></text>
                </staticText>
                <staticText>
                    <reportElement x="120" y="0" width="145" height="15" uuid="95bf7063-fe77-48d6-924a-e0028b8ec0c4"/>
                    <textElement textAlignment="Left" verticalAlignment="Middle">
                        <font isBold="true"/>
                    </textElement>
                    <text><![CDATA[Age]]></text>
                </staticText>
                <staticText>
                    <reportElement x="245" y="0" width="125" height="15" uuid="97f91177-abd3-416d-abb1-e8692f96dc5c"/>
                    <textElement textAlignment="Left" verticalAlignment="Middle">
                        <font isBold="true"/>
                    </textElement>
                    <text><![CDATA[Profession]]></text>
                </staticText>
            </band>
        </columnHeader>
        <detail>
            <band height="15" splitType="Stretch">
                <textField>
                    <reportElement x="0" y="0" width="120" height="15" uuid="b10df174-e36e-402d-ad52-4aa15dbebd49"/>
                    <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement x="120" y="0" width="125" height="15" uuid="5c66bca9-34af-49a6-a4fe-87c4e945f31b"/>
                    <textFieldExpression><![CDATA[$F{age}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement x="245" y="0" width="145" height="15" uuid="d169f529-349c-4c84-b697-680048eabd94"/>
                    <textFieldExpression><![CDATA[$F{profession}]]></textFieldExpression>
                </textField>
            </band>
        </detail>
        <summary>
            <band height="240" splitType="Stretch">
                <pieChart>
                    <chart evaluationTime="Report">
                        <reportElement x="50" y="40" width="200" height="200" uuid="7454b149-6176-4070-8f9b-dd10bbd8f47a"/>
                        <chartTitle/>
                        <chartSubtitle/>
                        <chartLegend/>
                    </chart>
                    <pieDataset>
                        <keyExpression><![CDATA[$F{profession}]]></keyExpression>
                        <valueExpression><![CDATA[$V{ProfessionGroup_COUNT}]]></valueExpression>
                        <labelExpression><![CDATA[$V{ProfessionGroup_COUNT}.toString()]]></labelExpression>
                    </pieDataset>
                    <piePlot>
                        <plot/>
                        <itemLabel/>
                    </piePlot>
                </pieChart>
            </band>
        </summary>
    </jasperReport>
    

    The key features of this example are:

    If not all values of profession field are unique you will get an error:

    net.sf.jasperreports.engine.JRRuntimeException: Key Professor is duplicated in pie dataset.

    The using net.sf.jasperreports.chart.pie.ignore.duplicated.key property allow us to build report even with duplicated values calculated by key expression:

    <keyExpression><![CDATA[$F{profession}]]></keyExpression>
    <valueExpression><![CDATA[$V{ProfessionGroup_COUNT}]]></valueExpression>
    <labelExpression><![CDATA[$V{ProfessionGroup_COUNT}.toString()]]></labelExpression>
    

    Without sorting the data (just remove <sortField name="profession"/> row from jrxml) the result will be wrong, like at your screenshot:

    The result with wrong sorting

    After applying sorting, for example with help of code

    <sortField name="profession"/>
    

    we will get the right result:

    The right result after applying sorting