5

The below code opens fine in all browsers, but when I'm trying to run the code in WPF web browser control, it gives a Javascript error "canvas.getContext("2d") is undefined".

<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>

Edit 1: chart.js can be download for here https://github.com/nnnick/Chart.js

Jarrod
  • 9,349
  • 5
  • 58
  • 73
Varun
  • 373
  • 1
  • 2
  • 11

1 Answers1

3

After one cup of coffee I have solved that issue by adding following line

<meta http-equiv="X-UA-Compatible" content="IE=9">
Varun
  • 373
  • 1
  • 2
  • 11
  • That's a good catch. i knew that when IE is hosted in as a `WebBrowser` control, [it defaults to **IE7** mode](http://stackoverflow.com/questions/4097593/how-to-put-the-webbrowser-control-into-ie9-into-standards) (on the assumption that it is an old critial line-of-business app; that cannot risk being put into IE9 mode, and cannot easily be changed to *force* `IE=7`). Is the *"WPF Web Browser"* really just Internet Explorer hosted in a WPF application? – Ian Boyd Jun 26 '13 at 20:10
  • *Question from [user3779105](http://stackoverflow.com/users/3779105/user3779105):* Where exactly did you add the tag? – Artjom B. Jun 26 '14 at 11:54
  • @ArtjomB. in `...` – Gerrit-K Aug 02 '14 at 14:10