cancel
Showing results for 
Search instead for 
Did you mean: 

DYNAMICALLY FORMATTED DATA LABELS

cartercjb
9 - Travel Pro
9 - Travel Pro

Does anyone have suggestions on how to do the following: 

cartercjb_0-1641935078307.png

  1. Script the data labels so that they never overlap? I'm not sure if that's possible but I can hope!
  2. Add additional formatting around the data labels similar to the screenshot below where each data label is surrounded in a colored shape?

Bar.png

1 ACCEPTED SOLUTION

@cartercjb 

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

View solution in original post

8 REPLIES 8

harikm007
9 - Travel Pro
9 - Travel Pro

Hi @cartercjb ,

1. I found this article to separate overlapping data labels : 

https://support.sisense.com/kb/en/article/separating-overlapping-value-labels

2. Please try this widget script for column chart:

//Formatting datalabels 
widget.on('processresult', function(se,ev){
	
	$.each(ev.result.series, function(index, value){
		value.dataLabels = {
									backgroundColor: value.color,
									color: 'white',
									padding: 5,
									borderRadius: 5
								}
	
	})
	
})

//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('width', barWidth)
		$(this).find('rect').attr('height', labelHeight - 5)
		$(this).find('rect').attr('x', ((labelWidth - barWidth)/2))
	
	})

});

This is how the column chart will looks like after applying the above script : 

Formatted datalabels.jpg

Thanks

-Hari

@harikm007 Hari,

First off, THANK  YOU!! You are extremely helpful. Two follow-up questions. Please reference the screenshot below. 

  1. Is there anything that can be done to make the spacing more dynamic? See the green bars on the REHAB DAYS widget? There is no space in between the bars and the label.
  2. Is there a way to make all data labels 1 single color?

 

cartercjb_0-1642186546932.png

 

@cartercjb 

Previous script is only for column chart. Please try below script for bar chart. Also script is updated to make all data labels same color

//Formatting datalabels 
widget.on('processresult', function(se,ev){
	
	$.each(ev.result.series, function(index, value){
		value.dataLabels = {
									backgroundColor: '#f5d142', //data label color
									color: 'white',
									padding: 5,
									borderRadius: 5
								}
	
	})
	
})

//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

@harikm007 Hari - 

Thanks for your feedback! That definitely helped - but there is still a bit of overlap that you will notice on the largest blue bar. Is there any additional scripting that can be done or is that as good as it will get? Thanks!

-Carter

 

cartercjb_0-1642197980340.png

 

@cartercjb 

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

View solution in original post

BOOM! That worked! Thank you so much! @harikm007 

 

@harikm007  what would the equivalent of this be for column chart?

@cartercjb This is the script for column chart : 

//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: 'black',
									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('width', barWidth)
		$(this).find('rect').attr('height', labelHeight - 5)
		$(this).find('rect').attr('x', ((labelWidth - barWidth)/2))
	
	})

});

Thanks

-Hari