我正在使用 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'
});
}
任何指针?