App/Chart/Gtk2/Graph/Plugin/Text.pm untested

filter: Statementstmt 0.0% 0 / 173 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 287 CC 18 SCAR 31.0
CC
18
Cov
0%
dividend_format
56.0
draw
42.0
dividend_qualifier_str
20.0
CRAP 22.2
1 # Copyright 2007, 2008, 2009, 2010, 2011 Kevin Ryde
2  
3 # This file is part of Chart.
4 #
5 # Chart is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3, or (at your option) any later version.
8 #
9 # Chart is distributed in the hope that it will be useful, but WITHOUT ANY
10 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 # details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with Chart. If not, see <http://www.gnu.org/licenses/>.
16  
17 package App::Chart::Gtk2::Graph::Plugin::Text;
18 use 5.010;
19 use strict;
20 use warnings;
21 use Gtk2;
22 use List::MoreUtils;
23 use List::Util qw(min max);
24 use Math::Round qw(round);
25  
26 use base 'App::Chart::Gtk2::Graph::Plugin';
27 use App::Chart;
28 use Gtk2::Ex::Units;
29 use Locale::TextDomain ('App-Chart');
30  
31 use constant {
32 X_MARGIN_EMS => 0.7,
33 Y_MARGIN_LINES => 0.2,
34 };
35  
36 sub draw {
37 my ($class, $graph, $region) = @_;
38  
39 my $series_list = $graph->{'series_list'};
40 my $series = $series_list->[0] || return;
41  
42 my $win = $graph->window;
43 my ($win_width, $win_height) = $win->get_size;
44 my $t_lo = $graph->x_to_date (-0.5 * $win_width);
45 my $t_hi = $graph->x_to_date (1.5 * $win_width);
46  
47 my @array;
48 {
49 my $dividend_list = $series->dividends;
50 foreach my $dividend (@$dividend_list) {
51 my $t = $dividend->{'ex_date_t'};
52 if ($t < $t_lo || $t > $t_hi) { next; }
53 push @array, [$t, dividend_format ($dividend)];
54 }
55 }
56 {
57 my $split_list = $series->splits;
58 foreach my $split (@$split_list) {
59 my $t = $split->{'date_t'};
60 if ($t < $t_lo || $t > $t_hi) { next; }
61 push @array, [$t, split_format ($split)];
62 }
63 }
64 {
65 my $annotation_list = $series->annotations;
66 foreach my $annotation (@$annotation_list) {
67 my $t = $annotation->{'date_t'};
68 if ($t < $t_lo || $t > $t_hi) { next; }
69 push @array, [$t, annotation_format ($annotation)];
70 }
71 }
72 if (! @array) { return; }
73  
74 my $cliprect = $region->get_clipbox;
75 my $x_step = $graph->scale_x_step;
76 my $scale_x = $graph->scale_x_proc;
77 my $style = $graph->style;
78 my $state = $graph->state;
79 my $gc = $style->text_gc ($state);
80 my $layout = ($graph->{'layout'} ||= $graph->create_pango_layout(''));
81 my $em = Gtk2::Ex::Units::em($layout);
82 my $line_height = Gtk2::Ex::Units::line_height($layout);
83 my $x_margin = round (X_MARGIN_EMS * $em);
84 my $y_margin = round (Y_MARGIN_LINES * $line_height);
85 my $y_top = round (1.5 * $line_height);
86 my $y_str = $y_top + $y_margin;
87  
88 foreach my $elem (@array) {
89 my ($t, $str) = @$elem;
90 my $x_centre = $scale_x->($t) + $x_step / 2; # centre of column
91 $layout->set_text ($str);
92 my ($str_width, $str_height) = $layout->get_pixel_size;
93 my $x_str = $x_centre - $str_width / 2;
94 my $x_left = $x_str - $x_margin;
95 my $width = $str_width + 2 * $x_margin;
96 my $height = $str_height + 2 * $y_margin;
97  
98 if ($region->rect_in (Gtk2::Gdk::Rectangle->new
99 ($x_left, $width, $y_top, $height)) eq 'out') {
100 next;
101 }
102  
103 $style->paint_layout ($win, # window
104 $state,
105 1, # use_text, for the text gc instead of the fg one
106 $cliprect,
107 $graph, # widget
108 __PACKAGE__, # style detail string
109 $x_str,
110 $y_str,
111 $layout);
112  
113 $win->draw_rectangle ($gc, 0, $x_left, $y_top, $width, $height);
114 $win->draw_line ($gc,
115 $x_centre, $y_top + $height,
116 $x_centre, $y_top + $height + $line_height);
117 }
118 }
119  
120 sub dividend_format {
121 my ($dividend) = @_;
122 return ($dividend->{__PACKAGE__.'.formatted'} ||= do {
123 ### $dividend
124  
125 my @parts;
126 if (defined $dividend->{'amount'}) {
127 push @parts, $dividend->{'amount'};
128 }
129 if (defined $dividend->{'imputation'}) {
130 push @parts, '+ ' . $dividend->{'imputation'};
131 }
132 if (my $type = $dividend->{'type'}) {
133 push @parts, $type;
134 }
135 if (my $qualifier = $dividend->{'qualifier'}) {
136 push @parts, dividend_qualifier_str ($qualifier);
137 }
138 if (! @parts) {
139 my $note = $dividend->{'note'};
140 if (defined $note && $note ne '') {
141 push @parts, $note;
142 }
143 }
144 ### @parts
145  
146 join ' ',@parts
147 });
148 }
149  
150 sub dividend_qualifier_str {
151 my ($qualifier) = @_;
152 if ($qualifier eq 'TBA') { return __('T.B.A.'); }
153 if ($qualifier eq 'estimated') { return __('Est'); }
154 if ($qualifier eq 'unknown') { return __('Unknown'); }
155 return $qualifier;
156 }
157  
158 sub split_format {
159 my ($split) = @_;
160 return ($split->{__PACKAGE__.'.formatted'} ||= do {
161 ($split->{'note'}
162 ? __x('{newnum} for {oldnum}, {note}',
163 newnum => $split->{'new'},
164 oldnum => $split->{'old'},
165 note => $split->{'note'})
166 : __x('{newnum} for {oldnum}',
167 newnum => $split->{'new'},
168 oldnum => $split->{'old'}))
169 });
170 }
171  
172 sub annotation_format {
173 my ($annotation) = @_;
174 return ($annotation->{__PACKAGE__.'.formatted'} ||= $annotation->{'note'});
175 }
176  
177 sub elem_sizes {
178 my ($elem_list, $graph) = @_;
179 my $layout = ($graph->{'layout'} ||= $graph->create_pango_layout(''));
180 my $em = Gtk2::Ex::Units::em ($layout);
181 my $line_height = Gtk2::Ex::Units::line_height($layout);
182 my $x_margin = round (X_MARGIN_EMS * $em);
183 my $y_margin = round (Y_MARGIN_LINES * $line_height);
184  
185 foreach my $elem (@$elem_list) {
186 $layout->set_text ($elem->{__PACKAGE__.'.formatted'});
187 my ($str_width, $str_height) = $layout->get_pixel_size;
188 my $x_str = $elem->{'x_str'} = - $str_width / 2;
189 $elem->{'x_left'} = $x_str - $x_margin;
190 $elem->{'width'} = $str_width + 2 * $x_margin;
191 $elem->{'height'} = $str_height + 2 * $y_margin;
192 }
193 }
194  
195 sub hrange {
196 my ($class, $graph, $series_list) = @_;
197 ### Graph-Text hrange on: "$graph $series_list"
198 $graph || return;
199 my $series = $series_list->[0] || return;
200  
201 my @ret;
202 {
203 my $dividend_list = $series->dividends;
204 foreach my $dividend (@$dividend_list) {
205 dividend_format ($dividend);
206 }
207 push @ret, @$dividend_list;
208 }
209 {
210 my $split_list = $series->splits;
211 foreach my $split (@$split_list) {
212 split_format ($split);
213 }
214 push @ret, @$split_list;
215 }
216 {
217 my $annotation_list = $series->annotations;
218 foreach my $annotation (@$annotation_list) {
219 annotation_format ($annotation);
220 }
221 push @ret, @$annotation_list;
222 }
223 elem_sizes (\@ret, $graph);
224  
225 my $x_step = $graph->scale_x_step;
226 @ret = map { my $elem = $_;
227 my $t = $elem->{'date_t'};
228 if (! defined $t) { $t = $elem->{'ex_date_t'}; }
229 my $t_extra = int ($elem->{'width'} / 2 / $x_step) + 1;
230 ($t - $t_extra, $t + $t_extra) } @ret;
231 ### Text range: join(' ',@ret)
232 if (@ret) {
233 return List::MoreUtils::minmax (@ret);
234 } else {
235 return ();
236 }
237 }
238  
239 1;
240 __END__