Skip to content

Commit

Permalink
fill nested subgraphs with distinct bg colors
Browse files Browse the repository at this point in the history
To distinguish inner subgraphs from outer subgraphs, this patch chooses a
background color based on the number of parent subgraphs.
  • Loading branch information
joostvanzwieten committed Mar 21, 2024
1 parent e5d7100 commit b07f60d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
8 changes: 4 additions & 4 deletions nutils/_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def generate_graphviz_source(self, *, fill_color: Optional[GraphvizColorCallback
subgraph_children = _collect_subgraphs(self)
id_gen = map(str, itertools.count())
self._collect_graphviz_nodes_edges({}, id_gen, nodes, edges, None, fill_color)
return ''.join(itertools.chain(['digraph {bgcolor="darkgray";'], _generate_graphviz_subgraphs(subgraph_children, nodes, None, id_gen), edges, ['}']))
return ''.join(itertools.chain(['digraph {bgcolor="darkgray";'], _generate_graphviz_subgraphs(subgraph_children, nodes, None, id_gen, 0), edges, ['}']))

def export_graphviz(self, *, fill_color: Optional[GraphvizColorCallback] = None, dot_path: str = 'dot', image_type: str = 'svg') -> None:
src = self.generate_graphviz_source(fill_color=fill_color)
Expand Down Expand Up @@ -250,9 +250,9 @@ def _generate_asciitree_subgraphs(children: Mapping[Optional[Subgraph], Sequence
yield from _generate_asciitree_subgraphs(children, id_gen_map, child, bridge+('├ ' if i else '└ '), bridge+('│ ' if i else ' '))


def _generate_graphviz_subgraphs(children: Mapping[Optional[Subgraph], Sequence[Subgraph]], nodes: Mapping[Optional[Subgraph], Sequence[str]], subgraph: Optional[Subgraph], id_gen: Iterator[str]) -> Iterator[str]:
def _generate_graphviz_subgraphs(children: Mapping[Optional[Subgraph], Sequence[Subgraph]], nodes: Mapping[Optional[Subgraph], Sequence[str]], subgraph: Optional[Subgraph], id_gen: Iterator[str], depth: int) -> Iterator[str]:
for child in children[subgraph]:
yield 'subgraph cluster{} {{bgcolor="lightgray";color="none";'.format(next(id_gen))
yield from _generate_graphviz_subgraphs(children, nodes, child, id_gen)
yield 'subgraph cluster{} {{bgcolor="{}";color="none";'.format(next(id_gen), 'darkgray' if depth % 2 else 'lightgray')
yield from _generate_graphviz_subgraphs(children, nodes, child, id_gen, depth + 1)
yield '}'
yield from nodes.get(subgraph, ())
4 changes: 2 additions & 2 deletions tests/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ def test_multiple_graphviz_source(self):
'bgcolor="lightgray";'
'color="none";'
'subgraph cluster8 {'
'bgcolor="lightgray";'
'bgcolor="darkgray";'
'color="none";'
'subgraph cluster9 {'
'bgcolor="lightgray";'
Expand All @@ -299,7 +299,7 @@ def test_multiple_graphviz_source(self):
'4 [shape=box,label="f"];'
'}'
'subgraph cluster10 {'
'bgcolor="lightgray";'
'bgcolor="darkgray";'
'color="none";'
'6 [shape=box,label="g"];'
'}'
Expand Down

0 comments on commit b07f60d

Please sign in to comment.