wpf canvas.getContext("2d") 未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17321856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
canvas.getContext("2d") is undefined
提问by Varun
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".
下面的代码在所有浏览器中都可以正常打开,但是当我尝试在 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>
Edit 1: chart.js can be download for here https://github.com/nnnick/Chart.js
编辑 1:chart.js 可以在这里下载https://github.com/nnnick/Chart.js
回答by Varun
After one cup of coffee I have solved that issue by adding following line
一杯咖啡后,我通过添加以下行解决了该问题
<meta http-equiv="X-UA-Compatible" content="IE=9">

