Chart.js是否存在错误?每次我将Chart.js中的任何图形添加到我的网站时,都会出现错误,但是当我将图形用作独立程序时,它可以平稳运行且没有错误。我正在使用HTML5。

   <html>
   <head>
      <meta charset="utf-8" />
      <title>Rice Consumption</title>
      <script src='Chart.min.js'></script>
    </head>
    <body>

      <canvas id="rice" width="600" height="400"></canvas>

      <script>
        var riceData = {
        labels : ["January","February","March","April","May","June"],
        datasets :
         [
            {
              fillColor : "rgba(172,194,132,0.4)",
              strokeColor : "#ACC26D",
              pointColor : "#fff",
              pointStrokeColor : "#9DB86D",
              data : [203000,15600,99000,25100,30500,24700]
            }
         ]
        }

          var rice = document.getElementById('rice').getContext('2d');
               new Chart(rice).Line(riceData);
    </script>
    </body>
    </html>

已解决:我只是将脚本与canvas元素解耦(为脚本执行功能创建了另一个文件)。

更新的HTML:
      <html>
      <head>
      <meta charset="utf-8" />
      <title>Rice Consumption</title>
      <script src='Chart.min.js'></script>
      </head>
      <body>
      <canvas id="rice" width="600" height="400"></canvas>
      <script src='Chart.min.js'></script>
      <script src='rice.js'></script>
      </body>
      </html>

新的JavaScript文件:
var riceData = {
    labels : ["January","February","March","April","May","June"],
    datasets : [
        {
            fillColor : "rgba(172,194,132,0.4)",
            strokeColor : "#ACC26D",
            pointColor : "#fff",
            pointStrokeColor : "#9DB86D",
            data : [203000,15600,99000,25100,30500,24700]
        }
    ]
}

var rice = document.getElementById('rice').getContext('2d');
new Chart(rice).Line(riceData);

最佳答案

这是您的代码的工作jsfiddle:new Chart(rice).Line(riceData); http://jsfiddle.net/mahmalsami/jqcthmyo/
所以问题肯定是来自您的外部Chart.min.js包含

您可能会在js上找到404。请确保您链接到正确的js文件夹。 (尝试访问您的localhost/Chart.min.js以查看是否可以访问您的文件)

关于javascript - ReferenceError : Chart is not defined - chartjs,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29785623/

10-11 06:51
查看更多