繁体   English   中英

D3中的Sankey图

[英]Sankey Diagram in d3

我想从bl.ocks.org( http://bl.ocks.org/d3noob/c2637e28b79fb3bfea13 )运行一个sankey图的示例,但是当我使用

python -m SimpleHTTPServer 8888 &

从下面一行中的index.html,sankey.js和sankey-formatted.json文件夹中

source.sourceLinks.push(link);

给出一个错误:

sankey.js:91TypeError: undefined is not an object (evaluating 'source.sourceLinks.push')

此代码来自的功能是:

  links.forEach(function(link) {
  var source = link.source,
      target = link.target;
  if (typeof source === "number") source = link.source = nodes[link.source];
  if (typeof target === "number") target = link.target = nodes[link.target];
  source.sourceLinks.push(link);
  target.targetLinks.push(link);
});

而我的json文件是:

{
"nodes": [
{
  "name": "Africa"
}, 
{
  "name": "America"
}, 
...
],
"links":[
{
  "source": "Africa", 
  "target": "America", 
  "value": 1
}, 
{
  "source": "America", 
  "target": "Africa", 
  "value": 2
}, 
...
]}
  // Populate the sourceLinks and targetLinks for each node.
  // Also, if the source and target are not objects, assume they are indices.
  function computeNodeLinks() {
    nodes.forEach(function(node) {
      node.sourceLinks = [];
      node.targetLinks = [];
    });
    links.forEach(function(link) {
      var source = link.source,
          target = link.target;
      if (typeof source === "number") source = link.source = nodes[link.source];
      if (typeof target === "number") target = link.target = nodes[link.target];
      source.sourceLinks.push(link);
      target.targetLinks.push(link);
    });
  }

我认为您的数据结构中缺少某些内容。 请使用此重构的数据再试一次。 node索引,这是建立链接所必需的。

{
"nodes": [
  {
    "node" : 0,
    "name": "Africa"
  }, 
  {
    "node" :1,
    "name": "America"
  }, 
  {
    "node" :2,
    "name": "Europe"
  }
],
"links":[
  {
    "source": 0, 
    "target": 2, 
    "value": 1
  }, 
  {
    "source": 1, 
    "target": 2, 
    "value": 2
  }, 
  {
    "source": 0, 
    "target": 1, 
    "value": 1
  }
]}

柱塞示例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM