2

I am displaying a bar chart and trying to have the labels above bars displayed in correct locale (they are floats). I am developing in JasperSoft Studio 6.2.0. I set the global and/or the report (execution-time) locale to en_US, but the labels are still displayed in my Windows locale (nl_NL). I then set the label expression to

new DecimalFormat("#,##0.0##;(#,##0.0##-)").format($F{Hours})

but it's still in Windows locale. Only when I explicitly set the label expression to en_US locale:

NumberFormat.getInstance(Locale.US).format($F{Hours})

do I get the correct result. At other places (TextFields), setting the format pattern (e.g. to "#,##0.0##;(#,##0.0##-)") leads to the correct locale being applied. In bar chart settings, there is no way to specify the pattern in the same fashion, that's why I am trying to do that in code.

Is this a bug or am I missing something?

Alex K
  • 22,315
  • 19
  • 108
  • 236
Andrey Ch
  • 193
  • 1
  • 12
  • I see I can use `NumberFormat.getInstance($P{REPORT_LOCALE}).format($F{Hours})`, but this doesn't let me set the pattern and is anyway far from ideal. – Andrey Ch Apr 03 '16 at 08:35
  • You can use $P{REPORT_FORMAT_FACTORY}.createNumberFormat("#,##0.0##;(#,##0.0##-)", $P{REPORT_LOCALE}).format($F{Hours}) This would create a fresh formatter on each call, which is obviously not ideal from a performance perspective. – dada67 Apr 04 '16 at 18:36

1 Answers1

2

Yes I verified, jasper reports do not use its $P{REPORT_LOCALE} when generating the chart and I would almost consider it a bug. They use metods to generate the chart that does not support passing the Locale, but they could automatically generate customizers with correct locale.

To get desired Locale in chart label your options are.

Set default locale of the entire application.

Locale.setDefault(Locale.US);

see Setting java locale settings for other methods as passing parameter when launching.

If you only want to change the Locale of the label in your chart you need to create a JRChartCustomizer

Example for BarChart

public class MyLocaleCustomizer implements JRChartCustomizer{
    @Override
    public void customize(JFreeChart chart, JRChart jrchart) {
        CategoryPlot plot = (CategoryPlot) chart.getPlot();
        StandardCategoryItemLabelGenerator lg = new StandardCategoryItemLabelGenerator("{2}",NumberFormat.getNumberInstance(Locale.US));
        plot.getRenderer().setBaseItemLabelGenerator(lg);
    }
}

In jrxml

<barChart>
    <chart customizerClass="MyLocaleCustomizer">
        ..
    </chart>
    ..
</barChart>
Community
  • 1
  • 1
Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
  • 1
    A bug indeed, fixed at https://sourceforge.net/p/jasperreports/code/ci/8387491cbd8077639cfc8869953664af8ccc4da4/ – dada67 Apr 04 '16 at 19:25
  • data67 thanks. So how do I now get the locale in a customizer? *** public class AssetsChartMod implements JRChartCustomizer { **** public void customize(JFreeChart chart, JRChart jasperChart) { – DavidDunham Jun 29 '17 at 15:55