我是D3的新手。我正在使用力导向图。我想在节点的位置添加两种不同类型的形状。
我的json正在关注:
{
"nodes":[
{"name":"00:00:00:00:00:00:00:01","group":0,"shape":1},
{"name":"00:00:00:00:00:00:00:02","group":1,"shape":1},
{"name":"00:00:00:00:00:00:00:03","group":2,"shape":1},
{"name":"00:00:00:00:00:00:00:11","group":0,"shape":0},
{"name":"00:00:00:00:00:00:00:21","group":1,"shape":0},
{"name":"00:00:00:00:00:00:00:31","group":2,"shape":0},
{"name":"00:00:00:00:00:00:00:32","group":2,"shape":0},
{"name":"00:00:00:00:00:00:00:12","group":0,"shape":0},
{"name":"00:00:00:00:00:00:00:22","group":1,"shape":0}
],
"links":[
{ "source": 0, "target": 0, "value": 5 },
{ "source": 1, "target": 1, "value": 5 },
{ "source": 2, "target": 2, "value": 5 },
{ "source": 3, "target": 0, "value": 5 },
{ "source": 4, "target": 1, "value": 5 },
{ "source": 5, "target": 2, "value": 5 },
{ "source": 6, "target": 2, "value": 5 },
{ "source": 7, "target": 0, "value": 5 },
{ "source": 8, "target": 1, "value": 5 }
]
}
如果形状值为1,则绘制圆,如果形状值为0,则绘制矩形。 强制有向图示例链接为:http://bl.ocks.org/mbostock/4062045
我尝试过示例链接JSFiddle:http://jsfiddle.net/mayurchavda87/Sc2xC/3/
答案 0 :(得分:5)
你可以这样做,如例如: this example,通过使用symbol generator和path
元素代替SVG元素来表示特定形状。添加形状的代码变为
var node = svg.selectAll(".node")
.data(data.nodes)
.enter().append("path")
.attr("class", "node")
.attr("d", d3.svg.symbol()
.type(function(d) { return d3.svg.symbolTypes[d.s]; }))
.style("fill", function(d) { return color(d.group); })
.call(force.drag);
然后,您还需要更改tick
处理程序,以更改transform
元素的path
属性:
node.attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")";
});
完成jsfiddle here。