Skip to content

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.

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.

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.

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:

The graph with its node IDs and the group of the partition
Names, the group of the partition and the edges on a cycle
0123group 0
abcdgroup 0
Input
  • Group of the partition
Context
  • 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.