| 1 | package Chart::Bokeh; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use vars qw(@EXPORT_OK); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | @EXPORT_OK = qw(show_plot); | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use JSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Params::Validate qw(:all); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Text::Template; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use Module::Load; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use Ref::Util; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use HTML::Show; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 1 | our $VERSION = '0.001'; # VERSION | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | # ABSTRACT: Generate html/javascript charts from perl data using javascript library BokehJS | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 0 | ▶ | sub render_full_html { | ||||||||||||||||||||||||||
|
Subroutine
render_full_html: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 23 | 0 | my %params = @_; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 0 | my $data = $params{'data'}; | |||||||||||||||||||||||||||
| 26 | 0 | my $chart_id = 'bokeh_graph'; | |||||||||||||||||||||||||||
| 27 | 0 | my $html; | |||||||||||||||||||||||||||
| 28 | 0 | ▶ | if ( Ref::Util::is_blessed_ref($data) && $data->isa('Chart::Bokeh::Plot') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
Ref::Util::XS::is_blessed_ref($data) and $data->isa("Chart::Bokeh::Plot")
MC/DC0%0/2
Ref::Util::XS::is_blessed_ref($data) and $data->isa("Chart::Bokeh::Plot")
Ref::Util::XS::is_blessed_ref($data)
$data->isa("Chart::Bokeh::Plot")
Truth table0%0/3
A and B
A= Ref::Util::XS::is_blessed_ref($data)B= $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 | 0 | ▶ | sub _render_html_wrap { | ||||||||||||||||||||||||||
|
Subroutine
_render_html_wrap: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 37 | 0 | my $body = shift; | |||||||||||||||||||||||||||
| 38 | 0 | my $html_begin = <<'HTML_BEGIN'; | |||||||||||||||||||||||||||
| 39 | <html> | ||||||||||||||||||||||||||||
| 40 | <head> | ||||||||||||||||||||||||||||
| 41 | <link rel="stylesheet" type="text/css" href="http://cdn.pydata.org/bokeh/release/bokeh-0.12.3.min.css"/> | ||||||||||||||||||||||||||||
| 42 | </head> | ||||||||||||||||||||||||||||
| 43 | <body> | ||||||||||||||||||||||||||||
| 44 | HTML_BEGIN | ||||||||||||||||||||||||||||
| 45 | 0 | my $html_end = <<'HTML_END'; | |||||||||||||||||||||||||||
| 46 | </body> | ||||||||||||||||||||||||||||
| 47 | </html> | ||||||||||||||||||||||||||||
| 48 | HTML_END | ||||||||||||||||||||||||||||
| 49 | 0 | return $html_begin . $body . $html_end; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 0 | ▶ | sub _render_cell { | ||||||||||||||||||||||||||
|
Subroutine
_render_cell: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 53 | 0 | my $data_string = shift(); | |||||||||||||||||||||||||||
| 54 | 0 | my $chart_id = shift(); | |||||||||||||||||||||||||||
| 55 | 0 | my $template = <<'TEMPLATE'; | |||||||||||||||||||||||||||
| 56 | <div id="{$chart_id}"></div> | ||||||||||||||||||||||||||||
| 57 | <script src="http://cdn.pydata.org/bokeh/release/bokeh-0.12.3.min.js"></script> | ||||||||||||||||||||||||||||
| 58 | <script src="http://cdn.pydata.org/bokeh/release/bokeh-api-0.12.3.min.js"></script> | ||||||||||||||||||||||||||||
| 59 | <script> | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | var source = new Bokeh.ColumnDataSource(\{ | ||||||||||||||||||||||||||||
| 62 | data: {$data} | ||||||||||||||||||||||||||||
| 63 | \}); | ||||||||||||||||||||||||||||
| 64 | var tools = "pan,crosshair,wheel_zoom,box_zoom,reset,save"; | ||||||||||||||||||||||||||||
| 65 | var p = Bokeh.Plotting.figure(\{ title: "", tools: tools \}); | ||||||||||||||||||||||||||||
| 66 | const line = new Bokeh.Line(\{x: \{field: "x"\}, y: \{field: "y"\}, line_color: "#666699", line_width: 2\}); | ||||||||||||||||||||||||||||
| 67 | p.add_glyph(line, source); | ||||||||||||||||||||||||||||
| 68 | Bokeh.Plotting.show(p); | ||||||||||||||||||||||||||||
| 69 | </script> | ||||||||||||||||||||||||||||
| 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 | 0 | ▶ | sub _process_data { | ||||||||||||||||||||||||||
|
Subroutine
_process_data: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 79 | 0 | my $data = shift; | |||||||||||||||||||||||||||
| 80 | 0 | my $json_formatter = JSON->new->utf8->allow_blessed(1)->convert_blessed(1); | |||||||||||||||||||||||||||
| 81 | 0 | ▶ | local *PDL::TO_JSON = sub { $_[0]->unpdl }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 82 | 0 | my $data_string = $json_formatter->encode($data); | |||||||||||||||||||||||||||
| 83 | 0 | return $data_string; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 1 | 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 | 0 | ▶ | sub show_plot { | ||||||||||||||||||||||||||
|
Subroutine
show_plot: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 126 | 0 | 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 | ▶ | if ( Ref::Util::is_blessed_ref($data) && $data->isa('Chart::Bokeh::Plot') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
Ref::Util::XS::is_blessed_ref($data) and $data->isa("Chart::Bokeh::Plot")
MC/DC0%0/2
Ref::Util::XS::is_blessed_ref($data) and $data->isa("Chart::Bokeh::Plot")
Ref::Util::XS::is_blessed_ref($data)
$data->isa("Chart::Bokeh::Plot")
Truth table0%0/3
A and B
A= Ref::Util::XS::is_blessed_ref($data)B= $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 | 1; | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | __END__ | ||||||||||||||||||||||||||||