gpt4 book ai didi

javascript - TypeError 没有正确读取属性

转载 作者:行者123 更新时间:2023-12-03 11:15:53 25 4
gpt4 key购买 nike

我正在关注 Chartjs documentation并且在我的数据对象中有两个数组(标签和数据集),当我尝试在数据集数组中定义属性时,我遇到了一条错误消息,Uncaught TypeError: Cannot read property 'datasets' of undefined ,一直没弄清楚为什么。信息与文档准确匹配,但由于某种原因它不起作用。

HTML:

<!doctype html>
<html>

<head>
<title>Google Super Proxy Test</title>
<script src="Chart.min.js"></script>
<script src="chart-options.js"></script>
</head>

<body>

<div style="width: 50%">
<canvas id="sessions-graph" height="450" width="600"></canvas>
</div>


</body>

</html>

图表选项.js

window.onload = function(){

// Get the context of the canvas element
var ctx = document.getElementById("sessions-graph").getContext("2d");
var sessionsGraph = new Chart(ctx).Bar(data); //Create a chart with "data" array

var data = {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [
{
label: "My First dataset",
fillColor: "rgba(220,220,220,0.5)",
strokeColor: "rgba(220,220,220,0.8)",
highlightFill: "rgba(220,220,220,0.75)",
highlightStroke: "rgba(220,220,220,1)",
data: [65, 59, 80, 81, 56, 55, 40]
}
]
};


Chart.defaults.global = {

//Boolean - Whether the scale should start at zero, or an order of magnitude down from the lowest value
scaleBeginAtZero : true,

//Boolean - Whether grid lines are shown across the chart
scaleShowGridLines : true,

//String - Colour of the grid lines
scaleGridLineColor : "rgba(0,0,0,.05)",

//Number - Width of the grid lines
scaleGridLineWidth : 1,

//Boolean - If there is a stroke on each bar
barShowStroke : true,

//Number - Pixel width of the bar stroke
barStrokeWidth : 2,

//Number - Spacing between each of the X value sets
barValueSpacing : 5,

//Number - Spacing between data sets within X values
barDatasetSpacing : 1

}

}

最佳答案

在将它传递给函数之前,您必须分配 data:

var data = {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [
{
label: "My First dataset",
fillColor: "rgba(220,220,220,0.5)",
strokeColor: "rgba(220,220,220,0.8)",
highlightFill: "rgba(220,220,220,0.75)",
highlightStroke: "rgba(220,220,220,1)",
data: [65, 59, 80, 81, 56, 55, 40]
}
]
};

var sessionsGraph = new Chart(ctx).Bar(data); //Create a chart with "data" array

您可能还应该在调用它之前分配 Chart.default.global,但我不确定何时使用它。

关于javascript - TypeError 没有正确读取属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25511012/

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