如何在创建条形图后更改条形图的背景颜色?

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

如何在创建条形图后更改其颜色?我有以下内容。

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中不起作用,并且它在我的本地站点上不起作用。

为什么它会忽略某些网站上的背景颜色而不忽略其他网站?

javascript css html5-canvas chart.js
1个回答
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>
© www.soinside.com 2019 - 2024. All rights reserved.