d3定向图编辑器的新增功能

swi*_*tor 5 javascript graph d3.js

我一直试图通过一些想法来增加d3定向图编辑器代码,这使得它成为想要"手动增长"图形的人们的非常直观的界面.我设法将节点ID更改为用户可以通过提示调用输入的字符串.将"类型"值添加到连接节点的链接也是如此.

我努力的部分是,当图形重绘时,我无法设法在链接中间呈现链接属性"类型".任何人都可以提供帮助/我能够管理的代码吗?

以下是设置示例节点和链接的行:

var nodes = [
    {id: 'some name', reflexive: false},
    {id: 'some other name', reflexive: true },
    {id: 'some 3rd name', reflexive: false}
],
links = [
    {source: nodes[0], target: nodes[1], left: false, right: true ,type: 'some type I'},
    {source: nodes[1], target: nodes[2], left: false, right: true, type: 'some type II'}
];
Run Code Online (Sandbox Code Playgroud)

当节点被渲染时,有一个代码将id添加为字符串:

g.append('svg:text')
    .attr('x', 0)
    .attr('y', 4)
    .attr('class', 'id')
    .text(function(d) { return d.id; });
Run Code Online (Sandbox Code Playgroud)

但是,当链接重新绘制时,没有函数可以获取type属性的值,并在源节点和目标节点的坐标之间解析它.这是代码处理,据我所知,渲染链接.

// update existing links
path.classed('selected', function(d) { return d === selected_link; })
    .style('marker-start', function(d) { return d.left ? 'url(#start-arrow)' : ''; })
    .style('marker-end', function(d) { return d.right ? 'url(#end-arrow)' : ''; });


// add new links
path.enter().append('svg:path')
    .attr('class', 'link')
    .classed('selected', function(d) { return d === selected_link; })
    .style('marker-start', function(d) { return d.left ? 'url(#start-arrow)' : ''; })
    .style('marker-end', function(d) { return d.right ? 'url(#end-arrow)' : ''; })
    .on('mousedown', function(d) {
        if(d3.event.ctrlKey) return;

        // select link
        mousedown_link = d;
        if(mousedown_link === selected_link) selected_link = null;
        else selected_link = mousedown_link;
        selected_node = null;
        restart();
    });
Run Code Online (Sandbox Code Playgroud)

这是一个带有最新版代码的jsfiddle链接.

Eli*_*jah 4

在您的刻度函数中,只需根据源 xy 和目标 xy 重新计算文本的 xy 位置:

 function tick() {
   d3.selectAll("text")
   .attr("x", function(d) {return (d.source.x + d.target.x) / 2})
   .attr("y", function(d) {return (d.source.y + d.target.y) / 2})
 }
Run Code Online (Sandbox Code Playgroud)

这是带有路径标签的更新后的 jsFiddle: http ://jsfiddle.net/rco31ofe/2/