如何在 Jasper Report 中转换名称缩写中的月数?

How to convert the number of month in the name abbreviation in Jasper Report?

我在 JasperReports 中有一个 XY 线图,我在我的图表中使用一个范围来显示月份,目前我正在使用这个配置并在 X 线中显示从 1 到 12 的值:

Image of currently chart

<?xml version="1.0" encoding="UTF-8"?>
<property name="net.sf.jasperreports.customizer.class.1" value="net.sf.jasperreports.customizers.axis.DomainAxisCustomizer"/>
                    <property name="net.sf.jasperreports.customizer.1.tickUnit" value="1.0"/>
                    <propertyExpression name="net.sf.jasperreports.customizer.1.minValue"><![CDATA["1"]]></propertyExpression>
                    <propertyExpression name="net.sf.jasperreports.customizer.1.maxValue"><![CDATA["12"]]></propertyExpression>

但是我需要显示月份的缩写,像这样:

1 - 一月, 2 - 二月, 3 - 三月, ...

有更好的方法吗?

您可以像这样实现自己的域轴定制器:

public class DomainAxisWithMonthsCustomizer extends JRAbstractChartCustomizer {
    @Override
    public void customize(JFreeChart chart, JRChart jasperChart) {
        XYPlot plot = chart.getXYPlot();
        String[] shortMonths = new DateFormatSymbols().getShortMonths();
        SymbolAxis domainAxis = new SymbolAxis("Month",
                IntStream.rangeClosed(1, 12)
                        .mapToObj(m -> m + " - " + shortMonths[m - 1].toUpperCase())
                        .toArray(String[]::new));
        plot.setDomainAxis(domainAxis);
    }
}

并在您的报告模板中使用它:

<chart customizerClass="my.project.report.DomainAxisWithMonthsCustomizer">
...
</chart>