Formatting numbers on chart (tooltip, scales, etc)


#1

How can i format a number in chart? i Use the template #total# as tooltip, it appears 2567388.95 an i need to be 2,567,388.97

tooltip: “#total#”

Sorry for my bad english and Thank you so much.

Jose Rodriguez


#2

The chart can use any property from the dataset as part of tooltips. It is rather limited in formatting though. So the best strategy will be to have a separate field in the loaded dataset with the formatted value.


#3

Thanks a lot, i was resolved as:

tooltip: {
template: function(obj) {
return '$ ’ + number_format(obj.total, 2);
}
}

function number_format(amount, decimals) {

    amount += ''; // por si pasan un numero en vez de un string
    amount = parseFloat(amount.replace(/[^0-9\.]/g, '')); // elimino cualquier cosa que no sea numero o punto

    decimals = decimals || 0; // por si la variable no fue fue pasada

    // si no es un numero o es igual a cero retorno el mismo cero
    if (isNaN(amount) || amount === 0)
        return parseFloat(0).toFixed(decimals);
    // si es mayor o menor que cero retorno el valor formateado como numero
    amount = '' + amount.toFixed(decimals);
    var amount_parts = amount.split('.'),
            regexp = /(\d+)(\d{3})/;
    while (regexp.test(amount_parts[0]))
        amount_parts[0] = amount_parts[0].replace(regexp, '$1' + ',' + '$2');
    return amount_parts.join('.');
}