如何在 jasper 报告 3d 条形图中显示总和值?

How to show sum values in jasper reports 3d bar charts?

我正在尝试创建一个 3d 条形图,它按类型分组并显示每种类型的平方英尺值的总和。我无法让条形图显示每种颜色每种类型的平方英尺总和。相反,当同一类型和颜色有多个平方英尺值时,它似乎显示最后一个平方英尺的值。

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.6.0.final using JasperReports Library version 6.6.0  -->
<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="barChart" pageWidth="842" pageHeight="595" orientation="Landscape" columnWidth="802" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="4f80af00-8345-4feb-bd0c-5b0b3b2b6232">
    <property name="com.jaspersoft.studio.data.sql.tables" value=""/>
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="hsqlCon"/>
    <queryString language="SQL">
        <![CDATA[select * from Item where id IN (select itemId from TempItem )]]>
    </queryString>
    <field name="COLOR" class="java.lang.String">
        <property name="com.jaspersoft.studio.field.label" value="COLOR"/>
        <property name="com.jaspersoft.studio.field.tree.path" value="ITEM"/>
    </field>
    <field name="SQUAREFEET" class="java.lang.Double">
        <property name="com.jaspersoft.studio.field.label" value="SQUAREFEET"/>
        <property name="com.jaspersoft.studio.field.tree.path" value="ITEM"/>
    </field>
    <field name="TYPE" class="java.lang.String">
        <property name="com.jaspersoft.studio.field.label" value="TYPE"/>
        <property name="com.jaspersoft.studio.field.tree.path" value="ITEM"/>
    </field>
    <group name="TYPE">
        <groupExpression><![CDATA[$F{TYPE}]]></groupExpression>
    </group>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="79" splitType="Stretch"/>
    </title>
    <pageHeader>
        <band height="35" splitType="Stretch"/>
    </pageHeader>
    <detail>
        <band height="257" splitType="Stretch">
            <bar3DChart>
                <chart evaluationTime="Report">
                    <reportElement x="139" y="0" width="581" height="257" uuid="f2ceb018-5078-4aeb-90df-125419604d39"/>
                    <chartTitle/>
                    <chartSubtitle/>
                    <chartLegend/>
                </chart>
                <categoryDataset>
                    <categorySeries>
                        <seriesExpression><![CDATA[$F{COLOR}]]></seriesExpression>
                        <categoryExpression><![CDATA[$F{TYPE}]]></categoryExpression>
                        <valueExpression><![CDATA[SUM($F{SQUAREFEET})]]></valueExpression>
                    </categorySeries>
                </categoryDataset>
                <bar3DPlot>
                    <plot/>
                    <itemLabel/>
                    <categoryAxisFormat>
                        <axisFormat labelColor="#000000" tickLabelColor="#000000" axisLineColor="#000000"/>
                    </categoryAxisFormat>
                    <valueAxisFormat>
                        <axisFormat labelColor="#000000" tickLabelColor="#000000" axisLineColor="#000000"/>
                    </valueAxisFormat>
                </bar3DPlot>
            </bar3DChart>
        </band>
    </detail>
    <pageFooter>
        <band height="54" splitType="Stretch"/>
    </pageFooter>
</jasperReport>

NOTICE: I would recommend that all the calculations are done in the sql query in your example it could be SELECT TYPE, COLOR, SUM(SQUAREFEET) AS SQUAREFEET FROM Item WHERE id IN (SELECT itemId FROM TempItem) GROUP BY TYPE, COLOR. This will give you a "flat" dataource and you do not need to add groups, variables etc in jasper-report also the retrieved data is less which will decrease execution time of report. The example below to create a chart with your current data using groups and variables presume that your data is ordered by TYPE and COLOR (grouping in jasper reports works only if you have ordered data)

完整示例前的一些关键points/quick提示

图表应该放在摘要带中,详细带针对每条记录执行,而不是我们想要所有记录的 1 个图表。

您的组既有 TYPE 又有 COLOR(当其中一个发生变化时,您就有了一个新组)因此分组表达式需要类似于:

<group name="TYPE_COLOR">
   <groupExpression><![CDATA[$F{TYPE}+"_"+$F{COLOR}]]></groupExpression>
</group>

我们需要一个变量来收集总和的值(组更改时重置),我们将在类别系列值表达式中使用它。

<variable name="SUM_SQUAREFEET" class="java.lang.Double" resetType="Group" resetGroup="TYPE_COLOR" calculation="Sum">
        <variableExpression><![CDATA[$F{SQUAREFEET}]]></variableExpression>
</variable>

并且我们需要告诉图表何时收集数据(何时向图表添加数据)

<dataset incrementType="Group" incrementGroup="TYPE_COLOR"/>

完整的 jrxml

<?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 http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="barChart" pageWidth="842" pageHeight="595" orientation="Landscape" columnWidth="802" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="4f80af00-8345-4feb-bd0c-5b0b3b2b6232">
    <queryString language="SQL">
    <![CDATA[select * from Item where id IN (select itemId from TempItem )]]>
    </queryString>
    <field name="TYPE" class="java.lang.String"/>
    <field name="COLOR" class="java.lang.String"/>
    <field name="SQUAREFEET" class="java.lang.Double"/>
    <variable name="SUM_SQUAREFEET" class="java.lang.Double" resetType="Group" resetGroup="TYPE_COLOR" calculation="Sum">
        <variableExpression><![CDATA[$F{SQUAREFEET}]]></variableExpression>
    </variable>
    <group name="TYPE_COLOR">
        <groupExpression><![CDATA[$F{TYPE}+"_"+$F{COLOR}]]></groupExpression>
    </group>
    <summary>
        <band height="267">
            <bar3DChart>
                <chart>
                    <reportElement x="90" y="10" width="581" height="257" uuid="bcbac117-a9ab-424c-ae81-6f68d1b01f0c"/>
                    <chartTitle/>
                    <chartSubtitle/>
                    <chartLegend/>
                </chart>
                <categoryDataset>
                    <dataset incrementType="Group" incrementGroup="TYPE_COLOR"/>
                    <categorySeries>
                        <seriesExpression><![CDATA[$F{COLOR}]]></seriesExpression>
                        <categoryExpression><![CDATA[$F{TYPE}]]></categoryExpression>
                        <valueExpression><![CDATA[$V{SUM_SQUAREFEET}]]></valueExpression>
                    </categorySeries>
                </categoryDataset>
                <bar3DPlot>
                    <plot/>
                    <itemLabel/>
                    <categoryAxisFormat>
                        <axisFormat labelColor="#000000" tickLabelColor="#000000" axisLineColor="#000000"/>
                    </categoryAxisFormat>
                    <valueAxisFormat>
                        <axisFormat labelColor="#000000" tickLabelColor="#000000" axisLineColor="#000000"/>
                    </valueAxisFormat>
                </bar3DPlot>
            </bar3DChart>
        </band>
    </summary>
</jasperReport>

输出