Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the 'yy-mm-dd' format to pass the 'start' and 'end' date when instanciating an event class #5

Merged
merged 2 commits into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Changes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

### Improved

- Use the 'yy-mm-dd' format to pass the 'start' and 'end' date when instanciating an event class

## [0.0.10] - 2022-06-14

### Fixed
Expand Down
27 changes: 24 additions & 3 deletions lib/SVG/Timeline.pm
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ SVG::Timeline - Create SVG timeline charts
});

$tl->add_event({
start => 1939,
end => 1945,
start => '1939-09-01', # 1 Sep 1939
end => '1945-05-08', # 8 May 1945
text => 'World War II',
});

Expand Down Expand Up @@ -263,6 +263,27 @@ sub events_in_timeline {
return $_[0]->count_events;
}

=head2 add_event

Takes a hash reference with event details and adds an L<SVG::Timeline::Event>
to the timeline. The following details are supported:

=over 4

=item * text - the name of the event that is displayed on the bar. This is required.

=item * start - the start year of the event. It is a string of format C<yy-mm-dd>.
For example, C<2017-07-02> is the 2nd of July 2017. C<-mm-dd> as well as C<-dd> can
be omitted.

=item * end - the end year of the event, requirements are the same as that of C<start>.

=item * colour - the colour that is used to fill the timeline block. This should be
defined in the RGB format used by SVG. For example, red would be 'RGB(255,0,0)'.
This is optional. If not provided, the C<default_color> is used.

=back

=head2 calculated_height

The height of the timeline in "calculated units".
Expand All @@ -287,7 +308,7 @@ sub calculated_height {

=head2 calculated_width

The widtn in "calulated units".
The width in "calulated units".

=cut

Expand Down
25 changes: 23 additions & 2 deletions lib/SVG/Timeline/Event.pm
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,18 @@ use 5.010;

use Moose;
use Moose::Util::TypeConstraints;
use DateTime;

coerce __PACKAGE__,
from 'HashRef',
via { __PACKAGE__->new($_) };

# Choosen format: yy-mm-dd
subtype 'Date',
as 'Str',
where { m/ \d{4} (?: -\d{2} (?: -\d{2} )? )? /ax },
message { "Date format, '$_', is not valid" };

has index => (
is => 'ro',
isa => 'Int',
Expand All @@ -31,16 +38,30 @@ has text => (
required => 1,
);

# Convert date to a floating year
sub floating_year_of {
my ( $self, $date_str, $attr ) = @_;
my ( $year, $month, $day ) = split m/-/, $date_str;

$self->{$attr} = $year;
my $date = DateTime->new( year => $year, month => $month // 1, day => $day // 1 );
my $ndays = $date->jd - $date->set( month => 1, day => 1 )->jd;

$self->{$attr} += $ndays / ( $date->is_leap_year ? 366 : 365 ) if $ndays > 0;
}

has start => (
is => 'ro',
isa => 'Int',
isa => 'Date',
required => 1,
trigger => sub { floating_year_of( @_[0..1], 'start' ) },
);

has end => (
is => 'ro',
isa => 'Int',
isa => 'Date',
required => 1,
trigger => sub { floating_year_of( @_[0..1], 'end' ) },
);

has colour => (
Expand Down
14 changes: 10 additions & 4 deletions t/01-basic.t
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@ use Time::Piece;
my $tl = SVG::Timeline->new;

$tl->add_event({
start => 1987,
end => localtime->year,
start => '1987',
end => '2022-10-23',
text => 'Perl',
});

is($tl->count_events, 1, 'Correct number of events');
$tl->add_event({
start => '2017-07-29',
end => '2022',
text => 'SVG::Timeline on CPAN',
});

is($tl->count_events, 2, 'Correct number of events');
is($tl->events->[0]->index, 1, 'Correct index for event');
isa_ok($tl->svg, 'SVG');
my $vb1 = $tl->svg->getChildren->[0]{viewBox};
Expand All @@ -23,7 +29,7 @@ $tl->add_event({
end => localtime->year,
text => 'Python',
});
is($tl->count_events, 2, 'Correct number of events');
is($tl->count_events, 3, 'Correct number of events');

my $vb2 = $tl->svg->getChildren->[0]{viewBox};
isnt($vb1, $vb2, 'Viewbox changed');
Expand Down