最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - json representation for d3 force directed networks - Stack Overflow

programmeradmin3浏览0评论

I am trying to make a network visualisation of RISK. You can view it with the required code at .

The visualisation works but it required a lot of manual labor. I manually adapted the json file such that the connections were of the following shape:

{
    "source": 1,
    "target": 0,
    "value": 5
} 

What would one need to do to the d3 code such that a connection is determined by the names of the nodes? The input would then be:

{
    "source": "WesternAustralia",
    "target": "NewGuinea",
    "value": 5
} 

Whenever I try that I get the following error:

Uncaught TypeError: Cannot call method 'push' of undefined 

I am trying to make a network visualisation of RISK. You can view it with the required code at http://bl.ocks/4683850.

The visualisation works but it required a lot of manual labor. I manually adapted the json file such that the connections were of the following shape:

{
    "source": 1,
    "target": 0,
    "value": 5
} 

What would one need to do to the d3 code such that a connection is determined by the names of the nodes? The input would then be:

{
    "source": "WesternAustralia",
    "target": "NewGuinea",
    "value": 5
} 

Whenever I try that I get the following error:

Uncaught TypeError: Cannot call method 'push' of undefined 
Share Improve this question edited Jun 17, 2014 at 13:27 VividD 10.5k8 gold badges66 silver badges112 bronze badges asked Jan 31, 2013 at 15:57 cantdutchthiscantdutchthis 34.7k17 gold badges77 silver badges116 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 11

The D3 docs provide an explanation of how links work:

https://github./mbostock/d3/wiki/Force-Layout#wiki-links

In short, the array of links can either contain indices for source and target, which get remapped to objects from nodes, or they contain the references to the objects from nodes themselves. You need to remap your links' source and target to the objects from nodes.

Assuming your source and target properties are using names as shown in your second example above, you can add the follow snippet to the beginning of your d3.json callback to do the remapping:

    var nodeMap = {};
    graph.nodes.forEach(function(x) { nodeMap[x.name] = x; });
    graph.links = graph.links.map(function(x) {
      return {
        source: nodeMap[x.source],
        target: nodeMap[x.target],
        value: x.value
      };
    });
发布评论

评论列表(0)

  1. 暂无评论