Browse Source

graph: use name as label

Fabian Peter Hammerle 4 years ago
parent
commit
90d4beaceb
2 changed files with 20 additions and 15 deletions
  1. 7 7
      tests/persons/digraph.gz
  2. 13 8
      yamily/_graphviz.py

+ 7 - 7
tests/persons/digraph.gz

@@ -1,31 +1,31 @@
 digraph yamily {
 	{
 		rank=same
-		"erika-mustermann" [shape=box]
-		"thomas-mustermann" [shape=box]
+		"erika-mustermann" [label="Erika Mustermann" shape=box]
+		"thomas-mustermann" [label="thomas-mustermann" shape=box]
 		"relation-erika-mustermann-thomas-mustermann" [shape=point width=0]
 		"erika-mustermann" -> "relation-erika-mustermann-thomas-mustermann" [arrowhead=none constraint=False]
 		"thomas-mustermann" -> "relation-erika-mustermann-thomas-mustermann" [arrowhead=none constraint=False]
 	}
 	{
 		rank=same
-		"alice-mother" [shape=box]
-		"alice-father" [shape=box]
+		"alice-mother" [label="Mum Test" shape=box]
+		"alice-father" [label="alice-father" shape=box]
 		"relation-alice-father-alice-mother" [shape=point width=0]
 		"alice-mother" -> "relation-alice-father-alice-mother" [arrowhead=none constraint=False]
 		"alice-father" -> "relation-alice-father-alice-mother" [arrowhead=none constraint=False]
 	}
 	{
 		rank=same
-		"alice-grandmother" [shape=box]
+		"alice-grandmother" [label="Grandma Test" shape=box]
 	}
 	{
 		rank=same
-		alice [shape=box]
+		alice [label="Alice Test" shape=box]
 	}
 	{
 		rank=same
-		"max-mustermann" [shape=box]
+		"max-mustermann" [label="Max Mustermann" shape=box]
 	}
 	"alice-grandmother" -> "alice-father"
 	"relation-alice-father-alice-mother" -> alice

+ 13 - 8
yamily/_graphviz.py

@@ -5,6 +5,11 @@ import graphviz
 from yamily import Person, PersonCollection
 
 
+def _add_person_node(graph: graphviz.dot.Digraph, person: Person) -> None:
+    label = person.name or person.identifier
+    graph.node(person.identifier, label=label, shape="box")
+
+
 def digraph(collection: PersonCollection) -> graphviz.dot.Digraph:
     """
     >>> bob = Person('bob')
@@ -31,27 +36,27 @@ def digraph(collection: PersonCollection) -> graphviz.dot.Digraph:
     digraph yamily {
     	{
     		rank=same
-    		grace [shape=box]
+    		grace [label=grace shape=box]
     	}
     	{
     		rank=same
-    		carol [shape=box]
-    		bob [shape=box]
+    		carol [label=carol shape=box]
+    		bob [label=bob shape=box]
     		"relation-bob-carol" [shape=point width=0]
     		carol -> "relation-bob-carol" [arrowhead=none constraint=False]
     		bob -> "relation-bob-carol" [arrowhead=none constraint=False]
     	}
     	{
     		rank=same
-    		frank [shape=box]
+    		frank [label=frank shape=box]
     	}
     	{
     		rank=same
-    		alice [shape=box]
+    		alice [label=alice shape=box]
     	}
     	{
     		rank=same
-    		david [shape=box]
+    		david [label=david shape=box]
     	}
     	grace -> carol
     	frank -> bob
@@ -69,10 +74,10 @@ def digraph(collection: PersonCollection) -> graphviz.dot.Digraph:
             continue
         with graph.subgraph() as subgraph:
             subgraph.attr(rank="same")
-            subgraph.node(person.identifier, shape="box")  # , str(person))
+            _add_person_node(subgraph, person)
             nodes.add(person)
             for coparent in collection.get_coparents(person):
-                subgraph.node(coparent.identifier, shape="box")
+                _add_person_node(subgraph, coparent)
                 nodes.add(coparent)
                 parents = tuple(sorted((person, coparent), key=lambda p: p.identifier))
                 parent_node_name = "relation-{}-{}".format(