line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Chart::Bokeh; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
83403
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
29
|
|
4
|
1
|
|
|
1
|
|
3
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
27
|
|
5
|
1
|
|
|
1
|
|
3
|
use utf8; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
4
|
|
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
18
|
use Exporter 'import'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
43
|
|
8
|
1
|
|
|
1
|
|
6
|
use vars qw(@EXPORT_OK); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
68
|
|
9
|
|
|
|
|
|
|
@EXPORT_OK = qw(show_plot); |
10
|
|
|
|
|
|
|
|
11
|
1
|
|
|
1
|
|
716
|
use JSON; |
|
1
|
|
|
|
|
15500
|
|
|
1
|
|
|
|
|
5
|
|
12
|
1
|
|
|
1
|
|
980
|
use Params::Validate qw(:all); |
|
1
|
|
|
|
|
9988
|
|
|
1
|
|
|
|
|
178
|
|
13
|
1
|
|
|
1
|
|
765
|
use Text::Template; |
|
1
|
|
|
|
|
2812
|
|
|
1
|
|
|
|
|
53
|
|
14
|
1
|
|
|
1
|
|
590
|
use Module::Load; |
|
1
|
|
|
|
|
964
|
|
|
1
|
|
|
|
|
5
|
|
15
|
1
|
|
|
1
|
|
538
|
use Ref::Util; |
|
1
|
|
|
|
|
652
|
|
|
1
|
|
|
|
|
76
|
|
16
|
1
|
|
|
1
|
|
545
|
use HTML::Show; |
|
1
|
|
|
|
|
16503
|
|
|
1
|
|
|
|
|
448
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
our $VERSION = '0.001'; # VERSION |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
# ABSTRACT: Generate html/javascript charts from perl data using javascript library BokehJS |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub render_full_html { |
23
|
0
|
|
|
0
|
1
|
|
my %params = @_; |
24
|
|
|
|
|
|
|
|
25
|
0
|
|
|
|
|
|
my $data = $params{'data'}; |
26
|
0
|
|
|
|
|
|
my $chart_id = 'bokeh_graph'; |
27
|
0
|
|
|
|
|
|
my $html; |
28
|
0
|
0
|
0
|
|
|
|
if ( Ref::Util::is_blessed_ref($data) && $data->isa('Chart::Bokeh::Plot') ) { |
29
|
0
|
|
|
|
|
|
$html = _render_html_wrap( $data->html( div_id => $chart_id ) ); |
30
|
|
|
|
|
|
|
} else { |
31
|
0
|
|
|
|
|
|
$html = _render_html_wrap( _render_cell( _process_data($data), $chart_id ) ); |
32
|
|
|
|
|
|
|
} |
33
|
0
|
|
|
|
|
|
return $html; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub _render_html_wrap { |
37
|
0
|
|
|
0
|
|
|
my $body = shift; |
38
|
0
|
|
|
|
|
|
my $html_begin = <<'HTML_BEGIN'; |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
HTML_BEGIN |
45
|
0
|
|
|
|
|
|
my $html_end = <<'HTML_END'; |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
HTML_END |
49
|
0
|
|
|
|
|
|
return $html_begin . $body . $html_end; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub _render_cell { |
53
|
0
|
|
|
0
|
|
|
my $data_string = shift(); |
54
|
0
|
|
|
|
|
|
my $chart_id = shift(); |
55
|
0
|
|
|
|
|
|
my $template = <<'TEMPLATE'; |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
TEMPLATE |
71
|
|
|
|
|
|
|
|
72
|
0
|
|
|
|
|
|
my $template_variables = { data => $data_string, |
73
|
|
|
|
|
|
|
chart_id => $chart_id, |
74
|
|
|
|
|
|
|
}; |
75
|
0
|
|
|
|
|
|
return Text::Template::fill_in_string( $template, HASH => $template_variables ); |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub _process_data { |
79
|
0
|
|
|
0
|
|
|
my $data = shift; |
80
|
0
|
|
|
|
|
|
my $json_formatter = JSON->new->utf8->allow_blessed(1)->convert_blessed(1); |
81
|
0
|
|
|
0
|
|
|
local *PDL::TO_JSON = sub { $_[0]->unpdl }; |
|
0
|
|
|
|
|
|
|
82
|
0
|
|
|
|
|
|
my $data_string = $json_formatter->encode($data); |
83
|
0
|
|
|
|
|
|
return $data_string; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
my $poc = ' |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
// set up some data |
89
|
|
|
|
|
|
|
var M = 100; |
90
|
|
|
|
|
|
|
var xx = []; |
91
|
|
|
|
|
|
|
var yy = []; |
92
|
|
|
|
|
|
|
var colors = []; |
93
|
|
|
|
|
|
|
var radii = []; |
94
|
|
|
|
|
|
|
for (var y = 0; y <= M; y += 4) \{ |
95
|
|
|
|
|
|
|
for (var x = 0; x <= M; x += 4) \{ |
96
|
|
|
|
|
|
|
xx.push(x); |
97
|
|
|
|
|
|
|
yy.push(y); |
98
|
|
|
|
|
|
|
colors.push(Bokeh.Plotting.color(50+2*x, 30+2*y, 150)); |
99
|
|
|
|
|
|
|
radii.push(Math.random() * 0.4 + 1.7) |
100
|
|
|
|
|
|
|
\} |
101
|
|
|
|
|
|
|
\} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
// create a data source |
104
|
|
|
|
|
|
|
var source = new Bokeh.ColumnDataSource(\{ |
105
|
|
|
|
|
|
|
data: \{ x: xx, y: yy, radius: radii, colors: colors \} |
106
|
|
|
|
|
|
|
\}); |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
// make the plot and add some tools |
109
|
|
|
|
|
|
|
var tools = "pan,crosshair,wheel_zoom,box_zoom,reset,save"; |
110
|
|
|
|
|
|
|
var p = Bokeh.Plotting.figure(\{ title: "Colorful Scatter", tools: tools \}); |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
// call the circle glyph method to add some circle glyphs |
113
|
|
|
|
|
|
|
var circles = p.circle(\{ field: "x" \}, \{ field: "y" \}, \{ |
114
|
|
|
|
|
|
|
source: source, |
115
|
|
|
|
|
|
|
radius: radii, |
116
|
|
|
|
|
|
|
fill_color: colors, |
117
|
|
|
|
|
|
|
fill_alpha: 0.6, |
118
|
|
|
|
|
|
|
line_color: null |
119
|
|
|
|
|
|
|
\}); |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
// show the plot |
122
|
|
|
|
|
|
|
Bokeh.Plotting.show(p); |
123
|
|
|
|
|
|
|
'; |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
sub show_plot { |
126
|
0
|
|
|
0
|
1
|
|
my @data_to_plot = @_; |
127
|
|
|
|
|
|
|
|
128
|
0
|
|
|
|
|
|
my $rendered_cells = ""; |
129
|
0
|
|
|
|
|
|
my $numeric_id = 0; |
130
|
0
|
|
|
|
|
|
for my $data (@data_to_plot) { |
131
|
0
|
|
|
|
|
|
my $id = 'chart_' . $numeric_id++; |
132
|
0
|
0
|
0
|
|
|
|
if ( Ref::Util::is_blessed_ref($data) && $data->isa('Chart::Bokeh::Plot') ) { |
133
|
0
|
|
|
|
|
|
$rendered_cells .= $data->html( div_id => $id ); |
134
|
|
|
|
|
|
|
} else { |
135
|
0
|
|
|
|
|
|
$rendered_cells .= _render_cell( _process_data($data), $id ); |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
} |
138
|
0
|
|
|
|
|
|
my $plot = _render_html_wrap($rendered_cells); |
139
|
0
|
|
|
|
|
|
HTML::Show::show($plot); |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
1; |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
__END__ |