line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Chart::Dygraphs; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
62535
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
44
|
|
4
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
47
|
|
5
|
1
|
|
|
1
|
|
6
|
use utf8; |
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
7
|
|
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
55
|
use Exporter 'import'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
145
|
|
8
|
|
|
|
|
|
|
our @EXPORT_OK = qw(show_plot); |
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
1911
|
use JSON; |
|
1
|
|
|
|
|
32695
|
|
|
1
|
|
|
|
|
12
|
|
11
|
1
|
|
|
1
|
|
2101
|
use Params::Validate qw(:all); |
|
1
|
|
|
|
|
15732
|
|
|
1
|
|
|
|
|
328
|
|
12
|
1
|
|
|
1
|
|
1179
|
use Text::Template; |
|
1
|
|
|
|
|
6439
|
|
|
1
|
|
|
|
|
92
|
|
13
|
1
|
|
|
1
|
|
1381
|
use HTML::Show; |
|
1
|
|
|
|
|
30187
|
|
|
1
|
|
|
|
|
555
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
our $VERSION = '0.005'; # VERSION |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=encoding utf-8 |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=head1 NAME |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
Chart::Dygraphs - Generate html/javascript charts from perl data using javascript library Dygraphs |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=head1 SYNOPSYS |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
use Chart::Dygraphs qw(show_plot); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
my $data = [map {[$_, rand($_)]} 1..10 ]; |
28
|
|
|
|
|
|
|
show_plot($data); |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
use Chart::Dygraphs qw(show_plot); |
31
|
|
|
|
|
|
|
use DateTime; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
my $start_date = DateTime->now(time_zone => 'UTC')->truncate(to => 'hour'); |
34
|
|
|
|
|
|
|
my $time_series_data = [map {[$start_date->add(hours => 1)->clone(), rand($_)]} 1..1000]; |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
show_plot($time_series_data); |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=head1 DESCRIPTION |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
Generate html/javascript charts from perl data using javascript library Dygraphs. The result |
41
|
|
|
|
|
|
|
is html that you could see in your favourite browser. |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
The interface is "sub" oriented, but the API is subject to changes. |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=head1 FUNCTIONS |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=cut |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=head2 render_full_html |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=head3 Parameters |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=over |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=item * data: |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
Data to be represented. The format is the perl version of the data expected by Dygraphs: L |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=item * options: |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
Hashref with options for graph. The format is the perl version of the options expected by Dygraphs: L |
62
|
|
|
|
|
|
|
Optional |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=item * render_html_options |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
Hashref with options controlling html output. With this you can inject html, javascript or styles. |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
Supported options: |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=over |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=item * pre_graph_html |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=item * post_graph_html |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=item * dygraphs_div_id |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=item * dygraphs_javascript_object_name |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=item * dygraphs_div_inline_style |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=back |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=back |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=cut |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub render_full_html { |
89
|
0
|
|
|
0
|
1
|
|
my %params = validate( @_, |
90
|
|
|
|
|
|
|
{ data => { type => SCALAR | ARRAYREF }, |
91
|
|
|
|
|
|
|
options => { type => HASHREF, default => { showRangeSelector => 1 } }, |
92
|
|
|
|
|
|
|
render_html_options => { |
93
|
|
|
|
|
|
|
type => HASHREF, |
94
|
|
|
|
|
|
|
optional => 1, |
95
|
|
|
|
|
|
|
default => { dygraphs_div_id => 'graphdiv', dygraphs_javascript_object_name => 'g' } |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
); |
99
|
|
|
|
|
|
|
|
100
|
0
|
|
|
|
|
|
my $template = <<'DYGRAPH_TEMPLATE'; |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
{$pre_graph_html} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
{$post_graph_html} |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
DYGRAPH_TEMPLATE |
124
|
|
|
|
|
|
|
|
125
|
0
|
|
|
|
|
|
my $transform_data; |
126
|
|
|
|
|
|
|
$transform_data = sub { |
127
|
0
|
|
|
0
|
|
|
my $data = shift; |
128
|
0
|
|
|
|
|
|
my $string_data = ""; |
129
|
0
|
0
|
|
|
|
|
if ( ref $data eq 'ARRAY' ) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
130
|
0
|
|
|
|
|
|
$string_data .= "[" . ( join( ',', map { $transform_data->($_) } @$data ) ) . "]"; |
|
0
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
} elsif ( ref $data eq 'HASH' ) { |
132
|
0
|
|
|
|
|
|
return "not supported"; |
133
|
|
|
|
|
|
|
} elsif ( ref $data eq 'DateTime' ) { |
134
|
0
|
|
|
|
|
|
return 'new Date("' . $data . '")'; |
135
|
|
|
|
|
|
|
} else { |
136
|
0
|
|
|
|
|
|
return $data; |
137
|
|
|
|
|
|
|
} |
138
|
0
|
|
|
|
|
|
return $string_data; |
139
|
0
|
|
|
|
|
|
}; |
140
|
0
|
|
|
|
|
|
my $data_string = $transform_data->( $params{'data'} ); |
141
|
|
|
|
|
|
|
|
142
|
0
|
|
|
|
|
|
my $json_formatter = JSON->new->utf8; |
143
|
|
|
|
|
|
|
my $template_variables = { |
144
|
0
|
|
|
|
|
|
%{ $params{'render_html_options'} }, |
145
|
0
|
|
|
|
|
|
data_and_options => join( ',', $data_string, $json_formatter->encode( $params{'options'} ) ), |
146
|
|
|
|
|
|
|
}; |
147
|
|
|
|
|
|
|
|
148
|
0
|
0
|
|
|
|
|
if ( !defined $template_variables->{'dygraphs_div_id'} ) { |
149
|
0
|
|
|
|
|
|
$template_variables->{'dygraphs_div_id'} = 'graphdiv'; |
150
|
|
|
|
|
|
|
} |
151
|
0
|
0
|
|
|
|
|
if ( !defined $template_variables->{'dygraphs_javascript_object_name'} ) { |
152
|
0
|
|
|
|
|
|
$template_variables->{'dygraphs_javascript_object_name'} = 'g'; |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
|
155
|
0
|
|
|
|
|
|
return Text::Template::fill_in_string( $template, HASH => $template_variables ); |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=head2 show_plot |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
Opens the plot in a browser locally |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=head3 Parameters |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
Data to be represented. The format is the same as the parameter data in render_full_html |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=cut |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
sub show_plot { |
169
|
0
|
|
|
0
|
1
|
|
my $data = shift(); |
170
|
0
|
|
|
|
|
|
HTML::Show::show( render_full_html( data => $data ) ); |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
1; |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
__END__ |