[Chartjs]-In ChartJS how do I change the color of a label in the legend?

4πŸ‘

βœ…

The legend config has been moved to the plugins section

Example:

var options = {
  type: 'line',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
        label: '# of Votes',
        data: [12, 19, 3, 5, 2, 3],
        borderWidth: 1
      },
      {
        label: '# of Points',
        data: [7, 11, 5, 8, 3, 7],
        borderWidth: 1
      }
    ]
  },
  options: {
    plugins: {
      legend: {
        labels: {
          color: 'red'
        }
      }
    }
  }
}

var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
  <canvas id="chartJSContainer" width="600" height="400"></canvas>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.3.2/chart.js"></script>
</body>

1πŸ‘

    var donutOptions = {
    maintainAspectRatio: false,
    responsive: true,
    options: {
        plugins: {
          legend: {
              display: true,
              labels: {
                  color: 'rgb(255, 99, 132)'
              }
          }
        }
      }
    }

-1πŸ‘

You can change the color by grabbing the class of that div tag using CSS selector. But It’s not the best idea because it will change the color in your whole app.

Leave a comment