File Coverage

blib/lib/Text/Sparkline.pm
Criterion Covered Total %
statement 52 53 98.1
branch 17 18 94.4
condition 3 3 100.0
subroutine 7 7 100.0
pod 1 2 50.0
total 80 83 96.3


line stmt bran cond sub pod time code
1             package Text::Sparkline;
2              
3 2     2   153343 use 5.10.1;
  2         19  
4 2     2   14 use strict;
  2         4  
  2         72  
5 2     2   13 use warnings;
  2         4  
  2         74  
6 2     2   13 use Scalar::Util;
  2         3  
  2         1100  
7              
8             =encoding utf8
9              
10             =head1 NAME
11              
12             Text::Sparkline - Creates text-based sparklines
13              
14             =head1 VERSION
15              
16             Version 0.1.0
17              
18             =cut
19              
20             our $VERSION = 'v0.1.0';
21              
22             =head1 SYNOPSIS
23              
24             Creates sparklines, mini graphs for use in text programs.
25              
26             my @temperatures = ( 75, 78, 80, 74, 79, 77, 80 );
27             print Text::Sparkline::sparkline( \@temperatures );
28              
29             ▂▅█▁▆▄█
30              
31             =head1 EXPORT
32              
33             A list of functions that can be exported. You can delete this section
34             if you don't export anything, such as for a purely object-oriented module.
35              
36             =head1 SUBROUTINES
37              
38             =head2 sparkline( \@values )
39              
40             =cut
41              
42             my @BARS= qw( ▁ ▂ ▃ ▄ ▅ ▆ ▇ █ );
43              
44             sub sparkline {
45 9     9 1 4825 my $raw_values = shift;
46              
47 9         23 my ($values, $min, $max) = _stats($raw_values);
48              
49 9         13 my @bars;
50 9         18 my $nbars = scalar(@BARS);
51 9         11 for my $val ( @{$values} ) {
  9         17  
52 62 100       93 if ( defined($val) ) {
53 54         88 my $height = int($val * $nbars / $max);
54 54 100       85 if ( $height == $nbars ) {
55 8         22 --$height;
56             }
57 54         93 push @bars, $BARS[$height];
58             }
59             else {
60 8         11 push @bars, ' ';
61             }
62             }
63              
64 9         46 return join( '', @bars );
65             }
66              
67              
68             sub sparkline_truncated {
69 2     2 0 1073 my $raw_values = shift;
70              
71 2         6 my ($values, $min, $max) = _stats($raw_values);
72              
73 2         4 my @bars;
74 2         4 my $nbars = scalar(@BARS);
75 2         4 my $span = $max - $min;
76 2         3 for my $val ( @{$values} ) {
  2         5  
77 19 50       30 if ( defined($val) ) {
78 19 100       26 if ( $span ) {
79 9         18 my $height = int(($val-$min) * $nbars / $span);
80 9 100       15 if ( $height == $nbars ) {
81 1         2 --$height;
82             }
83 9         19 push @bars, $BARS[$height];
84             }
85             else {
86 10         18 push @bars, $BARS[$nbars-1];
87             }
88             }
89             else {
90 0         0 push @bars, ' ';
91             }
92             }
93              
94 2         9 return join( '', @bars );
95             }
96              
97              
98             sub _stats {
99 11     11   17 my $raw_values = shift;
100              
101 11         28 my @values;
102             my $min;
103 11         0 my $max;
104 11         15 for my $val ( @{$raw_values} ) {
  11         25  
105 81 100 100     249 if ( Scalar::Util::looks_like_number($val) && ($val>=0) ) {
106 73         112 push @values, $val;
107 73 100       112 if ( !defined($min) ) {
108 9         16 $min = $max = $val;
109             }
110             else {
111 64 100       109 $min = $val if $val < $min;
112 64 100       121 $max = $val if $val > $max;
113             }
114             }
115             else {
116 8         15 push @values, undef;
117             }
118             }
119              
120 11         36 return ( \@values, $min, $max );
121             }
122              
123              
124             =head1 AUTHOR
125              
126             Andy Lester, C<< >>
127              
128             =head1 BUGS
129              
130             Please report any bugs or feature requests to
131             L.
132              
133             Note that Text::Sparline does NOT use L for bug tracking.
134              
135             =head1 SUPPORT
136              
137             You can find documentation for this module with the perldoc command.
138              
139             perldoc Text::Sparkline
140              
141             You can also look for information at:
142              
143             =over 4
144              
145             =item * MetaCPAN
146              
147             L
148              
149             =item * Text::Sparkline's bug queue
150              
151             L
152              
153             =item * Project repository at GitHub
154              
155             L
156              
157             =back
158              
159             =head1 ACKNOWLEDGEMENTS
160              
161             This code is adapted from
162             L,
163             based on Zach Holman's original L program.
164              
165             =head1 LICENSE AND COPYRIGHT
166              
167             This software is Copyright (c) 2021 by Andy Lester.
168              
169             This is free software, licensed under: The Artistic License 2.0 (GPL Compatible)
170              
171             =cut
172              
173              
174             1;