How to change the label color in chart.js?

Member

by lottie , in category: Javascript , a year ago

How to change the label color in chart.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by lily , a year 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.

Related Threads:

How to change the label and value like 500,000 to 500k in chart.js?
How to change x-axes label position in chart.js?
How to change the color of a specific y-axis in chart.js?
How to change color of dots in graph using chart.js?
How to remove background color and color example from tooltip in chart.js?
How to reverse chart.js label order?