Flow charts in code: enter graphviz and the “dot” language

If you’re like me, you like gui’s as long as they don’t push you in a direction other than your train of thought. Whenever the tool tends to distract you from the task you are performing, you get annoyed. Stuff like “why am I searching for such an over-obvious functionality”, or “why didn’t they think of making the clickable area a bit bigger”, or simply “aaaarggghhh, it crashed on me again”.

I like typing code. It says what it is. I even considered writing graphs such as flow charts or database models in SVG once, but that’s a bridge too far. What is pretty helpful though, is graphviz, and the “dot” language.

In essence, DOT1 is a declarative language in which you express nodes and their relations in a graph. You can label the nodes and their edges (links between nodes) and you have an array of styling and shaping tools at hand. I have never more quickly drawn a flow chart for some documenting work than with this tool. So I decided I’d use it for my database model too, and behold, it helped me focusing on just the content and logic in stead of any other crap.

I’ll focus just on the flow chart here, but I hope it’ll get you enthusiastic to apply it to other types of graphs.

Installing graphviz

Graphviz is oooold. It should be available in your distro’s package repository, or you can download binaries from the graphviz website.

The tools

Graphviz comes with a set of layout engines which have different command line tools. I have only used dot so far, tried a bit of fdp and neato, but as long as I do not really understand how they differ, I can’t really tell you anything about them.

So, you’ll only need dot for now.

How to get started

There are two basic structures you can use, a graph and a digraph which stand for a graph and a directed graph respectively. The difference is that a directed graph always has edges with a “value”, i.e., you’d speak of a “parent”-“child” type of relationship. A regular graph is just nodes that are somehow connected, but there is no direction or value to their relationship. For example, synonyms are synonyms either way.

In practice, most of the graphs or charts you’ll do are directed graphs. For now it suffices to say that a directed graph connects nodes by arrows (->) and regular graphs by a line (--).

My first graph

digraph {
    A -> B;
}

Using the dot utility, you can render the graph as an image or many other output formats:

dot -Tpng -o graph.png graph.dot


We declare a relationship between the parent and the child. By declaring the relationship, we also declare the nodes. So if we’d output this graph, we’d see the relationship.

We can continue declaring relationships and nodes like this, in this declarative form:

digraph {
    A -> B -> C -> D;
    C -> E -> F;
    F -> B;
}

As you can see, it is pretty darn easy. There is absolutely no way you could have done this faster, and have the advantages of easily editing the graph structure, be able to version control it, insert comments, etc.


How to start writing a flow chart

Flow charts are a typically good example for using graphviz. You’ll usually only need a few different shapes, layout of the resulting graph isn’t overly important, and specifying relationships between the different nodes really is all you need.

digraph {
    label="How to make sure 'input' is valid"
 
    start[shape="box", style=rounded];
    end[shape="box", style=rounded];
    if_valid[shape="diamond", style=""];
    message[shape="parallelogram", style=""]
    input[shape="parallelogram", style=""]
 
    start -> input;
    input -> if_valid;
    if_valid -> message[label="no"];
    if_valid -> end[label="yes"];
    message -> input;
}


Since all node mentioning is declarative, you can easily put labels in a different section than the structure of your graph:

digraph {
    label="How to make sure 'input' is valid"
 
    start[shape="box", style=rounded];
    end[shape="box", style=rounded];
    if_valid[shape="diamond", style=""];
    message[shape="parallelogram", style=""]
    input[shape="parallelogram", style=""]
 
    start -> input;
    input -> if_valid;
    if_valid -> message[label="no"];
    if_valid -> end[label="yes"];
    message -> input;
 
    if_valid[label="Is input\nvalid?"]
    message[label="Show\nmessage"]
    input[label="Prompt\nfor input"]
}


Moreover, you can use the node keyword to declare attributes for sets of nodes:

digraph {
    label="How to make sure 'input' is valid";
 
    node[shape="box", style="rounded"]
       start; end;
    node[shape="parallelogram", style=""]
       message; input;
    node[shape="diamond", style=""]
       if_valid;
 
    start -> input;
    input -> if_valid;
    if_valid -> message[label="no"];
    if_valid -> end[label="yes"];
    message -> input;     
 
    if_valid[label="Is input\nvalid?"]
    message[label="Show\nmessage"]
    input[label="Prompt\nfor input"]
}


This can add so much productivity to drawing graphs that it is silly not to use it. If you’re not happy about the layout, you can use ranking of nodes to make sure nodes are ranked equally so they are put next to each other:

digraph {
    label="How to make sure 'input' is valid";
 
    node[shape="box", style="rounded"]
       start; end;
    node[shape="parallelogram", style=""]
       message; input;
    node[shape="diamond", style=""]
       if_valid;
 
    start -> input;
    input -> if_valid;
    if_valid -> message[label="no"];
    if_valid -> end[label="yes"];
    message -> input;     
 
    {rank=same; message input}
}


But if I’m honest, you shouldn’t bother too much. If you want the layout more presentable, you should just output any of the vector-based output formats and tweak it yourself with any drawing program out there, or even better: output SVG and an XSL transformation to do that for you ;). The essence of it all is that you are productive in documenting something, not in making beautifully laid out graphs. Be focused on that rather than having people “ooh” and “aah” your handywork.

Summarizing

Keep focus on the structure and decisions in your chart, in stead of how stuff should be placed and how it is laid out. You should be worrying about the concept behind the chart in stead of the chart it self. What better way just to be concerning yourself with the nodes, their relations and their type?

The key is that you are documenting domain logic, which is expressed in a logical sense rather than some graphical visualization. You have many possibilities here: for example, generate these charts from static code analysis, generate model relationships from your domain model, class diagrams, etc, etc. I’d say the possibilities are endless, but that seems tautologic.

Have fun, and let me know if you have any tips regarding graphviz or the DOT language!


  1. Don’t worry, you don’t need to be able to read the BNF syntax description. There’s more docs out there. 

This entry was posted in Development, Linux & BSD and tagged , , . Bookmark the permalink. Trackbacks are closed, but you can post a comment.

17 Comments

  1. Marlin Forbes
    Posted July 20, 2016 at 06:36 | Permalink

    This package is especially useful for mapping out state changes and event flows in a workflow system. Instead of having a separate .dot file with your graphic defined in it, use a PHP package like this https://github.com/graphp/graphviz to generate a graphic from the runtime state of your application. You could also use PHP to generate the .dot file based on internal application state. Then you don’t have to worry about keeping the .dot file up-to-date, it’s live. 🙂

  2. Tom Newton
    Posted September 30, 2016 at 11:10 | Permalink

    Thanks – really useful primer.

  3. Mayank Jain
    Posted October 6, 2016 at 13:34 | Permalink

    Exactly what I was looking for.. Thanks!

  4. Chris Dowey
    Posted January 4, 2017 at 18:14 | Permalink

    Very useful overview, thanks!

  5. Jay
    Posted January 10, 2017 at 07:26 | Permalink

    Great intro, thanks!

  6. James
    Posted September 28, 2018 at 20:58 | Permalink

    This is the first graphviz walk-through I’ve found that’s been helpful to really understanding what’s going on. Thanks!

  7. Posted December 6, 2018 at 23:58 | Permalink

    My developer is trying to persuade me to move to .net from PHP. I have always disliked the idea because of the costs. But he’s tryiong none the less. I’ve been using Movable-type on numerous websites for about a year and am concerned about switching to another platform. I have heard good things about blogengine.net. Is there a way I can import all my wordpress posts into it? Any help would be really appreciated!

  8. Posted December 15, 2019 at 08:41 | Permalink

    The hydraulic ssystem is less realistic compared to water aand air movements however it is cheap in comparison with them.

    Here iis my web site … [link alternatif asikqq](http://tw.gs/Yzv8avi “link alternatif asikqq”)

  9. suba suba
    Posted December 23, 2019 at 12:19 | Permalink

    CfjC0U Precisely what I was searching for, thanks for posting. There are many victories worse than a defeat. by George Eliot.

  10. Posted January 26, 2020 at 20:40 | Permalink

    By performing simple calorie-burning actions, you may always keep within the healthy weight zone.

    Feell free to visit my web site: [agen capsa susun](https://qqcasa.com/ “agen capsa susun”)

  11. Posted January 27, 2020 at 12:25 | Permalink

    Some genuinely excellent info, Sword lily I discovered this.

  12. Posted January 27, 2020 at 12:27 | Permalink

    I gotta favorite this web site it seems very helpful handy.

  13. ciliegia
    Posted February 18, 2020 at 10:16 | Permalink

    Impressive how pleasurable it is to read this blog.

  14. for details
    Posted February 28, 2020 at 11:54 | Permalink

    Im thankful for the article.Really looking forward to read more. Much obliged.

  15. Posted March 7, 2020 at 03:44 | Permalink

    Depobola is one of Indonesia’s biggest online soccer gambling sites today. And exactly why must you choose Depobola since your partner?, because Depobola is one of the agents that has an official license and is also registered as a trusted organization at the center, Depobola is also here because an agency that can be believed to have a complete game.

    Depobola is usually also one of typically the Trusted Gambling Agents, in addition we here offer the best service through all experienced customer service professionals who will directly serve you, besides that here we always maintain the particular safety and comfort of members in playing on this website, Depobola’s website will be wrong one website that holds interesting events in addition to promotions that are extremely good for our loyal members, of course.

    To become Indonesia’s most significant soccer dealer, is a good online gambling site that already has licenses plus licenses as well as becoming an online gambling site that provides the best service. The Advantages of Depobola Along with other Sites The established online gambling game of which we provide here is Sportbook, Sportbook is one of the online gambling related to sports, and Depobola here provides ID with the best market, besides Sportbook here we also supply Livecasino, Slots, Joker123 Species of fish Shoot, S128 Cockfights, Togel Chicken Singapore, Hongkong Togel, Kuala Lumpur Togel, perform not hesitate to register plus get an account coming from Depobola, to Register Depobola is very simple the particular security of your info is maintained with a security system that is guaranteed.

    Here we provide items which have guaranteed safety, the items we provide are BOLA TANGKAS, MAXBET (IBCBET). Both associated with these products have already been tested from immunity against cyber crime, phishing, ripoffs, and fraud, besides that Depobola is also 1 of the biggest on-line gamblers in Asia.

    Separate from friendly professional client service, here you will additionally end up being given a very quick deposit and withdrawal procedure, not later than 5 minutes.

    Depobola also gives backup data where a person can login Depobola through the website that we offer, it avoids the improper use of data and maintains typically the security of information members.

    To conduct transactions, here Depobola provides complete banks such as BCA, BNI, Mandiri, BRI, CIMB, and then for dealings here Depobola serves day to day.

    And here Depobola furthermore gives access to members who join our site to see the assertions which may have been successfully wager, besides members who possess joined our website will certainly also get the possibility to take part in the sportbook tournament that we all have for free, typically the sportbook tournament is a single of the facilities that Depobola provides to Depobola loyal members.

    Depobola Alternative Links To make this easier for players to be able to play soccer gambling on the smartphone, the football wagering agent also provides a new link that can be accessed anytime and everywhere obviously with the availability of an alternative depobola link. Considering that right now there is of positive internet in Indonesia, the particular provider always provides updated alternative links in purchase to smooth the people to the original internet site. Besides the positive internet there are also several other sports agents sometimes ignorant about behalf of the depobola alternative site. So that makes us always recommend that loyal members need to really make certain that the site these people visit is depobola.

  16. Posted March 7, 2020 at 05:11 | Permalink

    Betberry is the official and trusted agent in Indonesia that gives soccer betting, casino, joker123 shooting, s1288 cock fighting and online slot machine games. Betberry is one regarding the sites that offers been established since 2014. With a total associated with hundreds of thousands more members throughout Indonesia, Betberry online betting is very popular ranging from teens to adults. Given the degree of enthusiasm of people, we all as a team created an official blog page of official2018 to provide more information and knowledge about online betting betting as well as how to win bets.

    BETBERRY SITE IS OFFICIALLY RELIABLE gambling agent considering that 2011 Like a trusted on the internet betting site with the most significant number of users in Asia, has a good official gambling site that has been established since 2011, namely Betberry. Betberry provides various games this kind of as:

    Maxbet Ball Wagering Games, Sbobet Soccer Betting Game, Online Casino Video games, Joker123 game shoot seafood, The S1288 cock combat game, And many more. To accessibility Betberry it is likewise very an easy task to access, all you have to perform is copy the betberry url address create the link in a new tab. Not simply convenience, various bonuses made available from Betberry are also really different and interesting. Properly, one of the best selling bonuses will be the welcome bonus of 20% for sportsbook games. Only by looking into making the particular first deposit, your IDENTIFICATION will immediately be added to 20% of the first deposit. How to state this bonus is very easy, you only must verify with CS, then the reward will be filled. With regard to withdrawals, it’s not as well difficult you only have to reach WIN 6x total (bonus + deposit), you can win WD with the 20% added bonus that we have provided. Very interesting promo right?

    MISSION ASSOCIATED WITH BETBERRY AGENTS FOR GENERATION OF Gamblers Betberry that has been going through the planet of online gambling with regard to 8 years. It usually wants to give the greatest for each and every member or prospective new members who would like to join. With Betberry’s trusted online betting site, consequently , a few of the leading sectors in accordance with Betberry. The primary headlines or focus in order to be improved this year are usually as follows:

    Interesting Promos: Betberry is confident of which attractive gambling promos will surely make players feel much more comfortable playing.

    Faster DP and WD services: Betberry constantly offers faster processing services and in less as compared to 3 minutes you are able to enjoy and withdraw their money.

    24/7 service: 24 hour without stopping service, always providing the very best service and assisted by professional staff.

    Transactions together with local banks: By supplying a nearby bank. Then that will be simpler for you when making a transaction. Newest security technology: Your protection and privacy are the golden gates needed to be able to maintain 100% as much as possible for your convenience. The noble quest of the Betberry real estate agent, of course, is centered on rules and rules. Which incidentally has been with us within the gambling world given that time immemorial. That is, producing the world of gambling one of the best solutions for the particular public in the world. To play finance with out breaking the rules. The best option would be to provide the best online wagering game facility. In this way all activities in addition to transactions must be completed online without breaking the particular rules.

  17. Posted March 18, 2020 at 14:00 | Permalink

    After looking over a number of the blog posts on your web page, I truly like your technique of writing a blog.

    I bookmarked it to my bookmark website list and will be checking back soon. Please visit my web site too and let me know how you feel.

Post a Comment

Your email is never published nor shared.

You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>