Chart/GGPlot/Backend/Plotly/Geom/Text.pm untested

filter: Statementstmt 0.0% 0 / 63 Branchbran 0.0% 0 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod n/a totaltotal 0.0% 0 / 80 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package Chart::GGPlot::Backend::Plotly::Geom::Text;
2  
3 # ABSTRACT: Chart::GGPlot's Plotly implementation for Geom::Text
4  
5 use Chart::GGPlot::Class;
6  
7 our $VERSION = '0.002003'; # VERSION
8  
9 extends qw(Chart::GGPlot::Backend::Plotly::Geom::Path);
10  
11 use Chart::Plotly::Trace::Scatter::Textfont;
12 use List::AllUtils qw(pairwise);
13 use Module::Load;
14 use PDL::Primitive qw(which);
15  
16 use Chart::GGPlot::Util qw(mm_to_pt);
17 use Chart::GGPlot::Backend::Plotly::Util
18 qw(group_to_NA to_rgb pdl_to_plotly);
19  
20 classmethod to_traces( $df, $params, $plot ) {
21 $df = group_to_NA($df);
22  
23 my $use_webgl = $class->use_webgl($df);
24 my $plotly_trace_class =
25 $use_webgl
26 ? 'Chart::Plotly::Trace::Scattergl'
27 : 'Chart::Plotly::Trace::Scatter';
28  
29 if ( $log->is_debug ) {
30 $log->debug( $use_webgl ? "to use webgl" : "not to use webgl" );
31 }
32  
33 load $plotly_trace_class;
34  
35 my ( $x, $y, $label, $hjust, $vjust ) =
36 map { $df->at($_) } qw(x y label hjust vjust);
37 my $textfont = Chart::Plotly::Trace::Scatter::Textfont->new(
38 color => pdl_to_plotly( to_rgb( $df->at('color'), $df->at('alpha') ) ),
39 size => pdl_to_plotly( mm_to_pt( $df->at('size') ) ),
40 family => pdl_to_plotly( $df->at('family') ),
41 );
42 my @computed_hjust = $class->_compute_hjust($hjust)->list;
43 my @computed_vjust = $class->_compute_vjust($vjust)->list;
44 my $textposition = pairwise { "$a $b" } @computed_vjust, @computed_hjust;
45 my $trace = $plotly_trace_class->new(
46 x => $x,
47 y => $y,
48 text => pdl_to_plotly($label),
49 mode => 'text',
50 textfont => $textfont,
51 textposition => $textposition,
52 hovertext => pdl_to_plotly( $df->at('hovertext') ),
53 hoverinfo => 'text',
54 hoveron => $class->hover_on,
55 );
56 return [$trace];
57 }
58  
59 sub _compute_hjust {
60 my ( $class, $just ) = @_;
61  
62 if ( $just->$_DOES('PDL::SV') ) {
63  
64 # Rename middle to center, because plotly needs left/center/right
65 # Btw this is actually different from ggplot2 which is
66 # left/middle/right and bottom/center/top..
67 my $new_just = $just->copy;
68 $new_just->slice( which( $just == 'middle' ) ) .= 'center';
69 return $just->copy;
70 }
71 else {
72 my $new_just = PDL::SV->new( [ ("center") x $just->length ] );
73 $new_just->slice( which( $just <= 0.25 ) ) .= 'right';
74 $new_just->slice( which( $just >= 0.75 ) ) .= 'left';
75 return $new_just;
76 }
77 }
78  
79 sub _compute_vjust {
80 my ( $class, $just ) = @_;
81  
82 if ( $just->$_DOES('PDL::SV') ) {
83  
84 # Rename center to middle, because plotly needs bottom/middle/top
85 my $new_just = $just->copy;
86 $new_just->slice( which( $just == 'center' ) ) .= 'middle';
87 return $new_just;
88 }
89 else {
90 my $new_just = PDL::SV->new( [ ('middle') x $just->length ] );
91 $new_just->slice( which( $just <= 0.25 ) ) .= 'top';
92 $new_just->slice( which( $just >= 0.75 ) ) .= 'bottom';
93 return $new_just;
94 }
95 }
96  
97 __PACKAGE__->meta->make_immutable;
98  
99 1;
100  
101 __END__