Chart.js出错:TypeError:t未定义

时间:2022-12-04 09:20:39

I keep getting this error and I wasn't able to display a Chart.js chart yet, although I have tried multiples codes I found on the internet.

我一直收到这个错误,但我还是无法显示Chart.js图表​​,尽管我已经尝试过在互联网上找到的多个代码。

Here is the relevant part of the HTML :

以下是HTML的相关部分:

<head>
    <script src = "https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.min.js"></script>
    ...
</head>

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

Here is the javascript :

这是javascript:

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
                }
            }]
        }
    }
});

I found that example here : http://www.chartjs.org/docs/

我在这里找到了这个例子:http://www.chartjs.org/docs/

And I get this error in the console :

我在控制台中收到此错误:

TypeError: t is undefined

Can someone help please ? Thanks.

有人可以帮忙吗?谢谢。

2 个解决方案

#1


2  

Solved, apparently the script link I was using was bugged. Changed it to https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.min.js and it works fine. See Chris G's comment.

解决了,显然我正在使用的脚本链接被窃听。将其更改为https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.min.js,它运行正常。请参阅Chris G的评论。

#2


0  

Put the javascript code (start with <script type="text/javascript">var ctx ...</script>) after the canvas element. This ensures that the canvas element is already present at the time when it gets referenced by var ctx = document.getElementById("myChart");

在canvas元素后面放置javascript代码(以

This should solve your problem.

这应该可以解决您的问题。

#1


2  

Solved, apparently the script link I was using was bugged. Changed it to https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.min.js and it works fine. See Chris G's comment.

解决了,显然我正在使用的脚本链接被窃听。将其更改为https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.min.js,它运行正常。请参阅Chris G的评论。

#2


0  

Put the javascript code (start with <script type="text/javascript">var ctx ...</script>) after the canvas element. This ensures that the canvas element is already present at the time when it gets referenced by var ctx = document.getElementById("myChart");

在canvas元素后面放置javascript代码(以

This should solve your problem.

这应该可以解决您的问题。