Forum Discussion

cartercjb's avatar
01-14-2022
Solved

Dynamic Decimal Points

Hello Sisense Community!

Is anyone aware of a method to dynamically round data labels so that: 

  1. Any value >= 1000.00 shows two decimal points (green in the screenshot)
  2. Any value < 1000.00 shows 0 decimal points (yellow in the screenshot)

 

  • cartercjb 

    I think there are 2 solution : 

    1. You can choose automatic rounding in 'Format Number' window. It will automatically remove '.00' from values (here we cannot control number of digits after decimal point as it is deciding decimal places dynamically)

     

    2. Here is a script for this purpose : 

    //Apply rounding in datalabel
    widget.on('processresult', function(se,ev){
    	
    	ev.result.plotOptions.series.dataLabels.formatter = function(){
    		return this.y < 1000 ? this.y.toFixed(0) : ev.result.series[0].mask(this.y)
    	}
    	
    })
    
    //applying rounding in tooltip
    widget.on('beforedatapointtooltip', function(se,ev){
    	
    	if(ev.context.pointScope.y < 1000)
    		ev.context.points[0].value = ev.context.pointScope.y.toFixed(0)
    		
    })

    (Note : percentage/currency symbol will not work with this script)

     

    Thanks,

    -Hari

6 Replies

Replies have been turned off for this discussion
  • Hey!
    You can achive this using simple widget script, please refer to this one:

    widget.on("beforeviewloaded", function (se, ev) {
    
        ev.options.plotOptions.series.dataLabels.formatter = function () {
            let y = this.y
    
            if (y >= 1000) {
                y = y.toFixed(2);
    
            } else {
    			y = y.toFixed(); 
    		}
            return y
        }
    })

     If you have any more questions feel free to reach out! We’re always here to help!

    • cartercjb's avatar
      cartercjb
      ETL

      Hi Alek_qbeeq - 

      Thanks for your suggestion and feedback! This script is super close, but off just a little when the values > 1000. It is reading as "1313.00" instead of "1.31 k". 

       

  • harikm007's avatar
    harikm007
    Data Warehouse

    cartercjb 

    I think there are 2 solution : 

    1. You can choose automatic rounding in 'Format Number' window. It will automatically remove '.00' from values (here we cannot control number of digits after decimal point as it is deciding decimal places dynamically)

     

    2. Here is a script for this purpose : 

    //Apply rounding in datalabel
    widget.on('processresult', function(se,ev){
    	
    	ev.result.plotOptions.series.dataLabels.formatter = function(){
    		return this.y < 1000 ? this.y.toFixed(0) : ev.result.series[0].mask(this.y)
    	}
    	
    })
    
    //applying rounding in tooltip
    widget.on('beforedatapointtooltip', function(se,ev){
    	
    	if(ev.context.pointScope.y < 1000)
    		ev.context.points[0].value = ev.context.pointScope.y.toFixed(0)
    		
    })

    (Note : percentage/currency symbol will not work with this script)

     

    Thanks,

    -Hari

    • cartercjb's avatar
      cartercjb
      ETL

      Thanks, harikm007! This script worked well! Fortunately, I do not need symbols for this metric. But, is there no way to append a symbol on top of the script you provided?

      Thanks,

      Carter

    • cartercjb's avatar
      cartercjb
      ETL

      harikm007  Hari - 

      Is this script specific to an unstacked chart? I tried adding this to a stacked chart and there was no change to the data labels.