cartercjb
01-11-2022ETL
DYNAMICALLY FORMATTED DATA LABELS
Does anyone have suggestions on how to do the following:
- Script the data labels so that they never overlap? I'm not sure if that's possible but I can hope!
- Add additional formatting around the data labels similar to the screenshot below where each data label is surrounded in a colored shape?
I believe there is insufficient space on the right side of the chart, which is why the data label is visible inside the bar. One way to fix this is to dynamically increase the Max value. The script has been updated. Please change value of the variable 'increasePercent' as required.
//Formatting datalabels widget.on('processresult', function(se,ev){ var maxValue = 0 var increasePercent = 0.2 //variable to adjust right space. Change this value as needed $.each(ev.result.series, function(seriesIndex, value){ $.each(value.data, function(di, dataItem){ if(dataItem.y > maxValue) maxValue = dataItem.y }) value.dataLabels = { backgroundColor: '#f5d142', //data label color color: 'white', padding: 5, borderRadius: 5 } }) ev.result.yAxis[0].max = maxValue + (increasePercent * maxValue) ; }) //Adjusting position of datalabels widget.on('domready', function(se,ev){ barWidth = $('.highcharts-series-group .highcharts-series rect', element).width() $('.highcharts-data-labels .highcharts-label', element).each(function(){ labelWidth = $(this).find('rect').width() labelHeight = $(this).find('rect').height() $(this).find('rect').attr('x', ($(this).find('rect').attr('x') + 2)) $(this).find('rect').attr('height', barWidth) $(this).find('rect').attr('y', ((labelHeight - barWidth)/2)) }) });
Thanks
-Hari