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

时间:2014-08-20 12:05:02

标签: javascript jquery canvas charts undefined

我正在尝试使用chartjs制作饼图。我已经按照chartjs文档中的步骤操作了,我已经包含了chart.js和canvas元素。我添加了应该创建图表的脚本,作为chartjs文档中提供的示例。我收到以下错误: 未捕获的TypeError:无法读取属性' canvas'未定义的 有没有人知道如何解决这个问题?我究竟做错了什么? Thanx提前!

这是代码:

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

2 个答案:

答案 0 :(得分:24)

问题出在这一行:

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

Fiddle Here

答案 1 :(得分:0)

我的解决方案略有不同,因为我希望动态更改图表(在我的应用程序中,它使用滑块移动)但避免可怕的闪烁。

在标准实例化之后,我更新了滑块拖动,如下所示:

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做了。现在没有闪烁,图表被重新创建,用户也不是更聪明。