Is it in any way possible to assign colors to fields via parameter? I can get a specific data-type with content AND color from out database and need to display that content (no problem at all) in the color from the database - can I do that somehow?
I didn't see a way to use a parameter or any kind of expression for colors, not even when using styles or conditional styles.
3 Answers:
it is possible, but you need to find all the distinct colors in your database and assign style expressions in your style.
working sample jrxml for this case is below.
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.2.0.final using JasperReports Library version 6.2.0 -->
<!-- 2016-12-13T08:40:11 -->
<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="two" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="794a4213-f2f1-4f2c-9086-ce9ee7c3cd23">
<property name="com.jaspersoft.studio.data.sql.tables" value=""/>
<style name="Style1">
<conditionalStyle>
<conditionExpression><![CDATA[$F{COLOR}.equals("#40FF00") ? true : false]]></conditionExpression>
<style mode="Opaque" backcolor="#40FF00"/>
</conditionalStyle>
<conditionalStyle>
<conditionExpression><![CDATA[$F{COLOR}.equals("#A8A7C9") ? true : false]]></conditionExpression>
<style mode="Opaque" backcolor="#A8A7C9"/>
</conditionalStyle>
<conditionalStyle>
<conditionExpression><![CDATA[$F{COLOR}.equals("#0FFAA0") ? true : false]]></conditionExpression>
<style mode="Opaque" backcolor="#0FFAA0"/>
</conditionalStyle>
<conditionalStyle>
<conditionExpression><![CDATA[$F{COLOR}.equals("#95FF00") ? true : false]]></conditionExpression>
<style mode="Opaque" backcolor="#95FF00"/>
</conditionalStyle>
<conditionalStyle>
<conditionExpression><![CDATA[$F{COLOR}.equals("#FF1500") ? true : false]]></conditionExpression>
<style mode="Opaque" backcolor="#FF1500"/>
</conditionalStyle>
<conditionalStyle>
<conditionExpression><![CDATA[$F{COLOR}.equals("#636363") ? true : false]]></conditionExpression>
<style mode="Opaque" backcolor="#636363"/>
</conditionalStyle>
<conditionalStyle>
<conditionExpression><![CDATA[$F{COLOR}.equals("#D90FF0") ? true : false]]></conditionExpression>
<style mode="Opaque" backcolor="#D90FF0"/>
</conditionalStyle>
</style>
<queryString>
<![CDATA[select 1 as ID,'#40FF00' as color from dual union
select 2 as ID,'#A8A7C9' as color from dual union
select 3 as ID,'#0FFAA0' as color from dual union
select 4 as ID,'#95FF00' as color from dual union
select 5 as ID,'#FF1500' as color from dual union
select 6 as ID,'#636363' as color from dual union
select 7 as ID,'#D90FF0' as color from dual]]>
</queryString>
<field name="ID" class="java.math.BigDecimal"/>
<field name="COLOR" class="java.lang.String"/>
<background>
<band splitType="Stretch"/>
</background>
<columnHeader>
<band height="20">
<staticText>
<reportElement mode="Opaque" x="0" y="0" width="80" height="20" backcolor="#FF0400" uuid="591ef2b2-f920-41a5-bce0-39da974d4a24"/>
<textElement>
<font isBold="true" isUnderline="true"/>
</textElement>
<text><![CDATA[ID]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20">
<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
<textField>
<reportElement style="Style1" x="0" y="0" width="80" height="20" uuid="e83fd994-5f64-4f18-9d1d-5e37b91c8e91"/>
<textFieldExpression><![CDATA[$F{ID}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
@reportdev
And what would I do if (and with our application this is the case) the user can freely choose the colors in our program and should see exactly those colors inside the report as well? We can save the colors inside the database and Jasper can even read them from there with a field of java.awt.Color - why can we not use this kind of field for dynamic coloring? o_O
I'm not sure about java.awt.Color, but found another easy way to perform the background coloring dynamically.
I have used a HTML element and used a bgcolor html tag to evaluate the hex color at runtime.
"<body bgcolor=" + $F{COLOR} + "><h1>Hello world!</h1><p></p></body>"