创建条形图后,如何更改其颜色?我有以下内容。
https://codepen.io/anon/pen/QoboRg?editors=1010#0
// can't move to home.js because the label is dynamic
var topClickSourceChart = new Chart('dashboard-click-source-chart', {
type: 'bar',
data: {
datasets: [
{label: "NONE", data: [14]},{label: "Facebook", data: [10]},{label: "Google", data: [5]},{label: "Reddit", data: [1]},{label: "Twitter", data: [1]},
]
},
});
topClickSourceChart.data.datasets[0].backgroundColor = '#4D90C3';
topClickSourceChart.data.datasets[1].backgroundColor = '#866DB2';
topClickSourceChart.data.datasets[2].backgroundColor = '#EA6F98';
topClickSourceChart.data.datasets[3].backgroundColor = '#61BDF6';
topClickSourceChart.data.datasets[4].backgroundColor = '#768BB7';
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.bundle.min.js" integrity="sha256-MZo5XY1Ah7Z2Aui4/alkfeiq3CopMdV/bbkc/Sh41+s=" crossorigin="anonymous"></script>
<canvas id="dashboard-click-source-chart" height="200" width="200"></canvas>
It used to work。我不知道发生了什么变化,特别是因为我具有资源完整性哈希属性。
它似乎可以在此处显示为“完整页”代码段,但不能在较小的代码段中使用,并且在CodePen中不起作用,并且在我的本地站点中也不起作用。
为什么它会忽略某些网站的背景颜色,而不是其他网站?
答案 0 :(得分:1)
我发现了.update()
函数。
https://www.chartjs.org/docs/latest/developers/updates.html
我只需要添加
topClickSourceChart.update();
根据窗口的大小,有时它是有趣的,但有时却不起作用。
// can't move to home.js because the label is dynamic
var topClickSourceChart = new Chart('dashboard-click-source-chart', {
type: 'bar',
data: {
datasets: [
{label: "NONE", data: [14]},{label: "Facebook", data: [10]},{label: "Google", data: [5]},{label: "Reddit", data: [1]},{label: "Twitter", data: [1]},
]
},
});
topClickSourceChart.data.datasets[0].backgroundColor = '#4D90C3';
topClickSourceChart.data.datasets[1].backgroundColor = '#866DB2';
topClickSourceChart.data.datasets[2].backgroundColor = '#EA6F98';
topClickSourceChart.data.datasets[3].backgroundColor = '#61BDF6';
topClickSourceChart.data.datasets[4].backgroundColor = '#768BB7';
topClickSourceChart.update();
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.bundle.min.js" integrity="sha256-MZo5XY1Ah7Z2Aui4/alkfeiq3CopMdV/bbkc/Sh41+s=" crossorigin="anonymous"></script>
<canvas id="dashboard-click-source-chart" height="200" width="200"></canvas>