canvas.getContext(“2d”)未定义

时间:2013-06-26 13:46:25

标签: html wpf canvas

以下代码在所有浏览器中都可以正常运行,但是当我尝试在WPF Web浏览器控件中运行代码时,它会出现Javascript错误“canvas.getContext(”2d“)未定义”。

<html>
   <head>
      <title>Bar Chart</title>
      <script src="Chart.js" type="text/javascript"></script>  
      <script type="text/javascript">
       var canvas = null;
       var context = null;

       window.onload = function () {
        invokeService();
        canvas = document.getElementById("canvas");
        var context = canvas.getContext("2d");
        alert(context);
        var myLine = new Chart(document.getElementById("canvas").getContext("2d")).Bar(barChartData);
    };

    var barChartData;
    function invokeService() {
        alert("q");
        barChartData = {
            labels: ["January", "February", "March", "April", "May", "June", "July"],
            datasets: [
                       {
                           fillColor: "rgba(220,220,220,0.5)",
                           strokeColor: "rgba(220,220,220,1)",
                           data: [65, 59, 90, 81, 56, 55, 40]
                       },
                       {
                           fillColor: "rgba(151,187,205,0.5)",
                           strokeColor: "rgba(151,187,205,1)",
                           data: [28, 48, 40, 19, 96, 27, 100]
                       }
                 ]
        }
    }
    </script>  
 </head>
 <body>
     <canvas id="canvas" height="450" width="600"></canvas>
 </body>
</html>

编辑1:可以在这里下载chart.js https://github.com/nnnick/Chart.js

1 个答案:

答案 0 :(得分:3)

喝完一杯咖啡后,我通过添加以下行解决了这个问题

<meta http-equiv="X-UA-Compatible" content="IE=9">