Planet Jaspersoft

March 11, 2015

Software : 
Jasper Design Studio : 6.0.1 Pro
Jasper Server : 6.0.1 Pro

1) Calculating Percentage from java.lang.BigDecimal Types

By default all the numeric fields in report takes java.math.BigDecimal & you can not perform arithmetic  operations like division for calculating percentages. (Alternatively you need to work with the methods from BigDecimal class to get % kind of things)

Find percentage : 
1) Create a variable  of type java.lang.Double
2) Calculation = Nothing
3) Expression :
      $F{Field1}.doubleValue()/$F{Filed2}.doubleValue())*100  

    NOTE : $F{Field1} and $F{Filed2} are of java.math.BigDecimal types .
4) Increment type : None
5) Reset group :  If you are creating variables in any data set & using it in table component give your GROUP Name

6) Drag the variable on to the design area of table component.
7) Double click the variable to open it's  expression
     a) By default you can see it's expression as $V{Variable_1}
     b) Change the variable expression  to
              new DecimalFormat("#.##").format(Double.valueOf($V{Variable_1}))+"%"

2) Two decimal places after floating point for java.math.BigDecimal type values

  $V{Variable_2}.setScale(2, java.math.RoundingMode.HALF_UP)+"%"
This is on dragged variable. Not in the variable directly. The variable initially $V{Variable_2}. Double click it to get it's expression editor

Example: 95.83%

NOTE : 
a) Create a variable of double type for finding %'s .
                   i.e.,
                       Variable Name : VAR_PER_1
                      Expression :  (a.doubleValue()/b.doubleValue()) *100
                      Example : 95.834444123

b)  Now convert the above variable to Decimal type by creating a new variable of type Decimal
                 i.e., Variable Name : VAR_PER_2
                 BigDecimal.valueOf($V{VAR_PER_1})    [Remember it VAR_PER_1]

c) Now get 2 decimal places  by applying   $V{VAR_PER_2}.setScale(2, java.math.RoundingMode.HALF_UP)+"%"


3) Get only  Numeric value as percentage by rounding the decimal places of Double value

Assume you have java.lang.Double type having 45.45 and want to get 46 from it. Then the below expression will help full to you.

Variable_1=45.45                                              of java.lang.Double type
Variable_2= (int) ($V{Variable_1} + 0.5)        of java.lang.Integer type

[i.e., you need to create another variable of integer type and in its expression type cast the double variable to integer]

There could be another ways to solve the same kind of solutions using java expressions but I hope somehow the above tips help full.

Thanks for reading this page & suggestions and improvements to this page are welcome in comments box :-)

:-) SADAKAR POCHAMPALLI :-)
BI DEVELOPER, HYDERABAD, INDIA.

March 11, 2015

March 5, 2015

1) Style
2) Condition-1
3) Condition-2
4) On the Text Field
5) Output
6) JRXML

1) Style

2) Condition-1
3) Condition-2
4)  On the Text Field

5) Output
6) JRXML

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.0.0.final using JasperReports Library version 6.0.0  -->
<!-- 2015-03-06T13:13:06 -->
<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="Blank_A4_22" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="ab524a87-197a-4dac-b294-ba81b7cb1409">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="foodmart"/>
    <style name="Style1">
        <conditionalStyle>
            <conditionExpression><![CDATA[$F{unitsales}.doubleValue()>=0 && $F{unitsales}.doubleValue()<=1000]]></conditionExpression>
            <style backcolor="#7679B0"/>
        </conditionalStyle>
        <conditionalStyle>
            <conditionExpression><![CDATA[$F{unitsales}.doubleValue()>1000 && $F{unitsales}.doubleValue()<=3000]]></conditionExpression>
            <style backcolor="#EBEDA4"/>
        </conditionalStyle>
    </style>
    <queryString>
        <![CDATA[SELECT c.city, SUM(unit_sales) UnitSales FROM customer c, sales_fact_1997 sf7 WHERE
c.customer_id=sf7.customer_id GROUP BY c.city]]>
    </queryString>
    <field name="city" class="java.lang.String"/>
    <field name="unitsales" class="java.math.BigDecimal"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="32" splitType="Stretch"/>
    </title>
    <pageHeader>
        <band height="35" splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="36" splitType="Stretch">
            <staticText>
                <reportElement x="0" y="0" width="278" height="30" uuid="621ecd38-7026-4d55-9afe-6a1fa2c37613"/>
                <text><![CDATA[city]]></text>
            </staticText>
            <staticText>
                <reportElement x="278" y="0" width="277" height="30" uuid="2c237fac-2804-4031-a064-a666b37fd164"/>
                <text><![CDATA[unitsales]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="46" splitType="Stretch">
            <textField>
                <reportElement x="0" y="13" width="278" height="30" uuid="a448fd0f-d6b7-45ef-8915-3250c06d178a"/>
                <textFieldExpression><![CDATA[$F{city}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement style="Style1" mode="Opaque" x="278" y="13" width="277" height="30" uuid="2040c97e-9bd0-4c05-b515-a06f60b315af"/>
                <textFieldExpression><![CDATA[$F{unitsales}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <columnFooter>
        <band height="45" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="54" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band height="42" splitType="Stretch"/>
    </summary>
</jasperReport>

March 5, 2015

About

Planet Jaspersoft aggregates blog posts from our community. If you would like your blog to be included in the Planet, please follow this help guide. Or just click this link to go straight to your Planet Feeds.

Feedback
randomness