无法推送到JavaScript数组

时间:2012-12-18 06:29:54

标签: javascript d3.js

我正在使用D3.js.原始源设置source对象中的targetlinks数组。

我正在尝试添加一个包含百分比的数组。我遵循的技术与源和技术相同。目标

但是,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'
    });
  }

任何指针?

0 个答案:

没有答案