39

can I create a flowchart like this one: enter image description here

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.

Sebastian
  • 7,729
  • 2
  • 37
  • 69
Dino
  • 1,307
  • 2
  • 16
  • 47
  • 1
    The JSON structure is really entirely up to you. As far as I know, there're no examples of flow charts with D3. – Lars Kotthoff Mar 06 '14 at 16:23
  • 1
    “D3.js is not suited very well for this kind of visualization.” exactly, Search for "Javascript Flow Chart" – Chaoyu Jul 16 '14 at 02:03
  • I think this gives you what you need: http://www.daviddurman.com/automatic-graph-layout-with-jointjs-and-dagre.html – Simon Jul 16 '14 at 01:49

3 Answers3

34

can I create a flowchart like this one?

D3 is capable of doing it.

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

Yes

What should the json structure look like?

Depends on how you approach the project. I'm using force layout and removing the force, so my JSON is

{node:
    [{
        id: 1,
        title: 'title'
    }],
 link: 
    [{
        source: 0,
        target: 1
    }]
 }

Have you got any example I can analyse?

For your inspiration

Starting point https://github.com/mbostock/d3/wiki/Force-Layout

how to wrap your text in block http://bl.ocks.org/mbostock/7555321

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

Not sure why, but IMO flowcharts are one of the simplest types of diagrams, blocks and lines that connect them. D3 has built-in means to draw nodes and connectors. Yes that is not out of the box, ready to use solution. But all you need to do is learn and customize.

Edmund Sulzanok
  • 1,883
  • 3
  • 20
  • 39
24

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:

Screenshot of image created with yFiles for HTML demo

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:

yFiles for HTML Flowchart Demo

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.

Sebastian
  • 7,729
  • 2
  • 37
  • 69
  • Hi, I like this demo .I can see this demo by http://live.yworks.com/yfiles-for-html/2.0/databinding/graphbuilder/index.html. But how can I get the source code of this demo . I have downloaded the yFiles-for-HTML-Complete-2.0.1.1-Evaluation\yFiles-for-HTML-Complete-2.0.1.1-Evaluation.zip. But how can I get the code of the demo you shared? – liam xu Jun 21 '17 at 12:07
  • 2
    The demo is part of the zip, linked from the aptly named README.html file. But SO is not the place for yFiles support. Please refer to the yFiles for HTML support team. – Sebastian Jun 21 '17 at 19:44
  • 1
    -1 from promoting paid software instead of limiting to answer the question. Remaining content is biased and there are alternatives open and free that were not taken into consideration. – Sebastian Jul 28 '20 at 15:23
  • @JeffRyan: and I know that for my project (covidgraph.org) it was free. The range is enormous and mentioning the extreme endpoints of the scale is not helpful and not even part of the discussion. You should disclose that. Downvoting an answer because there is price involved in the solution (and free was not part of the requirement) is not helpful. – Sebastian Sep 14 '20 at 06:50
  • @Sebastian You work for the company, this is a marketing effort. I'm adding context to tell people of the costs so they can make an informed decision. I don't know what the ranges are, since the estimator gave me 1 price, but the number of developers I have on this project would fit on one hand. Doesn't sound like an extreme end of the spectrum to me. – Jeff Ryan Sep 15 '20 at 18:12
  • 1
    @JeffRyan you quoted the wrong sebastian :) (not sure if he will receive the notification) – Sebastian Sep 15 '20 at 19:09
4

I have been looking into this today and flowchart.js looks promising. It supports start and end points, operations, and conditions. You can control which side of the drawn elements the lines come out of, etc.

http://adrai.github.io/flowchart.js/

Devon Holcombe
  • 518
  • 1
  • 5
  • 18
  • The library seems pretty rough around the edges, but all in all it's pretty cool! – joonas.fi Aug 01 '16 at 15:03
  • 2
    flowchart.js is a really nice lib but has some limitations. e.g. it does not support conditions with more/other than just YES/NO and operations can only have one outlet. – d00d Feb 02 '17 at 03:33
  • 1
    I was looking at flowchart.js too and my eyes widened when I saw that the only conditions you can choose from are yes/no. Sure wish I could customize those options. – jkj2000 Jul 12 '18 at 03:51