Discussions
Categories
Groups
Community Home
Categories
INTERNAL ENABLEMENT
POPULAR
PUBLIC CLOUD
PRIVATE CLOUD
Quick Links
MY LINKS
HELPFUL TIPS
Back to website
Home
Intelligence (Analytics)
Stacked bar chart - Displaying total on top
frames88
<p>I want to display totals on the stacked bar chart. Since there is no direct option to set this, I am trying to achieve this by scripting. </p><p> </p><p>I have computed the totals, but stuck in identifying the top stack to the label. How to identify the top stack for each bar?</p><p> </p><p>Here is my code on onRender event:</p><p> </p><p>[color=#000000;][font="consolas;"]<span style="font-size:10pt;">totalsArray = [/color][/font]</span><strong>[color=#7f0055;][font="consolas;"]<span style="font-size:10pt;">new[/color][/font]</span></strong>[color=#000000;][font="consolas;"]<span style="font-size:10pt;"> Array();[/color][/font]</span></p><p> </p><p><strong>[color=#7f0055;][font="consolas;"]<span style="font-size:10pt;">function[/color][/font]</span></strong>[color=#000000;][font="consolas;"]<span style="font-size:10pt;"> afterDataSetFilled(series, dataSet, icsc) {[/color][/font]</span></p><p>[color=#000000;][font="consolas;"]<span style="font-size:10pt;"> // Computed Totals for each bar from dataSet and stored in totalsArray.[/color][/font]</span></p><p>[color=#000000;][font="consolas;"]<span style="font-size:10pt;">}[/color][/font]</span></p><p> </p><p><strong>[color=#7f0055;][font="consolas;"]<span style="font-size:10pt;">function[/color][/font]</span></strong>[color=#000000;][font="consolas;"]<span style="font-size:10pt;"> beforeDrawDataPointLabel(dph, label, icsc) {[/color][/font]</span></p><p>[color=#000000;][font="consolas;"]<span style="font-size:10pt;"> // How to identify the top stack so that I can set the label here?[/color][/font]</span></p><p><strong>[color=#7f0055;][font="consolas;"]<span style="font-size:10pt;"> if[/color][/font]</span></strong>[color=#000000;][font="consolas;"]<span style="font-size:10pt;"> ( ####### top stack ###### ){[/color][/font]</span></p><p>[color=#000000;][font="consolas;"]<span style="font-size:10pt;"> label.getCaption().setValue(totalsArray[dph.getIndex()] );[/color][/font]</span></p><p>[color=#000000;][font="consolas;"]<span style="font-size:10pt;"> label.setVisible([/color][/font]</span><strong>[color=#7f0055;][font="consolas;"]<span style="font-size:10pt;">true[/color][/font]</span></strong>[color=#000000;][font="consolas;"]<span style="font-size:10pt;">);[/color][/font]</span></p><p>[color=#000000;][font="consolas;"]<span style="font-size:10pt;"> } [/color][/font]</span><strong>[color=#7f0055;][font="consolas;"]<span style="font-size:10pt;">else[/color][/font]</span></strong>[color=#000000;][font="consolas;"]<span style="font-size:10pt;">{[/color][/font]</span></p><p>[color=#000000;][font="consolas;"]<span style="font-size:10pt;"> label.getCaption().setValue([/color][/font]</span>[color=#2a00ff;][font="consolas;"]<span style="font-size:10pt;">""[/color][/font]</span>[color=#000000;][font="consolas;"]<span style="font-size:10pt;">);[/color][/font]</span></p><p>[color=#000000;][font="consolas;"]<span style="font-size:10pt;"> }[/color][/font]</span></p><p>[color=#000000;][font="consolas;"]<span style="font-size:10pt;">}[/color][/font]</span></p><p> </p><p> </p>
Find more posts tagged with
Comments
GLO_FR
<p>Hi,</p><p> </p><p>I know 2 solutions. </p><p> </p><p>First one is very simple and doen't need scripting. First, you need to create a "TOTAL" category in your dataset.</p><p>The trick is to use a second serie (line) which only show the TOTAL category. Unfortunately, for this example, you have to hide the legend or to find a way to remove some entries (I didn't find a solution for this yet
)</p><p> </p><p>For the second one, the starting point is to create a fake "TOTAL" category in your dataset and an array with the real total value (more or less what you have already done).</p><p>You have to choose a name so the category will be placed on the top of your chart (in my case ZZZ-TOTAL)</p><p>Afterward, with some scripting you will be able to update everything.</p><p> </p><p>sorry, it's hard to explain that why I join 2 examples</p><p> </p><p> </p>
SChu
<p>I have two solutions for this issue, to simply add a total over stacked bars as originally requested, this client script can be used:</p>
<p> </p>
<p> </p>
<blockquote class="ipsBlockquote">
<p> </p>
<p>beforeGeneration: function(options)<br>
{<br>
totalArray = []; //Define Array<br>
},<br><br>
beforeDrawDataPoint: function(point, pointOptions, chart, seriesIndex, pointIndex)<br>
{<br>
if(totalArray[point.x]){ //If array value already exists<br>
var tmp = totalArray[point.x]; //Get array value<br>
tmp += point.y; //Add next series value to total<br>
totalArray[point.x] = tmp; //Put new value back in array<br>
}<br>
else{ //If array value doesnt already exist<br>
totalArray[point.x] = point.y; //Create new array value<br>
}<br>
},<br><br>
beforeRendering: function(options, chart)<br>
{<br>
options.yAxis[0] ={ //Get yAxis 0<br>
stackLabels: { //Define stackLabels<br>
enabled: true, //Enable stackLabels<br>
style: { //Define style<br>
fontWeight: 'Bold',<br>
color: '#000', //Color to black<br>
},<br>
formatter: function() { //Label formatter<br>
var places = 1;<br>
if(!totalArray[this.x]){ //Check whether the value is a null<br>
return null;<br>
}<br>
else {<br>
var decCheck = totalArray[this.x].toFixed(places); //Loop through totalArray and display values<br>
if(totalArray[this.x].toFixed(places) == 0) { //avoid labeling if there is no value<br>
return null;<br>
}<br>
else {<br>
if (decCheck.toString().slice(-1) == 0){ //Check for trailing zero<br>
return parseInt(decCheck) + "M"; //drop trailing zero and append units<br>
}<br>
else {<br>
return decCheck + "M"; //return value if trailing value is not zeroand append units<br>
}<br>
}<br>
}<br>
}<br>
}<br>
} <br>
},</p>
</blockquote>
<p> </p>
<p>Second, on GLO_FR's point about hiding a Total series, below is a beforeGeneration script for the chart to achieve a total line when a "Total" value exists in your Y-Axis grouping.</p>
<p> </p>
<p>Note: I used two Y-axes for this so that I could toggle visibility on the bars and compare those slices to the total trend, for a single axis the script would need adjustment.</p>
<p> </p>
<blockquote class="ipsBlockquote">function beforeGeneration( chart, icsc )<br>
{<br>
xAxis = chart.getAxes().get(0);<br>
yAxis1 = xAxis.getAssociatedAxes().get(0);<br>
sd1 = yAxis1.getSeriesDefinitions().get(0);<br><br>
yAxis2 = xAxis.getAssociatedAxes().get(1);<br>
sd2 = yAxis2.getSeriesDefinitions().get(0);<br><br><br><br>
for(i=0;i<sd1.getSeries().size();i++){<br><br>
if(sd1.getSeries().get(i).getSeriesIdentifier() == "Series 1"){<br>
sd1.getSeries().get(i).setVisible(true);<br>
}<br>
else if(sd1.getSeries().get(i).getSeriesIdentifier() == "Total"){<br>
sd1.getSeries().get(i).setVisible(false);<br>
}<br>
else{<br>
sd1.getSeries().get(i).setVisible(true);<br>
}<br>
}<br>
for(i=0;i<sd2.getSeries().size();i++){<br><br>
if(sd2.getSeries().get(i).getSeriesIdentifier() == "Series 1"){<br>
sd2.getSeries().get(i).setVisible(true);<br>
}<br>
else if(sd2.getSeries().get(i).getSeriesIdentifier() == "Total"){<br>
sd2.getSeries().get(i).setVisible(true);<br>
}<br>
else{<br>
sd2.getSeries().get(i).setVisible(false);<br>
}<br>
}<br>
}</blockquote>
<p> </p>
<p> </p>