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.
Problem
Section titled “Problem”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.
Example
Section titled “Example”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:
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.
Solution
Section titled “Solution”// 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();// 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();// 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();// 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();Rendered, the text looks like this. The picture below is Graphity’s output, unchanged:
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.
Good to know
Section titled “Good to know”- Frames only where you want them. The partition passed to
clustersclustersclustersClustersdecides. Here, only the groups of the SCCs with a cycle have nodes. Every other rule is in no group (-1NONE-1-1), and a group without nodes gets no frame. - A class for the frames anyway. The writer names each frame
cand its group ID, such asc2. Define the class withclassDefclass_defclassDefClassDefand append a line such asclass c2,c4 groupto the text. Mermaid then draws those frames in that class. - Leaving nodes out.
MermaidWriter.nodeFilterMermaidWriter::node_filterMermaidWriter.nodeFilterMermaidWriter.NodeFilterleaves out nodes and their edges. See Highlight nodes, edges and groups in Graphviz for an example. - Undirected.
MermaidWriter.undirectedMermaidWriter::undirectedMermaidWriter.undirectedMermaidWriter.Undirecteddraws the edges without arrowheads, in the undirected reading: the edgesa -> bandb -> abecome one.
API used
Section titled “API used”MermaidWriter.classDef(String, String)MermaidWriter::class_defMermaidWriter.classDefMermaidWriter.ClassDefMermaidWriter.nodeClass(IntFunction)MermaidWriter::node_classMermaidWriter.nodeClassMermaidWriter.NodeClassMermaidWriter.edgeStyle(IntFunction)MermaidWriter::edge_styleMermaidWriter.edgeStyleMermaidWriter.EdgeStyleMermaidWriter.clusters(NodePartition, IntFunction)MermaidWriter::clustersMermaidWriter.clustersMermaidWriter.ClustersMermaidWriter.nodeFilter(IntPredicate)MermaidWriter::node_filterMermaidWriter.nodeFilterMermaidWriter.NodeFilterMermaidWriter.undirected()MermaidWriter::undirectedMermaidWriter.undirectedMermaidWriter.UndirectedMermaidWriter.of(Graph)MermaidWriter::ofMermaidWriter.ofMermaidWriter.OfMermaidWriter.nodeLabel(IntFunction)MermaidWriter::node_labelMermaidWriter.nodeLabelMermaidWriter.NodeLabelMermaidWriter.writeToString()MermaidWriter::write_to_stringMermaidWriter.writeToStringMermaidWriter.WriteToStringComponents.sccsOf(Graph)components::sccs_ofComponents.sccsOfComponents.SccsOfSccs.sccOf(int)Sccs::scc_ofSccs.sccOfSccs.SccOfSccs.containsCycle(int)Sccs::contains_cycleSccs.containsCycleSccs.ContainsCycleSccs.sccCount()Sccs::scc_countSccs.sccCountSccs.SccCountNodePartition.of(Graph, int[], int)NodePartition::ofNodePartition.ofNodePartition.OfGraph.edgeSource(int)Graph::edge_sourceGraph.edgeSourceGraph.EdgeSourceGraph.edgeTarget(int)Graph::edge_targetGraph.edgeTargetGraph.EdgeTarget