javascript - d3.js adding a triangles on lines in force-directed graph -
i have force directed graph id d3.js i'm using fun. looks this: http://jsfiddle.net/dzorz/wnstf/
i wanna add little triangles on 2 black lines, arrows. i've tried add triangle path, don't know how append lines..
script:
var data = {"nodes":[ {"name":"action 4", "type":5, "slug": "", "value":265000}, {"name":"action 5", "type":6, "slug": "", "value":23000}, {"name":"action 3", "type":4, "slug": "", "value":115000}, {"name":"iron man", "type":1, "slug": "iron_man", "img_href": "http://www.1sticondesign.com/core/free/ironman-128.png"}, {"name":"superman", "type":1, "slug": "superman", "img_href":"http://www.desktop-icon.com/stock-icons/desktop-boss/superman-icon.gif"}, {"name":"action 1", "type":2, "slug": "",}, {"name":"action 2", "type":3, "slug": "",}, {"name":"batman", "type":1, "slug": "batman", "img_href": "http://icons.iconarchive.com/icons/iconshock/batman/256/batman-icon.png"} ], "links":[ {"source":0,"target":3,"value":10}, {"source":4,"target":3,"value":1}, {"source":1,"target":7,"value":10}, {"source":2,"target":4,"value":10}, {"source":4,"target":7,"value":1}, {"source":4,"target":5,"value":10}, {"source":4,"target":6,"value":10} ] } var w = 560, h = 500, radius = d3.scale.log() .domain([0, 312000]) .range(["10", "50"]); var vis = d3.select("body") .append("svg:svg") .attr("width", w) .attr("height", h); //d3.json(data, function(json) { var force = self.force = d3.layout.force() .nodes(data.nodes) .links(data.links) .distance(100) .charge(-1000) .size([w, h]) .start(); var link = vis.selectall("line.link") .data(data.links) .enter().append("svg:line") .attr("class", function (d) { return "link" + d.value +""; }) .attr("x1", function(d) { return d.source.x; }) .attr("y1", function(d) { return d.source.y; }) .attr("x2", function(d) { return d.target.x; }) .attr("y2", function(d) { return d.target.y; }); function openlink() { return function(d) { var url = ""; if(d.type == 1) { url = "wiki/" + d.slug } //else if(d.type == 2) { //url = "clients/" + d.slug //} else if(d.type == 3) { //url = "agencies/" + d.slug //} window.open("https://en.wikipedia.org/"+url) } } var node = vis.selectall("g.node") .data(data.nodes) .enter().append("svg:g") .attr("class", "node") .call(force.drag); node.append("circle") .attr("class", function(d){ return "node type"+d.type}) .attr("r", function(d) { return radius(d.value) || 10 }) //.style("fill", function(d) { return fill(d.type); }) .call(force.drag); node.append("svg:image") .attr("class", "circle") .attr("xlink:href", function(d){ return d.img_href}) .attr("x", "-16px") .attr("y", "-16px") .attr("width", "32px") .attr("height", "32px") .on("click", openlink()); node.append("svg:text") .attr("class", "nodetext") .attr("dx", 16) .attr("dy", ".35em") .text(function(d) { return d.name }); force.on("tick", function() { link.attr("x1", function(d) { return d.source.x; }) .attr("y1", function(d) { return d.source.y; }) .attr("x2", function(d) { return d.target.x; }) .attr("y2", function(d) { return d.target.y; }); node.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; }); }); //});
css:
.link10 { stroke: #ccc; stroke-width: 3px; stroke-dasharray: 3, 3; } .link1 { stroke: #000; stroke-width: 3px;} .nodetext { pointer-events: none; font: 10px sans-serif; } .node.type1 { fill:brown; } .node.type2 { fill:#337147; } .node.type3 { fill:blue; } .node.type4 { fill:red; } .node.type5 { fill:#1bc9e0; } .node.type6 { fill:#e01b98; } image.circle { cursor:pointer; }
could possibly show me in jsfiddle?
if split data entry block , define variable
var links = svg.selectall('line.link') .data(data.link) .enter()
you can append multiple different things selection have defined links
. aren't adding more elements lines, per se, rather adding elements selections adding lines to, corresponding adding shapes lines in 1 one ratio.
i've modified your fiddle add circle middle of each line.
if want add elements black lines, can use filter creating new data set , operating on that.
Comments
Post a Comment