javascript - d3力导向树上的曲线

标签 javascript svg d3.js force-layout

d3 的新手,正在尝试开发一个力导向树,我们可以将 varios 数据集插入其中。我已经设法启动并运行了基本想法,但想使链接弯曲,以便我可以处理多个链接。我看过 http://bl.ocks.org/1153292我就是不明白。我得到的最近的结果是它在没有可见路径的情况下工作。这是我的直线代码,如果您有时间,我将不胜感激

感谢:

//Sets up the svg that holds the data structure and puts it in the div called mapBox
var svg = d3.select("div#mapBox.theMap").append("svg")
.attr("width", mapWidth)
.attr("height", mapHeight);

//Sets up the data structure and binds the data to it       
var force = d3.layout.force()
.nodes(data.nodes)
.links(data.links)
.size([mapWidth, mapHeight])
.charge(-600)
.linkDistance(60)
.start();

//Draws the links and set up their styles
var link = svg.selectAll("link")
.data(data.links)
.enter().append("line")
.attr("class", "link")
.style("stroke", "#ccc")

//Creates nodes and attached "g" element to append other things to
var node = svg.selectAll(".node")
.data(data.nodes)
.enter().append("g")
.call(force.drag);

//Appends the cirdle to the "g" element and defines styles
node.append("circle")
.attr("r", function(d) { if (d.weight<1.1) {return 5} else {return d.weight*1.3+5 }})
.style("fill", "White") 
.style("stroke-width", 3)
.style("stroke", function(d) { if (d.type==1) {return "#eda45e "} if(d.type==2) {return "#819e9a"}else {return "#c36256" }} ) // Node stroke colors
.on("mouseover", nodeMouseover)
on("mouseout", nodeMouseout)
.on("mousedown", nodeMousedown)
.call(force.drag);

//Appends text to the "g" element and defines styles
node.append("text")
.attr("class", "nodetext")
.attr("dx", 16)
.attr("dy", ".35em")
.attr("text-anchor", function(d) { if (d.type==1) {return "middle";} else {return "start";} })
.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 + ")"; });

});

最佳答案

Der,解决了。

改变

.enter().append("line")

.enter().append("path")

然后改变

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; });

link.attr("d", function(d) {
var dx = d.target.x - d.source.x,
    dy = d.target.y - d.source.y,
    dr = Math.sqrt(dx * dx + dy * dy);
return "M" + d.source.x + "," + d.source.y + "A" + dr + "," + dr + " 0 0,1 " + d.target.x + "," + d.target.y;
});

希望能帮助像我一样陷入困境的人

关于javascript - d3力导向树上的曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13455510/

相关文章:

javascript - 谷歌柱形图裁剪酒吧

javascript - 如何避免为同一链接创建新选项卡(已在浏览器中打开)

javascript - 如何使用坐标系在dc.js中画线?

javascript - 尝试扩展 Angular 2ExceptionHandler 类时没有提供程序异常

javascript - 是否可以在 WebKit 中覆盖 document.cookie?

javascript - 在焦点上动画绘制 SVG 形状的最简洁方法是什么?

d3.js - 中心 D3 饼图在 div 的中心,尊重它的尺寸

java - 如何将 Java 对象转换为 GeoJSON(d3 Graph 需要)

d3.js - 自定义 d3 月或年刻度格式

d3.js - 如何用D3.js限制Voronoi图中多边形的文字?