jquery - 无法读取未定义的属性 'labels'

标签 jquery chart.js radar-chart

我刚刚学习 Chart.js 的工作原理,因此我直接使用了 Chart.js API 页面上的示例代码,但它似乎不起作用。我将以下代码放入脚本标记中,并收到“无法读取未定义的属性‘标签’”错误。不确定这个错误意味着什么

$(function () {
                var ctx = $('#myCanv').get(0).getContext("2d");
                var myRadarChart = new Chart(ctx).Radar(data, options);
                var data = {
                    labels: ["Eating", "Drinking", "Sleeping", "Designing", "Coding", "Cycling", "Running"],
                    datasets: [
                        {
                            label: "My First dataset",
                            fillColor: "rgba(220,220,220,0.2)",
                            strokeColor: "rgba(220,220,220,1)",
                            pointColor: "rgba(220,220,220,1)",
                            pointStrokeColor: "#fff",
                            pointHighlightFill: "#fff",
                            pointHighlightStroke: "rgba(220,220,220,1)",
                            data: [65, 59, 90, 81, 56, 55, 40]
                        },
                        {
                            label: "My Second dataset",
                            fillColor: "rgba(151,187,205,0.2)",
                            strokeColor: "rgba(151,187,205,1)",
                            pointColor: "rgba(151,187,205,1)",
                            pointStrokeColor: "#fff",
                            pointHighlightFill: "#fff",
                            pointHighlightStroke: "rgba(151,187,205,1)",
                            data: [28, 48, 40, 19, 96, 27, 100]
                        }
                    ]
                };
                var options = {
                    //Boolean - Whether to show lines for each scale point
                    scaleShowLine: true,
                    //Boolean - Whether we show the angle lines out of the radar
                    angleShowLineOut: true,
                    //Boolean - Whether to show labels on the scale
                    scaleShowLabels: false,
                    // Boolean - Whether the scale should begin at zero
                    scaleBeginAtZero: true,
                    //String - Colour of the angle line
                    angleLineColor: "rgba(0,0,0,.1)",
                    //Number - Pixel width of the angle line
                    angleLineWidth: 1,
                    //String - Point label font declaration
                    pointLabelFontFamily: "'Arial'",
                    //String - Point label font weight
                    pointLabelFontStyle: "normal",
                    //Number - Point label font size in pixels
                    pointLabelFontSize: 10,
                    //String - Point label font colour
                    pointLabelFontColor: "#666",
                    //Boolean - Whether to show a dot for each point
                    pointDot: true,
                    //Number - Radius of each point dot in pixels
                    pointDotRadius: 3,
                    //Number - Pixel width of point dot stroke
                    pointDotStrokeWidth: 1,
                    //Number - amount extra to add to the radius to cater for hit detection outside the drawn point
                    pointHitDetectionRadius: 20,
                    //Boolean - Whether to show a stroke for datasets
                    datasetStroke: true,
                    //Number - Pixel width of dataset stroke
                    datasetStrokeWidth: 2,
                    //Boolean - Whether to fill the dataset with a colour
                    datasetFill: true,
                    //String - A legend template
                    legendTemplate: "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<datasets.length; i++){%><li><span style=\"background-color:<%=datasets[i].strokeColor%>\"></span><%if(datasets[i].label){%><%=datasets[i].label%><%}%></li><%}%></ul>"
                };
            });

最佳答案

我发现我的错误了! 在我声明任何数据或选项之前,雷达的创建位于代码的顶部。

var myRadarChart = new Chart(ctx).Radar(data, options);

此行必须放置在函数的底部

$(function () {
            var ctx = $('#myCanv').get(0).getContext("2d");
            var data = {
                labels: ["Eating", "Drinking", "Sleeping", "Designing", "Coding", "Cycling", "Running"],
                datasets: [
                    {
                        label: "My First dataset",
                        fillColor: "rgba(220,220,220,0.2)",
                        strokeColor: "rgba(220,220,220,1)",
                        pointColor: "rgba(220,220,220,1)",
                        pointStrokeColor: "#fff",
                        pointHighlightFill: "#fff",
                        pointHighlightStroke: "rgba(220,220,220,1)",
                        data: [65, 59, 90, 81, 56, 55, 40]
                    },
                    {
                        label: "My Second dataset",
                        fillColor: "rgba(151,187,205,0.2)",
                        strokeColor: "rgba(151,187,205,1)",
                        pointColor: "rgba(151,187,205,1)",
                        pointStrokeColor: "#fff",
                        pointHighlightFill: "#fff",
                        pointHighlightStroke: "rgba(151,187,205,1)",
                        data: [28, 48, 40, 19, 96, 27, 100]
                    }
                ]
            };
         var myRadarChart = new Chart(ctx).Radar(data, options);
        });

关于jquery - 无法读取未定义的属性 'labels',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31706577/

相关文章:

javascript - jQuery 向上滑动表行

javascript - 页面加载后显示弹出窗口

javascript - 遍历每个工具提示以添加不同的时间戳

javascript - 如何使用图表js在图表中显示我的数据

python - 如何将轮盘赌轮背景图像添加到 matplotlib 雷达图

charts - Chart.js和长标签

Python动画雷达图

javascript - SVG 元素未手动单击

javascript - 将对象解析为 Chart.js 而不是数组?

javascript - 即使在脚本之前加载 jQuery,Firebug 也会给出 jQuery "$ is undefined"错误