我正在使用D3.js.原始源设置source
对象中的target
和links
数组。
我正在尝试添加一个包含百分比的数组。我遵循的技术与源和技术相同。目标
但是,JavaScript控制台会抛出错误 - Uncaught TypeError: Cannot read property 'percentages' of undefined
在下面的代码中,我突出显示了为百分比添加的语句。
function computeNodeLinks() {
nodes.forEach(function(node) {
node.sourceLinks = [];
node.targetLinks = [];
node.percentages = []; //**** Added this
});
links.forEach(function(link) {
var source = link.source,
target = link.target,
pc = link.pc; //**** Added this
if (typeof source === "number") source = link.source = nodes[link.source];
if (typeof target === "number") target = link.target = nodes[link.target];
if (typeof pc === "number") pc = link.pc = nodes[link.pc]; //**** Added this
source.sourceLinks.push(link);
target.targetLinks.push(link);
pc.percentages.push(link); //**** Added this. At this statement I get: 'Uncaught TypeError: Cannot read property 'percentages' of undefined'
});
}
任何指针?