简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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