Skip to content

Commit

Permalink
Move everything into attributes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Cross committed Apr 9, 2015
1 parent 706301a commit d6285fc
Show file tree
Hide file tree
Showing 2 changed files with 228 additions and 43 deletions.
136 changes: 93 additions & 43 deletions Genealogy/Chart/SVG.pm
Expand Up @@ -9,31 +9,6 @@ use Time::Piece;
use File::Basename;
use SVG;

use constant BAR_HEIGHT => 18;
use constant YEARS => 200;
use constant PX_PER_YR => 5;
use constant HEIGHT => 1500;

my @colours = do {
no warnings 'qw'; # I know what I'm doing here!
map { "rgb($_)" }
qw[
0
255,127,127
127,255,127
127,127,255
255,255,127
255,127,255
127,255,255
];
};

my $grey = 'rgb(127,127,127)';
my $black = 'rgb(0,0,0)';

my $left = localtime->year;
my $right = $left - YEARS;

has svg => (
is => 'ro',
isa => 'SVG',
Expand All @@ -42,24 +17,91 @@ has svg => (
);

sub _build_svg {
return SVG->new(width => (YEARS * PX_PER_YR), height => HEIGHT);
my $self = shift;
return SVG->new(
width => ($self->years * $self->pixels_per_year),
height => $self->height,
);
}

has colours => (
is => 'ro',
isa => 'ArrayRef',
lazy_build => 1,
);

sub _build_colours {
no warnings 'qw'; # I know what I'm doing here!
return [ map { "rgb($_)" }
qw(
0
255,127,127
127,255,127
127,127,255
255,255,127
255,127,255
127,255,255
) ];
}

has bar_height => (
is => 'ro',
isa => 'Int',
default => 18,
);

has years => (
is => 'ro',
isa => 'Int',
default => 200,
);

has pixels_per_year => (
is => 'ro',
isa => 'Int',
default => 5,
);

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

has left => (
is => 'ro',
isa => 'Int',
default => localtime->year,
);

has decade_line_colour => (
is => 'ro',
isa => 'Str',
default => 'rgb(127,127,127)',
);

has bar_outline_colour => (
is => 'ro',
isa => 'Str',
default => 'rgb(0,0,0)',
);

sub BUILD {
my $self = shift;

my $curr_y = $left;
my $curr_y = $self->left;
my $x = 0;
# Draw the decade lines
while ($curr_y > ($left - YEARS)) {
while ($curr_y > ($self->left - $self->years)) {
unless ($curr_y % 10) {
$self->line(x1 => $x, y1 => 0, x2 => $x, y2 => HEIGHT,
stroke => $grey, stroke_width => 1);
$self->line(x1 => $x, y1 => 0, x2 => $x, y2 => $self->height,
stroke => $self->decade_line_colour,
stroke_width => 1);
$self->text(x => $x + 1, y => 12,
'font-size' => BAR_HEIGHT / 2)->cdata($curr_y);
'font-size' => $self->bar_height / 2)->cdata($curr_y);
}
$curr_y--;
$x += PX_PER_YR;
$x += $self->pixels_per_year;
}

return $self;
Expand All @@ -71,32 +113,40 @@ sub person {

my $gen = gen($n);

my $until = $d || $left;
my $until = $d || $self->left;

my $p = $self->rect(
x => ($left - $until) * PX_PER_YR,
y => (HEIGHT * y_pos($n)) - (BAR_HEIGHT / 2),
width => ($until - $b) * PX_PER_YR,
height => BAR_HEIGHT,
fill => $colours[$gen],
stroke => $black,
x => ($self->left - $until) * $self->pixels_per_year,
y => ($self->height * y_pos($n)) - ($self->bar_height / 2),
width => ($until - $b) * $self->pixels_per_year,
height => $self->bar_height,
fill => $self->colours->[$gen],
stroke => $self->bar_outline_colour,
'stroke-width' => 1
);

my $text = "$n: $name ($b - ";
$text .= $d if $d;
$text .= ')';
$self->text(x => ($left - $until + 2) * PX_PER_YR,
y => (HEIGHT * y_pos($n)) + (BAR_HEIGHT / 2) - 3,
'font-size' => BAR_HEIGHT - 4)->cdata($text);
$self->text(x => ($self->left - $until + 2) * $self->pixels_per_year,
y => ($self->height * y_pos($n)) + ($self->bar_height / 2) - 3,
'font-size' => $self->bar_height - 4)->cdata($text);
}

# Get the generation number from an Ahnentafel number.
# Person 1 is in generation 1
# Persons 2 & 3 are Person 1's parents and are in generation 2
# Persons 4, 5, 6 & 7 are Person 1's grandparents and are in generation 3
# etc ...
sub gen {
die unless @_;

return int log($_[0])/log(2) + 1;
}

# Calculate the y-position for a given person number.
# Not entirely sure how I did this, to be honest. Need to reverse engineer
# it and document it!
sub y_pos {
die unless @_;

Expand All @@ -116,4 +166,4 @@ sub den {
}


1;
1;
135 changes: 135 additions & 0 deletions tree3.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d6285fc

Please sign in to comment.