Chartjs-标签颜色不匹配

问题描述 投票:0回答:2

我为标签分配了三种不同的颜色,但是颜色点始终显示为蓝色,绿色和黄色。我敢肯定我做错了什么。

我编写的代码是:

var pieSimpleChart = {
  chart: {
    height: 350,
    type: 'pie',
  },
  legend: {
    position: 'bottom'
  },
  labels: ["IT", "Product Development", "Sales"],
  series: [2, 2, 6],
  fill: {
    colors: ["#447b40", "#cc7870", "#e74ce4"]
  }
}

var chart = new ApexCharts(document.querySelector("#task-pie-chart"), pieSimpleChart);
chart.render();

enter image description here

如何使标签与所选颜色匹配?

chart.js
2个回答
0
投票

使用backgroundColor代替fill: colors

此外,您应该使用dataset对象传递值:

var ctx = document.getElementById("myChart").getContext('2d');
var myChart = new Chart(ctx, {
    type: 'pie',
    data: {
        labels: ["IT", "Product Development", "Sales"],
        datasets: [{
            backgroundColor: [
                "#ff0000",
                "#00ff00",
                "#0000ff"
            ],
            data: [2, 2, 6]
        }]
    },
    options: {
        legend: {
            position: 'bottom'
        }
    }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.js"></script>
<div class="container">
  <div>
    <canvas id="myChart"></canvas>
  </div>
</div>

0
投票

我通过删除“填充”选项来修复它。现在代码看起来像:

    var pieSimpleChart = 
    {
        chart: 
        {
            height: 350,
            type: 'pie',
        },
        legend: 
        {
            position: 'bottom'
        },
        labels: ["IT", "Product Development", "Sales"],
        series: [2, 2, 6],
        colors: ["#447b40", "#cc7870", "#e74ce4"]
    }
© www.soinside.com 2019 - 2024. All rights reserved.