使用JasperReports根据线图中的值绘制具有不同颜色的点

时间:2010-01-12 05:53:46

标签: java jasper-reports

我有一个从SQL生成的折线图,我需要根据值以不同的颜色绘制数据。 例如:正值为绿色,负值为红色。

我添加了jrxml内容,

<?xml version="1.0" encoding="UTF-8"  ?>
<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<jasperReport
         name="escalationTriggersDrilldown"
         columnCount="1"
         printOrder="Vertical"
         orientation="Portrait"
         pageWidth="500"
         pageHeight="350"
         columnWidth="500"
         columnSpacing="0"
         leftMargin="0"
         rightMargin="0"
         topMargin="0"
         bottomMargin="0"
         whenNoDataType="AllSectionsNoDetail"
         isTitleNewPage="false"
         isSummaryNewPage="false"
         resourceBundle="conformus"
         whenResourceMissingType="Key">
    <property name="ireport.scriptlethandling" value="0" />
    <property name="ireport.encoding" value="UTF-8" />
    <import value="java.util.*" />
    <import value="net.sf.jasperreports.engine.*" />
    <import value="net.sf.jasperreports.engine.data.*" />

    <parameter name="conName" isForPrompting="true" class="java.lang.String"/>
    <parameter name="conId" isForPrompting="true" class="java.lang.String"/>
    <parameter name="orgId" isForPrompting="true" class="java.lang.String"/>
    <parameter name="chartName" isForPrompting="true" class="java.lang.String"/>
    <parameter name="sql" isForPrompting="true" class="java.lang.String"/>
    <parameter name="unit" isForPrompting="true" class="java.lang.String"/>
    <parameter name="indicatorName" isForPrompting="true" class="java.lang.String"/>
    <queryString><![CDATA[$P!{sql}]]></queryString>

    <field name="INDICATOR_NAME" class="java.lang.String"/>
    <field name="UNIT_OF_MEASURE" class="java.lang.String"/>
    <field name="E_DATE" class="java.lang.String"/>
    <field name="CALCULATED_VALUE" class="java.math.BigDecimal"/>
    <field name="FLAG_VALUE" class="java.math.BigDecimal"/>


        <summary>
            <band height="350"  isSplitAllowed="true" >
                <lineChart>
                    <chart  hyperlinkTarget="Self" >
                    <reportElement
                        x="0"
                        y="0"
                        width="500"
                        height="350"
                        key="element-1"/>
                    <box></box>
                        <chartTitle color="#000000" >
                    <font fontName="Trebuchet MS" pdfFontName="Helvetica" size="11" isBold="true" isItalic="false" isUnderline="false" isStrikeThrough="false" isPdfEmbedded="false" pdfEncoding="Cp1252"/>
                                <titleExpression><![CDATA[str($P{chartName})+" - "+$P{indicatorName}]]></titleExpression>
                    </chartTitle>
                        <chartLegend textColor="#000000" backgroundColor="#FFFFFF" >
                    </chartLegend>
                    </chart>
                    <categoryDataset>
                        <dataset >
                        </dataset>
                        <categorySeries>
                            <seriesExpression><![CDATA[$F{INDICATOR_NAME}]]></seriesExpression>
                            <categoryExpression><![CDATA[$F{E_DATE}]]></categoryExpression>
                            <valueExpression><![CDATA[$F{CALCULATED_VALUE}]]></valueExpression>
                <itemHyperlink   hyperlinkType="Reference">
                        <hyperlinkTooltipExpression><![CDATA[""+$F{CALCULATED_VALUE}]]></hyperlinkTooltipExpression>
                </itemHyperlink>
                        </categorySeries>
                    </categoryDataset>
                    <linePlot >
                        <plot />
                        <categoryAxisFormat>
                            <axisFormat >
                            </axisFormat>
                        </categoryAxisFormat>
                    <valueAxisLabelExpression><![CDATA[$P{unit}]]></valueAxisLabelExpression>
                        <valueAxisFormat>
                            <axisFormat >
                                <labelFont>
                    <font fontName="Trebuchet MS" pdfFontName="Helvetica" size="10" isBold="true" isItalic="false" isUnderline="false" isStrikeThrough="false" isPdfEmbedded="false" pdfEncoding="Cp1252"/>
                                </labelFont>
                                <tickLabelFont>
                                </tickLabelFont>
                            </axisFormat>
                        </valueAxisFormat>
                    </linePlot>
                </lineChart>
            </band>
        </summary>
</jasperReport> 

1 个答案:

答案 0 :(得分:0)

您可能需要使用自定义程序类来设置颜色。 您可以访问定制程序类中的数据。您可能需要为数据设置条件并将颜色分配给行。