Bar Chart - Prevent bars from hiding the value label text
Updated 03-25-2022
Hello! This script doesn't seem to work on stacked bar charts. On regular (non-stacked) bar charts it mostly works as expected, and I can adjust the number in the first line to adjust how much space there is. However, when I try the same script on stacked bar charts, it doesn't seem to have any effect, regardless of which number I put in the first line.
I bet it's due to the break-by splitting the bars into multiple stacked bars, and the script is only designed to shorten single bars. Is there a way to modify the script so it works on stacked bars instead?
Hi fabricio_fta and Jake_Raz, please try this script below. This should work for:
/*** This script is for increasing the y-axis max value by adding a percent of the highest value in the series to the y-axis max value.
Similarly, in cases where the minimum value is negative, it also decreases the y-axis min value by subtracting a percent of the lowest value in the series from the y-axis min value. ***/
//Set the offset percent to add/subtract to/from the y-axis max/min value here
var percentToAdd = 20;
widget.on('processresult', function(se,ev){
var numberOfSeries = ev.result.series.length;
var seriesData = [];
var dataPoints = [];
for(i=0; i < numberOfSeries; i++) {
seriesData[i] = ev.result.series[i].data;
var numOfDataPoints = seriesData[i].length;
if(widget.subtype.includes('stacked')) {
if(i==0) {
for(j=0; j < numOfDataPoints; j++) {
var dataToPush = seriesData[i][j].y;
dataPoints.push(dataToPush);
}
}
else {
for(j=0; j < numOfDataPoints; j++) {
dataPoints[j] = dataPoints[j] + seriesData[i][j].y;
}
}
}
else if(widget.subtype.includes('classic') || widget.subtype.includes('basic')) {
for(j=0; j < numOfDataPoints; j++) {
var dataToPush = seriesData[i][j].y;
dataPoints.push(dataToPush);
}
}
}
var maxValue = Math.max.apply(Math, dataPoints);
var minValue = Math.min.apply(Math, dataPoints);
if(ev.result.yAxis[0].max <= 0) {
ev.result.yAxis[0].max = 0
}
else {
ev.result.yAxis[0].max = maxValue * (1 + percentToAdd/100);
}
if(ev.result.yAxis[0].min >= 0) {
ev.result.yAxis[0].min = 0;
}
else {
ev.result.yAxis[0].min = minValue * (1 + percentToAdd/100);
}
})
This seems to work! Thanks!