Draw Total Line For A Stacked Column Or Bar Chart
Introduction
This script displays a total line over your stacked column / bar chart. It also works with the classic (side by side) grouping.

Instructions
Create your chart and add the following script. Edit > Options Menu > Edit Script.
Note: You can modify the first few lines under the Options comment to customize the line color, line type (line or spline) and label options.
widget.on('processresult', function(w, args) {
//debugger;
//Options
var lineColor = 'grey';
var lineTitle = 'Grand Total';
var lineType = 'spline';
var showLabels = true;
var sTotals = {};
var chartS = args.result.series;
//Calculate the Grand Totals
for(var s = 0; s < chartS.length; s++ ) {
for(var d = 0; d < chartS[s].data.length; d++) {
try {
var cat = '' + chartS[s].data[d].selectionData[0];
// check if the value is a date, and find the formatted label
if(w.metadata.panels[0].items[0].jaql.datatype == 'datetime') {
var dlevel = w.metadata.panels[0].items[0].jaql.level;
if(dlevel) {
var dformat = w.metadata.panels[0].items[0].format.mask[dlevel];
var dlabel = prism.dateFormatter(cat, dformat);
cat = dlabel;
}
}
// Add to the tally
if(sTotals[cat]) {
sTotals[cat] = sTotals[cat] + chartS[s].data[d].y;
}
else {
sTotals[cat] = chartS[s].data[d].y;
}
}
catch(e){}
}
}
//create another series for the grand total
var totalSeries = $$.object.clone(args.result.series[0], true);
//create the data object for the series, because all sub groups may not be present each category on X Axis
var seriesData = $$.object.clone(args.result.series[0].data[0], true);
totalSeries.type = lineType;
totalSeries.marker = { "enabled" : true };
totalSeries.color = lineColor;
totalSeries.name = lineTitle;
totalSeries.data = [];
totalSeries.dataLabels = {"enabled" : showLabels };
var maxCum = 0.0;
//Get the XAxis Labels
var xCat = args.result.xAxis.categories;
for(var d = 0; d < xCat.length; d++) {
try {
var catSelect = xCat[d];
var sd = $$.object.clone(args.result.series[0].data[0], true);
totalSeries.data.push(sd) ;
totalSeries.data[d].y = sTotals[catSelect];
maxCum = Math.max(sTotals[catSelect], maxCum);
}
catch(e) {}
}
args.result.series.push(totalSeries);
//adjusting the max of the Y Axis
args.result.yAxis[0].max = maxCum;
});
Updated 03-02-2023
intapiuser
Admin
Joined December 15, 2022