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.

20 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. safety razors for me
    Posted September 21, 2020 at 19:41 | Permalink

    Thanks so much for the blog article.Thanks Again. Keep writing.

  9. Recharge gotv subscr
    Posted October 28, 2020 at 18:38 | Permalink

    visit the website What is a good free blogging website that I can respond to blogs and others will respond to me?

  10. sage bundle kit
    Posted November 2, 2020 at 16:15 | Permalink

    Wow, that as what I was seeking for, what a stuff! present here at this blog, thanks admin of this site.

  11. IELTS Preparation in
    Posted November 9, 2020 at 15:56 | Permalink

    in life. I ?ant to encourage you to continue your great

  12. Question Paper 2021
    Posted November 16, 2020 at 17:16 | Permalink

    Thanks for sharing, this is a fantastic blog article.Really thank you! Awesome.

  13. Digital rådgivn
    Posted December 7, 2020 at 18:43 | Permalink

    I truly appreciate this article.Thanks Again. Cool.

  14. Posted May 13, 2021 at 04:03 | Permalink

    Are you a lover of online slot gambling games? This game has existed for hundreds of years but continues to be one of ended up being games in Usernesia even in luxury gambling places like Las Vegas or Macau, slots are always neatly arranged waiting for enthusiasts to be able to levers and win jackpots that reach billions of rupiah..

    However performed you know if this game was at first considered a flop game? Sure, at the starting of this game created by the creator named Charles August Fey in 1894 in the United States, this model was intended being an programmed poker game machine that could be moved using coins.

    After seeking to build his idea into actuality, Fey finally quit and considered his discovery a disappointment.

    But who would have thought that this machine turned out there to be one of the most widely used online games in the world. Not only in slot gambling video games provided by betting houses but also the concept utilized by various games as a “bonus game”.

    This is where the game relating to the RNG or Arbitrary Number Generator was initiated, where the machine will designate random values ??that will determine what you will get at each round of the game. Not only slots but additionally video games like virtual sports activities or bonus online games like gacha also use this kind of concept.

  15. Posted May 19, 2021 at 08:32 | Permalink

    Listing of Indonesia’s Latest Online Slots Gambling SiteUsernesia is a location to list Indonesian online slot gambling company accounts using the latest online games. The Usernesia internet site being an Indonesian slot machine gambling site will give you the most effective experience in actively playing online slots. Various advantages and facilities have become the particular icon of theUsernesia Site. All the slot gambling display features that we offer are the many sophisticated.

    With simply once registering because a member, an individual can enjoy almost all types of slot machine games provided by simply this site. Every single game is risk-free as it has acquired a special permit from the regional gambling authority in addition to has met international standards, pre-loaded with typically the latest technology that will spoil you together with outstanding graphic images and blaring noise.

    How to register for online slot machine IDs on the Usernesia website For anybody who else want to right away join the finest online slot betting gAfter that you will certainly be immediately taken to the registration webpage where you will certainly locate a registration contact form which you must fill in with your personal information completely and appropriately.ame on the webpage ofUsernesia, the method is very easy. You just must visit the site at www. SiteUsernesia. com in addition to enter the registration page by pressing the button situated at the leading right of the main page.

  16. Posted June 3, 2021 at 13:43 | Permalink

    Ubobet is an internationally trusted gambling dealer back 2001 until now. By holding approved entrance for online gambling sites that have the funds for various types of betting games. Games welcoming complement Sportsbook, living Casino and Cockfight. Ubobet has reached the entire world gambling push including the Indonesian market. At present, every hours of daylight there are more than 100 thousand players who entrust their bets in the city of Ubobet.

    Ubobet List Site

    To register an account, players cannot accomplish it directly on the Ubobet site. In other words, prospective players must register through the credited agent site for Ubobet listings such as Pandora188. By using Pandora188 ID the performer can bet on this city.

    How to register is quite easy, just by filling out the registration form correctly. After that create a enlargement of at least 25 thousand, later the artiste can bet on Ubobet. every performer who joins as a fanatic at Pandora188 is entitled to a freebet supplementary and along with a daily buildup other without exception.

    Ubobet Login

    After registering, players can log in on Ubobet in two ways. The first is through qualified buddies who have collaborations such as Pandora188.

    Because the two sites have already formed a partnership, which means that betting on the Pandora188 site is the thesame as betting upon Ubobet directly. Or by the second artifice you can directly log in to the Ubobet site if you already have an account. How to log in Ubobet is quite easy, just enter your ID & password correctly.

  17. Posted June 4, 2021 at 05:33 | Permalink

    satisfactory and thank you for visiting online casino agents for Indonesia. Here we are here to present permission for fans of sbobet casino who desire to member the international agent site Promosbobet.

    The products offered are entirely diverse and are unquestionably the best choice. Derived from providers that have credibility and have been proven to have the best predicate in serving their customers. Providers past Sbobet, Maxbet and many more.

    As the best online casino and soccer gambling site in Indonesia. We give maximum bolster like customer preserve that works online 24 full hours all day. correspondingly members can accomplishment whenever and wherever without fearing to point the possibility of difficulties or confusion.

  18. Posted June 27, 2021 at 08:31 | Permalink

    The Most Trusted Soccer Gambling Real estate agent in Indonesia Serving Listings of Gambling and Online casino Gambling Userbola is typically the Most Trusted and Official SBOBET Agent in Dalam negri. We serve bets of which provide soccer gambling games, online gambling games, on line casino gambling agents, poker and online lottery.

    We will wholeheartedly always be prepared to help whatever you require and help you when there are difficulties in making SBOBET Online Betting Company accounts, MAXBET Agents or earlier known as IBCBET, Serba4d or even KLIK4D lottery.

    Brand new 20% Member Bonus with typically the Best and Most Dependable Soccer Gambling Agent Userbola will be the Best SGD777 Bandar as well as the Biggest Football Bandar in Philippines, always offering 24-hour online service with the need to stop at the same time of registering, adding, withdrawing and responding to be able to your every complaint.

    Regarding more information, please go directly to livechat plus chat directly with our friendly CS

    Register with regard to Sbobet right now!

    Total Choice for Trustworthy Soccer Betting and Online casino Providers Regarding you fans of betting soccer matches both international and domestic, reliable soccer gambling provides various matches from all over the world. Not necessarily only that, basketball, rugby in addition to eSports tournaments will also be here.

    Especially for connoisseurs regarding Casino Providers, we possess probably the most complete range regarding video games ranging from Baccarat, Poker, Keno, Slot machine game and numerous more.

    Userbola is a single of the trusted football wagering sites that works as an agent of the particular world’s major soccer online bdtting shops for example SBOBET, Ibcbet in addition to Mahabet Sport. You may definitely get a pleasant experience while betting because of the interesting features made available from this site. Userbola has a professional support when you make a deposit transaction until pulling out funds. Before you can enter the football gambling game on Userbola you must register very first and become a customer. The technique is very easy, all you have to be able to do is click upon the list and enter in the registration form web page and you will proceed directly to our primary website.

  19. Italy Rome
    Posted June 28, 2021 at 10:24 | Permalink

    There is certainly a lot to know about this topic. I love all the points you made.

  20. Posted July 7, 2021 at 08:45 | Permalink

    On-line Slots Demo Web site No. 1 Indonesia – Demopedia

    Demopedia is the established provider of On the internet Demo Slot games that was established inside Indonesia. Currently Demopedia has presented a great online demo sport, which means a person can all enjoy demo games together with us. The Demopedia party presents a lot more than tens or also numerous types associated with demo slot games whose main goal is usually to provide pleasure to online slot machine game site lovers. You will always make improvements and continue to contribute to the launch regarding the latest online games on the Demopedia site. The thing to keep in mind is that to play on this web site you do not need to sign up and will not become charged a cent. Almost all games on Demopedia can be performed for free or even at no cost, and could be played on all types associated with smartphones, tablets, computer systems and laptops.

    The demo game may of course become played anywhere and there is no time limit to learn. All the facilities provided by Demopedia and also the particular convenience you obtain when you are all playing, this site has attained the nickname as the No. 1 Slot machine Demo Site in Indonesia.

Post a Reply to Marlin Forbes Cancel reply

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>