File Coverage

blib/lib/Chart/Plotly/Image/Orca.pm
Criterion Covered Total %
statement 38 75 50.6
branch 4 24 16.6
condition 5 21 23.8
subroutine 10 14 71.4
pod 4 4 100.0
total 61 138 44.2


line stmt bran cond sub pod time code
1              
2             # ABSTRACT: Export static images of Plotly charts using orca
3              
4             use 5.010;
5 2     2   784 use strict;
  2         7  
6 2     2   8 use warnings;
  2         3  
  2         34  
7 2     2   7  
  2         3  
  2         44  
8             use Config;
9 2     2   8 use File::Which;
  2         2  
  2         60  
10 2     2   650 use Path::Tiny;
  2         1388  
  2         99  
11 2     2   10 use File::ShareDir qw(dist_file);
  2         4  
  2         66  
12 2     2   10 use utf8;
  2         3  
  2         79  
13 2     2   8  
  2         3  
  2         11  
14             our $VERSION = '0.042'; # VERSION
15              
16             my $ORCA_COMMAND = 'orca';
17              
18             # have this in a sub to avoid breaking auto-generated tests like pod-coverage.
19             state $plotlyjs = dist_file( 'Chart-Plotly', 'plotly.js/plotly.min.js' );
20             return $plotlyjs;
21 0     0   0 }
22 0         0  
23             my ($force_check) = @_;
24              
25             state $has_alien;
26 2     2   105  
27             if ( !defined $has_alien or $force_check ) {
28 2         3 $has_alien = undef;
29             eval { require Alien::Plotly::Orca; };
30 2 50 33     10 if ( !$@ and Alien::Plotly::Orca->install_type eq 'share' ) {
31 2         4 $ENV{PATH} = join( $Config{path_sep}, Alien::Plotly::Orca->bin_dir, $ENV{PATH} );
32 2         3 $has_alien = 1;
  2         301  
33 2 50 33     15 } else {
34 0         0 $has_alien = 0;
35 0         0 }
36             }
37 2         5 return $has_alien;
38             }
39              
40 2         9 my %params = @_;
41              
42             if ( orca_available() ) {
43             my $plot = $params{plot};
44 0     0 1 0 my $file = path( $params{file} );
45             my $format = $params{format};
46 0 0       0 unless ( defined $format ) {
47 0         0 ($format) = $file =~ /\.([^\.]+)$/;
48 0         0 }
49 0         0 my $plotlyjs = $params{plotlyjs} // $params{plotly} // _plotlyjs;
50 0 0       0  
51 0         0 my $tmp_json = Path::Tiny->tempfile( SUFFIX => '.json' );
52             $tmp_json->spew_raw( $plot->to_json_text );
53 0   0     0  
      0        
54             # For now have to explicitly specify -d as otherwise orca would
55 0         0 # not be able to store output to a different path other than cwd.
56 0         0 # See https://github.com/plotly/orca/issues/101
57             my @orca_line = ( $ORCA_COMMAND, 'graph', $tmp_json, '--plotlyjs', $plotlyjs, '-d', $file->parent,
58             '-o', $file->basename, ( $format ? ( '--format', $format ) : () ) );
59             for my $arg (qw(mathjax scale width height)) {
60             if ( my $val = $params{$arg} ) {
61 0 0       0 push @orca_line, ( "--${arg}", $val );
62             }
63 0         0 }
64 0 0       0 for my $arg (qw(safe verbose debug)) {
65 0         0 if ( $params{$arg} ) {
66             push @orca_line, "--${arg}";
67             }
68 0         0 }
69 0 0       0  
70 0         0 my $rc = system(@orca_line);
71             return 1 unless ( $rc >> 8 );
72             }
73             return;
74 0         0 }
75 0 0       0  
76             my $orca_help = `$ORCA_COMMAND --help`;
77 0         0 return ( $orca_help =~ /plotly/i );
78             }
79              
80             my ($force_check) = @_;
81 0     0 1 0  
82 0         0 state $available;
83              
84             if ( !defined $available or $force_check ) {
85             $available = undef;
86 1     1 1 3 if ( not _check_alien($force_check)
87             and ( not which($ORCA_COMMAND) or not correct_orca() ) )
88 1         1 {
89             die "Orca tool (its 'orca' command) must be installed and in "
90 1 50 33     6 . "PATH in order to export images. "
91 1         2 . "Either install Alien::Plotly::Orca from CPAN, or install "
92 1 50 33     3 . "it manually (see https://github.com/plotly/orca#installation)";
      33        
93             }
94             $available = 1;
95 1         274 }
96             return $available;
97             }
98              
99             my ($force_check) = @_;
100 0            
101             state $version;
102 0            
103             if ( _check_alien($force_check) ) {
104             return Alien::Plotly::Orca->version;
105             }
106 0     0 1   if ( orca_available($force_check) ) {
107             $version = `$ORCA_COMMAND --version`;
108 0           chomp($version);
109             return $version;
110 0 0         }
111 0           return;
112             }
113 0 0          
114 0           1;
115 0            
116 0            
117             =pod
118 0            
119             =encoding utf-8
120              
121             =head1 NAME
122              
123             Chart::Plotly::Image::Orca - Export static images of Plotly charts using orca
124              
125             =head1 VERSION
126              
127             version 0.042
128              
129             =head1 SYNOPSIS
130              
131             #!/usr/bin/env perl
132            
133             use strict;
134             use warnings;
135             use utf8;
136            
137             use Chart::Plotly::Plot;
138             use Chart::Plotly::Trace::Scatter;
139             use Chart::Plotly::Image::Orca;
140            
141             my $plot = Chart::Plotly::Plot->new(traces => [ Chart::Plotly::Trace::Scatter->new( x => [ 1 .. 5 ], y => [ 1 .. 5 ] )]);
142            
143             Chart::Plotly::Image::Orca::orca(plot => $plot, file => "TestOrca.png");
144              
145             =head1 DESCRIPTION
146              
147             This module generate static images of Plotly charts without a browser using
148             L<Orca|https://github.com/plotly/orca>
149              
150             Orca is an L<Electron|https://electronjs.org/> app that must be installed before
151             using this module. You can either,
152              
153             =over 4
154              
155             =item *
156              
157             Install the L<Alien::Plotly::Orca> module from CPAN. Or,
158              
159             =item *
160              
161             Install plotly-orca yourself and have a C<orca> command findable via the
162             C<PATH> env var in your system, see also
163             L<https://github.com/plotly/orca#installation>.
164              
165             =back
166              
167             =head1 FUNCTIONS
168              
169             =head2 orca
170              
171             orca(plot => $plot, file => $file, %rest)
172              
173             Export L<Chart::Plotly::Plot> as a static image file.
174              
175             This function is a wrapper over the plotly orca command.
176             Most of its named parameters are mapped to orca's command line options.
177             See also the output of C<orca graph --help>.
178              
179             Returns a true value if the orca command is successful.
180              
181             =over 4
182              
183             =item plot
184              
185             Object to export
186              
187             =item file
188              
189             Filename (with or without path) to export
190              
191             =item format
192              
193             Sets the output format (png, jpeg, webp, svg, pdf, eps).
194             By default it's inferred from the specified file name extension.
195              
196             =item scale
197              
198             Sets the image scale.
199              
200             =item width
201              
202             Sets the image width.
203              
204             =item height
205              
206             Sets the image height.
207              
208             =item plotlyjs
209              
210             Sets plotlyjs file path. Default is the bundled plotly.min.js file.
211              
212             =item plotly
213              
214             This is same as the C<plotlyjs> parameter mentioned above.
215              
216             =item mathjax
217              
218             Sets path to MathJax files. Required to export LaTeX characters.
219              
220             =item safe
221              
222             Turns on safe mode: where figures likely to make browser window hang
223             during image generating are skipped.
224              
225             =item verbose
226              
227             Turn on verbose logging on stdout.
228              
229             =item debug
230              
231             Starts app in debug mode and turn on verbose logs on stdout.
232              
233             =back
234              
235             =head2 correct_orca
236              
237             Checks that orca command available is the plotly image exporter,
238             as there may be some other different command also named "orca", like
239             L<https://help.gnome.org/users/orca/stable/>
240              
241             =head2 orca_available
242              
243             Checks that orca command is available and the plotly image exporter
244              
245             =head2 orca_version
246              
247             Returns the orca version
248              
249             =head1 BUGS
250              
251             Please report any bugs or feature requests via github: L<https://github.com/pablrod/p5-Chart-Plotly/issues>
252              
253             =head1 DISCLAIMER
254              
255             This is an unofficial Plotly Perl module. Currently I'm not affiliated in any way with Plotly.
256             But I think plotly.js is a great library and I want to use it with perl.
257              
258             If you like plotly.js please consider supporting them purchasing a pro subscription: L<https://plot.ly/products/cloud/>
259              
260             =head1 SEE ALSO
261              
262             L<Alien::Plotly::Orca>
263              
264             =head1 AUTHOR
265              
266             Pablo Rodríguez González <pablo.rodriguez.gonzalez@gmail.com>
267              
268             =head1 COPYRIGHT AND LICENSE
269              
270             This software is Copyright (c) 2022 by Pablo Rodríguez González.
271              
272             This is free software, licensed under:
273              
274             The MIT (X11) License
275              
276             =cut