Skip to content

11.5 Highlight nodes, edges and groups in Mermaid

You set classes and styles from your data, draw groups of nodes as frames, leave nodes out, and show undirected graphs in a Mermaid flowchart.

You want a flowchart in Markdown that shows what an analysis found. A plain flowchart shows only the graph. You choose the style classes, styles and frames, and the nodes to leave out, based on your data and the results of your analyses. You need this for:

  • a pull request or a report in which the nodes an analysis found have a color of their own;
  • groups of nodes in frames, such as the strongly connected components or the classes of a package;
  • the edges that lie on a cycle, drawn in a color of their own.

The grammar of a small programming language has a rule for each construct of the language:

program = { statement } ;
statement = block | ifStatement | assignment ;
block = "{" { statement } "}" ;
ifStatement = "if" "(" expression ")" statement ;
assignment = name "=" expression ";" ;
expression = term { "+" term } ;
term = factor { "*" factor } ;
factor = number | name | "(" expression ")" ;

name and number are rules for single tokens. The example graph rules models the rules of this grammar. It has an edge from each rule to each rule it uses. The array names holds the names of the rules:

0: program1: statement2: block3: ifStatement4: assignment5: expression6: term7: factor8: name9: number

The rules that use each other, directly or through other rules, lie in a common SCC (strongly connected component). See Find the groups of nodes that reach each other. Every other rule is an SCC of its own, without a cycle. The drawing should frame the SCCs with a cycle and draw the edges that lie on a cycle in orange. It should also outline in purple the rule where a parser starts, which is program. These are the colors of the pictures of this guide.

// Frame the SCCs with a cycle: a partition in which every other rule is in no group
// (-1).
Sccs sccs = Components.sccsOf(rules);
int[] frameOf = new int[rules.nodeCount()];
for (int rule = 0; rule < frameOf.length; rule++) {
frameOf[rule] = sccs.containsCycle(sccs.sccOf(rule)) ? sccs.sccOf(rule) : -1;
}
NodePartition frames = NodePartition.of(rules, frameOf, sccs.sccCount());
String mermaid = MermaidWriter.of(rules)
.nodeLabel(rule -> rule + ": " + names[rule])
.clusters(frames, scc -> "SCC " + scc)
// Outline program, the rule where a parser starts.
.classDef("focus", "stroke:#8b5cf6,stroke-width:3px")
.nodeClass(rule -> names[rule].equals("program") ? "focus" : null)
// Draw the edges within an SCC, the edges that lie on a cycle, in orange.
.edgeStyle(edge ->
sccs.sccOf(rules.edgeSource(edge)) == sccs.sccOf(rules.edgeTarget(edge))
? "stroke:#f59e0b,stroke-width:3px" : null)
.writeToString();
Full example
// Frame the SCCs with a cycle: a partition in which every other rule is in no group
// (NONE).
let sccs = components::sccs_of(&rules);
let mut frame_of = vec![NONE; rules.node_count() as usize];
for rule in rules.nodes() {
if sccs.contains_cycle(sccs.scc_of(rule)) {
frame_of[rule as usize] = sccs.scc_of(rule);
}
}
let frames = NodePartition::of(&rules, &frame_of, sccs.scc_count())?;
let mermaid = MermaidWriter::of(&rules)
.node_label(|rule| Some(format!("{rule}: {}", names[rule as usize]).into()))
.clusters(&frames, |scc| Some(format!("SCC {scc}").into()))
// Outline program, the rule where a parser starts.
.class_def("focus", "stroke:#8b5cf6,stroke-width:3px")
.node_class(|rule| (names[rule as usize] == "program").then_some("focus".into()))
// Draw the edges within an SCC, the edges that lie on a cycle, in orange.
.edge_style(|edge| {
let within = sccs.scc_of(rules.edge_source(edge))
== sccs.scc_of(rules.edge_target(edge));
within.then_some("stroke:#f59e0b,stroke-width:3px".into())
})
.write_to_string();
Full example
// Frame the SCCs with a cycle: a partition in which every other rule is in no group
// (-1).
const sccs = Components.sccsOf(rules);
const frameOf = new Int32Array(rules.nodeCount);
for (let rule = 0; rule < frameOf.length; rule++) {
frameOf[rule] = sccs.containsCycle(sccs.sccOf(rule)) ? sccs.sccOf(rule) : -1;
}
const frames = NodePartition.of(rules, frameOf, sccs.sccCount);
const mermaid = MermaidWriter.of(rules)
.nodeLabel((rule) => `${rule}: ${names[rule]}`)
.clusters(frames, (scc) => `SCC ${scc}`)
// Outline program, the rule where a parser starts.
.classDef("focus", "stroke:#8b5cf6,stroke-width:3px")
.nodeClass((rule) => (names[rule] === "program" ? "focus" : null))
// Draw the edges within an SCC, the edges that lie on a cycle, in orange.
.edgeStyle((edge) =>
sccs.sccOf(rules.edgeSource(edge)) === sccs.sccOf(rules.edgeTarget(edge))
? "stroke:#f59e0b,stroke-width:3px" : null)
.writeToString();
Full example
// Frame the SCCs with a cycle: a partition in which every other rule is in no
// group (-1).
Sccs sccs = Components.SccsOf(rules);
int[] frameOf = new int[rules.NodeCount];
for (int rule = 0; rule < frameOf.Length; rule++)
{
frameOf[rule] = sccs.ContainsCycle(sccs.SccOf(rule)) ? sccs.SccOf(rule) : -1;
}
NodePartition frames = NodePartition.Of(rules, frameOf, sccs.SccCount);
string mermaid = MermaidWriter.Of(rules)
.NodeLabel(rule => $"{rule}: {names[rule]}")
.Clusters(frames, scc => $"SCC {scc}")
// Outline program, the rule where a parser starts.
.ClassDef("focus", "stroke:#8b5cf6,stroke-width:3px")
.NodeClass(rule => names[rule] == "program" ? "focus" : null)
// Draw the edges within an SCC, the edges that lie on a cycle, in orange.
.EdgeStyle(edge =>
sccs.SccOf(rules.EdgeSource(edge)) == sccs.SccOf(rules.EdgeTarget(edge))
? "stroke:#f59e0b,stroke-width:3px" : null)
.WriteToString();
Full example

Rendered, the text looks like this. The picture below is Graphity’s output, unchanged:

5: expression6: term7: factor1: statement2: block3: ifStatement0: program4: assignment8: name9: numberSCC 4SCC 2

The two SCCs with a cycle stand in their frames. Every edge between two rules of the same SCC is orange, since such an edge lies on a cycle. The frames have Mermaid’s default style, not the colors of the guide’s frames, as the writer cannot give a frame a class. Good to know shows how to add one.