I don’t understand how to make custom dot color depending on value in Chart.js?
if y <= 0 then dots red color, if y(0-4) blue color, etc
const ctx = document.getElementById('chart');
const request = [
{x: 'Station1',y: 5},
{x: 'Station2',y: 2},
{x: 'Station3',y: 1},
{x: 'Station4',y: 8},
{x: 'Station5',y: 7},
{x: 'Station6',y: -2},
{x: 'Station7',y: 10},
{x: 'Station8',y: 0}
]
const labels = request.map(function(e) {return e.x;});
const fuelChart = new Chart(ctx,{
type:'scatter',
data: {
datasets: [{
data: request,
label:'Day to posts'
}]
},
options: {
responsive: true,
scales:{
x:{
type:'category',
labels:labels,
}
}
}
})
2
Answers
You can make use of
backgroundColor
property of dataset and pass an array to it. The array can be built based on data so you can add your own logic to return the color you want.Something like this:
This can be done by using
Arrays.map()
as follows:Please take a look at your amended and runnable code below and see how it works.