lib/Text/HistogramChart.pm

filter: Statementstmt 93.0% 173 / 186 Branchbran 68.3% 82 / 120 Conditioncond 48.1% 13 / 27 MC/DCmcdc 33.3% 6 / 18 Subroutinesub 100% 11 / 11 Podpod 100% 3 / 3 totaltotal 78.9% 288 / 365 CC 75 SCAR 49.9
CC
75
Cov
80%
chart
174.6
center_text
5.0
BEGIN
1.0
CRAP 146.2
↑ Summary
1 package Text::HistogramChart;
2
3 ## no critic (Subroutines::RequireArgUnpacking)
4 ## no critic (RequirePodAtEnd)
5 # By using =encoding utf8 this module would require perl 5.10. No need for that!
6 ## no critic (Documentation::RequirePODUseEncodingUTF8 )
7
8 2 ▶use 5.008_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11
12
13 =head1 NAME
14
15 Text::HistogramChart - Make Text Histogram (Upright Bars) Charts
16
17 =head1 VERSION
18
19 Version 0.005
20
21 =cut
22
23 #use version 0.77 (); our $VERSION = 0.003; # Require version 0.77 of module "version". Even for Perl v.5.10.0, get latest bug-fixes and API
24 2 our $VERSION = 0.005;
25
26
27 =head1 SYNOPSIS
28
29 Text::HistogramChart creates graphical charts for terminal displays or any other display device where bitmap graphics is not available!
30 You can supply the Y axel legend (vertical) values or Text::HistogramChart can calculate them from the input values.
31
32 require Text::HistogramChart;
33
34 my $chart = Text::HistogramChart->new();
35
36 @values = (1, 2, 3, 4, 5, 4, 3, 2, 1, 0, -1, -2, -3, -4, -5, -4, -3, -2, -1, 0);
37 @legend_values = (4, 3, 2, 1, 0, -1, -2, -3, -4);
38 $chart->{'values'} = \@values;
39 $chart->{'legend_values'} = \@legend_values;
40 $chart->{'screen_height'} = 9; # (height reserved for the graph.)
41 $chart->{'roof_value'} = 0; # (active if != 0), # Arbitrarily squeeze or extend the size (height) of bars (not screen)
42 $chart->{'floor_value'} = 0; # (the "floor" of the chart, default: 0)
43 $chart->{'write_floor'} = 1; # (make floor visible)
44 $chart->{'use_floor'} = 1; # (use the floor value)
45 $chart->{'write_floor_value'} = 1; # If value == floor_value, then write value (mostly "0").
46 $chart->{'write_legend'} = 1; # (Prepend legend to each row.)
47 $chart->{'legend_horizontal_width'} = 4; # width of the space left for legend (left edge of chart)
48 $chart->{'horizontal_width'} = 2; # Horizontal width of one bar. This parameter directly influences the width of the screen (i.e. chart).
49 $chart->{'write_value'} = 1; # (YES = 1, NO = 0, default: no; write the value on the end of the bar),
50 $chart->{'write_always_over_value'} = 0; # (YES = 1, NO = 0, default: yes; write the value only if it is too high for the graph),
51 $chart->{'write_always_under_value'} = 0; # (YES = 1, NO = 0, default: yes; write the value only if it is too low for the graph),
52 $chart->{'bar_char'} = '|'; # (default: '|')
53 $chart->{'floor_char'} = '-'; # (default '-' )
54 $chart->{'over_value_char'} = '+'; # (default: '+')
55 $chart->{'under_value_char'} = '_'; # (default: '-' )
56 $rval = $chart->chart();
57 if($rval >= 1) {
58 my @ready_chart = @{$chart->{'screen'}};
59 print (join '\n', @ready_chart) . "\n";
60 } else {
61 print "Error in creating chart: " . $chart->error_string . "\n";
62 }
63
64 # Result:
65 # 4 4 5 4
66 # 3 3 | | | 3
67 # 2 2 | | | | | 2
68 # 1 1 | | | | | | | 1
69 # 0 ------------------0 ------------------0
70 # -1 -1| | | | | | | -1
71 # -2 -2| | | | | -2
72 # -3 -3| | | -3
73 # -4 -4-5-4
74
75
76 =head1 DESCRIPTION
77
78 Text::HistogramChart creates graphical charts for terminal displays or any other display device where bitmap graphics is not available or desired!
79 You can supply the Y axel legend (vertical) values or Text::HistogramChart can calculate them from the input values.
80
81
82 =head1 USAGE
83
84 The following variables are available to fine tune the chart
85 (see SYNOPSIS for an example of usage):
86
87 =over 4
88
89 =item B<screen_height>, B<horizontal_width>, B<legend_horizontal_width>
90
91 The 'screen' is the area in which the chart is drawn. The size is defined with three variables.
92 B<screen_height> is the absolute Y-axis height in character rows.
93 B<horizontal_width> is the number of characters used for one bar (one value). If you have 10 values and
94 B<horizontal_width> is 3, then the length of the screen (the X-axis) is 10 * 3 = 30 characters (without legend).
95 Use B<legend_horizontal_width> to define the legend width. The default for both is 5 characters.
96 Screen height defaults to 10 characters.
97
98 =item B<write_legend>
99 Set this to 1 if you want the legend values prepended to the left edge of the chart.
100
101 =item B<write_floor_value>, B<write_value>, B<write_always_over_value>, B<write_always_under_value>
102
103 Set B<write_value> to 1 if you want the value of each bar written to the top (or bottom if the value is negative).
104 Set B<write_always_over_value> and B<write_always_under_value> to 1 if you only want want the value written when
105 the value is greater than the maximum given legend value (or less than the minimum).
106 Set B<write_floor_value> to 1 if you want the value written when it equals to 'floor', normally when the value is 0.
107
108 =item B<bar_char>, B<floor_char>, B<over_value_char>, B<under_value_char>
109
110 These variables define the characters used for writing the chart
111 B<over_value_char> and B<under_value_char> are used when the value is off the scale (too big or too small).
112 B<floor_char> is the horizontal line usually at 0.
113 B<bar_char> is the normal 'bar'.
114
115 Any of these charaters can be more than one character is size.
116 If you want "fatter" vertical bars, just set bar_char to '||'.
117 Remember to set the other values to double digits as well.
118
119 =item B<roof_value>, B<bottom_value>, B<floor_value>, B<write_floor>, B<use_floor>
120
121 With B<roof_value> and B<bottom_value> you can restrict the chart into a certain 'height'.
122 E.g. you are measuring the CPU performance of a server. The performance is usually between 70% and 95% of total capacity.
123 To show the occasional drops to 0%-70% is a waste of (terminal) space. So you set
124 B<roof_value> to 95 and B<bottom_value> to 70.
125 This feature not yet implemented.
126 Set B<write_floor> to 1 if you want a horizontal bar across the screen at 0.
127
128
129 =back
130
131
132 =head1 DEPENDENCIES
133
134 Requires Perl version 5.008.001.
135
136 Requires the following modules:
137
138 =over 4
139
140 =item Hash::Util
141
142 =back
143
144
145 =cut
146
147 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
148 2 ▶use Hash::Util qw{lock_keys unlock_keys};
Subroutine BEGIN: called CC=1 SCAR=0.0
149
150 # Global creator
151 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
152 2 ▶ use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
153 2 our (@ISA, @EXPORT_OK, %EXPORT_TAGS);
154
155 2 @ISA = qw(Exporter DynaLoader);
156 2 %EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ],
157 2 @EXPORT_OK = qw();
158 }
159 2 our @EXPORT_OK;
160
161 # Global destructor
162 1 ▶END {
Subroutine END: called CC=1 SCAR=0.0
163 }
164
165 # CONSTANTS for this module
166 2 my $TRUE = 1;
167 2 my $FALSE = 0;
168 2 my $EMPTY_STR = q{};
169 2 my @EMPTY_ARRAY = (); ## no critic (ProhibitUselessInitialization)
170 2 my $SPACE = q{ };
171 2 my $HALF_ROW = 0.5; ## no critic (ValuesAndExpressions::ProhibitMagicNumbers)
172
173 # DEFAULTS
174 2 my $DEFAULT_SCREEN_HEIGHT = 10; ## no critic (ProhibitMagicNumbers)
175 2 my $DEFAULT_ROOF_VALUE = 0;
176 2 my $DEFAULT_BOTTOM_VALUE = 0;
177 2 my $DEFAULT_FLOOR_VALUE = 0;
178 2 my $DEFAULT_WRITE_FLOOR = 0; # FALSE
179 2 my $DEFAULT_USE_FLOOR = 0; # FALSE
180 2 my $DEFAULT_WRITE_FLOOR_VALUE = 0; # FALSE
181 2 my $DEFAULT_WRITE_LEGEND = 0;
182 2 my $DEFAULT_LEGEND_HORIZONTAL_WIDTH = 5; ## no critic (ProhibitMagicNumbers)
183 2 my $DEFAULT_HORIZONTAL_WIDTH = 5; ## no critic (ProhibitMagicNumbers)
184 2 my $DEFAULT_WRITE_VALUE = 0; # FALSE
185 2 my $DEFAULT_WRITE_ALWAYS_OVER_VALUE = 0; # FALSE
186 2 my $DEFAULT_WRITE_ALWAYS_UNDER_VALUE = 0; # FALSE
187
188 2 my $DEFAULT_BAR_CHAR = q{|};
189 2 my $DEFAULT_FLOOR_CHAR = q{-};
190 2 my $DEFAULT_OVER_VALUE_CHAR = q{+};
191 2 my $DEFAULT_UNDER_VALUE_CHAR = q{-};
192 2 my $DEFAULT_LEGEND_VALUES = \@EMPTY_ARRAY;
193 2 my $DEFAULT_VALUES = \@EMPTY_ARRAY;
194 2 my $DEFAULT_SCREEN = q{};
195 2 my $DEFAULT_ERROR_STRING = q{};
196
197 # GLOBALS
198 # No global variables
199
200
201
202 =head1 EXPORT
203
204 Text::HistogramChart is a purely object-oriented module.
205 No exported functions.
206
207
208 =head1 SUBROUTINES/METHODS
209
210 =head2 new
211
212 Creator function.
213
214 =cut
215
216 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
217 1 my $class = shift;
218 1 my $self;
219 1 my @self_keys = (
220 'screen_height', # (height reserved for the chart.)
221 'roof_value', # (active if != 0), # Arbitrarily squeeze or extend the size (height) of bars (not screen)
222 'bottom_value', # (below floor, active if != 0), # Not yet implemented.
223 'floor_value', # (the "floor" of the chart, default: 0)
224 'write_floor', # (make floor visible)
225 'use_floor', # (use the floor value)
226 'write_floor_value', # If value == floor_value, then write value (mostly "0").
227 'write_legend', # (Prepend legend to each row.)
228 'legend_horizontal_width', # width of the space left for legend (left edge of chart)
229 'horizontal_width', # Horizontal width of one bar. This parameter directly influences the width of the screen (i.e. chart).
230 'write_value', # (YES = 1, NO = 0, default: no; write the value on the end of the bar),
231 'write_always_over_value', # (YES = 1, NO = 0, default: yes; write the value only if it is too high for the graph),
232 'write_always_under_value', # (YES = 1, NO = 0, default: yes; write the value only if it is too low for the graph),
233 'bar_char', # (default: '|')
234 'floor_char', # (default: '-' )
235 'over_value_char', # (default: '+'; overruled by write_value and write_always_over_value)
236 'under_value_char', # (default: '-'; overruled by write_value and write_always_under_value)
237 'legend_values', # array of legend values (numbers). Pointer to.
238 'values', # array of values (numbers). Pointer to.
239 'screen', # The result: array of strings. Pointer to.
240 'error_string', # A meaningful error!
241 );
242 1 lock_keys(%{$self}, @self_keys);
243 1 $self->{'screen_height'} = $DEFAULT_SCREEN_HEIGHT;
244 1 $self->{'roof_value'} = $DEFAULT_ROOF_VALUE;
245 1 $self->{'bottom_value'} = $DEFAULT_BOTTOM_VALUE;
246 1 $self->{'floor_value'} = $DEFAULT_FLOOR_VALUE;
247 1 $self->{'write_floor'} = $DEFAULT_WRITE_FLOOR;
248 1 $self->{'use_floor'} = $DEFAULT_USE_FLOOR;
249 1 $self->{'write_floor_value'} = $DEFAULT_WRITE_FLOOR_VALUE;
250 1 $self->{'write_legend'} = $DEFAULT_WRITE_LEGEND;
251 1 $self->{'legend_horizontal_width'} = $DEFAULT_LEGEND_HORIZONTAL_WIDTH;
252 1 $self->{'horizontal_width'} = $DEFAULT_HORIZONTAL_WIDTH;
253 1 $self->{'write_value'} = $DEFAULT_WRITE_VALUE;
254 1 $self->{'write_always_over_value'} = $DEFAULT_WRITE_ALWAYS_OVER_VALUE;
255 1 $self->{'write_always_under_value'} = $DEFAULT_WRITE_ALWAYS_UNDER_VALUE;
256
257 1 $self->{'bar_char'} = $DEFAULT_BAR_CHAR;
258 1 $self->{'floor_char'} = $DEFAULT_FLOOR_CHAR;
259 1 $self->{'over_value_char'} = $DEFAULT_OVER_VALUE_CHAR;
260 1 $self->{'under_value_char'} = $DEFAULT_UNDER_VALUE_CHAR;
261 1 $self->{'legend_values'} = $DEFAULT_LEGEND_VALUES;
262 1 $self->{'values'} = $DEFAULT_VALUES;
263 1 $self->{'screen'} = $DEFAULT_SCREEN;
264 1 $self->{'error_string'} = $DEFAULT_ERROR_STRING;
265
266 1 unlock_keys(%{$self});
267 1 my $blessed_ref = bless $self, $class;
268 1 lock_keys(%{$self}, @self_keys);
269 1 return $blessed_ref;
270 }
271
272 =head2 chart
273
274 Create the chart. Writes the ready chart into $self->{'screen'}.
275 The ready chart is an array of strings without linefeed.
276 Returns >= 1, if successful, else $self->{'error_string'} contains the error.
277
278 =cut
279
280 7 ▶sub chart {
Subroutine chart: called CC=71 SCAR=51.6
Pod documented
281 7 my $return_value = 1;
282
283 7 my $self = shift;
284
285 7 my @values = @{$self->{'values'}};
286 7 my @legend_values;
287 7 my $horizontal_width_empty = $EMPTY_STR;
288 7 ▶ while(length $horizontal_width_empty < $self->{'horizontal_width'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $horizontal_width_empty < $self->{"horizontal_width"}) 27 7 34
289 27 $horizontal_width_empty .= $SPACE;
290 }
291
292 7 my @output_rows;
293 # If user wants, write only the legend.
294 # Always create the legend first so you know which rows have which values!
295 # If user gives the legend values (parameter LEGEND_VALUES)
296 # then all the better for you (no need to calculate the legend yourself).
297 # But only write the legend if user demands it (parameter WRITE_LEGEND).
298 # Even without writing the legend, the legend values define the distance between rows.
299 7 my $sprf_format = q{%-} . $self->{'legend_horizontal_width'} . q{s};
300 7 ▶ if(defined $self->{'legend_values'} && scalar @{$self->{'legend_values'}} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'legend_values'} and scalar @{$self->{'legend_values'};} > 0) { } 4 3 7
Condition66%2/3
defined $self->{"legend_values"} and scalar @{$self->{"legend_values"};} > 0
!ll&&!rl&&r
034
MC/DC50%1/2
defined $self->{"legend_values"} and scalar @{$self->{"legend_values"};} > 0
defined $self->{"legend_values"} scalar @{$self->{"legend_values"};} > 0
Truth table66%2/3
A and B
A= defined $self->{"legend_values"}B= scalar @{$self->{"legend_values"};} > 0
ABresult
0X0
100
111
301 4 @legend_values = @{$self->{'legend_values'}};
302 4 ▶ if(scalar(@legend_values) != $self->{'screen_height'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @legend_values != $self->{"screen_height"}) 0 4 4
303 0 $self->{'error_string'} = 'Screen height must be equal to the number of legend values!';
304 0 return 0;
305 }
306 4 @legend_values = sort {$a <=> $b} @legend_values; # Sort them to be sure
307 }
308 else {
309 3 my $highest_value = 0;
310 3 ▶ if($self->{'roof_value'} != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'roof_value'} != 0) { } 0 3 3
311 0 $highest_value = $self->{'roof_value'};
312 }
313 else {
314 3 foreach my $value (@values) {
315 43 ▶ if($value > $highest_value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value > $highest_value) 26 17 43
316 26 $highest_value = $value;
317 }
318 }
319 }
320 3 my $lowest_value = 0;
321 3 ▶ if($self->{'bottom_value'} != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'bottom_value'} != 0) { } 0 3 3
322 0 $lowest_value = $self->{'bottom_value'};
323 }
324 else {
325 3 foreach my $value (@values) {
326 43 ▶ if($value < $lowest_value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value < $lowest_value) 0 43 43
327 0 $lowest_value = $value;
328 }
329 }
330 }
331 3 my $rows_for_one = $self->{'screen_height'} / $highest_value;
332 3 my $amount_per_row = $highest_value / $self->{'screen_height'};
333 #my $rows_for_one = $self->{'screen_height'} / ($highest_value - $lowest_value + 1); //TODO
334 #my $amount_per_row = ($highest_value - $lowest_value + 1) / $self->{'screen_height'}; //TODO
335
336 # Make a legend based on lowest and highest value in @values
337 3 my $screen_top_row = $self->{'screen_height'} - 1;
338 3 my $screen_bottom_row = 0;
339 #my $screen_top_row = $highest_value - $lowest_value; //TODO
340 #my $screen_bottom_row = 0; //TODO
341 3 ▶ for(my $i_row = $screen_bottom_row; $i_row <= $screen_top_row; $i_row++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i_row <= $screen_top_row) 40 3 43
342 40 push @legend_values, (sprintf $sprf_format, int(($i_row + 1) * $amount_per_row + $HALF_ROW));
343 #push @legend_values, (sprintf $sprf_format, int(($i_row + $lowest_value) * $amount_per_row + 0.5)); //TODO
344 }
345 }
346 7 ▶ if($self->{'write_legend'} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"write_legend"} == 1) 6 1 7
347 6 ▶ for(my $i_row = $self->{'screen_height'} - 1; $i_row >= 0; $i_row--) { ## no critic (ControlStructures::ProhibitCStyleForLoops)
Branch: 1 branch
BranchTrueFalseTotal
if ($i_row >= 0) 74 6 80
348 74 $output_rows[$i_row] .= sprintf $sprf_format, int $legend_values[$i_row];
349 }
350 }
351
352 # Now the values
353 # We write one pillar at a time: one value = one pillar!
354 # So, we write from left to right, one pillar at a time!
355 # We write the pillar starting from the bottom.
356 7 my $screen_top_row = $self->{'screen_height'} - 1;
357 7 my $screen_bottom_row = 0;
358 7 my $screen_floor_row = $screen_bottom_row;
359 7 ▶ if($self->{'use_floor'} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"use_floor"} == 1) 3 4 7
360 3 for(0..@legend_values-1) {
361 34 ▶ if($legend_values[$_] == $self->{'floor_value'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($legend_values[$_] == $self->{"floor_value"}) 3 31 34
362 3 $screen_floor_row = $_;
363 }
364 }
365 }
366 7 foreach my $value (@values) {
367 108 ▶ for(my $i_row = $screen_bottom_row; $i_row <= $screen_top_row; $i_row++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i_row <= $screen_top_row) 1298 108 1406
368 1298 ▶ if($value != $self->{'floor_value'}) { # If value == 0, just write spaces.
Branch: 1 branch
BranchTrueFalseTotal
if ($value != $self->{'floor_value'}) { } 1194 104 1298
369 1194 ▶ if($i_row == $screen_bottom_row) { ## no critic (ControlStructures::ProhibitCascadingIfElse)
Branch: 5 branches
BranchTrueFalseTotal
if ($i_row == $screen_bottom_row) { } 99 1095 1194
elsif ($i_row < $screen_floor_row and $i_row > $screen_bottom_row) { } 203 892 1095
elsif ($i_row == $screen_floor_row) { } 47 845 892
elsif ($i_row > $screen_floor_row and $i_row < $screen_top_row) { } 746 99 845
elsif ($i_row == $screen_top_row) { } 99 0 99
370 99 ▶ if($i_row < $screen_floor_row) {
Branch: 2 branches
BranchTrueFalseTotal
if ($i_row < $screen_floor_row) { } 47 52 99
elsif ($i_row >= $screen_floor_row) { } 52 0 52
371 47 ▶ if($value > $legend_values[$i_row]) { # Write empty space
Branch: 2 branches
BranchTrueFalseTotal
if ($value > $legend_values[$i_row]) { } 43 4 47
elsif ($value <= $legend_values[$i_row]) { } 4 0 4
372 43 $output_rows[$i_row] .= $horizontal_width_empty;
373 }
374 elsif($value <= $legend_values[$i_row]) { # Write value
375 4 ▶ if(length($value) > $self->{'horizontal_width'}) { # Doesn't fit on the row.
Branch: 1 branch
BranchTrueFalseTotal
if (length $value > $self->{'horizontal_width'}) { } 0 4 4
376 $output_rows[$i_row] .= center_text(
377 $self->{'write_always_under_value'} ? $self->{'under_value_char'} : ($self->{'write_value'} ? $value : $self->{'bar_char'}),
378 0 ▶ $self->{'horizontal_width'}, $SPACE, 'right');
Branch: 2 branches
BranchTrueFalseTotal
$self->{'write_always_under_value'} ? : 0 0 0
$self->{'write_value'} ? : 0 0 0
379 }
380 else {
381 $output_rows[$i_row] .= center_text(
382 $self->{'write_always_under_value'} ? $value : ($self->{'write_value'} ? $value : $self->{'bar_char'}),
383 4 ▶ $self->{'horizontal_width'}, $SPACE, 'right');
Branch: 2 branches
BranchTrueFalseTotal
$self->{'write_always_under_value'} ? : 0 4 4
$self->{'write_value'} ? : 3 1 4
384 }
385 }
386 else {
387 }
388 }
389 elsif($i_row >= $screen_floor_row) {
390 52 ▶ if($value >= $legend_values[$i_row + 1]) { # Write bar char
Branch: 4 branches
BranchTrueFalseTotal
if ($value >= $legend_values[$i_row + 1]) { } 43 9 52
elsif ($value >= $legend_values[$i_row] and $value < $legend_values[$i_row + 1]) { } 7 2 9
elsif ($value < $legend_values[$i_row] and $value >= $self->{'floor_value'}) { } 2 0 2
elsif ($value < $legend_values[$i_row] and $value < $self->{'floor_value'}) { } 0 0 0
391 43 $output_rows[$i_row] .= center_text($self->{'bar_char'}, $self->{'horizontal_width'}, $SPACE, 'right');
392 }
393 ▶ elsif($value >= $legend_values[$i_row] && $value < $legend_values[$i_row + 1]) { # Write value
Condition66%2/3
$value >= $legend_values[$i_row] and $value < $legend_values[$i_row + 1]
!ll&&!rl&&r
207
MC/DC50%1/2
$value >= $legend_values[$i_row] and $value < $legend_values[$i_row + 1]
$value >= $legend_values[$i_row] $value < $legend_values[$i_row + 1]
Truth table66%2/3
A and B
A= $value >= $legend_values[$i_row]B= $value < $legend_values[$i_row + 1]
ABresult
0X0
100
111
394 7 ▶ $output_rows[$i_row] .= center_text($self->{'write_value'} ? $value : $self->{'bar_char'}, $self->{'horizontal_width'}, $SPACE, 'right');
Branch: 1 branch
BranchTrueFalseTotal
$self->{'write_value'} ? : 1 6 7
395 }
396 ▶ elsif($value < $legend_values[$i_row] && $value >= $self->{'floor_value'}) { # Write value maybe
Condition33%1/3
$value < $legend_values[$i_row] and $value >= $self->{"floor_value"}
!ll&&!rl&&r
002
MC/DC0%0/2
$value < $legend_values[$i_row] and $value >= $self->{"floor_value"}
$value < $legend_values[$i_row] $value >= $self->{"floor_value"}
Truth table33%1/3
A and B
A= $value < $legend_values[$i_row]B= $value >= $self->{"floor_value"}
ABresult
0X0
100
111
397 $output_rows[$i_row] .= center_text(
398 $self->{'write_always_under_value'} ? $value : '',
399 2 ▶ $self->{'horizontal_width'}, $SPACE, 'right');
Branch: 1 branch
BranchTrueFalseTotal
$self->{'write_always_under_value'} ? : 0 2 2
400 }
401 ▶ elsif($value < $legend_values[$i_row] && $value < $self->{'floor_value'}) { # Write value
Condition0%0/3
$value < $legend_values[$i_row] and $value < $self->{"floor_value"}
!ll&&!rl&&r
000
MC/DC0%0/2
$value < $legend_values[$i_row] and $value < $self->{"floor_value"}
$value < $legend_values[$i_row] $value < $self->{"floor_value"}
Truth table0%0/3
A and B
A= $value < $legend_values[$i_row]B= $value < $self->{"floor_value"}
ABresult
0X0
100
111
402 0 ▶ $output_rows[$i_row] .= center_text($self->{'write_value'} ? $value : $self->{'bar_char'}, $self->{'horizontal_width'}, $SPACE, 'right');
Branch: 1 branch
BranchTrueFalseTotal
$self->{'write_value'} ? : 0 0 0
403 }
404 else {
405 }
406 }
407 else {
408 }
409 }
410
411 # (Possible) middle rows (floor down)
412 ▶ elsif($i_row < $screen_floor_row && $i_row > $screen_bottom_row) {
Condition66%2/3
$i_row < $screen_floor_row and $i_row > $screen_bottom_row
!ll&&!rl&&r
8920203
MC/DC50%1/2
$i_row < $screen_floor_row and $i_row > $screen_bottom_row
$i_row < $screen_floor_row $i_row > $screen_bottom_row
Truth table66%2/3
A and B
A= $i_row < $screen_floor_rowB= $i_row > $screen_bottom_row
ABresult
0X0
100
111
413 203 ▶ if($value <= $legend_values[$i_row - 1]) { # Write bar char
Branch: 3 branches
BranchTrueFalseTotal
if ($value <= $legend_values[$i_row - 1]) { } 47 156 203
elsif ($value <= $legend_values[$i_row] and $value > $legend_values[$i_row - 1]) { } 20 136 156
elsif ($value > $legend_values[$i_row]) { } 136 0 136
414 47 $output_rows[$i_row] .= center_text($self->{'bar_char'}, $self->{'horizontal_width'}, $SPACE, 'right');
415 }
416 ▶ elsif($value <= $legend_values[$i_row] && $value > $legend_values[$i_row - 1]) { # Write value
Condition66%2/3
$value <= $legend_values[$i_row] and $value > $legend_values[$i_row - 1]
!ll&&!rl&&r
136020
MC/DC50%1/2
$value <= $legend_values[$i_row] and $value > $legend_values[$i_row - 1]
$value <= $legend_values[$i_row] $value > $legend_values[$i_row - 1]
Truth table66%2/3
A and B
A= $value <= $legend_values[$i_row]B= $value > $legend_values[$i_row - 1]
ABresult
0X0
100
111
417 20 ▶ $output_rows[$i_row] .= center_text($self->{'write_value'} ? $value : $self->{'bar_char'}, $self->{'horizontal_width'}, $SPACE, 'right');
Branch: 1 branch
BranchTrueFalseTotal
$self->{'write_value'} ? : 12 8 20
418 }
419 elsif($value > $legend_values[$i_row]) { # Write white space
420 136 $output_rows[$i_row] .= $horizontal_width_empty;
421 }
422 else {
423 }
424 }
425
426 # Floor row
427 elsif($i_row == $screen_floor_row) {
428 47 ▶ if($self->{'write_floor'} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'write_floor'} == 1) { } 47 0 47
429 47 $output_rows[$i_row] .= center_text('-', $self->{'horizontal_width'}, "-", 'right');
430 }
431 else {
432 0 ▶ if($value > $legend_values[$i_row - 1] && $value < $legend_values[$i_row + 1]) { # Write value
Branch: 1 branch
BranchTrueFalseTotal
if ($value > $legend_values[$i_row - 1] and $value < $legend_values[$i_row + 1]) { } 0 0 0
Condition0%0/3
$value > $legend_values[$i_row - 1] and $value < $legend_values[$i_row + 1]
!ll&&!rl&&r
000
MC/DC0%0/2
$value > $legend_values[$i_row - 1] and $value < $legend_values[$i_row + 1]
$value > $legend_values[$i_row - 1] $value < $legend_values[$i_row + 1]
Truth table0%0/3
A and B
A= $value > $legend_values[$i_row - 1]B= $value < $legend_values[$i_row + 1]
ABresult
0X0
100
111
433 0 ▶ $output_rows[$i_row] .= center_text($self->{'write_value'} ? $value : $self->{'bar_char'}, $self->{'horizontal_width'}, $SPACE, 'right');
Branch: 1 branch
BranchTrueFalseTotal
$self->{'write_value'} ? : 0 0 0
434 }
435 else { # Write bar char
436 0 $output_rows[$i_row] .= center_text($self->{'bar_char'}, $self->{'horizontal_width'}, $SPACE, 'right');
437 }
438 }
439 }
440
441 # (Possible) middle rows (floor up)
442 ▶ elsif($i_row > $screen_floor_row && $i_row < $screen_top_row) {
Condition66%2/3
$i_row > $screen_floor_row and $i_row < $screen_top_row
!ll&&!rl&&r
099746
MC/DC50%1/2
$i_row > $screen_floor_row and $i_row < $screen_top_row
$i_row > $screen_floor_row $i_row < $screen_top_row
Truth table66%2/3
A and B
A= $i_row > $screen_floor_rowB= $i_row < $screen_top_row
ABresult
0X0
100
111
443 746 ▶ if($value >= $legend_values[$i_row + 1]) { # Write bar char
Branch: 3 branches
BranchTrueFalseTotal
if ($value >= $legend_values[$i_row + 1]) { } 231 515 746
elsif ($value >= $legend_values[$i_row] and $value < $legend_values[$i_row + 1]) { } 59 456 515
elsif ($value < $legend_values[$i_row]) { } 456 0 456
444 231 $output_rows[$i_row] .= center_text($self->{'bar_char'}, $self->{'horizontal_width'}, $SPACE, 'right');
445 }
446 ▶ elsif($value >= $legend_values[$i_row] && $value < $legend_values[$i_row + 1]) { # Write value
Condition66%2/3
$value >= $legend_values[$i_row] and $value < $legend_values[$i_row + 1]
!ll&&!rl&&r
456059
MC/DC50%1/2
$value >= $legend_values[$i_row] and $value < $legend_values[$i_row + 1]
$value >= $legend_values[$i_row] $value < $legend_values[$i_row + 1]
Truth table66%2/3
A and B
A= $value >= $legend_values[$i_row]B= $value < $legend_values[$i_row + 1]
ABresult
0X0
100
111
447 59 ▶ $output_rows[$i_row] .= center_text($self->{'write_value'} ? $value : $self->{'bar_char'}, $self->{'horizontal_width'}, $SPACE, 'right');
Branch: 1 branch
BranchTrueFalseTotal
$self->{'write_value'} ? : 25 34 59
448 }
449 elsif($value < $legend_values[$i_row]) { # Write white space
450 456 $output_rows[$i_row] .= $horizontal_width_empty;
451 }
452 else {
453 }
454 }
455
456 # Top row, here value is only >= or <!
457 elsif($i_row == $screen_top_row) {
458 99 ▶ if($value >= $legend_values[$i_row]) { # Write value or bar char
Branch: 2 branches
BranchTrueFalseTotal
if ($value >= $legend_values[$i_row]) { } 6 93 99
elsif ($value < $legend_values[$i_row]) { } 93 0 93
459 6 ▶ if(length($value) > $self->{'horizontal_width'}) { # Doesn't fit on the row.
Branch: 1 branch
BranchTrueFalseTotal
if (length $value > $self->{'horizontal_width'}) { } 0 6 6
460 $output_rows[$i_row] .= center_text(
461 $self->{'write_always_over_value'} ? $self->{'over_value_char'} : ($self->{'write_value'} ? $value : $self->{'bar_char'}),
462 0 ▶ $self->{'horizontal_width'}, $SPACE, 'right');
Branch: 2 branches
BranchTrueFalseTotal
$self->{'write_always_over_value'} ? : 0 0 0
$self->{'write_value'} ? : 0 0 0
463 }
464 else {
465 $output_rows[$i_row] .= center_text(
466 $self->{'write_always_over_value'} ? $value : ($self->{'write_value'} ? $value : $self->{'bar_char'}),
467 6 ▶ $self->{'horizontal_width'}, $SPACE, 'right');
Branch: 2 branches
BranchTrueFalseTotal
$self->{'write_always_over_value'} ? : 0 6 6
$self->{'write_value'} ? : 3 3 6
468 }
469 }
470 elsif($value < $legend_values[$i_row]) { # Write white space
471 93 $output_rows[$i_row] .= $horizontal_width_empty;
472 }
473 else {
474 }
475 }
476 else {
477 }
478 }
479 else { # $value is same as $self->{'floor_value'}
480 104 ▶ if($self->{'floor_value'} == $legend_values[$i_row]) { # This is the floor row, the "0" row.
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'floor_value'} == $legend_values[$i_row]) { } 6 98 104
481 6 ▶ if($self->{'write_floor_value'} == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'write_floor_value'} == 1) { } 6 0 6
elsif ($self->{'write_floor'}) { } 0 0 0
482 6 $output_rows[$i_row] .= center_text($value, $self->{'horizontal_width'}, $SPACE, 'right');
483 }
484 elsif($self->{'write_floor'}) {
485 0 $output_rows[$i_row] .= center_text($self->{'floor_char'}, $self->{'horizontal_width'}, $SPACE, 'right');
486 }
487 else {
488 0 $output_rows[$i_row] .= $horizontal_width_empty;
489 }
490 }
491 else {
492 98 $output_rows[$i_row] .= $horizontal_width_empty;
493 }
494 }
495 }
496 }
497 # Now we have to flip the order!
498 7 my @reversed_rows;
499 7 foreach my $screen_row (@output_rows) {
500 84 unshift @reversed_rows, $screen_row;
501 }
502 7 $self->{'screen'} = \@reversed_rows;
503
504 # Clean up
505 7 return $return_value;
506 }
507
508 =head1 INTERNAL SUBROUTINES
509
510 =head2 center_text
511
512 Center a string into a string buffer. Return the buffer.
513 Parameters: text to be centered, field width, fill character (default: " "), start direction (default: left).
514 If text is longer than field width, it is not truncated!
515
516 =cut
517
518 472 ▶sub center_text {
Subroutine center_text: called CC=5 SCAR=16.1
Pod documented
519 472 my $text = $_[0];
520 472 my $field_width = $_[1];
521 472 ▶ my $fill_character = ($_[2] ? $_[2] : $SPACE);
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 472 0 472
522 472 ▶ my $start_direction = ($_[3] ? $_[3] : 'left'); ## no critic (ProhibitMagicNumbers)
Branch: 1 branch
BranchTrueFalseTotal
$_[3] ? : 472 0 472
523 472 my $next_add_direction = $start_direction;
524 # MODIFY BUFFER
525 472 ▶ while(length($text) < $field_width) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $text < $field_width) 1248 472 1720
526 1248 ▶ if($next_add_direction eq 'left') {
Branch: 1 branch
BranchTrueFalseTotal
if ($next_add_direction eq 'left') { } 475 773 1248
527 475 $text = $fill_character . $text;
528 475 $next_add_direction = 'right'
529 }
530 else {
531 773 $text = $text . $fill_character;
532 773 $next_add_direction = 'left'
533 }
534 }
535
536 472 return $text;
537 }
538
539 =head1 AUTHOR
540
541 Mikko Koivunalho, C<< <mikko.koivunalho at iki.fi> >>
542
543 =head1 BUGS
544
545 Please report any bugs or feature requests to C<bug-text-histogram at rt.cpan.org>, or through
546 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Text-HistogramChart>. I will be notified, and then you'll
547 automatically be notified of progress on your bug as I make changes.
548
549
550
551
552 =head1 SUPPORT
553
554 You can find documentation for this module with the perldoc command.
555
556 perldoc Text::HistogramChart
557
558
559 You can also look for information at:
560
561 =over 4
562
563 =item * RT: CPAN's request tracker (report bugs here)
564
565 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Text-HistogramChart>
566
567 =item * AnnoCPAN: Annotated CPAN documentation
568
569 L<http://annocpan.org/dist/Text-HistogramChart>
570
571 =item * CPAN Ratings
572
573 L<http://cpanratings.perl.org/d/Text-HistogramChart>
574
575 =item * Search CPAN
576
577 L<http://search.cpan.org/dist/Text-HistogramChart/>
578
579 =back
580
581
582 =head1 ACKNOWLEDGEMENTS
583
584 None.
585
586
587 =head1 DIAGNOSTICS
588
589 None.
590
591
592 =head1 CONFIGURATION AND ENVIRONMENT
593
594 Please see the examples.
595
596
597 =head1 INCOMPATIBILITIES
598
599 None known.
600
601
602 =head1 BUGS AND LIMITATIONS
603
604 Plenty I'm sure.
605 Using roof_value and bottom_value together to restrict the bars into a certain scope is not yet implemented.
606
607
608 =head1 LICENSE AND COPYRIGHT
609
610 Copyright 2012 Mikko Koivunalho.
611
612 This program is free software; you can redistribute it and/or modify it
613 under the terms of either: the GNU General Public License as published
614 by the Free Software Foundation; or the Artistic License.
615
616 See http://dev.perl.org/licenses/ for more information.
617
618
619 =cut
620
621 2 1; # End of Text::HistogramChart
↑ Summary