File Coverage

lib/Graph/D3.pm
Criterion Covered Total %
statement 40 40 100.0
branch 7 10 70.0
condition n/a
subroutine 7 7 100.0
pod 2 2 100.0
total 56 59 94.9


line stmt bran cond sub pod time code
1             package Graph::D3;
2              
3 1     1   687 use strict;
  1         2  
  1         36  
4 1     1   28 use 5.008_005;
  1         6  
  1         46  
5             our $VERSION = '0.03';
6              
7 1     1   2960 use Moo;
  1         28341  
  1         6  
8 1     1   2299 use JSON;
  1         2  
  1         10  
9 1     1   262 use Graph;
  1         2  
  1         777  
10              
11             has graph => (
12             is => 'ro',
13             required => 1
14             );
15              
16             has type => (
17             is => 'ro',
18             default => sub { 'ref' },
19             );
20              
21             # https://gist.github.com/mbostock/4062045
22             # http://bl.ocks.org/mbostock/4062045
23             sub force_directed_graph {
24 2     2 1 1096 my $self = shift;
25 2         10 my @nodes;
26             my %nodes;
27 2         4 my $counter = 0;
28 2         14 for my $vertex ($self->graph->vertices) {
29 10         160 my $group = $self->graph->get_vertex_attribute($vertex, 'group');
30 10 50       1002 $group = 1 unless (defined $group);
31 10         35 push @nodes, { name => $vertex, group => $group };
32 10         17 $nodes{$vertex} = $counter;
33 10         21 $counter++;
34             }
35 2         5 my @links;
36 2         11 for my $edges ($self->graph->edges) {
37 8         219 my ($n1, $n2) = @$edges;
38 8         33 my $value = $self->graph->get_edge_attribute($n1, $n2, 'value');
39 8 50       3561 $value = 1 unless (defined $value);
40 8         40 push @links, {source => $nodes{$n1}, target => $nodes{$n2}, value => $value};
41             }
42 2         10 my $output = { nodes => \@nodes, links => \@links };
43 2 100       62 return $self->type eq 'json' ? encode_json $output : $output;
44             }
45              
46             # http://mbostock.github.io/d3/talk/20111116/bundle.html
47             sub hierarchical_edge_bundling {
48 2     2 1 2425 my $self = shift;
49 2         5 my @bundle;
50 2         13 for my $vertex ($self->graph->vertices) {
51 10         163 my $size = $self->graph->get_vertex_attribute($vertex, 'size');
52 10 50       935 $size = 1 unless (defined $size);
53 10         43 my @imports = $self->graph->predecessors($vertex);
54 10         1024 push @bundle, {name => $vertex, size => $size, imports => \@imports };
55             }
56 2 100       26 return $self->type eq 'json' ? encode_json \@bundle : \@bundle;
57             }
58              
59              
60             1;
61             __END__