11.3 Highlight nodes, edges and groups in Graphviz
You set colors, shapes and line styles from your data, draw groups of nodes as frames, leave nodes out, and show undirected graphs.
Problem
Section titled “Problem”You want a picture of a graph that shows what an analysis found. A plain picture shows only the graph. You choose the colors, line styles and frames, and the nodes to leave out, based on your data and the results of your analyses. You need this for:
- a cycle, or the edges that close one, drawn in a color of their own;
- groups of nodes in frames, such as the strongly connected components or the classes of a package;
- nodes that only crowd the picture and are left out, such as a package of helpers that many packages use.
Example
Section titled “Example”The example graph dependencies models the dependencies between the eight packages of a small
program. It has an edge from each package to each package it uses. The array names holds the names
of the packages:
model and storage use each other, so they lie on a cycle. They form an SCC (strongly connected
component) of their own: a group of nodes that reach each other, and that no further node can join.
See Find the groups of nodes that reach each other. The
drawing should frame that SCC and draw the edge that closes the cycle dashed and red. It should also
leave out the package of helpers util, which four other packages use.
Solution
Section titled “Solution”// Frame the SCCs with a cycle: a partition in which every other package is in no// group (-1).Sccs sccs = Components.sccsOf(dependencies);int[] frameOf = new int[dependencies.nodeCount()];for (int node = 0; node < frameOf.length; node++) { frameOf[node] = sccs.containsCycle(sccs.sccOf(node)) ? sccs.sccOf(node) : -1;}NodePartition frames = NodePartition.of(dependencies, frameOf, sccs.sccCount());EdgeTypes edgeTypes = EdgeTypes.of(dependencies);String dot = DotWriter.of(dependencies) .nodeLabel(node -> names[node]) .clusters(frames, scc -> "SCC " + scc) // Draw the edges that close a cycle dashed and red. .edgeAttributes((edge, attributes) -> { if (edgeTypes.typeOf(edge) == EdgeType.BACK) { attributes.put("style", "dashed"); attributes.put("color", "red"); } }) // Leave out util, the package of helpers: four packages use it, and its edges // only crowd the picture. .nodeFilter(node -> !names[node].equals("util")) .writeToString();// Frame the SCCs with a cycle: a partition in which every other package is in no// group (NONE).let sccs = components::sccs_of(&dependencies);let mut frame_of = vec![NONE; dependencies.node_count() as usize];for node in dependencies.nodes() { if sccs.contains_cycle(sccs.scc_of(node)) { frame_of[node as usize] = sccs.scc_of(node); }}let frames = NodePartition::of(&dependencies, &frame_of, sccs.scc_count())?;let edge_types = EdgeTypes::of(&dependencies);let dot = DotWriter::of(&dependencies) .node_label(|node| Some(names[node as usize].into())) .clusters(&frames, |scc| Some(format!("SCC {scc}").into())) // Draw the edges that close a cycle dashed and red. .edge_attributes(|edge, attributes| { if edge_types.type_of(edge) == EdgeType::Back { attributes.insert("style", "dashed"); attributes.insert("color", "red"); } }) // Leave out util, the package of helpers: four packages use it, and its edges // only crowd the picture. .node_filter(|node| names[node as usize] != "util") .write_to_string();// Frame the SCCs with a cycle: a partition in which every other package is in no// group (-1).const sccs = Components.sccsOf(dependencies);const frameOf = new Int32Array(dependencies.nodeCount);for (let node = 0; node < frameOf.length; node++) { frameOf[node] = sccs.containsCycle(sccs.sccOf(node)) ? sccs.sccOf(node) : -1;}const frames = NodePartition.of(dependencies, frameOf, sccs.sccCount);const edgeTypes = EdgeTypes.of(dependencies);const dot = DotWriter.of(dependencies) .nodeLabel((node) => names[node]) .clusters(frames, (scc) => `SCC ${scc}`) // Draw the edges that close a cycle dashed and red. .edgeAttributes((edge, attributes) => { if (edgeTypes.typeOf(edge) === EdgeType.BACK) { attributes.set("style", "dashed"); attributes.set("color", "red"); } }) // Leave out util, the package of helpers: four packages use it, and its edges only // crowd the picture. .nodeFilter((node) => names[node] !== "util") .writeToString();// Frame the SCCs with a cycle: a partition in which every other package is in no// group (-1).Sccs sccs = Components.SccsOf(dependencies);int[] frameOf = new int[dependencies.NodeCount];for (int node = 0; node < frameOf.Length; node++){ frameOf[node] = sccs.ContainsCycle(sccs.SccOf(node)) ? sccs.SccOf(node) : -1;}NodePartition frames = NodePartition.Of(dependencies, frameOf, sccs.SccCount);EdgeTypes edgeTypes = EdgeTypes.Of(dependencies);string dot = DotWriter.Of(dependencies) .NodeLabel(node => names[node]) .Clusters(frames, scc => $"SCC {scc}") // Draw the edges that close a cycle dashed and red. .EdgeAttributes((edge, attributes) => { if (edgeTypes.TypeOf(edge) == EdgeType.Back) { attributes["style"] = "dashed"; attributes["color"] = "red"; } }) // Leave out util, the package of helpers: four packages use it, and its edges // only crowd the picture. .NodeFilter(node => names[node] != "util") .WriteToString();The partition passed to
clustersclustersclustersClusters has a group for every
SCC, but only the group of the SCC with a cycle has nodes. Every other package is in no group and
stands outside any frame, and a group without nodes gets no frame. EdgeTypes.ofEdgeTypes::ofEdgeTypes.ofEdgeTypes.Of
classifies the edges in a depth-first search. Its one back
edge is storage -> model, which closes the
cycle. The writer returns this text:
digraph { subgraph cluster_3 { label="SCC 3"; 4 [label="model"]; 5 [label="storage"]; } 0 [label="app"]; 1 [label="cli"]; 2 [label="ui"]; 3 [label="service"]; 6 [label="json"]; 0 -> 1; 0 -> 2; 0 -> 3; 2 -> 3; 2 -> 4; 3 -> 4; 3 -> 5; 4 -> 5; 5 -> 4 [style="dashed", color="red"]; 5 -> 6;}Graphviz draws it like this, with the SCC in its frame and without util and its edges:
Good to know
Section titled “Good to know”- The hook gets a map with the label already in it, and adds or replaces attributes. The writer escapes the values and keeps their order.
- Undirected graphs.
DotWriter.undirectedDotWriter::undirectedDotWriter.undirectedDotWriter.Undirectedwrites agraphwith--edges instead of adigraphwith->, for graphs that stand for undirected ones. The edgesa -> bandb -> athen become one edge. - Frames in Graphviz are subgraphs whose name starts with
cluster. The writer names them by group ID. - All frames alike. The writer writes the attributes of the graph before the frames, and
Graphviz passes them on to every frame:
graphAttributegraph_attributegraphAttributeGraphAttributewithcolorandbluedraws every frame blue. - In Mermaid, the same works. See Highlight nodes, edges and groups in Mermaid.
API used
Section titled “API used”DotWriter.nodeAttributes(AttributeHook)DotWriter::node_attributesDotWriter.nodeAttributesDotWriter.NodeAttributesDotWriter.edgeAttributes(AttributeHook)DotWriter::edge_attributesDotWriter.edgeAttributesDotWriter.EdgeAttributesAttributeHook.accept(int, Map)AttributeHook.accept(int, Map)AttributeHookAttributeHookDotWriter.clusters(NodePartition, IntFunction)DotWriter::clustersDotWriter.clustersDotWriter.ClustersDotWriter.nodeFilter(IntPredicate)DotWriter::node_filterDotWriter.nodeFilterDotWriter.NodeFilterDotWriter.undirected()DotWriter::undirectedDotWriter.undirectedDotWriter.UndirectedDotWriter.of(Graph)DotWriter::ofDotWriter.ofDotWriter.OfDotWriter.nodeLabel(IntFunction)DotWriter::node_labelDotWriter.nodeLabelDotWriter.NodeLabelDotWriter.graphAttribute(String, String)DotWriter::graph_attributeDotWriter.graphAttributeDotWriter.GraphAttributeDotWriter.writeToString()DotWriter::write_to_stringDotWriter.writeToStringDotWriter.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.OfEdgeTypes.of(Graph)EdgeTypes::ofEdgeTypes.ofEdgeTypes.OfEdgeTypes.typeOf(int)EdgeTypes::type_ofEdgeTypes.typeOfEdgeTypes.TypeOf