Different node symbols for d3.js force-directed graph

拈花ヽ惹草 提交于 2019-11-30 05:42:41

问题


How do I display nodes as different symbols in d3.js's force-directed library? I wanted to implement something similar to what I wrote below:

  var node = svg.selectAll(".node") 
      .data(graph.nodes) 
    .enter().append(function(d){return d.shape;}) 
      .attr("class", "node") 
      .attr("r", 5) 
      .style("fill", function(d) { return color(d.group); }) 
      .call(force.drag); 

Each node would have an encoded shape ("rect", "circle", etc.). However, I get the error:

Uncaught TypeError: Object function (d){return "circle";} has no method 'indexOf' 

The other question I have related to that is this: how would I toggle between applying different attributes for each shape? Circles need an "r" attribute refined, but rects require "height" and "width". Thanks!


回答1:


Use d3.svg.symbol, as in the force-directed symbols example.



来源:https://stackoverflow.com/questions/15855794/different-node-symbols-for-d3-js-force-directed-graph

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!