function findMaxIndexes(array, n) {
const maxValues = array.slice().sort((a, b) => b - a).slice(0, n);
return array.map((x, i) => [x, i]).filter(pair => maxValues.includes(pair[0])).map(pair => pair[1]);
}
// dummy data
var data = [12, 19, 1, 14, 3, 10, 9];
var labels = data.map((x, i) => i.toString());
// other data color
var color = data.map(x => 'rgba(75,192,192,0.4)');
// change max color
findMaxIndexes(data, 3).forEach(index => color[index] = 'red');
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: labels,
datasets: [{
label: 'value',
data: data,
backgroundColor: color,
}]
}
});
Hey Everyone! I stumbled upon this interesting code snippet on Stack Overflow regarding Chart.js and changing the color of maximum value bars. However, I'm unsure how to modify it to highlight the top three values.
Could someone guide me on adjusting this code to display the three highest values in a distinct color?