File Coverage

blib/lib/HTML/InfoVis/Graph.pm
Criterion Covered Total %
statement 25 27 92.5
branch 1 2 50.0
condition n/a
subroutine 7 8 87.5
pod 4 4 100.0
total 37 41 90.2


line stmt bran cond sub pod time code
1             package HTML::InfoVis::Graph;
2              
3             =pod
4              
5             =head1 NAME
6              
7             HTML::InfoVis::Graph - Generate a JSON structure suitable for loadJSON classes
8              
9             =head1 SYNOPSIS
10              
11             use HTML::InfoVis;
12            
13             my $graph = HTML::InfoVis::Graph->new;
14             $graph->add_edge( 'foo' => 'bar' );
15            
16             print "var json = " . $graph->as_json . "\n";
17              
18             =head1 DESCRIPTION
19              
20             HTML::InfoVis::Graph is used to generate a JSON structure suitable for loading
21             by any InfoVis Javascript object that has the C method.
22              
23             This is a basic first implementation, designed to prove the concept of converting
24             Perl graphs into InfoVis graphs.
25              
26             It provides a few L-like methods to populate the graph, and a single method
27             for generating an anonymous JSON structure representing the graph.
28              
29             =head1 METHODS
30              
31             =cut
32              
33 2     2   60 use 5.006;
  2         7  
  2         91  
34 2     2   10 use strict;
  2         4  
  2         70  
35 2     2   1301 use JSON 2.16 ();
  2         17502  
  2         51  
36 2     2   29624 use Graph 0.85 ();
  2         447670  
  2         591  
37              
38             our $VERSION = '0.03';
39              
40             =pod
41              
42             =head2 new
43              
44             my $graph = HTML::InfoVis::Graph->new(
45             Graph->new,
46             );
47              
48             The default constructor takes a single optional param of a L object
49             that represents the graph structure.
50              
51             Returns a new B object.
52              
53             =cut
54              
55             sub new {
56 1     1 1 11 my $class = shift;
57 1         4 my $self = bless { @_ }, $class;
58 1 50       9 unless ( defined $self->{graph} ) {
59 1         8 $self->{graph} = Graph->new;
60             }
61 1         237 return $self;
62             }
63              
64             =pod
65              
66             =head2 add_node
67              
68             $graph->add_node( 'foo' );
69              
70             The C method is a pass-through method to the underlying
71             L C method.
72              
73             =cut
74              
75             sub add_node {
76 0     0 1 0 my $self = shift;
77 0         0 $self->{graph}->add_vertex(@_);
78             }
79              
80             =pod
81              
82             =head2 add_edge
83              
84             $graph->add_edge( 'foo' => 'bar' );
85              
86             The C method is a pass-through method to the underlying
87             L C method.
88              
89             =cut
90              
91             sub add_edge {
92 1     1 1 421 my $self = shift;
93 1         7 $self->{graph}->add_edge(@_);
94             }
95              
96             =pod
97              
98             =head2 as_json
99              
100             my $json = $graph->as_json;
101              
102             The C method generates a serialized anonymous JSON structure that
103             represents the graph in a form suitable for loading by any InfoVis C
104             method.
105              
106             Because it is generated anonymously, if you wish to assign it to a variable
107             you will need to do that yourself in your JavaScript template.
108              
109             =cut
110              
111             sub as_json {
112 1     1 1 138 my $self = shift;
113 1         3 my $graph = $self->{graph};
114 1         2 my @nodes = ();
115 1         5 foreach my $name ( sort $graph->vertices ) {
116 2         104 push @nodes, {
117             id => $name,
118             name => $name,
119             adjacencies => [ $graph->successors($name) ],
120             };
121             }
122 1         85 JSON->new->canonical(1)->pretty(1)->encode(\@nodes);
123             }
124              
125             1;
126              
127             =pod
128              
129             =head1 SUPPORT
130              
131             Bugs should be reported via the CPAN bug tracker at
132              
133             L
134              
135             For other issues, or commercial enhancement or support, contact the author.
136              
137             =head1 AUTHORS
138              
139             Adam Kennedy Eadamk@cpan.orgE
140              
141             =head1 COPYRIGHT
142              
143             Copyright 2009 - 2012 Adam Kennedy.
144              
145             This program is free software; you can redistribute
146             it and/or modify it under the same terms as Perl itself.
147              
148             The full text of the license can be found in the
149             LICENSE file included with this module.
150              
151             =cut