【问题标题】:How to plot scatter chart in iReport with DB data?如何在 iReport 中使用 DB 数据绘制散点图?
【发布时间】:2016-06-28 08:01:47
【问题描述】:

我添加了数据库查询,我可以在详细信息视图中查看数据。

但无法绘制散点图。 X 轴一列,Y 轴一列,标签名称一列。如何使用 iReport 绘制此图?

我添加了散点图调色板,但没有获得任何关于如何设置 X 和 Y 轴的列以绘制所有点的文档。我在散点图中只得到一个点。

【问题讨论】:

  • 添加近距离投票的问题有什么问题?
  • 仅供参考,iReport 已被长期弃用
  • How to plot this with ireport tool? - 这个问题太笼统了吧?
  • 我无法使用 ireport 工具在散点图上绘制多个点?必须有一些我缺少的配置。您确定不推荐使用 ireport 吗? @AlexK
  • Are you sure ireport is deprecated? - 是的。最好使用Studio

标签: charts jasper-reports ireport scatter-plot


【解决方案1】:

您的第一个问题是将数据源传递给图表,如果您使用主数据源,则需要将图表放在 summary 带中,因为您的数据源在 title 带中尚未准备好并且正在迭代detail 乐队,因此它不能正常工作(注意:如果你使用你的数据源也详细乐队它需要是一个JRRewindableDataSource)。

如果您需要在其他波段中使用它,那么summary 波段我建议您制作一个子报告并将其包含在所需的波段传递图表数据源或与子报告的连接中。

散点图是使用<scatterChart><xyDataset> 实现的,设置系列和x,y 数据使用<xySeries> 标签

在iReport中,右键图表,选择Chart Data,选择tab Details添加 XY系列

<xySeries>
    <seriesExpression><![CDATA[$F{Series}]]></seriesExpression>
    <xValueExpression><![CDATA[$F{X}]]></xValueExpression>
    <yValueExpression><![CDATA[$F{Y}]]></yValueExpression>
</xySeries>

如果您只有 1 个系列,您可以在 seriesExpression 中传递一个虚拟变量

<seriesExpression><![CDATA["dummy"]]></seriesExpression>

完整的 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="ScatterPlot" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="ebc92eb9-c769-4888-98de-b1d60670725c">
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="Series" class="java.lang.String"/>
    <field name="X" class="java.lang.Double"/>
    <field name="Y" class="java.lang.Double"/>
    <columnHeader>
        <band height="20">
            <staticText>
                <reportElement x="100" y="0" width="100" height="20" uuid="978a0093-4e67-40e3-baee-8340ccc23b8f"/>
                <box topPadding="2" leftPadding="2" bottomPadding="2" rightPadding="2">
                    <pen lineWidth="0.5"/>
                    <topPen lineWidth="0.5"/>
                    <leftPen lineWidth="0.5"/>
                    <bottomPen lineWidth="0.5"/>
                    <rightPen lineWidth="0.5"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font isBold="true"/>
                </textElement>
                <text><![CDATA[X]]></text>
            </staticText>
            <staticText>
                <reportElement x="0" y="0" width="100" height="20" uuid="b97c9f78-ad74-4d9f-af46-ee3fc3e5351f"/>
                <box topPadding="2" leftPadding="2" bottomPadding="2" rightPadding="2">
                    <pen lineWidth="0.5"/>
                    <topPen lineWidth="0.5"/>
                    <leftPen lineWidth="0.5"/>
                    <bottomPen lineWidth="0.5"/>
                    <rightPen lineWidth="0.5"/>
                </box>
                <textElement verticalAlignment="Middle">
                    <font isBold="true"/>
                </textElement>
                <text><![CDATA[Series]]></text>
            </staticText>
            <staticText>
                <reportElement x="200" y="0" width="100" height="20" uuid="e50dab9b-c8f7-4cc0-a8f7-3ca4438c746b"/>
                <box topPadding="2" leftPadding="2" bottomPadding="2" rightPadding="2">
                    <pen lineWidth="0.5"/>
                    <topPen lineWidth="0.5"/>
                    <leftPen lineWidth="0.5"/>
                    <bottomPen lineWidth="0.5"/>
                    <rightPen lineWidth="0.5"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font isBold="true"/>
                </textElement>
                <text><![CDATA[Y]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="20">
            <textField pattern="###0.00">
                <reportElement x="100" y="0" width="100" height="20" uuid="8d365885-a6f8-40be-8ad0-012e45a37543"/>
                <box topPadding="2" leftPadding="2" bottomPadding="2" rightPadding="2">
                    <pen lineWidth="0.5"/>
                    <topPen lineWidth="0.5"/>
                    <leftPen lineWidth="0.5"/>
                    <bottomPen lineWidth="0.5"/>
                    <rightPen lineWidth="0.5"/>
                </box>
                <textElement textAlignment="Right" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$F{X}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="0" y="0" width="100" height="20" uuid="f08b3a38-8130-4609-b70d-b10b9d8d648b"/>
                <box topPadding="2" leftPadding="2" bottomPadding="2" rightPadding="2">
                    <pen lineWidth="0.5"/>
                    <topPen lineWidth="0.5"/>
                    <leftPen lineWidth="0.5"/>
                    <bottomPen lineWidth="0.5"/>
                    <rightPen lineWidth="0.5"/>
                </box>
                <textElement verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$F{Series}]]></textFieldExpression>
            </textField>
            <textField pattern="###0.00">
                <reportElement x="200" y="0" width="100" height="20" uuid="83638866-5bd1-4ee4-ac1a-455c406f3621"/>
                <box topPadding="2" leftPadding="2" bottomPadding="2" rightPadding="2">
                    <pen lineWidth="0.5"/>
                    <topPen lineWidth="0.5"/>
                    <leftPen lineWidth="0.5"/>
                    <bottomPen lineWidth="0.5"/>
                    <rightPen lineWidth="0.5"/>
                </box>
                <textElement textAlignment="Right" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$F{Y}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <summary>
        <band height="257" splitType="Stretch">
            <scatterChart>
                <chart>
                    <reportElement x="4" y="16" width="280" height="237" uuid="b9968e83-13aa-48fc-acf5-3d646f45f28d"/>
                    <chartTitle/>
                    <chartSubtitle/>
                    <chartLegend/>
                </chart>
                <xyDataset>
                    <xySeries>
                        <seriesExpression><![CDATA[$F{Series}]]></seriesExpression>
                        <xValueExpression><![CDATA[$F{X}]]></xValueExpression>
                        <yValueExpression><![CDATA[$F{Y}]]></yValueExpression>
                    </xySeries>
                </xyDataset>
                <scatterPlot isShowLines="false" isShowShapes="true">
                    <plot/>
                    <xAxisFormat>
                        <axisFormat/>
                    </xAxisFormat>
                    <yAxisFormat>
                        <axisFormat/>
                    </yAxisFormat>
                </scatterPlot>
            </scatterChart>
        </band>
    </summary>
</jasperReport>

csv 数据示例

+--------+-----+-----+
| Series |  X  |  Y  |
+--------+-----+-----+
| A      | 1.2 | 0.3 |
| A      | 0.5 | 0.2 |
| B      | 0.7 | 0.6 |
| B      | 0.1 | 0.5 |
+--------+-----+-----+

结果

要配置图表,请使用图表上的属性设置,如果您需要使用属性无法实现的特殊配置,您可以实现ChartCustomizer

例如看这个

How to put benchmark line on barchart?

【讨论】:

  • 如何制作象限?就像 X 轴将绘制在 Y 轴的中间而 Y 轴将绘制在 X 轴的中间?
  • 为此,您可能需要 ChartCustomizer,在 jfreechart public void setDomainZeroBaselineVisible(boolean visible); 中有一个方法,但您可能应该提出另一个问题,说明您需要什么。
  • @SomnathMuluk,还有这个问题吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-14
  • 2013-05-12
  • 2021-08-15
  • 2017-09-23
  • 2019-12-21
  • 1970-01-01
  • 2019-09-26
相关资源
最近更新 更多