Home
Analytics
How to format series labels of a chart by script
donino
<div>Hi,</div>
<div> </div>
<div>I have a chart report displaying indicator values over time. This indicator is a report parameter and can represent either a percent (%) or a currency ($ or Millions $) value.</div>
<div> </div>
<div>Here is an example with a percent type:</div>
<div> </div>
<div>
Find more posts tagged with
Comments
Clement Wong
<p>In the chart's onRender > beforeDrawDataPointLabel event, you can change the label for each of the points. You'll need to check your parameter to see which format to use and then format accordingly. Example:</p>
<pre class="_prettyXprint">
function beforeDrawDataPointLabel( dph, label, icsc )
{
//currentPoint = dph.getOrthogonalValue();
if (icsc.getExternalContext().getScriptable().getParameterValue("paramValue") == "percent")
label.getCaption().setValue(parseFloat(dph.getOrthogonalValue()).toFixed(1)+" %");
else
label.getCaption().setValue("$" + parseFloat(dph.getOrthogonalValue()).toFixed(2));
}
</pre>
donino
<p>Great suggestion! </p>
<p> </p>
<p>In my case each user can specify his favorite output format for each indicator and store this format in a database field, among many other things such alert thresholds etc. Therefore i slightly changed your example by using a "DecimalFormat" object such below. it works like a charm, thanks! </p>
<pre class="_prettyXprint">
importPackage(Packages.java.text);
nf = new DecimalFormat(getGlobalVariable("numberFormat"));
function beforeDrawDataPointLabel( dph, label, icsc ){
label.getCaption().setValue(nf.format(dph.getOrthogonalValue()));
}</pre>