How to set Locale to labels in Bar Chart?

假如想象 提交于 2020-01-04 05:50:10

问题


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?


回答1:


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>


来源:https://stackoverflow.com/questions/36383147/how-to-set-locale-to-labels-in-bar-chart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!