Chart/Plotly.pm

filter: Statementstmt 68.8% 73 / 106 Branchbran 29.1% 7 / 24 Conditioncond 21.4% 3 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 84.0% 21 / 25 Podpod 100% 6 / 6 totaltotal 59.7% 110 / 184 CC 19 SCAR 19.5
CC
19
Cov
58%
html_plot
20.0
_load_plotly
15.9
render_full_html
12.0
CRAP 7.0
« Chart/Plotly/Trace/Histogram.pm ↑ Summary Chart/Plotly/Trace/Funnel.pm »
1 package Chart::Plotly;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use vars qw(@EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 @EXPORT_OK = qw(show_plot);
10  
11 6 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use Params::Validate qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use Text::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use Module::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶use Ref::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 6 ▶use HTML::Show;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 ▶use UUID::Tiny ':std';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 6 ▶use File::ShareDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 6 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 6 our $VERSION = '0.042'; # VERSION
22  
23 # ABSTRACT: Generate html/javascript charts from perl data using javascript library plotly.js
24  
25 0 ▶sub render_full_html {
Subroutine render_full_html: not called CC=3 SCAR=24.8
Pod documented
26 ## no critic
27 0 my %params = validate( @_, { data => { type => ARRAYREF | OBJECT }, } );
28 ## use critic
29  
30 0 my $data = $params{'data'};
31 0 my $chart_id = create_uuid_as_string(UUID_TIME);
32 0 my $html;
33 0 ▶ if ( Ref::Util::is_blessed_ref($data) && $data->isa('Chart::Plotly::Plot') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (Ref::Util::XS::is_blessed_ref($data) and $data->isa('Chart::Plotly::Plot')) { } 0 0 0
Condition0%0/3
Ref::Util::XS::is_blessed_ref($data) and $data->isa("Chart::Plotly::Plot")
!ll&&!rl&&r
000
MC/DC0%0/2
Ref::Util::XS::is_blessed_ref($data) and $data->isa("Chart::Plotly::Plot")
Ref::Util::XS::is_blessed_ref($data) $data->isa("Chart::Plotly::Plot")
Truth table0%0/3
A and B
A= Ref::Util::XS::is_blessed_ref($data)B= $data->isa("Chart::Plotly::Plot")
ABresult
0X0
100
111
34 0 $html = _render_html_wrap( $data->html( div_id => $chart_id ) );
35 } else {
36 0 $html = _render_html_wrap( _render_cell( _process_data($data), $chart_id ) );
37 }
38 0 return $html;
39 }
40  
41 0 ▶sub _render_html_wrap {
Subroutine _render_html_wrap: not called CC=1 SCAR=6.9
42 0 my $body = shift;
43 0 my $html_begin = <<'HTML_BEGIN';
44 <!DOCTYPE html>
45 <head>
46 <meta charset="utf-8" />
47 </head>
48 <body>
49 HTML_BEGIN
50 0 my $html_end = <<'HTML_END';
51 </body>
52 </html>
53 HTML_END
54 0 return $html_begin . $body . $html_end;
55 }
56  
57 1 ▶sub _render_cell {
Subroutine _render_cell: called CC=7 SCAR=20.6
58 1 my $data_string = shift();
59 1 ▶ my $chart_id = shift() // create_uuid_as_string(UUID_TIME);
Condition33%1/3
shift() // create_uuid_as_string(1)
l!l&&r!l&&!r
100
MC/DC0%0/2
shift() // create_uuid_as_string(1)
shift() create_uuid_as_string(1)
Truth table33%1/3
A // B
A= shift()B= create_uuid_as_string(1)
ABresult
1X1
011
000
60 1 my $layout = shift();
61 1 my $config = shift();
62 1 ▶ my $extra = shift() // { load_plotly_using_script_tag => 1 };
Condition50%1/2
shift() // {"load_plotly_using_script_tag", 1}
l!l
10
MC/DC0%0/1
shift() // {"load_plotly_using_script_tag", 1}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
63 1 ▶ if ( defined $layout ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $layout) 1 0 1
64 1 $layout = "," . $layout;
65 }
66 1 ▶ if ( defined $config ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config) 1 0 1
67 1 $config = "," . $config;
68 }
69 1 my $load_plotly = _load_plotly( ${$extra}{'load_plotly_using_script_tag'} );
70 1 my $template = <<'TEMPLATE';
71 <div id="{$chart_id}"></div>
72 {$load_plotly}
73 <script>
74 Plotly.{$plotlyjs_plot_function}(document.getElementById('{$chart_id}'),{$data} {$layout} {$config});
75 </script>
76 TEMPLATE
77  
78 1 ▶ my $template_variables = { data => $data_string,
Branch: 2 branches
BranchTrueFalseTotal
defined $layout ? : 1 0 1
defined $config ? : 1 0 1
79 chart_id => $chart_id,
80 load_plotly => $load_plotly,
81 plotlyjs_plot_function => plotlyjs_plot_function(),
82 defined $layout ? ( layout => $layout ) : (),
83 defined $config ? ( config => $config ) : (),
84 };
85 1 return Text::Template::fill_in_string( $template, HASH => $template_variables );
86 }
87  
88 6 ▶sub _process_data {
Subroutine _process_data: called CC=3 SCAR=15.1
89 6 my $data = shift;
90 6 my $json_formatter = JSON->new->allow_blessed(1)->convert_blessed(1);
91 6 ▶ local *PDL::TO_JSON = sub { $_[0]->unpdl };
Subroutine __ANON__: called CC=1 SCAR=0.0
92 6 ▶ if ( Ref::Util::is_blessed_ref($data) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (Ref::Util::XS::is_blessed_ref($data)) 0 6 6
93 0 my $adapter_name = 'Chart::Plotly::Adapter::' . ref $data;
94 0 eval {
95 0 load $adapter_name;
96 0 my $adapter = $adapter_name->new( data => $data );
97 0 $data = $adapter->traces();
98 };
99 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
100 0 warn 'Cannot load adapter: ' . $adapter_name . '. ' . $@;
101 }
102 }
103 6 my $data_string = $json_formatter->encode($data);
104 6 return $data_string;
105 }
106  
107 1 ▶sub _load_plotly {
Subroutine _load_plotly: called CC=6 SCAR=27.7
108 1 my $how_to_load = shift;
109 1 ▶ if ($how_to_load) {
Branch: 1 branch
BranchTrueFalseTotal
if ($how_to_load) { } 1 0 1
110 1 ▶ if ( $how_to_load eq "1" || $how_to_load eq 'cdn' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($how_to_load eq '1' or $how_to_load eq 'cdn') { } 1 0 1
elsif ($how_to_load eq 'embed') { } 0 0 0
elsif ($how_to_load eq 'module_dist') { } 0 0 0
Condition33%1/3
$how_to_load eq 1 or $how_to_load eq "cdn"
l!l&&r!l&&!r
100
MC/DC0%0/2
$how_to_load eq 1 or $how_to_load eq "cdn"
$how_to_load eq 1 $how_to_load eq "cdn"
Truth table33%1/3
A or B
A= $how_to_load eq 1B= $how_to_load eq "cdn"
ABresult
1X1
011
000
111 1 return '<script src="https://cdn.plot.ly/plotly-' . plotlyjs_version() . '.min.js"></script>';
112 } elsif ( $how_to_load eq 'embed' ) {
113 0 my $minified_plotly = File::ShareDir::dist_file( 'Chart-Plotly', 'plotly.js/plotly.min.js' );
114 0 return '<script>' . Path::Tiny::path($minified_plotly)->slurp . '</script>';
115 } elsif ( $how_to_load eq 'module_dist' ) {
116 0 my $minified_plotly = File::ShareDir::dist_file( 'Chart-Plotly', 'plotly.js/plotly.min.js' );
117 0 return '<script src="file://' . $minified_plotly . '"></script>';
118 }
119 } else {
120 0 return '';
121 }
122 }
123  
124 0 ▶sub html_plot {
Subroutine html_plot: not called CC=4 SCAR=30.0
Pod documented
125 0 my @data_to_plot = @_;
126  
127 0 my $rendered_cells = "";
128 0 for my $data (@data_to_plot) {
129 0 my $id = create_uuid_as_string(UUID_TIME);
130 0 ▶ if ( Ref::Util::is_blessed_ref($data) && $data->isa('Chart::Plotly::Plot') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (Ref::Util::XS::is_blessed_ref($data) and $data->isa('Chart::Plotly::Plot')) { } 0 0 0
Condition0%0/3
Ref::Util::XS::is_blessed_ref($data) and $data->isa("Chart::Plotly::Plot")
!ll&&!rl&&r
000
MC/DC0%0/2
Ref::Util::XS::is_blessed_ref($data) and $data->isa("Chart::Plotly::Plot")
Ref::Util::XS::is_blessed_ref($data) $data->isa("Chart::Plotly::Plot")
Truth table0%0/3
A and B
A= Ref::Util::XS::is_blessed_ref($data)B= $data->isa("Chart::Plotly::Plot")
ABresult
0X0
100
111
131 0 $rendered_cells .= $data->html( div_id => $id );
132 } else {
133 0 $rendered_cells .= _render_cell( _process_data($data), $id );
134 }
135 }
136 0 return _render_html_wrap($rendered_cells);
137 }
138  
139 0 ▶sub show_plot {
Subroutine show_plot: not called CC=1 SCAR=6.9
Pod documented
140 0 HTML::Show::show( html_plot(@_) );
141 }
142  
143 1 ▶sub plotlyjs_version {
Subroutine plotlyjs_version: called CC=1 SCAR=0.0
Pod documented
144 1 return '2.14.0'; # plotlyjs_version_tag
145 }
146  
147 3 ▶sub plotlyjs_plot_function {
Subroutine plotlyjs_plot_function: called CC=1 SCAR=0.0
Pod documented
148 3 return 'react';
149 }
150  
151 1 ▶sub plotlyjs_plot_function_parameters {
Subroutine plotlyjs_plot_function_parameters: called CC=1 SCAR=0.0
Pod documented
152 1 return qw(div data layout config);
153 }
154  
155 6 1;
156  
157 __END__
« Chart/Plotly/Trace/Histogram.pm ↑ Summary Chart/Plotly/Trace/Funnel.pm »