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

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

问题描述

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

使用 .

将所有的点点滴滴放在一起,您可以轻松地自动实现这种结果:

可以在此 交互式流程图布局中找到并以交互方式尝试相同的示例演示.

免责声明:我为创建 yFiles(商业,价格范围:撰写本文时为 0k 到 ~104k 美元)库的公司工作,但是在 SO/SE 上我是这样做的不代表我的雇主.我的帖子、想法和建议都是我自己的.这不是广告.随意使用不同的解决方案.我研究这个话题已经将近 25 年了,那是因为我在这里.我觉得这是对 OP 问题的最佳解决方案和答案.

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 that 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.

Putting all the bits and pieces together you can easily achieve this kind of result, automatically:

The same example can be found and tried out interactively in this interactive flowchart layout demo.

Disclaimer: I work for the company that creates the yFiles (commercial, price range: 0k to ~104k USD at the time of writing) library, however on SO/SE I do not represent my employer. My posts, thoughts, and recommendations are my own. This is not an ad. Feel free to use a different solution. I've been researching this topic for almost 25 years and that's because I am here. I feel this is the best solution and answer to the OP's question.

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

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