Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chart.js data background color is overwriting point background color

I'm working with Chart.js. I have a single data line with a specific color. I want the points of these data line to be in a different color. According to the documentation you can do this with Chart.defaults.global.elements.point.backgroundColor

 var ctxLine = document.getElementById("lineChart").getContext('2d');
 lineChart = new Chart(ctxLine, {
 type: 'line',
 data: {
     labels: dates,
     datasets: [{
           data: ['...'],
           backgroundColor: "rgba(52,152,219,0.4)"
       }]
 },
 options: {
    elements: {
         point: {
              borderColor: "rgb(255,255,0)",
              backgroundColor: "rgb(255,0,0)"
          }
      }                     
   }
});

point.borderColor is working properly, but point.backgroundColor is only working if I remove the the first backgroundColor field.

like image 204
Ian Fako Avatar asked Mar 07 '23 20:03

Ian Fako


2 Answers

I've found a solution by myself. I wasn't aware that there are different versions of chart.js

I am using v2.0 and there exists a property named pointBackgroundColor

var ctxLine = document.getElementById("lineChart").getContext('2d');
lineChart = new Chart(ctxLine, {
type: 'line',
data: {
     labels: dates,
     datasets: [{
           data: ['...'],
           backgroundColor: "rgba(52,152,219,0.4)",
           pointBackgroundColor: "#fff"
       }]
}
});
like image 123
Ian Fako Avatar answered Apr 09 '23 02:04

Ian Fako


It looks like Chart.defaults.global.elements.point.backgroundColor only takes a single Color string.

I don't believe would be possible to have different colored points. Here is the documentation page for it.

I tried to plug in an array into that backgroundColor property but it defaulted to a different color. Have a look at this fiddle, if you want to play around.

You could always submit a feature request.

like image 33
tyelford Avatar answered Apr 09 '23 03:04

tyelford