Displaying Percentage In Pie Chart Labels

To display percentage for a pie chart slice, pie-slice-value over sum-of-all-slice-values, user needs to provide the sum tally prior to the first query row is fetched from the charts data source. User can either use the sum function in the query to retrieve this tally if chart is using main data source,  or summarize the tally in the main report and pass this value through the sub data set parameter if chart is using a sub data set.

The attached report zip file contains a sample report using sub dataset to feed the charts. The sample report lists all organizational users from repository profile database in the report detail band, and displays user counts per organization in pie charts with percentage information displayed in the summary band. The total user count is summarized in the main report variable using “Count” function:

<variable calculation="Count" class="java.lang.Integer" name="totalUsers">
    <variableexpression>
        <!--[CDATA[$F{username}]]-->
    </variableexpression>
</variable>

And this field is passed through sub dataset parameter and used to generate the percentage display in the sub datset variable field:

<subdataset name="ChartData" uuid="a99d5294-19f3-42f0-8e23-a76189b7ad1a">
    <parameter class="java.lang.Integer" isforprompting="false" name="totalUsers">
    ……
    <variable class="java.lang.String" name="percentage">
        <variableexpression>
            <!--[CDATA[(new BigDecimal($V{tenantname_COUNT}.intValue())
                            .divide(new BigDecimal($P{totalUsers}.intValue()),4,BigDecimal.ROUND_HALF_UP)
                            .multiply(new BigDecimal(100))
                            .divide(new BigDecimal("1"),2,BigDecimal.ROUND_HALF_UP))+"%"]]-->
        </variableexpression>
    </variable>
    ……
    </parameter>
</subdataset>

The charts will use this sub dataset to generate pie chart slices where the percentage information can be displayed using the variable. This report renders a pie chart upon user's selection from three charts in the last page of the report summary band. The followings are the configuration information for there different charts

Pie 3D Chart

<pie3dchart>
   <chart>
      <reportelement uuid="0e096585-658a-4094-99d6-74e072f64ac0"
                     height="572" width="572" x="0" y="0">
         <printwhenexpression>
            <!--[CDATA[$P{chartType}.intValue()==1]]-->
         </printwhenexpression>
      </reportelement>
      <charttitle></charttitle>
      <chartsubtitle></chartsubtitle>
      <chartlegend></chartlegend>
   </chart>
   <piedataset>
      <dataset>
         <datasetrun subdataset="ChartData"
                     uuid="3e9d1aa3-1962-4e14-b601-64a54c42c731">
            <datasetparameter name="totalUsers">
               <datasetparameterexpression>
                  <!--[CDATA[$V{totalUsers}]]-->
               </datasetparameterexpression>
            </datasetparameter>
            <datasetparameter name="query">
               <datasetparameterexpression>
                  <!--[CDATA[$P{query}]]-->
               </datasetparameterexpression>
            </datasetparameter>
            <connectionexpression>
               <!--[CDATA[$P{REPORT_CONNECTION}]]-->
            </connectionexpression>
         </datasetrun>
      </dataset>
      <keyexpression>
         <!--[CDATA[$F{tenantname}]]-->
      </keyexpression>
      <valueexpression>
         <!--[CDATA[$V{tenantname_COUNT}]]-->
      </valueexpression>
      <labelexpression>
         <!--[CDATA[$V{tenantname_COUNT}+": "+$V{percentage}]]-->
      </labelexpression>
      <sectionhyperlink>
         <hyperlinktooltipexpression>
            <!--[CDATA[$F{tenantname}+": "+$V{tenantname_COUNT}+", "+$V{percentage}]]-->
         </hyperlinktooltipexpression>
      </sectionhyperlink>
   </piedataset>
   <pie3dplot>
      <plot>
         <itemlabel></itemlabel>
      </plot>
   </pie3dplot>
</pie3dchart>

Chart Pro (Pie3D)

<componentelement>
   <reportelement uuid="5f12a7ea-6e24-49da-986f-8a852b6466f5"
                  height="572" width="572" x="0" y="0">
      <printwhenexpression>
         <!--[CDATA[$P{chartType}.intValue()==2]]-->
      </printwhenexpression>
   </reportelement>
   <fc:chart type="Pie3D"
             xmlns:fc="http://jaspersoft.com/fusion"
             xsi:schemalocation="http://jaspersoft.com/fusion http://jaspersoft.com/schema/fusion.xsd">
      <fc:chartproperty name="showLegend">
               <fc:propertyexpression>
                  <!--[CDATA[Boolean.FALSE]]-->
               </fc:propertyexpression>
      </fc:chartproperty>
      <fc:chartdataset>
         <dataset incrementgroup="tenantname"
                  incrementtype="Group">
            <datasetrun subdataset="ChartData"
                        uuid="3f2c351c-0dca-42c7-8006-6bcf2932296d">
               <datasetparameter name="query">
                  <datasetparameterexpression>
                     <!--[CDATA[$P{query}]]-->
                  </datasetparameterexpression>
               </datasetparameter>
               <datasetparameter name="totalUsers">
                  <datasetparameterexpression>
                     <!--[CDATA[$V{totalUsers}]]-->
                  </datasetparameterexpression>
               </datasetparameter>
               <connectionexpression>
                  <!--[CDATA[$P{REPORT_CONNECTION}]]-->
               </connectionexpression>
            </datasetrun>
         </dataset>
         <fc:item>
            <fc:seriesexpression>
               <!--[CDATA[""]]-->
            </fc:seriesexpression>
            <fc:labelexpression>
               <!--[CDATA[$F{tenantname}]]-->
            </fc:labelexpression>
            <fc:valueexpression>
               <!--[CDATA[$V{tenantname_COUNT}]]-->
            </fc:valueexpression>
            <fc:displayvalueexpression>
               <!--[CDATA[$F{tenantname}+": "+$V{tenantname_COUNT}+", "+$V{percentage}]]-->
            </fc:displayvalueexpression>
            <fc:hyperlink>
               <hyperlinktooltipexpression>
                  <!--[CDATA[$F{tenantname}+": "+$V{tenantname_COUNT}+", "+$V{percentage}]]-->
               </hyperlinktooltipexpression>
            </fc:hyperlink>
         </fc:item>
      </fc:chartdataset>
   </fc:chart>
</componentelement>

HighCharts/HTML5 Chart (Pie)

<componentElement>
    <reportElement x="0" y="0" width="572" height="572" uuid="36f7ea2d-b489-4ce5-8b38-d318778f4bc9">
        <printWhenExpression><![CDATA[$P{chartType}.intValue()>2]]></printWhenExpression>
    </reportElement>
    <hc:chart xmlns:hc="http://jaspersoft.com/highcharts" xsi:schemaLocation="http://jaspersoft.com/highcharts http://jaspersoft.com/schema/highcharts.xsd" type="Pie">
        <hc:chartSetting name="default">
            <hc:chartProperty name="title.text" value=""/>
            <hc:chartProperty name="credits.text" value=""/>
            <hc:chartProperty name="credits.enabled" value="false"/>
            <hc:chartProperty name="credits.href" value=""/>
            <hc:chartProperty name="yAxis.title.text" value=""/>
            <hc:chartProperty name="plotOptions.pie.showInLegend" value="true"/>
            <hc:chartProperty name="tooltip.useHTML">
                <hc:propertyExpression><![CDATA[Boolean.TRUE]]></hc:propertyExpression>
            </hc:chartProperty>
            <hc:chartProperty name="tooltip.pointFormat">
                <hc:propertyExpression><![CDATA["<b>{point.y}</b>"]]></hc:propertyExpression>
            </hc:chartProperty>
        </hc:chartSetting>
        <multiAxisData>
            <multiAxisDataset>
                <dataset>
                    <datasetRun subDataset="ChartData_HTML5" uuid="26fc5cda-189b-4262-ae60-2c2c39bd4237">
                        <datasetParameter name="totalUsers">
                            <datasetParameterExpression><![CDATA[$V{totalUsers}]]></datasetParameterExpression>
                        </datasetParameter>
                    </datasetRun>
                </dataset>
            </multiAxisDataset>
            <dataAxis axis="Rows">
                <axisLevel name="tenantname">
                    <labelExpression><![CDATA["tenantname"]]></labelExpression>
                    <axisLevelBucket class="java.lang.Comparable">
                        <bucketExpression><![CDATA[$F{tenantname}.toUpperCase()+"-"+$V{percentage}]]></bucketExpression>
                    </axisLevelBucket>
                </axisLevel>
            </dataAxis>
            <multiAxisMeasure name="Measure1" class="java.lang.Integer" calculation="Nothing">
                <labelExpression><![CDATA[$F{tenantname}]]></labelExpression>
                <valueExpression><![CDATA[$F{tenantname_count}]]></valueExpression>
            </multiAxisMeasure>
        </multiAxisData>
        <hc:series name="Measure1"/>
    </hc:chart>
</componentElement>

User can download “sample-chart-report.zip” file from the attachment and import it into JasperReports Server 5.x repository. The report unit “23. Chart Report with Percentage Display” can be tested under Public/Samples/Reports repository folder.


(TTC)

Feedback
randomness