Conditional Background Color - Style

0

Using iReports 3.7.5 and trying to give a database field element a different background depending on it's value
Been reading up on it, but can't get it to work. This is what it's using for the style and cell right now

 

 

Code:
<style name="Background_Style">
    <conditionalStyle>
        <conditionExpression><![CDATA[new Boolean($F{Class_Code}.toUpperCase().equals("PC"))]]></conditionExpression>
        <style mode="Transparent" backcolor="#FF0000"/>
    </conditionalStyle>
</style>
 
<textField isStretchWithOverflow="true">
    <reportElement style="Background_Style" mode="Transparent" x="432" y="20" width="86" height="20" backcolor="#FFFFFF"/>
    <textElement markup="none">
        <font size="14" isBold="true"/>
    </textElement>
    <textFieldExpression class="java.lang.String"><![CDATA[$F{Class_Code}]]></textFieldExpression>
</textField></td></tr></tbody></table>
mlg's picture
mlg
5
Joined: Oct 27 2010 - 7:16am
Last seen: 8 years 6 months ago

12 Answers:

1

I often run into that problem, and it's usually because when I select a displayed field and select the style from the dropdown, I forget to also click the "opaque" box that's right above that dropdown.

Carl

cbarlow3's picture
1387
Joined: Mar 4 2010 - 8:59am
Last seen: 3 months 2 weeks ago
0
even if I select opaque the box still doesn't change color.
mlg's picture
mlg
5
Joined: Oct 27 2010 - 7:16am
Last seen: 8 years 6 months ago
-1

I didn't notice before that there are actually TWO places that have the "opaque" checkbox.  If I have the style itself selected in the report inspector, the "opaque" checkbox in the properties pane doesn't seem to help at all.  If I select the displayed field itself, either in the design pane or in the detail section of the report inspector, that opaque checkbox (which is right above the dropdown where you pick the style for that displayed field) DOES work for me.  I've pasted the jrxml for a very simple report (and attached a screenshot of the result) so you can see if you can figure out what the salient difference is.

Good luck!

Carl

Code:
<?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 <a href="http://jasperreports.sourceforge.net/xsd/jasperreport.xsd"" target="_blank">http://jasperreports.sourceforge.net/xsd/jasperreport.xsd"</a> name="ConditionalBackgroundTest" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
	<property name="ireport.zoom" value="1.0"/>
	<property name="ireport.x" value="0"/>
	<property name="ireport.y" value="0"/>
	<style name="BandedTable" isDefault="false">
		<conditionalStyle>
			<conditionExpression><![CDATA[$V{REPORT_COUNT}%2==1]]></conditionExpression>
			<style isDefault="false" style="BandedTable" backcolor="#99FF99"/>
		</conditionalStyle>
	</style>
	<queryString>
		<![CDATA[SELECT
     mytable.`id` AS id,
     mytable.`locationName` AS locationName
FROM
     `mytable` mytable]]>
	</queryString>
	<field name="id" class="java.math.BigInteger">
		<fieldDescription><![CDATA[]]></fieldDescription>
	</field>
	<field name="locationName" class="java.lang.String">
		<fieldDescription><![CDATA[]]></fieldDescription>
	</field>
	<background>
		<band splitType="Stretch"/>
	</background>
	<title>
		<band height="32" splitType="Stretch">
			<staticText>
				<reportElement x="0" y="0" width="296" height="32"/>
				<textElement>
					<font size="14" isBold="true"/>
				</textElement>
				<text><![CDATA[Conditional Background Test]]></text>
			</staticText>
		</band>
	</title>
	<columnHeader>
		<band height="20" splitType="Stretch">
			<staticText>
				<reportElement x="0" y="0" width="51" height="20"/>
				<textElement>
					<font isBold="true"/>
				</textElement>
				<text><![CDATA[ID]]></text>
			</staticText>
			<staticText>
				<reportElement x="51" y="0" width="132" height="20"/>
				<textElement>
					<font isBold="true"/>
				</textElement>
				<text><![CDATA[Location]]></text>
			</staticText>
		</band>
	</columnHeader>
	<detail>
		<band height="20" splitType="Stretch">
			<textField>
				<reportElement style="BandedTable" mode="Opaque" x="0" y="0" width="51" height="20"/>
				<textElement/>
				<textFieldExpression class="java.lang.Number"><![CDATA[$F{id}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement style="BandedTable" mode="Opaque" x="51" y="0" width="132" height="20"/>
				<textElement/>
				<textFieldExpression class="java.lang.String"><![CDATA[$F{locationName}]]></textFieldExpression>
			</textField>
		</band>
	</detail>
</jasperReport>
</td></tr></tbody></table>
cbarlow3's picture
1387
Joined: Mar 4 2010 - 8:59am
Last seen: 3 months 2 weeks ago
0
Thank you.

I think I have it figured out. Rather than changing the background of the data field, I put a frame behind it and change the background color if it.
mlg's picture
mlg
5
Joined: Oct 27 2010 - 7:16am
Last seen: 8 years 6 months ago
0

Well, a frame sounds like more work, and my version is a conditional background color, so it's definitely possible.  On the other hand, I think your method has the advantage that it gives a nicer appearance if your fields don't touch or if a field is suppressed because it is a repeat value, etc.  My "banded table" effect really only looks the way you expect if you make all your fields touch each other, always print, and all use the same style.  If you get yours working, I'd be interested in seeing your jrxml.  I have one report in particular where I've abandoned my usual conditional background style because the report looks cleaner when I suppress repeat values, but I might give frames a try if you get it working.

Carl

cbarlow3's picture
1387
Joined: Mar 4 2010 - 8:59am
Last seen: 3 months 2 weeks ago
0

Hey, I just tried the frame, and it worked great after playing around with a little.  Here's what I did:

1. created a frame that took up my entire detail row.  I set the style to my "Banded Table" style, which is a conditional background color, and I set the "Opaque" checkbox on the frame.

2. I made sure that the normal fields for that detail record were set to NOT be opaque and also had no Style selected.

When I ran the report, the alternating background frame was covering up my data!  I right-clicked on the frame in the Detail part of the Report Inspector and selected "Move to Back".  BINGO!  My detail rows look good, even in the parts that don't have any data!  Screenshot attached.  And of course, it wasn't difficult at all.  Thanks for the idea!

Carl

P.S.: Just in case anyone is concerned, this screenshot is from our test database, which is only fictional accounts.



Post Edited by cbarlow3 at 10/28/2010 17:10
cbarlow3's picture
1387
Joined: Mar 4 2010 - 8:59am
Last seen: 3 months 2 weeks ago
0

Below are my two styles, one is for a background (opaque) and one is for text (transparent)
After those is my frame.

Code:
	<style name="Background_Style">
		<conditionalStyle>
			<conditionExpression><![CDATA[!$F{Registration_Status}.toUpperCase().equals("A")]]></conditionExpression>
			<style mode="Opaque" backcolor="#FF0000"/>
		</conditionalStyle>
	</style>
	<style name="Font_Style">
		<conditionalStyle>
			<conditionExpression><![CDATA[!$F{Registration_Status}.toUpperCase().equals("A")]]></conditionExpression>
			<style mode="Transparent" forecolor="#FF0000" backcolor="#EEEEEE"/>
		</conditionalStyle>
	</style>
 
<frame>
	<reportElement style="Background_Style" x="554" y="20" width="66" height="20"/>
	<textField isStretchWithOverflow="true">
		<reportElement x="0" y="0" width="66" height="20"/>
		<box leftPadding="2"/>
		<textElement>
			<font size="14" isBold="true"/>
		</textElement>
		<textFieldExpression class="java.lang.String"><![CDATA[$F{Registration_Status}]]></textFieldExpression>
	</textField>
	</frame></td></tr></tbody></table>
mlg's picture
mlg
5
Joined: Oct 27 2010 - 7:16am
Last seen: 8 years 6 months ago
0

I hate to piggy-back onto your question, but now I have a related problem:  I love the way my opaque frame looks behind my non-opaque fields in my iReport PREVIEW (both internal and PDF), but after I replace the report in my repository with the one open in iReport, I can't see these same colored backgrounds in JasperServer that I saw in the preview.  I changed a letter in the title and redeployed just to make sure I was really running the new version, and I am, but I see no evidence of the colored frame.

JRXML pasted below (sorry the SQL is so long and ugly) and two screenshots attached:  the iReport preview (desired effect) and the way it looks when I run it on the server (not so much).

Carl

Code:
<?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 <a href="http://jasperreports.sourceforge.net/xsd/jasperreport.xsd"" target="_blank">http://jasperreports.sourceforge.net/xsd/jasperreport.xsd"</a> name="Uninsurability" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
	<property name="ireport.zoom" value="1.0"/>
	<property name="ireport.x" value="0"/>
	<property name="ireport.y" value="0"/>
	<style name="BandedTable" isDefault="false">
		<conditionalStyle>
			<conditionExpression><![CDATA[$V{REPORT_COUNT}%2==1]]></conditionExpression>
			<style isDefault="false" style="BandedTable" backcolor="#99FF99"/>
		</conditionalStyle>
	</style>
	<parameter name="CutoffAmt" class="java.math.BigDecimal">
		<defaultValueExpression><![CDATA[250000.00]]></defaultValueExpression>
	</parameter>
	<queryString>
		<![CDATA[select
personSerial,
personLastName,
personFirstName,
PERSONLASTFIRST,
accountNumber,
shareId,
balance,
singleJoint,
personCount,
divBal,
aggrBal,
aggrBal-$P{CutoffAmt} AS ExceedsCutoffAmt FROM
(
select
personSerial,
personLastName,
personFirstName,
personLastName || ',' || personFirstName AS PERSONLASTFIRST,
accountNumber,
shareId,
balance,
singleJoint,
personCount,
divBal,
sum(divBal) over (partition by personSerial, singleJoint) as aggrBal
from
(
select
personLastName,
personFirstName,
personSerial,
accountNumber,
shareId,
balance,
case
  personCount
  WHEN 1 THEN 'SINGLE'
  ELSE 'JOINT'
end as singleJoint,
personCount,
balance/personCount as divBal
from
(
select
personSerial,
personLastName,
personFirstName,
accountNumber,
shareId,
shareSerial,
balance,
count(personSerial) over (partition by shareSerial) personCount
from
(
select
  person.serial as personSerial,
  person.last_name as personLastName,
  person.first_name as personFirstName,
  account.account_number as accountNumber,
  share.id as shareId,
  share.serial as shareSerial,
  share.balance as balance
from core.sh_person_link as spl inner join
  core.share as share on
  spl.parent_serial=share.serial inner join
  core.person as person on
  spl.person_serial=person.serial inner join
  core.account as account on
  share.parent_serial=account.serial
WHERE spl.category IN
        ('PR', 'TP', 'JT', 'CB', 'AS',
         'PA', 'TR', 'CU')
union
select person.serial as personSerial,
  person.last_name as personLastName,
  person.first_name as personFirstName,
  account.account_number as accountNumber,
  share.id as shareId,
  share.serial as shareSerial,
  share.balance as balance
from core.share as share inner join
  core.account as account on
  share.parent_serial=account.serial inner join
  core.person as person on
  account.primary_person_serial=person.serial
))))
where aggrBal > $P{CutoffAmt}
order by personLastFirst, singleJoint,
accountNumber, shareId]]>
	</queryString>
	<field name="PERSONSERIAL" class="java.lang.Long">
		<fieldDescription><![CDATA[]]></fieldDescription>
	</field>
	<field name="PERSONLASTNAME" class="java.lang.String">
		<fieldDescription><![CDATA[]]></fieldDescription>
	</field>
	<field name="PERSONFIRSTNAME" class="java.lang.String">
		<fieldDescription><![CDATA[]]></fieldDescription>
	</field>
	<field name="PERSONLASTFIRST" class="java.lang.String">
		<fieldDescription><![CDATA[]]></fieldDescription>
	</field>
	<field name="ACCOUNTNUMBER" class="java.lang.String">
		<fieldDescription><![CDATA[]]></fieldDescription>
	</field>
	<field name="SHAREID" class="java.lang.String">
		<fieldDescription><![CDATA[]]></fieldDescription>
	</field>
	<field name="BALANCE" class="java.math.BigDecimal">
		<fieldDescription><![CDATA[]]></fieldDescription>
	</field>
	<field name="SINGLEJOINT" class="java.lang.String">
		<fieldDescription><![CDATA[]]></fieldDescription>
	</field>
	<field name="PERSONCOUNT" class="java.lang.Integer">
		<fieldDescription><![CDATA[]]></fieldDescription>
	</field>
	<field name="DIVBAL" class="java.math.BigDecimal">
		<fieldDescription><![CDATA[]]></fieldDescription>
	</field>
	<field name="AGGRBAL" class="java.math.BigDecimal">
		<fieldDescription><![CDATA[]]></fieldDescription>
	</field>
	<field name="EXCEEDSCUTOFFAMT" class="java.math.BigDecimal">
		<fieldDescription><![CDATA[]]></fieldDescription>
	</field>
	<variable name="PersonSJ" class="java.lang.String">
		<variableExpression><![CDATA[""+$F{PERSONSERIAL}+$F{SINGLEJOINT}]]></variableExpression>
	</variable>
	<variable name="ExceedsCutoffPerPerson" class="java.math.BigDecimal" resetType="Group" resetGroup="PersonSJGroup" calculation="First">
		<variableExpression><![CDATA[$F{EXCEEDSCUTOFFAMT}]]></variableExpression>
	</variable>
	<variable name="TotalExceeded" class="java.math.BigDecimal" incrementType="Group" incrementGroup="PersonSJGroup" calculation="Sum">
		<variableExpression><![CDATA[$V{ExceedsCutoffPerPerson}]]></variableExpression>
	</variable>
	<group name="PersonGroup">
		<groupExpression><![CDATA[$F{PERSONSERIAL}]]></groupExpression>
	</group>
	<group name="PersonSJGroup">
		<groupExpression><![CDATA[$V{PersonSJ}]]></groupExpression>
		<groupHeader>
			<band height="20">
				<frame>
					<reportElement mode="Opaque" x="0" y="0" width="555" height="20" backcolor="#0099CC"/>
				</frame>
				<textField>
					<reportElement isPrintRepeatedValues="false" mode="Transparent" x="114" y="0" width="44" height="20" printWhenGroupChanges="PersonSJGroup"/>
					<textElement/>
					<textFieldExpression class="java.lang.String"><![CDATA[$F{SINGLEJOINT}]]></textFieldExpression>
				</textField>
				<textField pattern="¤ #,##0.00">
					<reportElement isPrintRepeatedValues="false" mode="Transparent" x="158" y="0" width="82" height="20" printWhenGroupChanges="PersonSJGroup"/>
					<textElement textAlignment="Right">
						<font isBold="false"/>
					</textElement>
					<textFieldExpression class="java.math.BigDecimal"><![CDATA[$F{AGGRBAL}]]></textFieldExpression>
				</textField>
				<textField evaluationTime="Group" evaluationGroup="PersonSJGroup" pattern="¤ #,##0.00">
					<reportElement mode="Transparent" x="240" y="0" width="116" height="20" forecolor="#990000"/>
					<textElement textAlignment="Right">
						<font isBold="true"/>
					</textElement>
					<textFieldExpression class="java.math.BigDecimal"><![CDATA[$F{EXCEEDSCUTOFFAMT}]]></textFieldExpression>
				</textField>
				<textField>
					<reportElement isPrintRepeatedValues="false" mode="Transparent" x="0" y="0" width="114" height="20" printWhenGroupChanges="PersonGroup"/>
					<textElement/>
					<textFieldExpression class="java.lang.String"><![CDATA[$F{PERSONLASTFIRST}]]></textFieldExpression>
				</textField>
			</band>
		</groupHeader>
	</group>
	<background>
		<band splitType="Stretch"/>
	</background>
	<title>
		<band height="20" splitType="Stretch">
			<staticText>
				<reportElement x="0" y="0" width="114" height="20"/>
				<textElement>
					<font size="14" isBold="true"/>
				</textElement>
				<text><![CDATA[Uninsurability >]]></text>
			</staticText>
			<textField pattern="¤ #,##0.00">
				<reportElement x="114" y="0" width="126" height="20"/>
				<textElement>
					<font size="14" isBold="true"/>
				</textElement>
				<textFieldExpression class="java.math.BigDecimal"><![CDATA[$P{CutoffAmt}]]></textFieldExpression>
			</textField>
		</band>
	</title>
	<columnHeader>
		<band height="20" splitType="Stretch">
			<staticText>
				<reportElement x="0" y="0" width="114" height="20"/>
				<textElement>
					<font isBold="true"/>
				</textElement>
				<text><![CDATA[Name]]></text>
			</staticText>
			<staticText>
				<reportElement x="114" y="0" width="126" height="20"/>
				<textElement textAlignment="Right">
					<font isBold="true"/>
				</textElement>
				<text><![CDATA[TotShareBalDivide]]></text>
			</staticText>
			<staticText>
				<reportElement x="240" y="0" width="116" height="20"/>
				<textElement textAlignment="Right">
					<font isBold="true"/>
				</textElement>
				<text><![CDATA[ExceedsCutoffAmt]]></text>
			</staticText>
			<staticText>
				<reportElement x="356" y="0" width="75" height="20"/>
				<textElement textAlignment="Right">
					<font isBold="true"/>
				</textElement>
				<text><![CDATA[Share Balance]]></text>
			</staticText>
			<staticText>
				<reportElement x="431" y="0" width="45" height="20"/>
				<textElement textAlignment="Right">
					<font isBold="true"/>
				</textElement>
				<text><![CDATA[Owners]]></text>
			</staticText>
			<staticText>
				<reportElement x="476" y="0" width="79" height="20"/>
				<textElement textAlignment="Right">
					<font isBold="true"/>
				</textElement>
				<text><![CDATA[DividedBal]]></text>
			</staticText>
		</band>
	</columnHeader>
	<detail>
		<band height="20" splitType="Stretch">
			<frame>
				<reportElement style="BandedTable" mode="Opaque" x="0" y="0" width="555" height="20"/>
			</frame>
			<textField pattern="¤ #,##0.00">
				<reportElement mode="Transparent" x="356" y="0" width="77" height="20"/>
				<textElement textAlignment="Right"/>
				<textFieldExpression class="java.math.BigDecimal"><![CDATA[$F{BALANCE}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement mode="Transparent" x="433" y="0" width="43" height="20"/>
				<textElement textAlignment="Right"/>
				<textFieldExpression class="java.lang.Integer"><![CDATA[$F{PERSONCOUNT}]]></textFieldExpression>
			</textField>
			<textField pattern="¤ #,##0.00">
				<reportElement mode="Transparent" x="476" y="0" width="79" height="20"/>
				<textElement textAlignment="Right"/>
				<textFieldExpression class="java.math.BigDecimal"><![CDATA[$F{DIVBAL}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement mode="Transparent" x="240" y="0" width="116" height="20"/>
				<textElement/>
				<textFieldExpression class="java.lang.String"><![CDATA["  "+$F{ACCOUNTNUMBER}+" S "+$F{SHAREID}]]></textFieldExpression>
			</textField>
		</band>
	</detail>
	<summary>
		<band height="20">
			<textField pattern="¤ #,##0.00">
				<reportElement x="240" y="0" width="116" height="20" forecolor="#990000"/>
				<textElement textAlignment="Right">
					<font size="12" isBold="true"/>
				</textElement>
				<textFieldExpression class="java.math.BigDecimal"><![CDATA[$V{TotalExceeded}]]></textFieldExpression>
			</textField>
			<staticText>
				<reportElement x="140" y="0" width="100" height="20"/>
				<textElement>
					<font size="12" isBold="true"/>
				</textElement>
				<text><![CDATA[Total Exceeded:]]></text>
			</staticText>
		</band>
	</summary>
</jasperReport>
</td></tr></tbody></table>
cbarlow3's picture
1387
Joined: Mar 4 2010 - 8:59am
Last seen: 3 months 2 weeks ago
0
I haven't been using iReports very long (like a week) but is it because in your "detail" section, the fields aren't inside the frame?
mlg's picture
mlg
5
Joined: Oct 27 2010 - 7:16am
Last seen: 8 years 6 months ago
0

[QUOTE=cbarlow3]I have a related problem:  I can't see these same colored backgrounds in JasperServer that I saw in the iReport preview. [/QUOTE]

Stab in the dark #1:

Check that the JasperReports jar on the JasperServer implementation is the same version as you are using for iReports.

Stab in the dark #2:

If JasperServer is running on headless (no screen) hardware then it might be running a screen emulator (eg. X11 frame buffer) to render the reports (I think only applies to installations running on JDK 1.4 (hopefully this is not you).  Make sure that emulator or the server's video driver is able to handle the appropriate color depth.



Post Edited by jmurray at 10/28/2010 23:00
jmurray's picture
549
Joined: Dec 11 2006 - 11:19am
Last seen: 12 years 4 months ago
0

Stab #1 sounded more likely, but on my PC, iReport is using C:\\Program Files (x86)\\Jaspersoft\\iReport-Professional-3.7.1.1\\ireportpro\\modules\\ext\\jasperreports-pro.3.7.0.jar, and on our AIX server it'susing /cr/web/apache-tomcat/webapps/jasperserver-pro/WEB-INF/lib/jasperreports-pro-3.7.0.jar.

As for stab #2, it's an interesting, blood-curdling theory.  I'd hate to have my hands tied to one or two colors!  I know I've done several reports that use my "Banded Table" style successfully on the server before, but I don't know that I also had some cells with a red foreground or group headers with yet another color (blue), so I took out the red and blue and made it so the only color other than black and white is the alternating bands of background color on the opaque frame in the detail band (a sort of aqua color, as I recall).  Still no dice.

So then I set two fields back to red, I changed my detail fields from no style to "Banded Table" style and checked "Opaque" (this is how I used to set fields to alternating colors per detail), and I set the background color on my group header fields to the same color blue I was using in that frame and again checked "Opaque".  The result still looks fine on iReport.  When I push it to the server, the output looks like the attached screenshot.  It definitely puts all the colors out on a single page now.  But it doesn't show the frame in those places where there aren't fields to cover it.  It's only showing field background colors.  Another odd thing is that on the server, it's not showing the person's name again on the blue line when it repeats (I go from a group header of "Benavidez,Theresa        Joint"  to "Benavidez,Theresa     Single", for example), even though this was working correctly on the iReport preview.  Ugh.  Frame sounded like a nice idea, but I think I'm going to have to stretch my fields out again so they cover the entire detail band and just use conditional field backgrounds instead of the frame backgroung.  Can't imagine why it would be different on the AIX server than on my PC's version of iReport after testing your two theories.  Thanks for the feedback anyway.

Carl

cbarlow3's picture
1387
Joined: Mar 4 2010 - 8:59am
Last seen: 3 months 2 weeks ago
0

Easy enough to make the repeats appear now that I have them in a group header instead of in the detail, I was fine with checking the "print when repeat" box.  I stretched the fields out to cover the lines (I even added a text field that is just a right justified period on the right hand side of my group header) and took out my frames.  Now I have a version that looks the same in iReport as it does in JasperServer, but I'm still a little disappointed that the frame idea didn't work, as that sounded like a good shortcut for achieving this effect without worrying about changing the style of individual fields or having to worry about covering the entire detail band with fields to make it look right.

Carl

cbarlow3's picture
1387
Joined: Mar 4 2010 - 8:59am
Last seen: 3 months 2 weeks ago
Feedback
randomness