Subject: | Quoted Strings as node-names and label for the Graph itself |
I encountered the following problem :
When a node has a name with a Slash "/" (or other "weird characters") it
gets a generated name ("node1" ... ).
But unfortunately the behaviour is not complete consistent, because the
returned value on $g->add_node() is not the generated name, but it is
the original given name.
When later on trying to identify the node, this turns out to be
difficult, because I have no "mapping" of internal generated name to my
original name.
See this piece of code:
---snip---
#!/usr/bin/perl
use strict;
use warnings;
use GraphViz;
my $a = "a/z";
my $a_label = "Label for a/z";
my $b = "\"b/y\"";
my $b_label = "Label for b/y";
my $c = "c";
my $g = GraphViz->new();
my $newname = $g->add_node($a, label => $a_label);
print "New node-name $newname for $a generated\n";
$g->add_edge($a => $c);
$newname = $g->add_node( $b, label => $b_label );
print "New node-name $newname for $b generated\n";
$g->add_edge($b => $c);
$g->as_dot("test.dot");
---snip---
and then the generated dot-File.
My original node $a with the name "a/z" get's a new name node1, but
there is later "no" way to to find out the original name "a/z".
Dot itself allows also strings quoted with "" as node-names, but this
does not work with the current version 2.02 of GraphViz.pm.
Here comes my patch, which keeps node-names (even with weird characters)
when they are quoted.
I also realized, that within GraphViz.pm a label is allowed for edges
and nodes, but not for the graph itself. When - in dot - giving a graph
a label, you get a nice legend for the graph at the bottom of the image.
So I also changed this.
I attach the unified diff in the hope that you find it usefull and are
willing to use it for the next update of GraphViz.pm.
Subject: | GraphViz_V202_to_V204.diff |
--- ../../GraphViz-2.02/lib/./GraphViz.pm 2005-01-07 19:24:54.000000000 +0100
+++ GraphViz.pm 2006-06-17 12:13:18.000000000 +0200
@@ -8,7 +8,7 @@
use IPC::Run qw(run binary);
# This is incremented every time there is a change to the API
-$VERSION = '2.02';
+$VERSION = '2.04';
=head1 NAME
@@ -323,6 +323,11 @@
my $g = GraphViz->new(node => {shape => 'box'});
+=item label
+
+The 'label' of a complete graph is the legend for the image and will be put
+at the bottom of the image.
+
=back
=cut
@@ -382,6 +387,9 @@
$self->{OVERLAP} = 'false' if (exists $config->{no_overlap});
$self->{RATIO} = $config->{ratio} || 'fill';
+ #label of the whole graph
+ $self->{LABEL} = $config->{label} if (exists $config->{label});
+
# Global node, edge and graph attributes
$self->{NODE_ATTRS} = $config->{node} if (exists $config->{node});
@@ -1014,6 +1022,9 @@
# color, bgcolor
$dot .= "\tbgcolor=\"" . $self->{BGCOLOR} . "\";\n" if $self->{BGCOLOR};
+ # label of the graph
+ $dot .= "\tlabel=\"" . $self->{LABEL} . "\";\n" if $self->{LABEL};
+
# Global node, edge and graph attributes
$dot .= "\tnode" . _attributes($self->{NODE_ATTRS}) . ";\n"
if exists($self->{NODE_ATTRS});
@@ -1164,6 +1175,8 @@
} elsif (defined $name && $name =~ /^[a-zA-Z](\w| )*$/) {
# name contains spaces, so quote it
$name = '"' . $name . '"';
+ } elsif (defined $name && $name =~ /^\"[^"]*\"$/) {
+ #name is fine because already quoted
} else {
# name contains weird characters - let's make up a name for it
$name = 'node' . ++$self->{_NAME_COUNTER};
@@ -1223,7 +1236,7 @@
=head1 COPYRIGHT
-Copyright (C) 2000-4, Leon Brocard
+Copyright (C) 2000-6, Leon Brocard
This module is free software; you can redistribute it or modify it
under the same terms as Perl itself.