简体   繁体   English

通过Jasper-Reports将语言环境详细信息传递给JFreechart

[英]Passing Locale details via Jasper-Reports to JFreechart

I'm adding Internationalization into a Tapestry web-app which uses Jasper Reports to generate normal tabular reports and also charts and graphs via JFreeChart. 我正在将国际化添加到Tapestry网络应用程序中,该应用程序使用Jasper Reports通过JFreeChart生成常规的表格报表以及图表和图形。

Using the Jasper REPORT_LOCALE parameter, I can set the Locale for Jasper reports and this works beautifully for the tabular reports but it doesn't work for the JFreeChart reports. 使用Jasper REPORT_LOCALE参数,我可以为Jasper报表设置区域设置,这对于表格报表可以很好地工作,但不适用于JFreeChart报表。

The Axis tick labels are coming out in the default Locale so that if I'm doing a time-series, I get month-names coming out in the wrong language. Axis刻度标签以默认的区域设置显示,因此,如果我正在进行时间序列,则会得到以错误语言显示的月份名称。 The only way I've figured out how to deal with this is to change the JVM default locale which I'm not happy about. 我弄清楚如何处理此问题的唯一方法是更改​​我不满意的JVM默认语言环境。

Does anyone know if there's some way to configure JFreeChart to use a particular Locale so that when Jasper calls it, it uses that Locale? 有谁知道是否有某种方法可以将JFreeChart配置为使用特定的语言环境,以便当Jasper调用它时,它使用该语言环境?

I just ran into this, and was able to get around it by creating a Chart Customizer class. 我刚遇到这个问题,并且可以通过创建Chart Customizer类来解决它。

In my .jrxml: 在我的.jrxml中:

<chart evaluationTime="Report" customizerClass="foo.Customizer" renderType="image">

My Customizer class then looks like this: 然后,我的Customizer类如下所示:

package foo;

import java.text.NumberFormat;
import java.util.Locale;

import net.sf.jasperreports.engine.JRAbstractChartCustomizer;
import net.sf.jasperreports.engine.JRChart;
import net.sf.jasperreports.engine.JRParameter;

import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;

public class Customizer extends JRAbstractChartCustomizer {

    public void customize(JFreeChart chart, JRChart jasperChart) {
        CategoryPlot plot = (CategoryPlot) chart.getPlot();
        NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setNumberFormatOverride(
          NumberFormat.getInstance((Locale)
          getParameterValue(JRParameter.REPORT_LOCALE)));
    }

}

Yeah, that code makes a lot of assumptions. 是的,该代码有很多假设。 But I always explicitly set the locale before running a report, so I know that's not null. 但是我总是在运行报告之前明确设置语言环境,因此我知道这不是null。 And I know my particular chart has a CategoryPlot and a NumberAxis, so I don't do the instanceof checks. 而且我知道我的特定图表有一个CategoryPlot和一个NumberAxis,所以我不执行instanceof检查。 But you get the idea. 但是你明白了。

Charles 查尔斯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM