11 Showing graphs
This chapter turns graphs into text for logs and tests, and into pictures drawn by Graphviz and by Mermaid, with labels, styles and frames from your own data.
A picture of a graph shows at a glance what a list of edges hides, for example where the cycles are, which nodes many edges lead to, or what an analysis found. Graphviz and Mermaid are two widespread tools for such pictures.
Problems in this chapter
Section titled “Problems in this chapter”- 11.1 Print a graph as textYou print the whole graph as an adjacency list, or a short summary for logs.
- 11.2 Draw a graph with GraphvizYou write a graph in the DOT language of Graphviz, with labels from your own data.
- 11.3 Highlight nodes, edges and groups in GraphvizYou set colors, shapes and line styles from your data, draw groups of nodes as frames, leave nodes out, and show undirected graphs.
- 11.4 Draw a graph as a Mermaid flowchartYou write a graph as a Mermaid flowchart, with labels, a direction and node shapes.
- 11.5 Highlight nodes, edges and groups in MermaidYou set classes and styles from your data, draw groups of nodes as frames, leave nodes out, and show undirected graphs in a Mermaid flowchart.
The examples of this chapter use the steps that build a small program, the dependencies between its packages, and the rules of a small grammar.
Text and pictures
Section titled “Text and pictures”For logs and tests, text is enough. An adjacency list shows a graph in one line per node with its out-nodes. For a picture, Graphity writes the text that one of two tools draws:
- Graphviz is a program that lays out a graph and draws it as SVG, PNG or PDF. It reads a small text language called DOT and handles large graphs.
- Mermaid draws flowcharts from a short text inside Markdown, and many viewers render it without any extra tool, for example GitHub, many wikis and this guide. It draws graphs of up to a few hundred nodes well; for larger ones, use Graphviz.
Writers
Section titled “Writers”DotWriterDotWriterDotWriterDotWriter writes DOT, and MermaidWriterMermaidWriterMermaidWriterMermaidWriter writes Mermaid. A writer is
configured once and can write any number of times. The graph holds no data, so every label and style
comes from a function that receives a node ID or an edge
index and reads your arrays, such as names or the results of an analysis.
The frames come from a partition of the nodes into groups, with
a label for each group ID. The writers call a frame a cluster. Your data decides what the picture
shows. Here, the labels come from an array of names, and the frame comes from the partition in the
table below. The color of the edges comes from the result of an analysis that found the edges on a
cycle:
- Group of the partition
- Edge on a cycle
| node ID | 0 | 1 | 2 | 3 |
|---|---|---|---|---|
| name (the label) | a | b | c | d |
| group of the partition (the frame) | 0 | 0 | 0 | none |
The writers are in libraries of their own:
graphity-dotgraphity-dot@kobayami/graphity-dotGraphity.Dot and
graphity-mermaidgraphity-mermaid@kobayami/graphity-mermaidGraphity.Mermaid.
graphitygraphity-all@kobayami/graphityGraphity.All
includes both.