SINTEF-9012 / PruneCluster

Fast and realtime marker clustering for Leaflet
MIT License
551 stars 131 forks source link

How to convert a large number on the type abbreviations in 1000 1k, 2m #126

Closed ivanssa closed 8 years ago

ivanssa commented 8 years ago

Hello I am using a script to convert large numbers in friendly style formats instagram ex 1k 1m 1b etc .. I would like to know how to implement this function in the cluster display number?

follows function used:

function abbreviateNumber(value) { var newValue = value; if (value >= 1000) { var suffixes = ["", "k", "m", "b","t"]; var suffixNum = Math.floor( (""+value).length/3 ); var shortValue = ''; for (var precision = 2; precision >= 1; precision--) { shortValue = parseFloat( (suffixNum != 0 ? (value / Math.pow(1000,suffixNum) ) : value).toPrecision(precision)); var dotLessShortValue = (shortValue + '').replace(/[^a-zA-Z 0-9]+/g,''); if (dotLessShortValue.length <= 2) { break; } } if (shortValue % 1 != 0) shortNum = shortValue.toFixed(1); newValue = shortValue+suffixes[suffixNum]; } return newValue; }

fungiboletus commented 8 years ago

Hello, you need to overload the BuildLeafletClusterIcon method in your prunecluster layer.

Like this:

leafletView.BuildLeafletClusterIcon = function (cluster) {
        var icon = PruneClusterForLeaflet.prototype.BuildLeafletClusterIcon.call(this, cluster);
        icon.options.html = "<div><span>" + abbreviateNumber(cluster.population) + "</span></div>";
        return icon;
};
ivanssa commented 8 years ago

@yellowiscool Thank you for the help, it worked perfectly, is a tip if you want to implement this functionality in the next release ...

ex: 1000 = 1k

maps

fungiboletus commented 8 years ago

It looks better yes :-)

ivanssa commented 8 years ago

Thank you... After seeing the possibility of including more this and other that I reported in https://github.com/SINTEF-9012/PruneCluster/issues/121 in the next release ...