标记d3.js中的多个边

时间:2015-06-03 10:39:22

标签: javascript d3.js label force-layout edge

我想在d3力图中标记两个节点之间的多个边。通过修改此示例(http://jsfiddle.net/7HZcR/3/),我有两条边分开显示,但与边相关的标签在一条边上重叠,而不是坐在自己的线上。我无法理解为什么会这样。我有什么想法可以解决这个问题?这是我的代码的相关部分:

d3.csv("GHT_1_A.csv", function(error, links) {

//sort links by source, then target
links.sort(function(a,b) {
if (a.source > b.source) {return 1;}
else if (a.source < b.source) {return -1;}
else {
    if (a.target > b.target) {return 1;}
    if (a.target < b.target) {return -1;}
    else {return 0;}
}
});
//any links with duplicate source and target get an incremented 'linknum'
for (var i=0; i<links.length; i++) {
if (i != 0 &&
    links[i].source == links[i-1].source &&
    links[i].target == links[i-1].target) {
        links[i].linknum = links[i-1].linknum + 1;
    }
else {links[i].linknum = 1;};
};

// Compute the distinct nodes from the links.
links.forEach(function(link) {
link.source = nodes[link.source] || 
    (nodes[link.source] = {name: link.source});
link.target = nodes[link.target] || 
    (nodes[link.target] = {name: link.target});
});

force = d3.layout.force()
.nodes(d3.values(nodes))
.links(links)
.size([width, height])
.linkDistance(200)
.charge(-1000)
.on("tick", tick)
.start();

svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);

// build the arrow.
svg.append("svg:defs").selectAll("marker")
.data(["end"])      // Different link/path types can be defined here
.enter().append("svg:marker")    // This section adds in the arrows  
.attr("id", String)
.attr("viewBox", "0 -5 10 10")
.attr("refX", 15)
.attr("refY", -1.5)
.attr("markerWidth", 6)
.attr("markerHeight", 6)
.attr("orient", "auto")
.append("svg:path")
.attr("d", "M0,-5L10,0L0,5");

var link = svg.append("svg:g").selectAll("g.link")
    .data(force.links())
  .enter().append('g')

var linkPath = link.append("svg:path")
    .attr('class', 'link')
    .attr("marker-end", "url(#end)");

var textPath = link.append("svg:path")
    .attr("id", function(d) { return d.source.index + "_" + d.target.index; })
    .attr("class", "textpath");

var path_label = svg.append("svg:g").selectAll(".path_label")
    .data(force.links())
.enter().append("svg:text")
    .attr("class", "path_label")
    .append("svg:textPath")
        .attr("startOffset", "50%")
        .attr("text-anchor", "middle")
        .attr("xlink:href", function(d) { return "#" + d.source.index + "_" + d.target.index; })
        .style("fill", "#000")
        .style("font-family", "Arial")
        .text(function(d) { return d.linkQ; });

function arcPath(leftHand, d) {
    var start = leftHand ? d.source : d.target,
        end = leftHand ? d.target : d.source,
        dx = end.x - start.x,
        dy = end.y - start.y,
        dr = 100/d.linknum;
        //dr = Math.sqrt(dx * dx + dy * dy),
        sweep = leftHand ? 0 : 1;
    return "M" + start.x + "," + start.y + "A" + dr + "," + dr + " 0 0," + sweep + " " + end.x + "," + end.y;
} 

var circle = svg.append("svg:g").selectAll("circle")
.data(force.nodes())
.enter().append("svg:circle")
.attr("r", 6)
.call(force.drag);

var text = svg.append("svg:g").selectAll("g")
.data(force.nodes())
.enter().append("svg:g");

// A copy of the text with a thick white stroke for legibility.
text.append("svg:text")
.attr("x", 8)
.attr("y", ".31em")
.attr("class", "shadow")
.text(function(d) { return d.name; });

text.append("svg:text")
.attr("x", 8)
.attr("y", ".31em")
.text(function(d) { return d.name; });

// Use elliptical arc path segments to doubly-encode directionality.
function tick() {
linkPath.attr("d", function(d) {
return arcPath(false, d);
});

textPath.attr("d", function(d) {
return arcPath(d.source.x < d.target.x, d);
});

circle.attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")";
});

text.attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")";
});
};
});

0 个答案:

没有答案