views:

794

answers:

2

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 some way to configure JFreeChart to use a particular Locale so that when Jasper calls it, it uses that Locale?

A: 

I recall most JFreeChart methods having a locale parameter. Doesn't Jasper pass it along?

jitter
No. I've looked at the source code and Jasper carefully propagates the parameter-Map which includes the Locale to the reporting code but not to the charting code.
Adrian Pronk
A: 

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

Charles O.
Thanks, I'll try this the next time I'm working on the charting code and post an update on how it goes
Adrian Pronk
I just realized I didn't exactly answer your question -- my issue was that numbers weren't being formatted with the correct decimals and thousands separators according to the locale, whereas you want month names to be localized. But I think you can apply the same concept -- you've probably got a DateAxis instead of a NumberAxis, and you should be able to call DateAxis.setDateFormatOverride(). My customizer definitely works for what I'm doing; what I'm suggesting in this comment is untested but should work.
Charles O.