Knowledge Base Article

Changing Value Label's Font Size, Weight And Rotation

UPDATED SCRIPT IN COMMENTS
 
Introduction
Value labels at the floating figures above a series in a bar chart, column chart, or line chart.  
Purpose/Benefits
Changing value labels can be useful to make a measure stand out more.
Example
Steps
1.  Open the Edit Script window in the widget editor.  
2.  Paste the below code.
3.  Change "YourMeasure" to the measure name you would like to change.  (note: be sure to keep the quotes)
4.  Click Save
5  Refresh the dashboard page and click Apply.
/******* user configuration **********/ 
var seriesName = "YourMeasure"; 
/*************************************/ 
widget.on('processresult', function(sender, ev){ 

var data = _.find(ev.result.series, function (ser) {return ser.name == seriesName}).data 
_.each(data, function(value){ 
//enable dataLabels, change its font, make it bold and rotate by 270 
value.dataLabels = {enabled:true, style:{'fontSize':'35px', 'fontWeight':'bold'}, rotation: 270} 
}) 
})
Updated 07-22-2024

16 Comments

  • Hi SiftHealthcare, are you referring to the 'y' property below?

    // Change the y position offset of the labels
    series.dataLabels.y = 40;

    This property is for the vertical offset of the label relative to the point in pixels. It cannot be set to a percentage.

    However, in a bar chart, you likely want to configure the 'x' property instead, which is the horizontal offset of the label relative to the point.

    For example, here is how the labels look like with x = 0.

    Here is how they look like with x = 40.

    Here is how they look like with x = -60.

    Let me know if this is helpful.

  • Hi Tri,

    Thanks for the update but unfortunately this does not resolve my issue. In your screenshots the highest value is still not showing. The issue I am trying to correct is to show all data labels on top of the column or bar. 

  • SiftHealthcare Thank you for clarifying that! To ensure all data labels are shown, you can use the script below to dynamically set the y-axis maximum value. Set the variable percentToAdd to the percentage of the maximum value to add to the y-axis, e.g. if the maximum value in the chart is 100, setting the percentToAdd variable to 10 will change the y-axis max value to 110.

    Let me know if this works for you.

    /***  This script is for increasing the y-axis max value by adding a percent of the maximum value from the series to the y-axis. ***/
    
    //Set the offset percent to add to the y-axis max value here
    var percentToAdd = 10;
    
    widget.on('processresult', function(se,ev){
    	
    	var seriesData = ev.result.series[0].data;
    	var numOfDataPoints = seriesData.length;
    	var dataPoints = [];
    
    	for(i=0; i < numOfDataPoints; i++) {
    		dataPoints.push(seriesData[i].y)
    	}
    	
    	var maxValue = Math.max.apply(Math, dataPoints);
    	
    	ev.result.yAxis[0].max = maxValue * (1 + percentToAdd/100);
    	
    })

     

  • Hi SiftHealthcare, I wanted to follow up on my previous comment. The script I sent in that comment is limited to just positive values and classic (non stacked) chart. Please use the updated script below instead.

    This script works for:

    • all Cartesian charts (Column, Bar, Line, and Area charts),
    • negative values,
    • single and multiple series (classic or stacked, using Break By or multiple values).
    /***  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);
    	}
    
    
    })
  • Hi TriAnthony thanks for the update! However it is working for some widgets but not for others. Any ideas why this would happen?