cancel
Showing results for 
Search instead for 
Did you mean: 
intapiuser
Community Team Member
Community Team Member
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;
 


});
Comments
dayna
7 - Data Storage
7 - Data Storage

this is great. just what I was looking for. thanks! 

dayna
7 - Data Storage
7 - Data Storage

this works well when i have a dimension as my category but not with a date field as the category. Any idea on how to update it for time series?

Version history
Last update:
‎03-02-2023 09:18 AM
Updated by:
Contributors
Community Toolbox

Recommended quick links to assist you in optimizing your community experience:

Developers Group:

Product Feedback Forum:

Need additional support?:

Submit a Support Request

The Legal Stuff

Have a question about the Sisense Community?

Email [email protected]

Share this page: