- Package:
- libgraph-writer-graphviz-perl
- Source:
- libgraph-writer-graphviz-perl
- Submitter:
- Ian Jackson
- Date:
- 2014-10-22 15:12:05 UTC
- Severity:
- normal
IMO it should be possible to specify more edge attributes. For example, "label". The attached patch does this for the attributes "style", "label", "color" in roughly the same way as is done for node attributes. (To be honest, it would be better to have some general way to specify arbitrary attributes for the dot output, rather than insisting on mapping the dot attribute namespace directly but piecemeal into the Graph.pm. But I haven't implemented that here.) There is a possible compatibility implication for this patch: if someone sets these attributes on edges and uses Graph::Writer::GraphViz then there will be a chane in behaviour. (Perhaps a desired behaviour which didn't turn up but which now would be surprising.) But I think this risk is probably tolerable. Thanks, Ian.
Ian Jackson writes ("want to be able to specify more edge attributes"):
I have now done this. I'm going to send the patches (to upstream too)
as followups to this bug.
Thanks,
Ian.
I'm using Graph.pm and wanted to feed the results to dot. Graph::Writer::GraphViz seemed like the right thing. However, I found that I wanted to set some graphviz attributes support for which was lacking in ::Writer:GraphViz. Here is a series of three patches to improve this: 1/3 Honour edge attributes `style', `label', `color': pass 2/3 Document the edge and vertex attributes which are passed 3/3 Provide a "graphviz" attribute for setting other I'd appreciate it if you would let me know whether you approve of this approach. Best of all would be for you to include this in your next version. But I'd also like to know what you think of it so that the Debian maintainer can know whether it's a good idea to apply this patch ahead of it being in upstream. Thanks, Ian.
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
---
lib/Graph/Writer/GraphViz.pm | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/lib/Graph/Writer/GraphViz.pm b/lib/Graph/Writer/GraphViz.pm
index 8302354..bbdcb22 100644
--- a/lib/Graph/Writer/GraphViz.pm
+++ b/lib/Graph/Writer/GraphViz.pm
@@ -81,6 +81,12 @@ sub add_edges {
my $w = $g->get_edge_weight($a,$b);
$param{weight} = $w;
}
+ for my $attr (qw/style label color/) {
+ if($g->has_edge_attribute($a,$b,$attr)) {
+ my $w = $g->get_edge_attribute($a,$b,$attr);
+ $param{$attr} = $w;
+ }
+ }
$r->add_edge($a,$b,%param);
}
}
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk> --- lib/Graph/Writer/GraphViz.pm | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/Graph/Writer/GraphViz.pm b/lib/Graph/Writer/GraphViz.pm index bbdcb22..163f0c0 100644 --- a/lib/Graph/Writer/GraphViz.pm +++ b/lib/Graph/Writer/GraphViz.pm @@ -155,6 +155,12 @@ Valid format depends on those GraphViz B<as_fmt> methods on your system, like, 'gif' if you have 'as_gif', 'text' if you can do 'as_text'. +=head1 VERTEX AND EDGE ATTRIBUTES + +Certain Graph vertex and edge attributes are automatically transferred +to GraphViz. For vertices these are: style shape label color +fillcolor rank cluster; for edges these are: style label color. + =head1 SEE ALSO L<Graph>, L<Graph::Writer>, L<GraphViz>
lib/Graph/Writer/GraphViz.pm | 30 ++++++++++++++++++++++++++++--
1 file changed, 28 insertions(+), 2 deletions(-)
diff --git a/lib/Graph/Writer/GraphViz.pm b/lib/Graph/Writer/GraphViz.pm
index 163f0c0..2767c21 100644
--- a/lib/Graph/Writer/GraphViz.pm
+++ b/lib/Graph/Writer/GraphViz.pm
@@ -71,6 +71,14 @@ sub graph2graphviz {
return $r;
}
+sub _strip_undefs ($) {
+ my ($param) = @_;
+ foreach my $key (keys %$param) {
+ next if defined $param->{$key};
+ delete $param->{$key};
+ }
+}
+
sub add_edges {
my ($self,$r,$g) = @_;
my @e = $g->edges;
@@ -87,6 +95,10 @@ sub add_edges {
$param{$attr} = $w;
}
}
+ if($g->has_edge_attribute($a,$b,'graphviz')) {
+ %param = (%param, %{ $g->get_edge_attribute($a,$b,'graphviz') });
+ _strip_undefs(\%param);
+ }
$r->add_edge($a,$b,%param);
}
}
@@ -102,6 +114,10 @@ sub add_nodes {
$param{$attr} = $w;
}
}
+ if($g->has_vertex_attribute($_,'graphviz')) {
+ %param = (%param, %{ $g->get_vertex_attribute($_,'graphviz') });
+ _strip_undefs(\%param);
+ }
$r->add_node($_,%param) ;
}
return $r;
@@ -157,9 +173,19 @@ system, like, 'gif' if you have 'as_gif', 'text' if you can do
=head1 VERTEX AND EDGE ATTRIBUTES
+If a Graph edge or vertex has an attribute named C<graphviz>, it
+should be a hashref whose names are Graphviz attribute names, and
+whose values are the values to be used.
+
Certain Graph vertex and edge attributes are automatically transferred
-to GraphViz. For vertices these are: style shape label color
-fillcolor rank cluster; for edges these are: style label color.
+to GraphViz, without them being included in the C<graphviz> attribute.
+For vertices these are: style shape label color fillcolor rank
+cluster; for edges these are: style label color. Values in the
+C<graphviz> attribute override ones straight out of the vertex or edge
+attribute namespace.
+
+Entries in the C<graphviz> vertex attribute with the value undef
+suppress those graphiviz attributes.
=head1 SEE ALSO