开发者

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.

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.

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

Does anyone know if there's开发者_C百科 some way to configure JFreeChart to use a particular Locale so that when Jasper calls it, it uses that Locale?


I just ran into this, and was able to get around it by creating a Chart Customizer class.

In my .jrxml:

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

My Customizer class then looks like this:

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. And I know my particular chart has a CategoryPlot and a NumberAxis, so I don't do the instanceof checks. But you get the idea.

Charles

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜