cfchart and non-American locales

This post is more than 2 years old.

A poster on cf-talk today noticed an interesting issue. Even though she was trying to use the English (UK) locale (or as I call it, Dr. Who's locale), her cfchart was not using Pound symbols for the values. I whipped up a quick example to verify this issue. Luckily, it's easy to get around with - you guessed it - the chart style editor.

First, an example that demonstrates the bug.

<cfset setLocale("English (UK)")>

<cfchart chartheight="500" chartwidth="500" title="Average Price Per" labelFormat="currency">

&lt;cfchartseries type="bar"&gt;
	&lt;cfchartdata item="Apples" value="50.99"&gt;
	&lt;cfchartdata item="Bananas" value="40.12"&gt;
	&lt;cfchartdata item="Cherries" value="72.00"&gt;
	&lt;cfchartdata item="Donuts" value="61.21"&gt;
&lt;/cfchartseries&gt;

</cfchart>

This produces:

You can see both on the left hand side, and in the tool tip, the values are in American dollars. To fix this, I simply opened up the chart editor, clicked the Y-Axis section, and picked Format. I changed Style to currency and then turned off the click for system locale.

I took - and stripped down - the XML to get the following code:

<cfsavecontent variable="style"> <?xml version="1.0" encoding="UTF-8"?> <frameChart is3D="false"> <yAxis scaleMin="0"> <labelFormat style="Currency" pattern="#,##0.00"> <locale lang="en" country="GB" variant=""/> </labelFormat> <parseFormat pattern="#,##0.###"/> <groupStyle> <format pattern="#,##0.###"/> </groupStyle> </yAxis> </frameChart> </cfsavecontent>

<cfchart chartheight="500" chartwidth="500" title="Average Price Per" labelFormat="currency" style="#style#">

&lt;cfchartseries type="bar"&gt;
	&lt;cfchartdata item="Apples" value="50.99"&gt;
	&lt;cfchartdata item="Bananas" value="40.12"&gt;
	&lt;cfchartdata item="Cherries" value="72.00"&gt;
	&lt;cfchartdata item="Donuts" value="61.21"&gt;
&lt;/cfchartseries&gt;

</cfchart>

And here is the result:

Fixed! In case you're wondering about the other changes, when you use cfchart and don't specify an XML file, ColdFusion passes a set of values based on defaults and the arguments you used. When you specify an XML style yourself, those defaults go away. Sometimes this means a bit more work, but overall you get much more control over the final result.

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by Lou posted on 5/19/2011 at 9:53 PM

Is that chart editor in dreamweaver or CFbuilder?

Comment 2 by Raymond Camden posted on 5/19/2011 at 9:55 PM

The chart editor ships with ColdFusion.

Comment 3 by Steve W posted on 5/19/2011 at 11:15 PM

For those that aren't familiar, webcharts.bat can found in the charting folder.

Comment 4 by Raymond Camden posted on 5/19/2011 at 11:17 PM

Thanks Steve. Yeah, that detail woulda been helpful. ;)

Comment 5 by Jenny Gavin-Wear posted on 5/20/2011 at 6:18 PM

The cause of the currency hitch is the underlying JRE from which CF takes the locale setting, not from setlocale.

If you have control of your server you can make the locale change to the JRE.

Here is an example in CFadmin => Server Settings => Java and JVM

-server -Dsun.io.useCanonCaches=false -XX:MaxPermSize=128m -Dcoldfusion.rootDir={application.home}/../ -Dcoldfusion.libPath={application.home}/../lib -Duser.language=en -Duser.region=GB

To change the locale, just append the last two entries:-

-Duser.language=en -Duser.region=GB

With your language and locale. This will need a server reboot.

Just to add a little to Ray's post about using styles and xml.
Firstly, you can either have all of the cfchart attribues in styles, or none of them, you can't mix.

If you are using a xml file instead of cfsavecontent, the path to is is the same as if you were using a cfinclude.

For reference, here is a list of all of the JRE locales:-

http://download.oracle.com/...

Comment 6 by Raymond Camden posted on 5/20/2011 at 6:30 PM

Thanks for adding that Jenny!