Search code examples
jasper-reports

How can I use parameter for lineColor in JasperReports?


I would like to declare a parameter/variable in JasperReports to pass in for color hex code. So far I have not been able to.

for example :lineColor="#215995" is repeated 180 times. And it is one of the 3 mains colours I have used in this report. If this is to change then I can replace all or just edit the parameter/variable. but so far nothing has worked.

<box>
    <pen lineWidth="0.25" lineColor="#215995"/>
    <topPen lineWidth="0.25" lineStyle="Solid" lineColor="#215995"/>
    <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
    <bottomPen lineWidth="0.25" lineStyle="Solid" lineColor="#215995"/>
    <rightPen lineWidth="0.25" lineStyle="Solid" lineColor="#215995"/>
</box>

This is what I have tried:

<parameter name="COMPANY_BRANDED_COLOUR_DARK_MAIN_BLUE" class="java.lang.String">
    <defaultValueExpression><![CDATA["#215995"]]></defaultValueExpression>
</parameter>

and replaced lineColor="#215995"/> to lineColor="$P{COMPANY_BRANDED_COLOUR_DARK_MAIN_BLUE}"/> but did not worked.

Tried Googling, AI, documentation....


Solution

  • You can use a parameter to set the border color for a specific element via properties such as net.sf.jasperreports.style.box.top.pen.lineColor. Like this:

            <textField>
                <reportElement style="Sans_Normal" mode="Opaque" x="0" y="0" width="555" height="90" backcolor="#DDDDDD" uuid="fd7b67ac-fe76-4008-b276-ca666fd05561">
                    <propertyExpression name="net.sf.jasperreports.style.box.top.pen.lineColor"><![CDATA[$P{COMPANY_BRANDED_COLOUR_DARK_MAIN_BLUE}]]></propertyExpression>
                </reportElement>
                ...
    

    But you'll have to do this for every element.

    One alternative approach is to create one or more styles, either local the report or in an independent style template file, and set the border color in the style. Then when you want to change the border color you will only have to change in the style(s) and not for each individual report element.