line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Chart::Plotly::Image; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# ABSTRACT: Export static images of Plotly charts |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
733
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
37
|
|
6
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
35
|
|
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
7
|
use utf8; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
7
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = '0.041'; # VERSION |
11
|
|
|
|
|
|
|
|
12
|
1
|
|
|
1
|
|
472
|
use Chart::Plotly::Image::Orca; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
35
|
|
13
|
1
|
|
|
1
|
|
471
|
use Chart::Plotly::Image::Orca::Client; |
|
1
|
|
|
|
|
6
|
|
|
1
|
|
|
|
|
43
|
|
14
|
1
|
|
|
1
|
|
10
|
use Exporter qw(import); |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
137
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
our @EXPORT_OK = qw(save_image); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub save_image { |
19
|
2
|
|
|
2
|
1
|
192810
|
my %params = @_; |
20
|
2
|
|
50
|
|
|
20
|
my $engine = ( delete $params{engine} ) || 'auto'; |
21
|
|
|
|
|
|
|
|
22
|
2
|
|
|
|
|
22
|
my @supported_engines = qw(kaleido orca); |
23
|
2
|
50
|
|
|
|
11
|
unless ( grep { $_ eq $engine } ( 'auto', @supported_engines ) ) { |
|
6
|
|
|
|
|
27
|
|
24
|
0
|
|
|
|
|
0
|
die "Unsupported engine: $engine"; |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
2
|
50
|
|
|
|
15
|
if ( $params{orca_server} ) { |
28
|
0
|
|
|
|
|
0
|
_save_image_orca_client(%params); |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
2
|
100
|
|
|
|
9
|
if ( $engine eq 'auto' ) { |
32
|
1
|
|
|
|
|
13
|
for my $candidate (qw(kaleido orca)) { |
33
|
1
|
|
|
|
|
10
|
my $func_has = "_has_$candidate"; |
34
|
1
|
|
|
1
|
|
8
|
no strict 'refs'; ## no critic |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
112
|
|
35
|
1
|
50
|
|
|
|
9
|
if ( $func_has->() ) { |
36
|
1
|
|
|
|
|
13
|
$engine = $candidate; |
37
|
1
|
|
|
|
|
9
|
last; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
} |
41
|
2
|
50
|
|
|
|
10
|
if ( $engine eq 'auto' ) { |
42
|
0
|
|
|
|
|
0
|
die "None of @{[join(', ', @supported_engines)]} are available. " |
|
0
|
|
|
|
|
0
|
|
43
|
|
|
|
|
|
|
. "Please install Chart::Kaleido::Plotly (recommended) or Alien::Plotly::Orca. "; |
44
|
|
|
|
|
|
|
} |
45
|
2
|
|
|
|
|
12
|
my $func_save = "_save_image_$engine"; |
46
|
1
|
|
|
1
|
|
7
|
no strict 'refs'; ## no critic |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
417
|
|
47
|
2
|
|
|
|
|
14
|
return $func_save->(%params); |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub _has_kaleido { |
51
|
4
|
|
|
4
|
|
18
|
eval { |
52
|
4
|
|
|
|
|
556
|
require Chart::Kaleido::Plotly; |
53
|
4
|
|
|
|
|
282995
|
Chart::Kaleido::Plotly->VERSION(0.005); |
54
|
|
|
|
|
|
|
}; |
55
|
4
|
|
|
|
|
119
|
return !$@; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub _has_orca { |
59
|
1
|
|
|
1
|
|
5
|
my $has_orca; |
60
|
1
|
|
|
|
|
2
|
eval { $has_orca = Chart::Plotly::Image::Orca::orca_available }; |
|
1
|
|
|
|
|
5
|
|
61
|
1
|
|
|
|
|
4
|
return $has_orca; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub _save_image_kaleido { |
65
|
2
|
|
|
2
|
|
6
|
my %params = @_; |
66
|
|
|
|
|
|
|
|
67
|
2
|
|
|
|
|
7
|
_has_kaleido(); |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
# Chart::Kaleido::Plotly's interface uses plotlyjs not plotly |
70
|
2
|
|
33
|
|
|
66
|
$params{plotlyjs} ||= delete $params{plotly}; |
71
|
|
|
|
|
|
|
my %kaleido_params = |
72
|
2
|
100
|
|
|
|
5
|
map { exists $params{$_} ? ( $_ => delete $params{$_} ) : () } @{ Chart::Kaleido::Plotly->scope_flags }; |
|
8
|
|
|
|
|
41
|
|
|
2
|
|
|
|
|
17
|
|
73
|
2
|
|
|
|
|
47
|
my $kaleido = Chart::Kaleido::Plotly->new(%kaleido_params); |
74
|
2
|
|
|
|
|
8225
|
$kaleido->save(%params); |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub _save_image_orca { |
78
|
0
|
|
|
0
|
|
|
my %params = @_; |
79
|
|
|
|
|
|
|
|
80
|
0
|
|
|
|
|
|
Chart::Plotly::Image::Orca::orca(%params); |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub _save_image_orca_client { |
84
|
0
|
|
|
0
|
|
|
my %params = @_; |
85
|
|
|
|
|
|
|
|
86
|
0
|
|
|
|
|
|
$params{server} = delete $params{orca_server}; |
87
|
0
|
|
|
|
|
|
Chart::Plotly::Image::Orca::Client::save_image(%params); |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
1; |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
__END__ |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=pod |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=encoding UTF-8 |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head1 NAME |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
Chart::Plotly::Image - Export static images of Plotly charts |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=head1 VERSION |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
version 0.041 |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=head1 SYNOPSIS |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
use Chart::Plotly::Plot; |
109
|
|
|
|
|
|
|
use Chart::Plotly::Trace::Scatter; |
110
|
|
|
|
|
|
|
use Chart::Plotly::Image qw(save_image); |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
my $plot = Chart::Plotly::Plot->new( |
113
|
|
|
|
|
|
|
traces => [ |
114
|
|
|
|
|
|
|
Chart::Plotly::Trace::Scatter->new( x => [ 1 .. 5 ], y => [ 1 .. 5 ] ) |
115
|
|
|
|
|
|
|
] |
116
|
|
|
|
|
|
|
); |
117
|
|
|
|
|
|
|
save_image(file => 'TestOrca.png', plot => $plot, |
118
|
|
|
|
|
|
|
width => 1024, height => 768, |
119
|
|
|
|
|
|
|
engine => 'auto'); |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=head1 DESCRIPTION |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
This module generate static images of Plotly charts. |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
It internally uses either of below modules, |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=over 4 |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=item * |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
L<Chart::Kaleido::Plotly>'s save() method. |
132
|
|
|
|
|
|
|
Note that you will need to explicitly install L<Chart::Kaleido::Plotly> |
133
|
|
|
|
|
|
|
for kaleido to work. |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=item * |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
L<Chart::Plotly::Image::Orca>'s orca() function |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=item * |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
L<Chart::Plotly::Image::Orca::Client>'s save_image() function |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=back |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=head1 FUNCTIONS |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=head2 save_image |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
save_image(file => $file, plot => $plot, |
150
|
|
|
|
|
|
|
width => $width, height => $height, |
151
|
|
|
|
|
|
|
engine => $engine, |
152
|
|
|
|
|
|
|
%rest) |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
Parameters |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=over 4 |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=item * file |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
Image file path. |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=item * engine |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
One of "auto", "kaleido", "orca". |
165
|
|
|
|
|
|
|
Default is "auto", it tries in this order: kaleido, orca. |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=item * orca_server |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
If this parameter is specified it will use L<Chart::Plotly::Image::Orca::Client>. |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
For example, |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
save_image(file => $file, plot => $plot, |
174
|
|
|
|
|
|
|
width => $width, height => $height, |
175
|
|
|
|
|
|
|
orca_server => 'http://localhost:9999') |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=item * %rest |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
Rest parameters are passed through to the lower-level functions. |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=back |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=head1 BUGS |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
Please report any bugs or feature requests via github: L<https://github.com/pablrod/p5-Chart-Plotly/issues> |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=head1 DISCLAIMER |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
This is an unofficial Plotly Perl module. Currently I'm not affiliated in any way with Plotly. |
190
|
|
|
|
|
|
|
But I think plotly.js is a great library and I want to use it with perl. |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
If you like plotly.js please consider supporting them purchasing a pro subscription: L<https://plot.ly/products/cloud/> |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=head1 SEE ALSO |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
L<Chart::Kaleido::Plotly>, |
197
|
|
|
|
|
|
|
L<Chart::Plotly::Image::Orca>, |
198
|
|
|
|
|
|
|
L<Chart::Plotly::Image::Orca::Client>, |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
=head1 AUTHOR |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
Pablo Rodríguez González <pablo.rodriguez.gonzalez@gmail.com> |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
This software is Copyright (c) 2020 by Pablo Rodríguez González. |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
This is free software, licensed under: |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
The MIT (X11) License |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
=cut |