无法读取未定义的属性“ canvas”

未完成

我正在尝试使用Chartsjs制作饼图。我已经按照chartjs文档中的步骤进行操作,并且包括了chart.js和canvas元素。我添加了应该创建图表的脚本,作为chartjs文档中提供的示例。我收到以下错误:未捕获的TypeError:无法读取未定义的属性'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);

小提琴在这里

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Uncaught TypeError:无法读取未定义的属性“ canvas”-Javascript对象

来自分类Dev

Google Map Canvas上的灰色区域,错误为“未捕获的TypeError:无法设置未定义的属性'position'”

来自分类Dev

无法读取未定义/未定义的属性

来自分类Dev

TypeError:无法读取未定义的属性“未定义”

来自分类Dev

无法读取未定义的属性“ $ valid”

来自分类Dev

无法读取未定义的属性“ ObjectID”

来自分类Dev

angularjs无法读取未定义的属性

来自分类Dev

无法读取未定义的属性“ main”

来自分类Dev

无法读取未定义的属性“ forEach”

来自分类Dev

无法读取未定义的属性“ MyProperty”

来自分类Dev

无法读取未定义的属性“ split”

来自分类Dev

无法读取未定义的属性“ $ scope”

来自分类Dev

无法读取未定义的属性“协议”

来自分类Dev

无法读取未定义的属性“ toJSON”

来自分类Dev

无法读取未定义的属性“ attr”

来自分类Dev

无法读取未定义的属性“ helpers”

来自分类Dev

无法读取未定义的属性“ setBounds”

来自分类Dev

无法读取未定义的属性“模块”

来自分类Dev

.done()无法读取未定义的属性

来自分类Dev

无法读取未定义的属性“ getTime”

来自分类Dev

无法读取未定义的属性'indexOf'

来自分类Dev

无法读取未定义的属性“ pushState”

来自分类Dev

无法读取未定义的属性“ debugHosts”

来自分类Dev

无法读取未定义的属性“ toJS”

来自分类Dev

无法读取未定义的属性“ entityState”

来自分类Dev

无法读取未定义的属性“ displayImage”

来自分类Dev

错误:无法读取未定义的属性

来自分类Dev

无法读取未定义的属性“ newPost”

来自分类Dev

无法读取未定义的属性“ $ apply”

Related 相关文章

热门标签

归档