Mad*_*vda 5 d3.js force-layout
我是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 }
]
}
Run Code Online (Sandbox Code Playgroud)
如果形状值为1,则绘制圆形,如果形状值为0,则绘制矩形.强制有向图的示例链接是:http://bl.ocks.org/mbostock/4062045
我试过示例链接JSFiddle:http://jsfiddle.net/mayurchavda87/Sc2xC/3/
您可以执行此操作,如本示例所示,通过使用符号生成器和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);
Run Code Online (Sandbox Code Playgroud)
然后,您还需要更改tick处理程序以更改元素的transform属性path:
node.attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")";
});
Run Code Online (Sandbox Code Playgroud)
在这里完成jsfiddle .