Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jfree chart org.jfree.chart.plot.CategoryPlot cannot be cast to org.jfree.chart.plot.XYPlot

I am using Studio to create a JasperReports's report. I need to increase the width of the line chart (default is too thin). As far I know JasperReports use Jfeechart for the chart, and I googled this piece of code to do the customization.

public class TSChartCustomizer extends JRAbstractChartCustomizer {
    public void customize(JFreeChart chart, JRChart jasperChart) {
       XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) chart.getXYPlot().getRenderer();
        BasicStroke stroke = new BasicStroke(3f);
        renderer.setSeriesStroke(1, stroke);
    }
}

I have built and added above class to a jar and added it in the jasper project. And in the chart properties can select the class in customizer class. But when I run the report I got this error:

java.lang.ClassCastException: org.jfree.chart.plot.CategoryPlot cannot be cast to org.jfree.chart.plot.XYPlot

I think the problem is probably with the code, but I don't know Jfreechart...

P.S below is the report code, a very simple one for demonstration:

<?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="test6" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="f147421f-bbe6-42bd-b26e-7f4f7cee333a">
    <queryString>
        <![CDATA[select 'p1' as prod, 'm1' as mon, 10 as num
union all select 'p1' as prod, 'm2' as mon, 20 as num
union all select 'p1' as prod, 'm3' as mon, 30 as num]]>
    </queryString>
    <field name="prod" class="java.lang.String"/>
    <field name="mon" class="java.lang.String"/>
    <field name="num" class="java.lang.Integer"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <summary>
        <band height="325" splitType="Stretch">
            <lineChart>
                <chart evaluationTime="Report">
                    <reportElement x="178" y="125" width="200" height="200" uuid="124ec6e6-b32d-477d-84a4-36c8d4df3ac2"/>
                    <chartTitle/>
                    <chartSubtitle/>
                    <chartLegend/>
                </chart>
                <categoryDataset>
                    <categorySeries>
                        <seriesExpression><![CDATA[$F{prod}]]></seriesExpression>
                        <categoryExpression><![CDATA[$F{mon}]]></categoryExpression>
                        <valueExpression><![CDATA[$F{num}]]></valueExpression>
                    </categorySeries>
                </categoryDataset>
                <linePlot>
                    <plot/>
                    <categoryAxisFormat>
                        <axisFormat/>
                    </categoryAxisFormat>
                    <valueAxisFormat>
                        <axisFormat/>
                    </valueAxisFormat>
                </linePlot>
            </lineChart>
        </band>
    </summary>
</jasperReport>
like image 783
thotwielder Avatar asked Jan 23 '26 18:01

thotwielder


1 Answers

The chart customizer does not work for category charts, it was meant to be used for XY or timeseries charts.

For category charts you will need something like the following (also note that the series index is 0-based):

public class TSChartCustomizer extends JRAbstractChartCustomizer {
    public void customize(JFreeChart chart, JRChart jasperChart) {
        AbstractCategoryItemRenderer renderer = (AbstractCategoryItemRenderer) chart.getCategoryPlot().getRenderer();
        BasicStroke stroke = new BasicStroke(3f);
        renderer.setSeriesStroke(0, stroke);
    }
}
like image 80
dada67 Avatar answered Jan 26 '26 08:01

dada67



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!