Jump to content

Calculating Percent of Total for each row


gstephens

Recommended Posts

Each row of a report has a category with a value.  I'd like to display on each row, the percentage of the total that the value represents:

CatA    10%    100
CatB    78%    780
CatC    12%    120

Total        1,000

I can't get the percentage field to calculate correctly.  I've tried setting different evaluationTime's for the textfield.

    <variable name="cat_PCT" class="java.lang.Float" resetType="Group" resetGroup="cat_class">
        <variableExpression><![CDATA[$V{cat_value}.doubleValue()/$V{total_cat}.doubleValue()]]></variableExpression>
    </variable>


    <variable name="total_cat" class="java.math.BigDecimal" calculation="Sum">
        <variableExpression><![CDATA[$F{cat_value}.plus()]]></variableExpression>
    </variable>

<textField evaluationTime="Auto" pattern="#,##0.0 %">
  <reportElement x="415" y="0" width="39" height="20"/>
  <textElement textAlignment="Right"/>
  <textFieldExpression class="java.lang.Float"><![CDATA[$V{cat_PCT}]]></textFieldExpression>
</textField>

<textField evaluationTime="Report">
  <reportElement x="254" y="0" width="100" height="20"/>
  <textElement textAlignment="Right"/>
  <textFieldExpression class="java.math.BigDecimal"><![CDATA[$V{total_cat}]]></textFieldExpression>
</textField>

 

 

Link to comment
Share on other sites

  • Replies 4
  • Created
  • Last Reply

Top Posters In This Topic

The First thing you must do it's create a variable that calculates the total, and later create a text field that divide the individual value of each category and total (in the variable you created) and set the evaluation time to auto.

 

 

Link to comment
Share on other sites

total_cat (show above) is a variable I created to calculate the total.  cat_PCT is the variable where I divide the individual value into the category (however it's a float, not a string as you suggested).  If I change it to a string I get an "error evaluating expression".

Link to comment
Share on other sites

I found an example of what jahp00 was referring to in the ShipmentsReport.jrxml sample included with iReport and it worked. Thanks.

<textFieldExpression class="java.lang.String"><![CDATA[msg("{0,number,##0.0%}", new Float($V{cat_value}.floatValue() / $V{total_cat}.floatValue()))]]></textFieldExpression>

 



Post Edited by gstephens at 10/18/2009 15:39
Link to comment
Share on other sites

  • 11 months later...

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...