【问题标题】:jfree chart org.jfree.chart.plot.CategoryPlot cannot be cast to org.jfree.chart.plot.XYPlotjfree 图表 org.jfree.chart.plot.CategoryPlot 不能转换为 org.jfree.chart.plot.XYPlot
【发布时间】:2016-06-15 11:53:40
【问题描述】:

我正在使用 Studio 创建 JasperReports 的报告。我需要增加折线图的宽度(默认太细)。据我所知,JasperReports 使用 Jfeechart 作为图表,我在 google 上搜索了这段代码来进行自定义。

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);
    }
}

我已经构建了上面的类并将其添加到 jar 中,并将其添加到 jasper 项目中。并且在图表属性中可以选择定制器类中的类。但是当我运行报告时,我得到了这个错误:

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

我认为问题可能出在代码上,但我不知道 Jfreechart...

P.S下面是报表代码,很简单的演示:

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

【问题讨论】:

    标签: java jasper-reports jfreechart


    【解决方案1】:

    图表定制器不适用于类别图表,它旨在用于 XY 或时间序列图表。

    对于类别图表,您将需要以下内容(另请注意,系列索引是从 0 开始的):

    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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多