File Coverage

File:blib/lib/SVG/Sparkline/Line.pm
Coverage:100.0%

linestmtbrancondsubtimecode
1package SVG::Sparkline::Line;
2
3
7
7
7
25
10
213
use warnings;
4
7
7
7
21
9
126
use strict;
5
7
7
7
17
9
374
use Carp;
6
7
7
7
22
4
109
use SVG;
7
7
7
7
10112
11
163
use SVG::Sparkline::Utils;
8
9
7
7
7
277
19
2859
use 5.008000;
10our $VERSION = 0.35;
11
12# aliases to make calling shorter.
13*_f = *SVG::Sparkline::Utils::format_f;
14
15sub valid_param {
16
2
6
2
29
    return scalar grep { $_[1] eq $_ } qw/thick xrange yrange/;
17}
18
19sub make
20{
21
35
37
    my ($class, $args) = @_;
22    # validate parameters
23
35
443
    SVG::Sparkline::Utils::validate_array_param( $args, 'values' );
24
32
368
    my $valdesc = SVG::Sparkline::Utils::summarize_xy_values( $args->{values} );
25
26
30
135
    my $thick = $args->{thick} || 1;
27
30
355
    SVG::Sparkline::Utils::calculate_xscale( $args, $valdesc->{xrange} );
28
30
358
    SVG::Sparkline::Utils::calculate_yscale_and_offset( $args, $valdesc->{yrange}, $valdesc->{offset} );
29
30
340
    my $svg = SVG::Sparkline::Utils::make_svg( $args );
30
31
30
360
    my $points = SVG::Sparkline::Utils::xypairs_to_points_str(
32        $valdesc->{vals}, $args->{xscale}, $args->{yscale}
33    );
34
30
278
    $svg->polyline( fill=>'none', 'stroke-width'=>$thick, stroke=>$args->{color}, 'stroke-linecap'=>'round', points=>$points );
35
36
30
1376
    if( exists $args->{mark} )
37    {
38
16
27
        _make_marks( $svg,
39            thick=>$thick, xscale=>$args->{xscale}, yscale=>$args->{yscale},
40            values=>$valdesc->{vals}, mark=>$args->{mark}
41        );
42    }
43
44
30
380
    return $svg;
45}
46
47sub _make_marks
48{
49
16
30
    my ($svg, %args) = @_;
50
51
16
16
8
24
    my @marks = @{$args{mark}};
52
16
128
16
8
88
15
    my @yvalues = map { $_->[1] } @{$args{values}};
53
16
24
    while(@marks)
54    {
55
16
18
        my ($index,$color) = splice( @marks, 0, 2 );
56
16
193
        $index = SVG::Sparkline::Utils::mark_to_index( 'Line', $index, \@yvalues );
57
16
33
        _make_mark( $svg, %args, index=>$index, color=>$color );
58    }
59
16
22
    return;
60}
61
62sub _make_mark
63{
64
16
32
    my ($svg, %args) = @_;
65
16
15
    my $index = $args{index};
66
16
187
    my $x = _f($args{xscale} * $args{values}->[$index]->[0]);
67
16
193
    my $y = _f($args{yscale} * $args{values}->[$index]->[1]);
68
16
135
    $svg->circle( cx=>$x, cy=>$y, r=>$args{thick},
69        stroke=>'none', fill=>$args{color}
70    );
71
16
706
    return;
72}
73
741; # Magic true value required at end of module