jquery - 使用 Chart.js 时 $(...).getContext ("2d") 不是函数

标签 jquery node.js charts pug chart.js

我正在使用nodejs和jade View 引擎。我想使用chart.js 在我的页面中加载图表,我添加了jquery 和chart.js 库,但它仍然按标题给出错误$(...).getContext("2d") is not a function。 请帮我解答这个问题,谢谢。

$(document).ready(function(){
var ctx = $("#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
            }
        }]
        }
    }
});
});

最佳答案

这个问题已经得到解答,但是,如果有人偶然发现这篇文章,为了让 getContext 函数正常工作,请确保您在 <canvas id='myChart'> </canvas> 内渲染图表。元素。

关于jquery - 使用 Chart.js 时 $(...).getContext ("2d") 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38979768/

相关文章:

javascript - 是否可以设置 .element :before or . element :after via javascript? 的 CSS 属性

javascript - 使用 jQuery 过滤类别

node.js - Mongoose 使用 SSL digital ocean 与 CA_CERT 连接

node.js - 如何在 sails.js 模型中添加具有整数数据类型数组的属性?

javascript - 如何在 d3 中的条形图更新数据之间转换?

javascript - 如何设计带有内部 float 的 div 以水平扩展以允许所有内部 float div 适合?

jquery - 从 jquery 片段中删除函数有困难

javascript - Grunt Browserify - 设置包含、回退路径

javascript - 为 Google 图表(线)中的每个列值定义标签

excel - 在 Excel 用户窗体中显示 "live"图表数据