InfoVis/Graph.pm

filter: Statementstmt 92.8% 26 / 28 Branchbran 50.0% 1 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 87.5% 7 / 8 Podpod 100% 4 / 4 totaltotal 90.4% 38 / 42 CC 3 SCAR 4.1
CC
3
Cov
90%
new
2.0
add_node
2.0
as_json
2.0
CRAP 1.5
↑ Summary InfoVis.pm »
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<loadJSON> 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<Graph>-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 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 2 ▶use JSON 2.16 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
36 2 ▶use Graph 0.85 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 2 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<Graph> object
49 that represents the graph structure.
50  
51 Returns a new B<HTML::InfoVis::Graph> object.
52  
53 =cut
54  
55 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
56 1 my $class = shift;
57 1 my $self = bless { @_ }, $class;
58 1 ▶ unless ( defined $self->{graph} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"graph"}) 1 0 1
59 1 $self->{graph} = Graph->new;
60 }
61 1 return $self;
62 }
63  
64 =pod
65  
66 =head2 add_node
67  
68 $graph->add_node( 'foo' );
69  
70 The C<add_node> method is a pass-through method to the underlying
71 L<Graph> C<add_vertex> method.
72  
73 =cut
74  
75 0 ▶sub add_node {
Subroutine add_node: not called CC=1 SCAR=6.9
Pod documented
76 0 my $self = shift;
77 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<add_edge> method is a pass-through method to the underlying
87 L<Graph> C<add_edge> method.
88  
89 =cut
90  
91 1 ▶sub add_edge {
Subroutine add_edge: called CC=1 SCAR=0.0
Pod documented
92 1 my $self = shift;
93 1 $self->{graph}->add_edge(@_);
94 }
95  
96 =pod
97  
98 =head2 as_json
99  
100 my $json = $graph->as_json;
101  
102 The C<as_json> method generates a serialized anonymous JSON structure that
103 represents the graph in a form suitable for loading by any InfoVis C<loadJSON>
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 1 ▶sub as_json {
Subroutine as_json: called CC=2 SCAR=6.9
Pod documented
112 1 my $self = shift;
113 1 my $graph = $self->{graph};
114 1 my @nodes = ();
115 1 foreach my $name ( sort $graph->vertices ) {
116 2 push @nodes, {
117 id => $name,
118 name => $name,
119 adjacencies => [ $graph->successors($name) ],
120 };
121 }
122 1 JSON->new->canonical(1)->pretty(1)->encode(\@nodes);
123 }
124  
125 2 1;
126  
127 =pod
128  
129 =head1 SUPPORT
130  
131 Bugs should be reported via the CPAN bug tracker at
132  
133 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=HTML-InfoVis>
134  
135 For other issues, or commercial enhancement or support, contact the author.
136  
137 =head1 AUTHORS
138  
139 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
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
↑ Summary InfoVis.pm »