Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/473.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 图表js有时找不到id_Javascript_Html_Chart.js - Fatal编程技术网

Javascript 图表js有时找不到id

Javascript 图表js有时找不到id,javascript,html,chart.js,Javascript,Html,Chart.js,我的问题是,Chart js有时会找到我的图表的ID,但有时不会。在10个案例中,有3个没有找到图表,并指出错误: 未捕获类型错误:无法在window.onload(dashbaord.php)设置未定义的属性“options” 以下是HTML代码: <div class="chart-area" id="myChart" style="height: 100%;"> <canvas id="myChar

我的问题是,Chart js有时会找到我的图表的ID,但有时不会。在10个案例中,有3个没有找到图表,并指出错误:

未捕获类型错误:无法在window.onload(dashbaord.php)设置未定义的属性“options”

以下是HTML代码:

<div class="chart-area" id="myChart" style="height: 100%;">
  <canvas id="myChartye" data-bss-chart="{&quot;type&quot;:&quot;line&quot;,&quot;data&quot;:{&quot;labels&quot;:[],&quot;datasets&quot;:[{&quot;label&quot;:&quot;Earnings&quot;,&quot;fill&quot;:true,&quot;data&quot;:[],&quot;backgroundColor&quot;:&quot;rgba(78, 115, 223, 0.05)&quot;,&quot;borderColor&quot;:&quot;rgba(78, 115, 223, 1)&quot;}]},&quot;options&quot;:{&quot;maintainAspectRatio&quot;:false,&quot;legend&quot;:{&quot;display&quot;:false},&quot;title&quot;:{},&quot;scales&quot;:{&quot;xAxes&quot;:[{&quot;gridLines&quot;:{&quot;color&quot;:&quot;rgb(234, 236, 244)&quot;,&quot;zeroLineColor&quot;:&quot;rgb(234, 236, 244)&quot;,&quot;drawBorder&quot;:false,&quot;drawTicks&quot;:false,&quot;borderDash&quot;:[&quot;2&quot;],&quot;zeroLineBorderDash&quot;:[&quot;2&quot;],&quot;drawOnChartArea&quot;:false},&quot;ticks&quot;:{&quot;fontColor&quot;:&quot;#858796&quot;,&quot;padding&quot;:20}}],&quot;yAxes&quot;:[{&quot;gridLines&quot;:{&quot;color&quot;:&quot;rgb(234, 236, 244)&quot;,&quot;zeroLineColor&quot;:&quot;rgb(234, 236, 244)&quot;,&quot;drawBorder&quot;:false,&quot;drawTicks&quot;:false,&quot;borderDash&quot;:[&quot;2&quot;],&quot;zeroLineBorderDash&quot;:[&quot;2&quot;]},&quot;ticks&quot;:{&quot;fontColor&quot;:&quot;#858796&quot;,&quot;padding&quot;:20}}]}}}"></canvas></div>
</div>

下面是JavaScript:

window.onload = function () {
        var monthNames = [
                "January ", "February ", "March ", "April ", "May ", "June ", "July ", "August ", "September ", "October ", "November ", "December "
            ];

        var d = new Date();
        var chart = document.getElementById("myChartye").chart;
        chart.options = {
                elements: {
                    line: {
                        tension: 0
                    }
                }
            };
        chart.update();
        
        for(var i = 5; i>-1; i--) {
            var year = d.getFullYear();
            var month = d.getMonth();
            var months = month - i;
            
            if (months <=-1) {
                month = 11;
                year--;
                
                months = month + months + 1;
            }
            var label = monthNames[months] + year;

            if (i == 5) {
                chart.data.labels.push(label);
                chart.data.datasets.forEach((dataset) => {
                    dataset.data.push('<?php echo "10";?>');
                });
                chart.update();
            } else if (i == 4) {
                chart.data.labels.push(label);
                chart.data.datasets.forEach((dataset) => {
                    dataset.data.push('324');
                });
                chart.update();
            } else if (i == 3) {
                chart.data.labels.push(label);
                chart.data.datasets.forEach((dataset) => {
                    dataset.data.push('23');
                });
                chart.update();
            } else if (i == 2) {
                chart.data.labels.push(label);
                chart.data.datasets.forEach((dataset) => {
                    dataset.data.push('1');
                });
                chart.update();
            } else if (i == 1) {
                chart.data.labels.push(label);
                chart.data.datasets.forEach((dataset) => {
                    dataset.data.push('54');
                });
                chart.update();
            } else if (i == 0) {
                chart.data.labels.push(label);
                chart.data.datasets.forEach((dataset) => {
                    dataset.data.push('56');
                });
                chart.update();
            }
        }
    };
window.onload=函数(){
var monthNames=[
“一月”、“二月”、“三月”、“四月”、“五月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”
];
var d=新日期();
var chart=document.getElementById(“myChartye”).chart;
chart.options={
要素:{
行:{
张力:0
}
}
};
chart.update();
对于(变量i=5;i>-1;i--){
var year=d.getFullYear();
var month=d.getMonth();
var月数=第i个月;
如果(月){
dataset.data.push(“”);
});
chart.update();
}else如果(i==4){
图表.数据.标签.推送(标签);
chart.data.dataset.forEach((数据集)=>{
dataset.data.push('324');
});
chart.update();
}else如果(i==3){
图表.数据.标签.推送(标签);
chart.data.dataset.forEach((数据集)=>{
dataset.data.push('23');
});
chart.update();
}else如果(i==2){
图表.数据.标签.推送(标签);
chart.data.dataset.forEach((数据集)=>{
dataset.data.push('1');
});
chart.update();
}else如果(i==1){
图表.数据.标签.推送(标签);
chart.data.dataset.forEach((数据集)=>{
dataset.data.push('54');
});
chart.update();
}else如果(i==0){
图表.数据.标签.推送(标签);
chart.data.dataset.forEach((数据集)=>{
dataset.data.push('56');
});
chart.update();
}
}
};

有人知道错误可能是什么吗?

如果错误是您所说的,那么它总是找到您的
画布
,但它并不总是有
图表
属性。我猜您可能还有其他代码,使
图表
属性可用,但并不总是在正确的时间运行。您的一些
标记上是否碰巧有
async
defer
属性?或者是一些异步代码,比如Ajax请求在创建图表之前获取数据?