gpt4 book ai didi

javascript - 引用错误 : Chart is not defined - chartjs

转载 作者:技术小花猫 更新时间:2023-10-29 12:20:26 27 4
gpt4 key购买 nike

Chart.js 有错误吗?每次我将 Chart.js 中的任何图表添加到我的网站时,我都会收到错误,但是当我将图表用作独立程序时,它运行顺利,没有错误。我正在使用 HTML5。

   <html>
<head>
<meta charset="utf-8" />
<title>Rice Consumption</title>
<script src='Chart.min.js'></script>
</head>
<body>

<canvas id="rice" width="600" height="400"></canvas>

<script>
var riceData = {
labels : ["January","February","March","April","May","June"],
datasets :
[
{
fillColor : "rgba(172,194,132,0.4)",
strokeColor : "#ACC26D",
pointColor : "#fff",
pointStrokeColor : "#9DB86D",
data : [203000,15600,99000,25100,30500,24700]
}
]
}

var rice = document.getElementById('rice').getContext('2d');
new Chart(rice).Line(riceData);
</script>
</body>
</html>

已解决:我只是将脚本与 canvas 元素分离(为脚本创建了另一个文件来执行其功能)。

更新的 HTML:

      <html>
<head>
<meta charset="utf-8" />
<title>Rice Consumption</title>
<script src='Chart.min.js'></script>
</head>
<body>
<canvas id="rice" width="600" height="400"></canvas>
<script src='Chart.min.js'></script>
<script src='rice.js'></script>
</body>
</html>

新的 JavaScript 文件:

var riceData = {
labels : ["January","February","March","April","May","June"],
datasets : [
{
fillColor : "rgba(172,194,132,0.4)",
strokeColor : "#ACC26D",
pointColor : "#fff",
pointStrokeColor : "#9DB86D",
data : [203000,15600,99000,25100,30500,24700]
}
]
}

var rice = document.getElementById('rice').getContext('2d');
new Chart(rice).Line(riceData);

最佳答案

我也遇到了同样的错误。为了解决这个问题,我将图表脚本移到了单独的 graph.js 文件中。

我仍然遇到同样的错误。当我在标签结束之前按以下顺序放置标签时,稍后会修复该问题,如下所示。

<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.min.js"></script>
<script type="text/javascript" src="jscript/graph.js"></script>
</body>

页面是这样的: enter image description here

var ctx = document.getElementById("myChart").getContext('2d');
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
}
}]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.min.js"></script>
<!DOCTYPE html>

<canvas id="myChart"></canvas>

关于javascript - 引用错误 : Chart is not defined - chartjs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29785623/

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