Graphviz Examples

You may also like to read about Flowcharts in Graphviz.

Sketchviz uses Graphviz, which translates descriptions of graphs written in the DOT language into images. The official documentation is a great reference, but a poor tool for beginners. Instead, we've written this Graphviz tutorial that provides an introduction to its most useful features. You can click any image to launch an interactive editor of that graph.

The Basics

A graph is a collection of nodes and edges. You can create edges between nodes with the -- or -> operator. By default, a node's label is its name.

You can have a graph, which uses --:

An example of a regular, non-directed graph in Graphviz.
Hosted on Sketchviz

Or a digraph, which uses ->:

An example of a directed graph (or digraph) in Graphviz.
Hosted on Sketchviz

The presentation can be customized by specifying attributes. Attributes for the graph as a whole come at the top of the graph declaration, while attributes for nodes and edges come in square brackets immediately after they are declared.

A Graphviz example with customized attributes.
Hosted on Sketchviz

Graphviz itself supports any font, but for our hosted graphs to work, you'll need to stick to one of these three fonts:

For convenience, any text contained in asterixes (*like this*) will render in the Segdwick Ave font.

A graph with multiple font.
Hosted on Sketchviz

You can provide defaults for nodes and edges to avoid repeating yourself:

A graph showing how to set default values.
Hosted on Sketchviz

Advanced Graphs

Congratulations, you now know enough to make useful graphs! These next tricks can come in handy, but don't feel like you need to learn them immediately.

Records

Nodes with shape=record are treated specially. These are useful for displaying tables and object layouts. Note that you need to use ports, signified by <angle-brackets> to create edges to and from records.

An example of a Graphviz record.
Hosted on Sketchviz

Clusters (or Subgraphs)

You can group related nodes by putting them in a subgraph whose name begins with cluster_.

An example of a subgraph (or cluster) in a larger Graphviz graph.
Hosted on Sketchviz

Yuck, that didn't lay out very nicely! We'll add constraint=false to the edge between expressjs and aws-sdk to let Graphviz know that the edge doesn't imply that expressjs should be ranked higher in precedence:

An example of using constraint=false to control node position.
Hosted on Sketchviz

Clusters can also be nested:

An example of a nested subgraph in Graphviz.
Hosted on Sketchviz

If you'd like your arrow to start or stop at the cluster boundary, you have to set compound=true and use the lhead or ltail attributes on the edge:

A graph whose edges stop at the cluster boundary due to the lhead/ltail attribute.
Hosted on Sketchviz

Further Reading