user7950856
user7950856

Reputation:

How to change the label color in chart.js?

I have a pie chart defined like so,

var myChart = new Chart(ctx, {
        type: 'doughnut',
        data: {
            labels: data.labels,
            datasets: [{
                data: data.values,
                backgroundColor: [
                'rgb(255, 99, 132)',
                'rgb(54, 162, 235)',
                'rgb(255, 206, 86)',
                'rgb(75, 192, 192)',
                'rgb(153, 102, 255)',
                'rgb(255, 159, 64)',
                'rgb(204, 255, 64)',
                'rgb(64, 159, 255)',
                'rgb(175, 64, 255)'
                ],
                options: {
                    responsive : true,
                }
            }],
            fontColor : '#FFFFFF'

        }
    });

how the chart looks,

enter image description here

This however is setting the font color of the labels to black, how can I change this color. Any pointers on this is highly appreciated. Thanks!

Upvotes: 5

Views: 17181

Answers (4)

Cliff
Cliff

Reputation: 66

The only solution i found to work for this problem is the following line

Chart.defaults.color = '#fff';

Hope it helps

Upvotes: 2

Daniel Sihaloho
Daniel Sihaloho

Reputation: 11

To change label color with Chart.js, you must set the fontColor.

to set the fontColor of the labels by setting the fontColor in the options object property.

for example;

    fontColor: "white", // set color

or you can visit the following link : https://thewebdev.info/2022/06/27/how-to-change-label-color-with-chart-js-and-javascript/

Upvotes: 1

Eric Guillemot
Eric Guillemot

Reputation: 1

Sorry to unbury this but trying any solutions and only this works. create plugin before chart :

Chart.plugins.register({
    beforeDraw: function(c) {
        c.legend.options.labels.fontColor = 'hsl(0,0%,85%)';
    }
});
var chart = new Chart(ctx, {
....

Upvotes: 0

ɢʀᴜɴᴛ
ɢʀᴜɴᴛ

Reputation: 32889

You can change the font color of legend­'s label, in the following way ...

options: {
   legend: {
      labels: {
         fontColor: 'white'
      }
   },
   ...
}

ᴡᴏʀᴋɪɴɢ ᴇxᴀᴍᴘʟᴇ

var chart = new Chart(ctx, {
   type: 'doughnut',
   data: {
      labels: ['A', 'B', 'C', 'D', 'E'],
      datasets: [{
         data: [1, 1, 1, 1, 1],
         backgroundColor: [
            'rgb(255, 99, 132)',
            'rgb(54, 162, 235)',
            'rgb(255, 206, 86)',
            'rgb(75, 192, 192)',
            'rgb(153, 102, 255)'
         ]
      }]
   },
   options: {
      legend: {
         labels: {
            fontColor: 'white' //set your desired color
         }
      }
   }
});
canvas{background: #222}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
<canvas id="ctx"></canvas>

Upvotes: 4

Related Questions