File Coverage

blib/lib/Chart/Plotly/Plot.pm
Criterion Covered Total %
statement 51 53 96.2
branch 15 20 75.0
condition 2 5 40.0
subroutine 9 9 100.0
pod 4 4 100.0
total 81 91 89.0


line stmt bran cond sub pod time code
1             package Chart::Plotly::Plot;
2              
3 5     5   580935 use Moose;
  5         2362121  
  5         41  
4 5     5   38983 use JSON qw();
  5         19226  
  5         116  
5 5     5   28 use utf8;
  5         11  
  5         37  
6              
7 5     5   2657 use UUID::Tiny ':std';
  5         57295  
  5         1255  
8              
9             our $VERSION = '0.041'; # VERSION
10              
11 5     5   1849 use Chart::Plotly;
  5         14  
  5         3360  
12              
13             has traces => ( traits => ['Array'],
14             is => 'rw',
15             isa => 'ArrayRef',
16             default => sub { [] },
17             handles => { add_trace => 'push',
18             get_trace => 'get',
19             insert_trace => 'insert',
20             delete_trace => 'delete'
21             }
22             );
23              
24             has layout => ( is => 'rw',
25             isa => 'HashRef' );
26              
27             has config => ( is => 'rw',
28             isa => 'HashRef' );
29              
30             sub html {
31 1     1 1 809 my $self = shift;
32 1         3 my %params = @_;
33 1   33     9 my $chart_id = $params{'div_id'} // create_uuid_as_string(UUID_TIME);
34 1   50     418 my $load_plotly_using_script_tag = $params{'load_plotly_using_script_tag'} // 1;
35 1         36 my $layout = $self->layout;
36 1         27 my $config = $self->config;
37 1 50       4 if ( defined $config ) {
38 1         4 $config = Chart::Plotly::_process_data($config);
39 1 50       4 if ( !defined $layout ) {
40 1         3 $layout = {};
41             }
42             }
43 1 50       3 if ( defined $layout ) {
44 1         4 $layout = Chart::Plotly::_process_data($layout);
45             }
46             return
47 1         31 Chart::Plotly::_render_cell( Chart::Plotly::_process_data( $self->traces() ),
48             $chart_id, $layout, $config, { load_plotly_using_script_tag => $load_plotly_using_script_tag } );
49             }
50              
51             sub TO_JSON {
52 5     5 1 13585 my $self = shift;
53 5         184 my $layout = $self->layout;
54 5         135 my $config = $self->config;
55 5         145 my %json = ( data => $self->traces() );
56 5 100       26 if ( defined $layout ) {
57 2         5 $json{layout} = $layout;
58             }
59 5 100       15 if ( defined $config ) {
60 2         4 $json{config} = $config;
61             }
62 5         25 return \%json;
63             }
64              
65             sub to_json_text {
66 2     2 1 1023 my $self = shift;
67 2         67 my $layout = $self->layout;
68 2         52 my $config = $self->config;
69 2         56 my $json = '{ "data": ' . Chart::Plotly::_process_data( $self->traces() );
70 2 50       10 if ( defined $layout ) {
71 0         0 $layout = Chart::Plotly::_process_data($layout);
72 0         0 $json .= ', "layout": ' . $layout;
73             }
74 2 100       7 if ( defined $config ) {
75 1         4 $config = Chart::Plotly::_process_data($config);
76 1         4 $json .= ', "config": ' . $config;
77             }
78 2         35 return $json . " }";
79             }
80              
81             sub from_json {
82 3     3 1 3529 my $class = shift;
83 3         7 my $json = shift;
84 3         7 my %data = %{ JSON::from_json($json) };
  3         14  
85             return
86             $class->new( ( defined $data{"data"} ? ( traces => $data{"data"} ) : () ),
87             ( defined $data{"layout"} ? ( layout => $data{"layout"} ) : () ),
88 3 50       147 ( defined $data{"config"} ? ( config => $data{"config"} ) : () )
    100          
    100          
89             );
90             }
91              
92             1;
93              
94             __END__
95              
96             =pod
97              
98             =encoding utf-8
99              
100             =head1 NAME
101              
102             Chart::Plotly::Plot
103              
104             =head1 VERSION
105              
106             version 0.041
107              
108             =head1 SYNOPSIS
109              
110             use Chart::Plotly::Trace::Scatter;
111             use Chart::Plotly::Plot;
112             use Chart::Plotly qw(show_plot);
113             use HTML::Show;
114            
115             my $x = [1 .. 15];
116             my $y = [map {rand 10 } @$x];
117             my $scatter = Chart::Plotly::Trace::Scatter->new(x => $x, y => $y);
118             my $plot = Chart::Plotly::Plot->new();
119             $plot->add_trace($scatter);
120            
121             show_plot($plot);
122            
123             # This also works
124             # HTML::Show::show(Chart::Plotly::render_full_html(data => $plot));
125             # HTML::Show::show($plot->html);
126              
127             =head1 DESCRIPTION
128              
129             Represent a full plot composed of one or more traces (Chart::Plotly::Trace::*)
130              
131             =head1 NAME
132              
133             Chart::Plotly::Plot - Set of traces with their options and data
134              
135             =head1 METHODS
136              
137             =head2 config
138              
139             Configuration options for the plot. See L<https://plot.ly/javascript/configuration-options/>
140              
141             =head2 html
142              
143             Returns the html corresponding to the plot
144              
145             =head3 Parameters
146              
147             =over 4
148              
149             =item div_id
150              
151             =item load_plotly_using_script_tag
152              
153             Add the script tag neccesary for loading plotly.js. Default 1.
154              
155             If plotly.js is going to be loaded in another place or some other way (e.g.: via RequireJS) is better to set to 0
156              
157             =back
158              
159             =head2 TO_JSON
160              
161             Returns the structure suitable to serialize to JSON corresponding to the plot.
162              
163             This method is meant to be called by a JSON serializer, not directly.
164              
165             Beware with nested data. For example piddle are still there and you're
166             responsible to provide an appropiatte serializer. If you want
167             the text representation of json use the method: to_json_text
168              
169             =head2 to_json_text
170              
171             Returns the plot serialized in JSON. Not suitable to use in
172             nested structures. For nested structures you should check TO_JSON
173              
174             =head2 from_json
175              
176             Build the plot from a json string in the format returned by to_json_text.
177              
178             Beware when using to_json_text and from_json the plot object can be
179             slightly different (although the representation is the same). That is the
180             reconstructed plot is equivalent to the first, for example when using PDL,
181             piddles are serialized to simple arrays and when deserialized are just plain
182             arrays.
183              
184             =head1 AUTHOR
185              
186             Pablo Rodríguez González
187              
188             =head1 BUGS
189              
190             Please report any bugs or feature requests via github: L<https://github.com/pablrod/p5-Chart-Plotly/issues>
191              
192             =head1 DISCLAIMER
193              
194             This is an unofficial Plotly Perl module. Currently I'm not affiliated in any way with Plotly.
195             But I think plotly.js is a great library and I want to use it with perl.
196              
197             If you like plotly.js please consider supporting them purchasing a pro subscription: L<https://plot.ly/products/cloud/>
198              
199             =head1 LICENSE AND COPYRIGHT
200              
201             Copyright 2016 Pablo Rodríguez González.
202              
203             The MIT License (MIT)
204              
205             Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
206              
207             The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
208              
209             THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
210              
211             =head1 AUTHOR
212              
213             Pablo Rodríguez González <pablo.rodriguez.gonzalez@gmail.com>
214              
215             =head1 COPYRIGHT AND LICENSE
216              
217             This software is Copyright (c) 2020 by Pablo Rodríguez González.
218              
219             This is free software, licensed under:
220              
221             The MIT (X11) License
222              
223             =cut