| 1 | package Graph::Writer::GraphViz; | ||
| 2 | 3 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 3 | ▶ | use parent 'Graph::Writer'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | 3 | ▶ | use IO::All; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 0 | ▶ | use GraphViz; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 7 | use Graph::Writer; | ||
| 8 | |||
| 9 | our $VERSION = '0.12'; | ||
| 10 | |||
| 11 | # Global GraphViz Parameters | ||
| 12 | my %graph_param; | ||
| 13 | my %edge_param; | ||
| 14 | my %node_param; | ||
| 15 | |||
| 16 | my @param_keys = qw/format layout ranksep rankdir | ||
| 17 | no_overlap concentrate epsilon ratio | ||
| 18 | splines shape fontsize fontcolor overlap | ||
| 19 | directed | ||
| 20 | /; | ||
| 21 | |||
| 22 | my @edge_keys = qw/color arrowsize minlen weight fontsize fontname fontcolor | ||
| 23 | color style dir tailclip headclip arrowhead arrowtail | ||
| 24 | labeldistance port_label_distance decorateP samehead sametail | ||
| 25 | constraint /; | ||
| 26 | |||
| 27 | my @node_keys = qw/color height width shape fontsize fontname | ||
| 28 | fontcolor color fillcolor style rank/; | ||
| 29 | |||
| 30 | sub _init { | ||
| 31 | my ($self,%param) = @_; | ||
| 32 | $self->SUPER::_init(); | ||
| 33 | for(@param_keys) { | ||
| 34 | $graph_param{$_} = $param{"-$_"} | ||
| 35 | if(defined $param{"-$_"}); | ||
| 36 | } | ||
| 37 | |||
| 38 | for(@edge_keys) { | ||
| 39 | $edge_param{$_} = $param{"-edge_$_"} if defined $param{"-edge_$_"}; | ||
| 40 | } | ||
| 41 | |||
| 42 | for(@node_keys) { | ||
| 43 | $node_param{$_} = $param{"-node_$_"} if defined $param{"-node_$_"}; | ||
| 44 | } | ||
| 45 | |||
| 46 | $graph_param{format} ||= 'png'; | ||
| 47 | $edge_param{color} ||= 'black'; | ||
| 48 | $node_param{color} ||= 'black'; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub _write_graph { | ||
| 52 | my ($self, $graph, $FILE) = @_; | ||
| 53 | my $grvz = $self->graph2graphviz($graph); | ||
| 54 | my $as_fmt = 'as_' . $graph_param{format}; | ||
| 55 | if(ref($FILE) =~ '^IO::All') { | ||
| 56 | my $f = $grvz->$as_fmt; | ||
| 57 | $f > $FILE; | ||
| 58 | } else { | ||
| 59 | $grvz->$as_fmt($FILE); | ||
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | sub graph2graphviz { | ||
| 64 | my ($self,$g) = @_; | ||
| 65 | $graph_param{directed} = $g->directed; | ||
| 66 | my $r = GraphViz->new(%graph_param, | ||
| 67 | node => \%node_param, | ||
| 68 | edge => \%edge_param); | ||
| 69 | $self->add_nodes($r,$g); | ||
| 70 | $self->add_edges($r,$g); | ||
| 71 | return $r; | ||
| 72 | } | ||
| 73 | |||
| 74 | sub add_edges { | ||
| 75 | my ($self,$r,$g) = @_; | ||
| 76 | my @e = $g->edges; | ||
| 77 | for (@e) { | ||
| 78 | my ($a,$b) = @$_; | ||
| 79 | my %param; | ||
| 80 | if($g->has_edge_weight($a,$b)) { | ||
| 81 | my $w = $g->get_edge_weight($a,$b); | ||
| 82 | $param{weight} = $w; | ||
| 83 | } | ||
| 84 | $r->add_edge($a,$b,%param); | ||
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | sub add_nodes { | ||
| 89 | my ($self,$r,$g) = @_; | ||
| 90 | my @v = $g->vertices; | ||
| 91 | for (@v) { | ||
| 92 | my %param; | ||
| 93 | for my $attr (qw/style shape label color fillcolor rank cluster/) { | ||
| 94 | if($g->has_vertex_attribute($_,$attr)) { | ||
| 95 | my $w = $g->get_vertex_attribute($_,$attr); | ||
| 96 | $param{$attr} = $w; | ||
| 97 | } | ||
| 98 | } | ||
| 99 | $r->add_node($_,%param) ; | ||
| 100 | } | ||
| 101 | return $r; | ||
| 102 | } | ||
| 103 | |||
| 104 | |||
| 105 | 1; | ||
| 106 | __END__ | ||