javascript - In ChartJS how do I change the color of a label in the legend? - Stack Overflow

I've got the following function making doughnut charts in ChartJS, the function imports the data,

I've got the following function making doughnut charts in ChartJS, the function imports the data, label text, and the id of the element. For some reason the options legend labels does not work for me. The default color of '#666' is not usable for my site's layout either.

my function:

function newDoughnutChart(id, labels, data) {
    var donutChartCanvas = $(id).get(0).getContext('2d')
    var donutData = {
        labels: labels,
        datasets: [
            {
                data: data,
                backgroundColor: backgroundColor,
            }
        ]
    }
    var donutOptions = {
        maintainAspectRatio: false,
        responsive: true,
        options: {
            legend: {
                labels: {
                    usePointStyle: true,
                    fontColor: "#fff",
                }
            }
        }
    }
    new Chart(donutChartCanvas, {
        type: 'doughnut',
        data: donutData,
        options: donutOptions
    })
}

backgroundColor is a variable I've set globally for this js file.

I've got the following function making doughnut charts in ChartJS, the function imports the data, label text, and the id of the element. For some reason the options legend labels does not work for me. The default color of '#666' is not usable for my site's layout either.

my function:

function newDoughnutChart(id, labels, data) {
    var donutChartCanvas = $(id).get(0).getContext('2d')
    var donutData = {
        labels: labels,
        datasets: [
            {
                data: data,
                backgroundColor: backgroundColor,
            }
        ]
    }
    var donutOptions = {
        maintainAspectRatio: false,
        responsive: true,
        options: {
            legend: {
                labels: {
                    usePointStyle: true,
                    fontColor: "#fff",
                }
            }
        }
    }
    new Chart(donutChartCanvas, {
        type: 'doughnut',
        data: donutData,
        options: donutOptions
    })
}

backgroundColor is a variable I've set globally for this js file.

Share Improve this question asked Jun 8, 2021 at 19:52 sbrevolution5sbrevolution5 1833 silver badges11 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 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./ajax/libs/Chart.js/3.3.2/chart.js"></script>
</body>

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

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744210317a4563300.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信