gpt4 book ai didi

javascript - 未捕获的类型错误 : Cannot read property 'offsetWidth' of undefined - chart. js

转载 作者:可可西里 更新时间:2023-11-01 01:56:04 25 4
gpt4 key购买 nike

我有这个简单的 html:

<canvas id="myChart" width="400" height="400"></canvas>

和这个 js:

var ctx = document.getElementById("myChart");

var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});

即使 Canvas 配置了 myChart id,我还是收到以下错误:

Uncaught TypeError: Cannot read property 'offsetWidth' of undefined

JSFiddle: https://jsfiddle.net/kroejrhx/

最佳答案

重新审视问题后,您的问题是:您使用了错误版本的 chart.js。根据this section ,如果您想使用基于时间的轴,则需要显式包含 moment.js,或使用捆绑版本。

将 jsfiddle 中的资源更改为 https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.4/Chart.bundle.min.js 将显示预期的图表。根本不需要更改代码。

关于javascript - 未捕获的类型错误 : Cannot read property 'offsetWidth' of undefined - chart. js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37575549/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com