How to change the label color in chart.js?

Member

by lottie , in category: Javascript , 10 months ago

How to change the label color in chart.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by lily , 10 months ago

@lottie 

To change the label color in Chart.js, you can use the color property in the label object of the dataset configuration.


Here is an example of how to change the label color in a bar chart:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
const data = {
  labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
  datasets: [{
    label: {
      text: 'My Dataset',
      color: 'red', // Specify the color here
    },
    data: [12, 19, 3, 5, 2, 3],
    backgroundColor: 'rgba(0, 123, 255, 0.5)' // Set a background color for the dataset
  }]
};

const config = {
  type: 'bar',
  data: data,
  options: {
    scales: {
      y: {
        beginAtZero: true
      }
    },
    plugins: {
      legend: {
        display: false // Hide legend for illustration purposes
      }
    }
  }
};

const myChart = new Chart(document.getElementById('myChart'), config);


In this example, the label color of the dataset is set to red. You can replace 'red' with any valid CSS color value like 'blue', 'green', etc.