| 1 | =head1 NAME | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | SVG::Timelime::Event - A single event in an SVG timeline. | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | =head1 SYNOPSIS | ||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | See L<SVG::Timeline>. | ||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | =cut | ||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | package SVG::Timeline::Event; | ||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 7 | ▶ | use 5.014; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 7 | ▶ | use Moose; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | 7 | ▶ | use Moose::Util::TypeConstraints; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | 7 | ▶ | use DateTime; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | 7 | ▶ | use DateTime::Format::Strptime; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | coerce __PACKAGE__, | ||||||||||||||||||||
| 21 | from 'HashRef', | ||||||||||||||||||||
| 22 | 15 | ▶ | via { __PACKAGE__->new($_) }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | # Chosen format: yyyy-mm-dd | ||||||||||||||||||||
| 25 | subtype 'SVG::Timeline::DateStr', | ||||||||||||||||||||
| 26 | as 'Str', | ||||||||||||||||||||
| 27 | 4 | ▶ | where { m/ \d{4}-\d{2}-\d{2} /ax }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | subtype 'SVG::Timeline::YearMonthStr', | ||||||||||||||||||||
| 30 | as 'Str', | ||||||||||||||||||||
| 31 | 0 | ▶ | where { m/ \d{4}-\d{2} /ax }; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | subtype 'SVG::Timeline::YearStr', | ||||||||||||||||||||
| 34 | as 'Str', | ||||||||||||||||||||
| 35 | 0 | ▶ | where { m/ \d{4} /as }; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | 7 | subtype 'SVG::Timeline::Num', | |||||||||||||||||||
| 38 | as 'Num'; | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | coerce 'SVG::Timeline::DateStr', | ||||||||||||||||||||
| 41 | from 'SVG::Timeline::YearStr', | ||||||||||||||||||||
| 42 | 0 | ▶ | via { $_ . '-01-01' }; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | coerce 'SVG::Timeline::DateStr', | ||||||||||||||||||||
| 45 | from 'SVG::Timeline::YearMonthStr', | ||||||||||||||||||||
| 46 | 0 | ▶ | via { $_ . '-01' }; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 7 | coerce 'SVG::Timeline::Num', | |||||||||||||||||||
| 49 | from 'SVG::Timeline::DateStr', | ||||||||||||||||||||
| 50 | via \&_str2num; | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 7 | has index => ( | |||||||||||||||||||
| 53 | is => 'ro', | ||||||||||||||||||||
| 54 | isa => 'Int', | ||||||||||||||||||||
| 55 | required => 1, | ||||||||||||||||||||
| 56 | ); | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | 7 | has text => ( | |||||||||||||||||||
| 59 | is => 'ro', | ||||||||||||||||||||
| 60 | isa => 'Str', | ||||||||||||||||||||
| 61 | required => 1, | ||||||||||||||||||||
| 62 | ); | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 4 | ▶ | sub _str2num { | ||||||||||||||||||
|
Subroutine
_str2num: called CC=2 SCAR=7.1
| |||||||||||||||||||||
| 65 | 4 | my ($datestr) = @_; | |||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 4 | my $date = DateTime::Format::Strptime->new( pattern => '%Y-%m-%d' ) | |||||||||||||||||||
| 68 | ->parse_datetime($datestr); | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 4 | ▶ | return $date->year + ( $date->day_of_year / ($date->is_leap_year ? 366 : 365) ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 71 | } | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 7 | has start => ( | |||||||||||||||||||
| 74 | is => 'ro', | ||||||||||||||||||||
| 75 | isa => 'SVG::Timeline::Num', | ||||||||||||||||||||
| 76 | required => 1, | ||||||||||||||||||||
| 77 | coerce => 1, | ||||||||||||||||||||
| 78 | ); | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 7 | has end => ( | |||||||||||||||||||
| 81 | is => 'ro', | ||||||||||||||||||||
| 82 | isa => 'SVG::Timeline::Num', | ||||||||||||||||||||
| 83 | required => 1, | ||||||||||||||||||||
| 84 | coerce => 1, | ||||||||||||||||||||
| 85 | ); | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 7 | has colour => ( | |||||||||||||||||||
| 88 | is => 'ro', | ||||||||||||||||||||
| 89 | isa => 'Maybe[Str]', | ||||||||||||||||||||
| 90 | required => 0, | ||||||||||||||||||||
| 91 | ); | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | =head1 METHODS | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | =head2 start_year | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | Return the year that the event started. | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | =cut | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 227 | ▶ | sub start_year { | ||||||||||||||||||
|
Subroutine
start_year: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 102 | 227 | my $self = shift; | |||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | 227 | return int $self->start; | |||||||||||||||||||
| 105 | } | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | =head2 end_year | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | Return the year that the event ended. | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | =cut | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | 684 | ▶ | sub end_year { | ||||||||||||||||||
|
Subroutine
end_year: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||
| 114 | 684 | my $self = shift; | |||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | 684 | ▶ | if ($self->end) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 117 | 684 | return int $self->end; | |||||||||||||||||||
| 118 | } else { | ||||||||||||||||||||
| 119 | 0 | return DateTime->now->year; | |||||||||||||||||||
| 120 | } | ||||||||||||||||||||
| 121 | } | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | =head2 draw_on($tl) | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | Draw the event inside the given timeline object. | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | =cut | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | 15 | ▶ | sub draw_on { | ||||||||||||||||||
|
Subroutine
draw_on: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||
| 130 | 15 | my $self = shift; | |||||||||||||||||||
| 131 | 15 | my ($tl) = @_; | |||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | 15 | my $x = $self->start * $tl->units_per_year; | |||||||||||||||||||
| 134 | 15 | my $y = ($tl->bar_height * $self->index) | |||||||||||||||||||
| 135 | + ($tl->bar_height * $tl->bar_spacing | ||||||||||||||||||||
| 136 | * ($self->index - 1)); | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | 15 | my $width = ($self->end - $self->start) * $tl->units_per_year; | |||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | 15 | ▶ | $tl->rect( | ||||||||||||||||||
|
Condition66%2/3
$self->colour // $tl->default_colour
MC/DC0%0/2
$self->colour // $tl->default_colour
$self->colour
$tl->default_colour
Truth table66%2/3
A // B
A= $self->colourB= $tl->default_colour
| |||||||||||||||||||||
| 141 | x => $x, | ||||||||||||||||||||
| 142 | y => $y, | ||||||||||||||||||||
| 143 | width => $width, | ||||||||||||||||||||
| 144 | height => $tl->bar_height, | ||||||||||||||||||||
| 145 | fill => $self->colour // $tl->default_colour, | ||||||||||||||||||||
| 146 | stroke => $tl->bar_outline_colour, | ||||||||||||||||||||
| 147 | 'stroke-width' => 1 | ||||||||||||||||||||
| 148 | ); | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | 15 | my $font_size = $tl->bar_height * 0.8; | |||||||||||||||||||
| 151 | 15 | my $padding = $tl->bar_height * 0.2; | |||||||||||||||||||
| 152 | 15 | my $text_x = $x + $padding; | |||||||||||||||||||
| 153 | 15 | my $text_anchor = 'start'; | |||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | # Estimate text width: approximate average character width is 0.6 × font-size | ||||||||||||||||||||
| 156 | 15 | my $approx_text_width = length($self->text) * $font_size * 0.6; | |||||||||||||||||||
| 157 | 15 | my $right_edge = $x + $width; | |||||||||||||||||||
| 158 | 15 | my $image_right_edge = $tl->max_year * $tl->units_per_year; | |||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | 15 | ▶ | if ($text_x + $approx_text_width > $image_right_edge) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 161 | 7 | ▶ | $text_x = ($right_edge < $image_right_edge ? $right_edge : $image_right_edge) - $padding; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 162 | 7 | $text_anchor = 'end'; | |||||||||||||||||||
| 163 | } | ||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | $tl->text( | ||||||||||||||||||||
| 166 | 15 | x => $text_x, | |||||||||||||||||||
| 167 | y => $y + $font_size, | ||||||||||||||||||||
| 168 | 'font-size' => $font_size, | ||||||||||||||||||||
| 169 | 'text-anchor' => $text_anchor, | ||||||||||||||||||||
| 170 | )->cdata($self->text); | ||||||||||||||||||||
| 171 | } | ||||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | 7 | ▶ | no Moose; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 174 | 7 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | =head1 AUTHOR | ||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | Dave Cross <dave@perlhacks.com> | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | =head1 COPYRIGHT AND LICENCE | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | Copyright (c) 2017, Magnum Solutions Ltd. All Rights Reserved. | ||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | This library is free software; you can redistribute it and/or modify it | ||||||||||||||||||||
| 185 | under the same terms as Perl itself. | ||||||||||||||||||||
| 186 | |||||||||||||||||||||
| 187 | =cut | ||||||||||||||||||||
| 188 | |||||||||||||||||||||
| 189 | 7 | 1; | |||||||||||||||||||