Ask a Jedi: Showing the values on a chart

Misha asks:

Hi Raymond, I would really appreciate you if you could help me with CFCHART (CF8). In CF5 in CFGRAPH it was possible to display the value by using showValueLabel attribute. I don't see anything in CF8. Is it possible to display the value from the ValueColumn attribute on the actual bar itself?

At first I wasn’t sure what she meant. I could easily see the values when I moused over the bars in a barchart. But after speaking with her I figured out that what she really wanted was to see the values without a mouseover. This would be especially useful on a printout. Turns out this is rather easy. You can use the dataLabelStyle attribute to specify a label for your data points. Valid values are none (default), value, rowLabel (kinda silly), columnLabel (ditto), pattern (a data pattern you can’t modify, more on that in a bit). So as a concrete example, consider this chart:

<cfchart format="flash" xaxistitle="respondents" yaxistitle="numbers">

&lt;cfchartseries type="bar" serieslabel="result" seriescolor="##6633CC" datalabelstyle="value"&gt;
	&lt;cfchartdata item="result1" value="75"&gt;
	&lt;cfchartdata item="result2" value="80"&gt;
&lt;/cfchartseries&gt;

</cfchart> </code>

Which creates:

Almost right - the chart isn’t quite tall enough. We can fix that by using scaleTo:

<cfchart format="flash" xaxistitle="respondents" yaxistitle="numbers" scaleTo="100">

That’s better, but obviously we would have to make scaleTo a bit dynamic if we weren’t sure of our value range. I mentioned ‘pattern’ above. What does that do?

That’s cool, but you don’t have control over what is there. It is kind of silly that the label is repeated, but that second line is really nice.

That got me digging into the chart editor. First I was curious about the data label placement. What if you didn’t want to use scaleTo? In my digging I found that you can use an ‘Extra Lines’ attribute. If set to something above 0, like 1 for example (duh), the chart will always ensure there is an extra line. This handles ensuring the values fit ok. You can also switch to an inner display as well. Here is an example with extra lines turned on:

<cfsavecontent variable="style"> <?xml version="1.0" encoding="UTF-8"?> <frameChart is3D="false"> <yAxis scaleMin="0"> <labelFormat pattern="#,##0.###"/> <parseFormat pattern="#,##0.###"/> </yAxis> <legend allowSpan="true" equalCols="false" isVisible="false" showColumnLegend="false" halign="Right" isMultiline="true"> <decoration style="None"/> </legend> <dataLabels style="Value" extraLines="1" autoControl="true"/> </frameChart> </cfsavecontent>

<cfchart format=”flash” xaxistitle=”respondents” yaxistitle=”numbers” style=”#style#”>

&lt;cfchartseries type="bar" serieslabel="result"&gt;
	&lt;cfchartdata item="result1" value="75"&gt;
	&lt;cfchartdata item="result2" value="80"&gt;
&lt;/cfchartseries&gt;

</cfchart>

<p/> </code>

Notice I also turned off the legend in the chart. This resulted in:

So - remember how I said that pattern was hard coded? Well with the chart editor you can muck with that. Consider this XML:

<dataLabels style="Pattern" placement="Inside" extraLines="1" autoControl="true"> <![CDATA[ $(value) [$(colPercent) of $(colTotal)] ]]> </dataLabels>

I specified both the value, and the percent of total, and got rid of the newline there by default. Also note I moved the labels inside. I could get rid of extraLines now if I wanted to. This resulted in the following chart:

Pretty cool! But I’d probably want to change that font color. Anyway, I hope this is useful!

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless 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

Comments