【问题标题】:Getting 'Chart is not defined' error when using chart.js in Meteor在 Meteor 中使用 chart.js 时出现“未定义图表”错误
【发布时间】:2017-04-06 01:55:39
【问题描述】:

我在我的流星应用程序中使用官方chart.js atmosphere package。我尝试运行示例图表以查看是否可以将其拉起,但是我遇到了一个问题,提示“ReferenceError: Chart is not defined”

这是我安装大气包和运行代码以生成图表的步骤。

  1. 使用meteor add chart:chart安装包
  2. 在我的 HTML 中,我添加了 canvas 标记,以调用图表及其信息
  3. 在 JS 中,我添加了创建图表及其相关信息的函数

但是,当我转到该页面时,我只得到一个空的 400x400 图像,它是画布,但图表应该在的地方没有内容。有人可以帮我弄清楚我缺少哪一步才能让图表出现吗?

HTML

<template name="Home_page">
<canvas id="myChart" width="400" height="400"></canvas>
</template>

JS

import './home-page.html';


Template.Home_page.onRendered(function homePageOnRendered() {
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
                    }
                }]
            }
        }
    });
});

【问题讨论】:

    标签: javascript jquery html charts meteor-blaze


    【解决方案1】:

    好的,忘记添加了

    import Chart from 'chart.js' 
    

    在我的 js 文件的顶部。这样就解决了。

    【讨论】:

    • 谢谢!在我发现这个之前,我一直在与这个确切的问题作斗争几个小时。问题解决了!
    • 在yii2中怎么做?你能看看我的相关@​​987654321@吗?
    猜你喜欢
    • 1970-01-01
    • 2019-04-14
    • 1970-01-01
    • 1970-01-01
    • 2018-02-04
    • 1970-01-01
    • 1970-01-01
    • 2021-06-16
    • 1970-01-01
    相关资源
    最近更新 更多