Skip to content

Commit

Permalink
Clean up attributes and methods
Browse files Browse the repository at this point in the history
  • Loading branch information
davorg committed Sep 25, 2016
1 parent 78bde55 commit 1b30299
Showing 1 changed file with 31 additions and 20 deletions.
51 changes: 31 additions & 20 deletions lib/Timeline/SVG.pm
Expand Up @@ -19,6 +19,18 @@ has events => (
},
);

has width => (
is => 'ro',
isa => 'Int',
default => 2048,
);

has height => (
is => 'ro',
isa => 'Int',
default => 1152,
);

has svg => (
is => 'ro',
isa => 'SVG',
Expand All @@ -29,7 +41,7 @@ has svg => (
sub _build_svg {
my $self = shift;
return SVG->new(
width => ( $self->years * $self->pixels_per_year ),
width => $self->width,
height => $self->height,
);
}
Expand All @@ -44,27 +56,13 @@ sub _build_default_colour {
return 'rgb(255,127,127)';
}

# The height of a bar in pixels
has bar_height => (
is => 'ro',
isa => 'Int',
default => 18,
);

# The number of years between vertical grid lines
has years_per_grid => (
is => 'ro',
isa => 'Int',
default => 10, # One decade by default
);

# The number of horizontal pixels to use for each year
has pixels_per_year => (
is => 'ro',
isa => 'Int',
default => 5,
);

# Padding at the top and bottom of each person bar (in pixels)
has bar_padding => (
is => 'ro',
Expand Down Expand Up @@ -133,9 +131,12 @@ sub draw {

my $curr_event_idx = 1;
foreach ($self->all_events) {
my $x = $_->{start} - $self->min_year;
my $y = $self->bar_height * $curr_event_idx;

$self->rect(
x => $_->{start} - $self->min_year,
y => $self->bar_height * $curr_event_idx,
x => $x,
y => $y,
width => $_->{end} - $_->{start},
height => $self->bar_height,
fill => $_->{colour} // $self->default_colour,
Expand All @@ -144,9 +145,9 @@ sub draw {
);

$self->text(
x => $_->{start} - $self->min_year + 1,
y => $self->bar_height * (1+$curr_event_idx),
'font-size' => $self->bar_height - $self->bar_padding,
x => $x + $self->bar_height * 0.2,
y => $y + $self->bar_height * 0.8,
'font-size' => $self->bar_height * 0.8,
)->cdata($_->{text});

$curr_event_idx++;
Expand Down Expand Up @@ -174,4 +175,14 @@ sub years {
return $self->max_year - $self->min_year;
}

sub pixels_per_year {
my $self = shift;
return $self->width / $self->years;
}

sub bar_height {
my $self = shift;
return $self->height / ($self->count_events + 1);
}

1;

0 comments on commit 1b30299

Please sign in to comment.