无法读取property 'canvas'的定义
Cannot read property 'canvas' of undefined
我正在尝试使用chartsjs制作一个饼状图。我遵循了chartjs文档中的步骤,并包含了chart.js和canvas元素。我添加了应该创建图表的脚本,就像chartjs文档中提供的示例一样。我得到以下错误:未捕获的类型错误:无法读取未定义的属性"canvas"有人知道怎么解决这个问题吗?我做错了什么?提前感谢!
代码如下:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="<?php echo base_url(); ?>media/js/chart.js"></script>
<script type="text/javascript" src="<?php echo base_url(); ?>media/js/jquery.js"></script>
</head>
<canvas id="myChart" width="400" height="400"></canvas>
<script type="text/javascript">
$(function() {
options = {
//Boolean - Show a backdrop to the scale label
scaleShowLabelBackdrop: true,
//String - The colour of the label backdrop
scaleBackdropColor: "rgba(255,255,255,0.75)",
// Boolean - Whether the scale should begin at zero
scaleBeginAtZero: true,
//Number - The backdrop padding above & below the label in pixels
scaleBackdropPaddingY: 2,
//Number - The backdrop padding to the side of the label in pixels
scaleBackdropPaddingX: 2,
//Boolean - Show line for each value in the scale
scaleShowLine: true,
//Boolean - Stroke a line around each segment in the chart
segmentShowStroke: true,
//String - The colour of the stroke on each segement.
segmentStrokeColor: "#fff",
//Number - The width of the stroke value in pixels
segmentStrokeWidth: 2,
//Number - Amount of animation steps
animationSteps: 100,
//String - Animation easing effect.
animationEasing: "easeOutBounce",
//Boolean - Whether to animate the rotation of the chart
animateRotate: true,
//Boolean - Whether to animate scaling the chart from the centre
animateScale: false,
//String - A legend template
legendTemplate: "<ul class='"<%=name.toLowerCase()%>-legend'"><% for (var i=0; i<segments.length; i++){%><li><span style='"background-color:<%=segments[i].fillColor%>'"></span><%if(segments[i].label){%><%=segments[i].label%><%}%></li><%}%></ul>"
};
data = [
{
value: 300,
color: "#F7464A",
highlight: "#FF5A5E",
label: "Red"
},
{
value: 50,
color: "#46BFBD",
highlight: "#5AD3D1",
label: "Green"
},
{
value: 100,
color: "#FDB45C",
highlight: "#FFC870",
label: "Yellow"
}
];
ctx = $("#myChart").get(0).getContext("2d");
myNewChart = new Chart(ctx[0]).Pie(data, options);
});
</script>
</html>
问题出在这一行:
myNewChart = new Chart(ctx[0]).Pie(data, options);
,特别是在ctx[0]
。当您在这里定义ctx
时:
ctx = $("#myChart").get(0).getContext("2d");
ctx
是一个称为CanvasRenderingContext2D
的对象,它具有属性。当它不是时,您试图将其视为数组。因此,ctx[0]
是undefined
。所以解其实很简单,正如你们所发现的。
将ctx[0]
更改为ctx
,你就有了一个漂亮的动画饼状图。
ctx = $("#myChart").get(0).getContext("2d");
myNewChart = new Chart(ctx).Pie(data, options);
这里的小提琴
我的解决方案有点不同,因为我想让图表动态变化(在我的应用程序中,它是用滑块移动的),但避免可怕的闪烁。
在标准实例化之后,我像这样更新滑块拖动:
var chartData = getChartData();
for(var i=0; i<chartData.length; i++)
{
barChart.datasets[0].bars[i].value = chartData[i];
}
barChart.update();
这很好地显示了变化,但在动画完成后,为了防止当用户将鼠标悬停在上面时发生奇怪的闪烁(工具提示对我来说也是必不可少的),我在鼠标上销毁并重新创建图表,如下所示:
if(barChart) {
barChart.clear();
barChart.destroy();
}
chartDataObject = getChartData();
var chartData = {
labels: getChartLabels(),
datasets: [
{
label: "label",
fillColor: "rgba(151,187,205,0.5)",
strokeColor: "rgba(151,187,205,0.8)",
highlightFill: "rgba(151,187,205,0.75)",
highlightStroke: "rgba(151,187,205,1)",
data: chartDataObject
}
]
};
var chartContext = $("#visualizeEfficacyBar").get(0).getContext("2d");
barChart = new Chart(chartContext).Bar(chartData, { tooltipTemplate: "<%if (label){%><%=label%>: <%}%><%= value %>", responsive : true , animate : false, animationSteps : 1 });
这里重要的是不要动画的娱乐,因为它会导致一个非常尴尬的视觉效果,设置animate : false
没有做到这一点,但animationSteps : 1
做到了。现在没有闪烁,图表被重新创建,用户也不知道。
相关文章:
- 用嵌套函数和默认函数定义函数
- Dojo不解析自定义小部件的模板html中的小部件声明性
- Canvas Html5绘图应用程序,移动画布会导致重大问题
- 为什么“;未定义的“;在JavaScript中结束循环
- 要求未定义JS回调参数
- 控制台返回var不是't定义,但它是
- 在自定义mean.io包中使用angular-chart.js作为依赖项
- 如何检查管道中未定义的项目
- TypeError:无法读取属性'推'未定义的JavaScript
- jQuery自定义验证比较多个输入的序列
- 查看JS对象的所有键,甚至是getter定义的键
- fetch() 函数未在 Ubuntu Chromium 浏览器上定义
- Canvas仅使用fabric.js在点击事件时渲染自定义Web字体
- 为什么我的代码认为 canvas 函数在 JavaScript 中的构造函数原型中是未定义的
- 读取'时出错;无法读取属性'x'未定义的'在HTML5 Canvas代码中
- 如何将自定义形状变形/设置动画为圆形?CANVAS JS
- 无法读取property 'canvas'的定义
- highcharts canvas-tools:无法读取property 'prototype'的定义
- 不能用Javascript访问聚合物自定义元素中的Canvas元素
- 在Canvas中单击形状(objects of fabric.js)时显示自定义上下文菜单