![](/img/trans.png)
[英]How are svg, d3, dagre, dagre-d3 and graphlib dependent on each other?
[英]How to create a decision tree / flow chart in D3/dagre-D3/javascript?
所以我想創建一個如下的問題流程圖: 不確定從哪里開始最好......這是有向圖嗎? 其中一些最終真的被隔開並且看起來不太適合像這樣的“流”: https : //observablehq.com/@d3/force-directed-graph
我見過的最好的例子是一個非 D3 庫(yworks),但它似乎要花費 1.5 萬美元: 這是我見過的唯一一個僅引用 yworks 的相關 StackOverflow: 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({});
我想添加一些很酷的可選內容:
*我還希望能夠控制 Circles 上的 css,就像在某些情況下根據該節點的數據,有些人會變成綠色有些紅色。
*每個邊緣箭頭我還想添加 onHovers 事件,因此會出現一個工具提示以顯示實際規則,例如“if(Question1 == A || B)”
*使節點/邊緣可拖動或“有彈性”(如果拖動,它們會彈回原點位置)。 聽起來很花哨,但有時用戶可能會在規則過於擁擠時使用此功能(因為智能自動布局)並且他們想拖動東西以查看箭頭指向的位置。
我想我是用 dagre-d3 搞定的。 這是我最初的 jsfiddle: http : //jsfiddle.net/armyofda12mnkeys/4gv90qhx/2/
這里也是同樣的例子,邊緣也有彈出窗口(雖然我不喜歡節點彈出窗口的實現) http://jsfiddle.net/armyofda12mnkeys/4gv90qhx/37/
這是我如何在我的糖尿病問卷項目中使用的完整示例(我將代碼升級到最新的 d3.v5+dagre,並使節點+邊緣可拖動......許多初始 JSON 解析代碼以獲得轉換成我實際上可以循環的格式,抱歉): https ://jsfiddle.net/armyofda12mnkeys/1burht5j/44/ 注意:如果我使用的“cors-anywhere”網站關閉,最后一個鏈接可能無法工作。 那就試試下載吧。
// 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&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&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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.