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              
2             use Moose;
3 5     5   509455 use JSON qw();
  5         1951664  
  5         32  
4 5     5   32418 use utf8;
  5         15465  
  5         105  
5 5     5   25  
  5         9  
  5         40  
6             use UUID::Tiny ':std';
7 5     5   2220  
  5         43015  
  5         1070  
8             our $VERSION = '0.042'; # VERSION
9              
10             use Chart::Plotly;
11 5     5   1660  
  5         22  
  5         2520  
12             has traces => ( traits => ['Array'],
13             is => 'rw',
14             isa => 'ArrayRef',
15             default => sub { [] },
16             handles => { add_trace => 'push',
17             get_trace => 'get',
18             insert_trace => 'insert',
19             delete_trace => 'delete'
20             }
21             );
22              
23             has layout => ( is => 'rw',
24             isa => 'HashRef' );
25              
26             has config => ( is => 'rw',
27             isa => 'HashRef' );
28              
29             my $self = shift;
30             my %params = @_;
31 1     1 1 729 my $chart_id = $params{'div_id'} // create_uuid_as_string(UUID_TIME);
32 1         4 my $load_plotly_using_script_tag = $params{'load_plotly_using_script_tag'} // 1;
33 1   33     9 my $layout = $self->layout;
34 1   50     355 my $config = $self->config;
35 1         34 if ( defined $config ) {
36 1         25 $config = Chart::Plotly::_process_data($config);
37 1 50       4 if ( !defined $layout ) {
38 1         4 $layout = {};
39 1 50       4 }
40 1         3 }
41             if ( defined $layout ) {
42             $layout = Chart::Plotly::_process_data($layout);
43 1 50       4 }
44 1         19 return
45             Chart::Plotly::_render_cell( Chart::Plotly::_process_data( $self->traces() ),
46             $chart_id, $layout, $config,
47 1         31 { load_plotly_using_script_tag => $load_plotly_using_script_tag } );
48             }
49              
50             my $self = shift;
51             my $layout = $self->layout;
52             my $config = $self->config;
53 5     5 1 42779 my %json = ( data => $self->traces() );
54 5         158 if ( defined $layout ) {
55 5         118 $json{layout} = $layout;
56 5         126 }
57 5 100       24 if ( defined $config ) {
58 2         6 $json{config} = $config;
59             }
60 5 100       19 return \%json;
61 2         6 }
62              
63 5         22 my $self = shift;
64             my $layout = $self->layout;
65             my $config = $self->config;
66             my $json = '{ "data": ' . Chart::Plotly::_process_data( $self->traces() );
67 2     2 1 895 if ( defined $layout ) {
68 2         62 $layout = Chart::Plotly::_process_data($layout);
69 2         47 $json .= ', "layout": ' . $layout;
70 2         59 }
71 2 50       8 if ( defined $config ) {
72 0         0 $config = Chart::Plotly::_process_data($config);
73 0         0 $json .= ', "config": ' . $config;
74             }
75 2 100       8 return $json . " }";
76 1         3 }
77 1         4  
78             my $class = shift;
79 2         22 my $json = shift;
80             my %data = %{ JSON::from_json($json) };
81             return
82             $class->new( ( defined $data{"data"} ? ( traces => $data{"data"} ) : () ),
83 3     3 1 3381 ( defined $data{"layout"} ? ( layout => $data{"layout"} ) : () ),
84 3         9 ( defined $data{"config"} ? ( config => $data{"config"} ) : () )
85 3         6 );
  3         20  
86             }
87              
88             1;
89 3 50       160  
    100          
    100          
90              
91             =pod
92              
93             =encoding utf-8
94              
95             =head1 NAME
96              
97             Chart::Plotly::Plot
98              
99             =head1 VERSION
100              
101             version 0.042
102              
103             =head1 SYNOPSIS
104              
105             use Chart::Plotly::Trace::Scatter;
106             use Chart::Plotly::Plot;
107             use Chart::Plotly qw(show_plot);
108             use HTML::Show;
109            
110             my $x = [1 .. 15];
111             my $y = [map {rand 10 } @$x];
112             my $scatter = Chart::Plotly::Trace::Scatter->new(x => $x, y => $y);
113             my $plot = Chart::Plotly::Plot->new();
114             $plot->add_trace($scatter);
115            
116             show_plot($plot);
117            
118             # This also works
119             # HTML::Show::show(Chart::Plotly::render_full_html(data => $plot));
120             # HTML::Show::show($plot->html);
121              
122             =head1 DESCRIPTION
123              
124             Represent a full plot composed of one or more traces (Chart::Plotly::Trace::*)
125              
126             =head1 NAME
127              
128             Chart::Plotly::Plot - Set of traces with their options and data
129              
130             =head1 METHODS
131              
132             =head2 config
133              
134             Configuration options for the plot. See L<https://plot.ly/javascript/configuration-options/>
135              
136             =head2 html
137              
138             Returns the html corresponding to the plot
139              
140             =head3 Parameters
141              
142             =over 4
143              
144             =item div_id
145              
146             =item load_plotly_using_script_tag
147              
148             Add the script tag neccesary for loading plotly.js. Default 1.
149              
150             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
151              
152             =back
153              
154             =head2 TO_JSON
155              
156             Returns the structure suitable to serialize to JSON corresponding to the plot.
157              
158             This method is meant to be called by a JSON serializer, not directly.
159              
160             Beware with nested data. For example piddle are still there and you're
161             responsible to provide an appropiatte serializer. If you want
162             the text representation of json use the method: to_json_text
163              
164             =head2 to_json_text
165              
166             Returns the plot serialized in JSON. Not suitable to use in
167             nested structures. For nested structures you should check TO_JSON
168              
169             =head2 from_json
170              
171             Build the plot from a json string in the format returned by to_json_text.
172              
173             Beware when using to_json_text and from_json the plot object can be
174             slightly different (although the representation is the same). That is the
175             reconstructed plot is equivalent to the first, for example when using PDL,
176             piddles are serialized to simple arrays and when deserialized are just plain
177             arrays.
178              
179             =head1 AUTHOR
180              
181             Pablo Rodríguez González
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 LICENSE AND COPYRIGHT
195              
196             Copyright 2016 Pablo Rodríguez González.
197              
198             The MIT License (MIT)
199              
200             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:
201              
202             The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
203              
204             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.
205              
206             =head1 AUTHOR
207              
208             Pablo Rodríguez González <pablo.rodriguez.gonzalez@gmail.com>
209              
210             =head1 COPYRIGHT AND LICENSE
211              
212             This software is Copyright (c) 2022 by Pablo Rodríguez González.
213              
214             This is free software, licensed under:
215              
216             The MIT (X11) License
217              
218             =cut