chart.js标签基于值的颜色变化

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

我有一个chart.js的水平图表,看起来像this.my bar chart

我正在寻找一种方法来改变左边的标签“Boozman John,Eldridge Conner等”基于我从ajax调用获得的列表中的值。

我尝试过类似的东西

labels {
    fontColor: labelcolors
},

使用for循环获取列表“labelcolors”中的颜色但不起作用。

这是我的代码

`var ctx = document.getElementById("GeneralChart");
var GeneralChart = new Chart(ctx, {
    type: "horizontalBar",
    data: {
  labels: gen_candidate,
  datasets: [
    {
      label: "Committee expenditure",
      backgroundColor: "orange",
      borderColor: "yellow",
      borderWidth: 1,
      xAxisID: "expenditure",
      data: gen_expenditure
    },
    {
      label: "General votes",
      backgroundColor: "green",
      borderColor: "green",
      borderWidth: 1,
      xAxisID: "votes",
      data: general_votes
    },
  ]
},
    options: {
    responsive: true,
    legend: {
        position: "top"
    },
    title: {
        display: true,
        text: "Money and Votes for General Election "+ state
    },
    scales: {
        yAxes: [{
                    position:"left",
                    id: "y-axis",
                    ticks: {
                        autoSkip: false,
                    }
                }],
        xAxes: [{
            type: 'linear',  //type: 'linear', is needed for horizontal vote
            position: "top",
            id: "expenditure",
        }, {
            type: 'linear',
            position: "bottom",
            id: "votes",
        ticks: {
            beginAtZero: true
                }
            }]
        }   
    }
});`

任何的想法?

javascript charts chart.js chartjs-2.6.0
1个回答
0
投票

使用:

ticks: { 
    fontColor: "#377216", 
    beginAtZero: true 
    }
© www.soinside.com 2019 - 2024. All rights reserved.