如何在D3 / dagre-D3 / javascript中创建决策树/流程图?

时间:2019-05-03 16:10:16

标签: d3.js dagre-d3 dagre

所以我想创建一个如下的问题流程图: 'question flow' chart example 不确定最好的起点在哪里...这是有向图吗? 其中一些最终实际上是间隔开的,因此看起来不太适合“流”:https://observablehq.com/@d3/force-directed-graph

我见过的最好的例子是一个非D3库(yworks),但它似乎要花费15,000美元: yworks example 这是我所见的唯一相关的StackOverflow,仅引用yworks:Can I create a flow chart (no tree chart) using D3.js 也许也有这个dagre-d3示例: http://jsfiddle.net/armyofda12mnkeys/9L50of2c/2/

var g = new dagreD3.graphlib.Graph().setGraph({});

我想添加一些很酷的可选内容:

*我还希望能够控制Circle上的css,例如在某些情况下会基于该节点的数据将绿色变为红色。

*每个Edge箭头我也想添加onHovers事件,因此出现一个工具提示以显示诸如'if(Question1 == A || B)'之类的实际规则。

1 个答案:

答案 0 :(得分:0)

我想我与dagre-d3非常接近。 这是我的jsfiddle: http://jsfiddle.net/armyofda12mnkeys/4gv90qhx/2/

唯一可能丢失的是在连接的边缘线上悬停以显示悬停文本(我已经有一个不错的工具提示可用于Nodes,但不能用于Edges / arrows)。 例如,将鼠标悬停在QS1-> QS2行上,我想在工具提示弹出div中显示“ A> B”。注意:我不希望使用下方的元素,因为它真的很难悬停在细线上以激活它;还要对边缘使用类似的工具提示div弹出窗口。

有什么想法吗? 我有点接近答案弹出窗口,但是以更丑陋的方式完成(请看第一行): http://jsfiddle.net/armyofda12mnkeys/4gv90qhx/37/

// Create a new directed graph
var g = new dagreD3.graphlib.Graph().setGraph({});

var nodes = [ 
{'qs_code':"QS1", 'hovertext': 'This is QS1', 'proto_logic_type': 'none' },
{'qs_code':"QS2", 'hovertext': 'This is QS2', 'proto_logic_type': 'disqualify'},
{'qs_code':"QS3", 'hovertext': 'This is QS3', 'proto_logic_type': 'qualify'},
{'qs_code':"QS4", 'hovertext': 'This is QS4', 'proto_logic_type': 'both'},
{'qs_code':"QS5", 'hovertext': 'This is QS5', 'proto_logic_type': 'none'},
{'qs_code':"QS6", 'hovertext': 'This is QS6', 'proto_logic_type': 'none'}
];

// Automatically label each of the nodes
nodes.forEach(function(node) {
    g.setNode(node.qs_code, { label: node.qs_code, shape: "circle", class: [node.proto_logic_type], hovertext: node.hovertext  });  //style: 'fill: red' 
});

// Set up the edges
g.setEdge("QS1", "QS2", { label: "<u onmouseover='(function(){ return $(\"#tooltip_template\").css(\"visibility\", \"visible\"); })()' onmouseout='(function(){ return $(\"#tooltip_template\").css(\"visibility\", \"hidden\"); })()' onmousemove='(function(){ $(\"#tooltip_template\").html(\"AAA&amp;gt;BBB\").css(\"top\", (event.pageY-10)+\"px\").css(\"left\",(event.pageX+10)+\"px\"); })()'>Rule1</u>", hovertext:"A>B", labelType: "html" });
g.setEdge("QS1", "QS3", { label: "<u onmouseover='(function(){ return $(\"#tooltip_template\").css(\"visibility\", \"visible\"); })()' onmouseout='(function(){ return $(\"#tooltip_template\").css(\"visibility\", \"hidden\"); })()' onmousemove='(function(){ $(\"#tooltip_template\").html(\"AAA&amp;lt;BBB\").css(\"top\", (event.pageY-10)+\"px\").css(\"left\",(event.pageX+10)+\"px\"); })()'>Rule2</u>", hovertext:"A<B", labelType: "html" });
g.setEdge("QS1", "QS4", { label: "<u onmouseover='(function(){ return $(\"#tooltip_template\").css(\"visibility\", \"visible\"); })()' onmouseout='(function(){ return $(\"#tooltip_template\").css(\"visibility\", \"hidden\"); })()' onmousemove='(function(){ $(\"#tooltip_template\").html(\"AAA==BBB\").css(\"top\", (event.pageY-10)+\"px\").css(\"left\",(event.pageX+10)+\"px\"); })()'>Rule3</u>", hovertext:"A==B", labelType: "html" });

g.setEdge("QS2", "QS5", { label: "Rule1", arrowhead: "vee", hovertext:"(A+B)>1" });

g.setEdge("QS3", "QS5", { label: "Rule1", hovertext:"(A-B)<2" });
g.setEdge("QS3", "QS6", { label: "Rule2", hovertext:"(A*B)>=3" });

g.setEdge("QS4", "QS6", { label: "Rule2", arrowhead: "vee", hovertext:"(A>10)||(B<20)" });


var svg = d3.select("svg"),
    inner = svg.select("g");

// Set the rankdir
g.graph().rankdir = 'TB';//'LR';
g.graph().nodesep = 50;

// Set up zoom support
var zoom = d3.behavior.zoom().on("zoom", function() {
      inner.attr("transform", "translate(" + d3.event.translate + ")" +
                                  "scale(" + d3.event.scale + ")");
    });
svg.call(zoom);

// Create the renderer
var render = new dagreD3.render();


// Run the renderer. This is what draws the final graph.
render(inner, g);


var tooltip = d3.select("body")
    .append("div")
  .attr('id', 'tooltip_template')
    .style("position", "absolute")
    .style("background-color", "white")
  .style("border", "solid")
  .style("border-width", "2px")
  .style("border-radius", "5px")  
  .style("padding", "5px")
    .style("z-index", "10")
    .style("visibility", "hidden")
    .text("Simple Tooltip...");

inner.selectAll('g.node')
  .attr("data-hovertext", function(v) { 
        return g.node(v).hovertext
    })
    .on("mouseover", function(){return tooltip.style("visibility", "visible");})
    .on("mousemove", function(){ 
    tooltip.text( this.dataset.hovertext)   
        .style("top", (event.pageY-10)+"px")
        .style("left",(event.pageX+10)+"px");
  })
    .on("mouseout", function(){return tooltip.style("visibility", "hidden");});

inner.selectAll('g.edgePath')
//inner.selectAll('path')
.append('title').text('This is a line.');

// Center the graph
var initialScale = 0.75;
zoom
  .translate([(svg.attr("width") - g.graph().width * initialScale) / 2, 20])
  .scale(initialScale)
  .event(svg);
svg.attr('height', g.graph().height * initialScale + 40);