D3.js force directed graph, each group different color?

前端 未结 5 1404
小蘑菇
小蘑菇 2021-01-12 11:27

I\'ve made a force directed graph with d3.js plugin, and I wanna color the nodes and the labels with the different color according to group which they belong.

I\'ve

5条回答
  •  迷失自我
    2021-01-12 12:10

    Your group info is only available in the links object, like @ckersch already pointed out. You would need to add the group info to you nodes object too. For this example that can be done by changing line 16 into:

    link.target = nodes[link.target] || (nodes[link.target] = {name: link.target, group: link.group});
    

    But for more complex data, with more than one source, all sources would have the same colour (or would that be OK?).

    I made that change in this Fiddle: http://jsfiddle.net/WBkw9/19/.

提交回复
热议问题