File Coverage

File:blib/lib/SVG/Sparkline/Area.pm
Coverage:95.4%

linestmtbrancondsubtimecode
1package SVG::Sparkline::Area;
2
3
7
7
7
23
9
222
use warnings;
4
7
7
7
21
9
123
use strict;
5
7
7
7
17
6
356
use Carp;
6
7
7
7
21
9
107
use SVG;
7
7
7
7
9921
11
151
use SVG::Sparkline::Utils;
8
9
7
7
7
267
17
3123
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
0
0
0
0
    return scalar grep { $_[1] eq $_ } qw/xrange yrange/;
17}
18
19sub make
20{
21
33
37
    my ($class, $args) = @_;
22    # validate parameters
23
33
468
    SVG::Sparkline::Utils::validate_array_param( $args, 'values' );
24
30
351
    my $valdesc = SVG::Sparkline::Utils::summarize_xy_values( $args->{values} );
25
26
28
323
    SVG::Sparkline::Utils::calculate_xscale( $args, $valdesc->{xrange} );
27
28
345
    SVG::Sparkline::Utils::calculate_yscale_and_offset( $args, $valdesc->{yrange}, $valdesc->{offset} );
28
28
318
    my $svg = SVG::Sparkline::Utils::make_svg( $args );
29
30
28
379
    my $points = SVG::Sparkline::Utils::xypairs_to_points_str(
31
28
42
        [ [0,0], @{$valdesc->{vals}}, [$valdesc->{vals}->[-1]->[0],0] ],
32        $args->{xscale}, $args->{yscale}
33    );
34
28
281
    $svg->polygon( fill=>$args->{color}, points=>$points, stroke=>'none' );
35
36
28
1161
    if( exists $args->{mark} )
37    {
38
15
26
        _make_marks( $svg,
39            xscale=>$args->{xscale}, yscale=>$args->{yscale},
40            values=>$valdesc->{vals}, mark=>$args->{mark}
41        );
42    }
43
44
28
368
    return $svg;
45}
46
47sub _make_marks
48{
49
15
24
    my ($svg, %args) = @_;
50
51
15
15
8
21
    my @marks = @{$args{mark}};
52
15
120
15
9
87
20
    my @yvalues = map { $_->[1] } @{$args{values}};
53
15
17
    while(@marks)
54    {
55
15
18
        my ($index,$color) = splice( @marks, 0, 2 );
56
15
174
        $index = SVG::Sparkline::Utils::mark_to_index( 'Area', $index, \@yvalues );
57
15
28
        _make_mark( $svg, %args, index=>$index, color=>$color );
58    }
59
15
22
    return;
60}
61
62sub _make_mark
63{
64
15
27
    my ($svg, %args) = @_;
65
15
11
    my $index = $args{index};
66
15
173
    my $x = _f($args{xscale} * $args{values}->[$index]->[0]);
67
15
173
    my $y = _f($args{yscale} * ($args{values}->[$index]->[1]));
68
15
25
    if(abs($y) <= 0.01)
69    {
70
2
17
        $svg->circle( cx=>$x, cy=>$y, r=>1, fill=>$args{color}, stroke=>'none' );
71    }
72    else
73    {
74
13
108
        $svg->line( x1=>$x, y1=>0, x2=>$x, y2=>$y,
75            fill=>'none', stroke=>$args{color}, 'stroke-width'=>1
76        );
77    }
78
15
698
    return;
79}
80
811; # Magic true value required at end of module