Chartjs-How do I change the label and value like 500,000 to 500k in Chart.js?

1👍

If you just want to add ‘k’, pass a function in the options ..

scaleLabel: function (v) {
    return v.value / 1000 + 'k';
}

.. but this will break for anything over a million (ie, 10,000,000 will be 10000k). So a function to fix that ..

function formatNumber( v ) {
    // Change the '1' here to adjust decimal places
    var numOfDecimalPlaces = Math.pow( 10, 1 ) ;
    var suffixList = [ "k", "m", "b", "t" ] ;

    for ( var i = suffixList.length - 1; i >= 0; i-- ) {
        var size = Math.pow( 10, ( i + 1 ) * 3 ) ;
        if( size <= v ) {
             v = Math.round( v * numOfDecimalPlaces / size ) / numOfDecimalPlaces ;
             if ( ( v == 1000) && ( i < abbrev.length - 1 ) ) {
                 v = 1 ;
                 i++ ;
             }
             v += suffixList[ i ] ;
             break ;
        }
    }

    return v ;
}

.. and call it with ..

scaleLabel: function (v) {
    return formatNumber(v.value) ;
}

Leave a comment