ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Javascript-d3.js-未捕获的TypeError:无法读取未定义的属性’data’

2019-10-29 09:36:57  阅读:245  来源: 互联网

标签:d3-js extjs javascript nvd3-js


我一直无法解决2个错误,我一直无法解决…我正在尝试根据一些后端数据构建散点图.后端向我发送了一个Javascript对象(tabularData),其中包含我需要的信息,并使用它来创建图形.
我正在使用ExtJS 4.2.2以及d3.js和nv.d3.js的最新版本

第一个错误是未捕获的typeerror,似乎抱怨nv.d3.js

Uncaught TypeError: Cannot read property 'data' of undefined nv.d3.js:11193
(anonymous function) nv.d3.js:11193
attrFunction d3.js:597
(anonymous function) d3.js:884
d3_selection_each d3.js:890
d3_selectionPrototype.each d3.js:883
d3_selectionPrototype.attr d3.js:580
updateInteractiveLayer nv.d3.js:11192

第二个错误是与d3.js有关的错误

Error: Invalid value for <g> attribute transform="translate(NaN,5)" d3.js:591
attrConstant d3.js:591
(anonymous function) d3.js:884
d3_selection_each d3.js:890
d3_selectionPrototype.each d3.js:883
d3_selectionPrototype.attr d3.js:580
(anonymous function) nv.d3.js:5010
(anonymous function) d3.js:884
d3_selection_each d3.js:890
d3_selectionPrototype.each d3.js:883
chart nv.d3.js:4872
d3_selectionPrototype.call d3.js:897
(anonymous function) nv.d3.js:11818
(anonymous function) d3.js:8562
d3_selection_each d3.js:890
d3_transitionPrototype.each d3.js:8560
chart nv.d3.js:11729
d3_selectionPrototype.call d3.js:897
chart.update nv.d3.js:11738
window.onresize nv.d3.js:904
window.onresiz

我很确定它与d3.select有关,但是因为buildData创建了一个合法的Object,所以它没有任何意义.我还认为这可能是因为我的许多x值和y值相同,但是在nvd3.org上使用live code scatter plot example却表明这不是原因.

这是我的实际代码供参考…

 buildScatterPlot: function buildScatterPlot(tabularData){
     Ext.vcops.chrome.D3js.load(function() {
        Ext.vcops.chrome.nvd3.load(function(){

            nv.addGraph(function() {
                var chart = nv.models.scatterChart()
                    .showDistX(false)    //showDist, when true, will display those little distribution lines on the axis.
                    .showDistY(false)
                    .transitionDuration(350)
                    .color(d3.scale.category20().range());

                //Configure how the tooltip looks.
                chart.tooltipContent(function(key) {
                    return '<h3>' + key + '</h3>';
                });

                //Axis settings
                var xAxisLabel = tabularData.columns[1].label;
                var yAxisLabel = tabularData.columns[2].label;
                var xFormat;
                var yFormat;
                var xUnitId = tabularData.columns[1].unit === null ? null : tabularData.columns[1].unit.unitId;
                var yUnitId = tabularData.columns[2].unit === null ? null : tabularData.columns[2].unit.unitId;
                switch(xUnitId){
                    case "percent":
                        xFormat = '.02%'
                        break;
                    case null:
                    default:
                        xFormat = 'd'
                        break;
                }
                switch(yUnitId){
                    case "percent":
                        yFormat = '.02%';
                        break;
                    case null:
                    default:
                        yFormat = 'd';
                        break;
                }
                chart.xAxis
                    .axisLabel(xAxisLabel)
                    .tickFormat(d3.format(xFormat));
                chart.yAxis
                    .axisLabel(yAxisLabel)
                    .tickFormat(d3.format(yFormat));

                var d3data = buildData(xUnitId, yUnitId);
                console.log(d3data);
                d3.select('#chart svg')
                    .datum(d3data)
                    .call(chart);
                nv.utils.windowResize(chart.update);

                return chart;
            });

            /**************************************
             * Data generator
             */
            function buildData(xUnitId, yUnitId) { //# groups,# points per group
                var data = [];
                var skipped = 0;
                for (var i = 0; i < tabularData.totalRowCount; i++) {
                    var xVal;
                    var yVal;
                    if(tabularData.rows[i].cells[2].renderedValue === "-"){
                        skipped++;
                        continue;
                    }
                    switch(xUnitId){
                        case "percent":
                            xVal = tabularData.rows[i].cells[1].value / 100.0;
                            break;
                        case null:
                            xVal = tabularData.rows[i].cells[1].value;
                            break;
                    }
                    if(tabularData.rows[i].cells[2].renderedValue === "-"){
                        skipped++;
                        continue;
                    }
                    switch(yUnitId){
                        case "percent":
                            yVal = tabularData.rows[i].cells[2].value / 100.0;
                            break;
                        case null:
                            yVal = tabularData.rows[i].cells[2].value;
                            break;
                    }
                    if(xVal === null || yVal === null){
                        continue;
                    }
                    console.log(xVal);
                    console.log(yVal);
                    data.push({
                        key: tabularData.rows[i].objectIdentifier.resourceKey.resourceName,
                        values: []
                    });
                    data[i-skipped].values.push({
                        x: xVal,
                        y: yVal,
                        size: Math.random()  //Configure the size of each scatter point
                    });
                }
                return data;
            };
        });
    });
}

解决方法:

对于我的问题,这证明与d3.v3不兼容.我正在使用d3.v3,但是nvd3的稳定版本使用d3.v2:
https://github.com/novus/nvd3/commit/7e9b8c013c4d8e8ad5775062c438c842bc112585

我通过包括nvd3 / lib中提供的d3.v2版本解决了这个问题:
https://github.com/novus/nvd3/blob/master/lib/d3.v2.min.js

在这里找到答案:
https://stackoverflow.com/a/20994982/469594

标签:d3-js,extjs,javascript,nvd3-js
来源: https://codeday.me/bug/20191029/1959084.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有