Chartjs-How to order bubbles according their size and not by the order of their datasets?

0👍

The easiest way would be to just sort the data in the datasets and then the datasets themselves before drawing them.

An easy way to do this is provided by Array.prototype.forEach and Array.prototype.sort

First sort the data within each dataset like this:

config.data.datasets.forEach(function(element){
    element.data.sort(function (a, b) {
        return a.r - b.r;
    });
});

Then you can sort the data sets by their smallest element like this:

config.data.datasets.sort(function (a, b) {
    return a.data[0].r - b.data[0].r;
});

After that, you can regularly pass your config object with ordered datasets to your library call just the way you do it above:

const chart = new Chart(ctx, config);

Leave a comment