[Chartjs]-Sorting the Bars in Chart.JS Bar Graph

1đź‘Ť

âś…

Are you sure the “beforeupdate” is called when you click the “Sort Data” button, because it seems you use the wrong variable.

barChart → barGraph

UPDATED
The old one link
UPDATED
Here is the link

// Get the data from each datasets.
var dataArray = [];
$.each(chart.data.datasets, function() {
    dataArray.push(this.data);
});
// Get the index after sorted.
let dataIndexes = dataArray.map((d, i) => i);
dataIndexes.sort((a, b) => {
    return dataArray[a] - dataArray[b];
});
// create after sorted datasets.
var tempDatasets = [];
$.each(dataIndexes, function() {
    tempDatasets.push(chart.data.datasets[this]);
});
// apply it
chart.data.datasets = tempDatasets;

What data is returned using Ajax?

data = [{
    sample : "ALPINE",
    electric : 100,
    mpg: 200,
    urban: 300,
    vmt: 400,
    airtravel: 500,
    renewable: 100,
    conservation : 200,
    heating: 300,
    water: 400,
    cefficiency: 500,
    shiftc: 100,
    healthyd: 200
}];

Leave a comment