我可以使用D3.js创建流程图(无树图) [英] Can I create a flow chart (no tree chart) using D3.js

查看:2891
本文介绍了我可以使用D3.js创建流程图(无树图)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以创建一个这样的流程图:



使用。



免责声明:我在创建该库的公司工作,但是在SO / SE我不代表我的雇主。我的帖子是我自己的。


can I create a flowchart like this one:

starting form a json object using the D3.js library?

What should the json structure look like?

Have you got any example I can analyse?

Thank you very much.

解决方案

In my humble opinion D3.js is not suited very well for this kind of visualization. The visualization is just too complex to do a simple mapping from data to SVG (and that's what D3.js is mainly for: generating DOM (documents) from Data through concise mappings).

You can work around those limitations by introducing more logic in between so that data is not directly being displayed, but rather a model is being created, some calculations are being done and then the result is transformed to SVG by D3. This is what dagre is trying to do and it works rather well.

However your graph has some special constraints that are currently not supported by simpler implementations of the layout algorithm: In the last layer you get a fork-like rendering. In the "decision"-layer you assign constraints to the edges so that they leave at the left and right of the nodes, you also constrain the decision nodes to be on the same layer.

All of this information is not visible in the graph structure itself. Therefor you need to annotate that information into your model and tell the layout algorithm to honor these constraints. To the best of my knowledge, only commercial graph drawing library implementations currently support these advanced layout features.

yFiles for HTML is such a library: In this demo you can use the following JSON to get a result like this:

Switch to the combobox entry at the top: "5 - Complex Objects + Edge Labels"

And modify the JSON in the Nodes Sources, Edges Source and Node Template sections as follows:

Nodes Source

{
0:{'name':'Start',color:'green'}, 
1:{'name':'Read A,B,C',color:'yellow'}, 
2:{'name':'Is B>C?',color:'green'},
3:{'name':'Is A>B?',color:'green'},
4:{'name':'Is A>C?',color:'green'},
5:{'name':'Print B',color:'green'},
6:{'name':'Print C',color:'green'},
7:{'name':'Print C',color:'green'},
8:{'name':'Print A',color:'green'},
9:{'name':'End',color:'red'}
}

Edge Source

[
{from:'0', to:'1', label:''}, 
{from:'1', to:'3', label:''}, 
{from:'3', to:'2', label:'No'}, 
{from:'3', to:'4', label:'Yes'},
{from:'2', to:'5', label:'Yes'},
{from:'2', to:'6', label:'No'},
{from:'4', to:'7', label:'No'},
{from:'4', to:'8', label:'Yes'},
{from:'5', to:'9', label:''},
{from:'6', to:'9', label:''},
{from:'7', to:'9', label:''},
{from:'8', to:'9', label:''}
]

Template

<rect fill='{Binding color}' stroke='LightBlue' stroke-width='2' 
    width='{TemplateBinding width}' 
    height='{TemplateBinding height}'></rect>
<text transform='translate(10 20)' data-content='{Binding name}'   
    style='font-size:18px; font-family:Arial; fill:#000; text-anchor: left;
    dominant-baseline: central;'></text>

Note that using a different kind of JSON is possible, too (as the demo shows). I don't believe that the JSON format is going to be a problem in any way at all and as you can see you get a decent result, but still the constraints I mentioned have not been considered and added to the JSON. Unfortunately adding these constraints cannot be done through the demo interface I'm using above right now, but needs to be done by adding more code to the actual source code of the demo. You can see how these constraints work in another demo (though without the JSON) in this interactive layout demo.

Disclaimer: I work for the company that creates that library, however on SO/SE I do not represent my employer. My posts are my own.

这篇关于我可以使用D3.js创建流程图(无树图)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆