JasperReports + TimeSeriesChart:月份数据,年份标签

时间:2017-02-01 18:27:47

标签: charts jasper-reports timeserieschart

我一直在使用JasperReports来制作PDF报告,但是有一个我无法工作的功能。我正在尝试生成一个具有月度数据但仅显示年份标签的TimeSeriesCharts。不幸的是,这是最终结果:

enter image description here

我希望每年只出现一次。我该怎么办?

下面的jrxml代码:

<timeSeriesChart>
                <chart evaluationTime="Report" isShowLegend="false">
                    <reportElement x="0" y="32" width="462" height="180" />
                    <chartTitle/>
                    <chartSubtitle/>
                    <chartLegend/>
                </chart>
                <timeSeriesDataset timePeriod="Month">
                    <dataset>
                        <datasetRun subDataset="chartDataSet" uuid="abec2dce-b670-4e84-b71f-469d954dbcb5">
                            <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{data})]]></dataSourceExpression>
                        </datasetRun>
                    </dataset>
                    <timeSeries>
                        <seriesExpression><![CDATA[$F{serie}]]></seriesExpression>
                        <timePeriodExpression><![CDATA[$F{category}]]></timePeriodExpression>
                        <valueExpression><![CDATA[$F{value}]]></valueExpression>
                    </timeSeries>
                </timeSeriesDataset>
                <timeSeriesPlot isShowShapes="false">
                    <plot>
                        <seriesColor seriesOrder="1" color="#0000FF" />
                    </plot>
                    <timeAxisFormat>
                        <axisFormat labelColor="#000000" tickLabelColor="#000000" axisLineColor="#000000" verticalTickLabels="true" tickLabelMask="yyyy" />
                    </timeAxisFormat>
                    <valueAxisFormat>
                        <axisFormat labelColor="#000000" tickLabelColor="#000000" axisLineColor="#000000"/>
                    </valueAxisFormat>
                </timeSeriesPlot>
            </timeSeriesChart>

提前致谢

1 个答案:

答案 0 :(得分:1)

原来这很容易。我必须创建一个用于自定义图表的Java类:

public class MyChartCustomizer implements JRChartCustomizer {
@Override
public void customize(JFreeChart chart, JRChart jasperChart){
    DateAxis axis = (DateAxis) chart.getXYPlot().getDomainAxis();
    axis.setTickUnit(new DateTickUnit(DateTickUnitType.YEAR, 1));
}
}

然后在jrxml中引用它:

<timeSeriesChart>
                <chart evaluationTime="Report" isShowLegend="false" customizerClass="my.org.MyChartCustomizer">
....

希望这有助于某人。