1

I have added DB query, with that I am able to view data in details view.

But not able to plot scatter chart. There is one column for X axis and one column for Y axis and one for label name. How to plot this using iReport?

I have added scatter chart palette but not getting any documentation of how to set columns for X and Y axis to plot all points. I am getting only one point in scatter chart.

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
Somnath Muluk
  • 55,015
  • 38
  • 216
  • 226
  • What's wrong with the question for adding close votes? – Somnath Muluk Jun 28 '16 at 08:12
  • FYI, iReport is deprecated for a long period of time – Alex K Jun 28 '16 at 08:13
  • `How to plot this with ireport tool?` - It is too broad question, isn't it? – Alex K Jun 28 '16 at 08:16
  • I am not able to plot multiple points on scatter chart with ireport tool? there must be some configuration which I am missing. Are you sure ireport is deprecated? @AlexK – Somnath Muluk Jun 28 '16 at 08:26
  • `Are you sure ireport is deprecated?` - Yes. It is better to use [Studio](http://community.jaspersoft.com/project/jaspersoft-studio) – Alex K Jun 28 '16 at 08:27
  • Yes it is deprecated. Thanks for suggesting. I will use Studio. I have to make this report with tight deadlines so can't afford to learn Studio right now. If you know how to print it with ireport or if you can give some pointers that will be good @AlexK – Somnath Muluk Jun 28 '16 at 08:32
  • If you give me (community) more information (the small sample to reproduce with an image will be great to have) about your issue - I'll try tonight (but I think that other guys help you in near future) :) – Alex K Jun 28 '16 at 08:37

1 Answers1

2

Your first problem is to pass a datasource to the chart, if you are using main datasource, you need to put the chart in the summary band, since your datasource is not ready in title band and is iterating on detail band, hence it will not work correctly (note: if you use your datasource also in detail band it need to be a JRRewindableDataSource).

If you need to use it in other bands then summary band I suggest you make a subreport and included this in desired band passing chart datasource or the connection to the subreport.

The scatter plot is achieved using a <scatterChart> and the <xyDataset>, to set series and x,y data you use the <xySeries> tag

In iReport, Right click chart, select Chart Data, select tab Details and add XY series

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

If you have only 1 series you can pass a dummy variable in seriesExpression

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

Full jrxml example

<?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 data example

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

Result

outcome

To configure your chart use the properties settings on chart, if you need special configuration that you can not achieve with the properties you can implement a ChartCustomizer

see this for example

How to put benchmark line on barchart?

Community
  • 1
  • 1
Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
  • How to make quadrants? Like X axis will be plotted on middle of Y axis and Y axis will be plotted on middle of X axis? – Somnath Muluk Jun 28 '16 at 11:21
  • 1
    For that you probably will need the ChartCustomizer, there is a method in jfreechart public void setDomainZeroBaselineVisible(boolean visible);, but you probably should make another questions, illustrating exactly what you need. – Petter Friberg Jun 28 '16 at 11:26
  • @SomnathMuluk, still having problem with this? – Petter Friberg Jul 28 '16 at 22:44