| 1 | package SVG::Graph::Glyph::pictogram; | ||
| 2 | |||
| 3 | use base SVG::Graph::Glyph; | ||
| 4 | use strict; | ||
| 5 | use Data::Dumper; | ||
| 6 | |||
| 7 | our @lifts; | ||
| 8 | |||
| 9 | =head2 draw | ||
| 10 | |||
| 11 | Title : draw | ||
| 12 | Usage : | ||
| 13 | Function: | ||
| 14 | Example : | ||
| 15 | Returns : | ||
| 16 | Args : | ||
| 17 | |||
| 18 | |||
| 19 | =cut | ||
| 20 | |||
| 21 | sub draw{ | ||
| 22 | my ($self,@args) = @_; | ||
| 23 | |||
| 24 | my($x1,$x2,$y1,$y2) = (undef,undef,undef,undef); | ||
| 25 | #this will be the width of a bar, we need to calculate the | ||
| 26 | #minimum distance between any two values in the data, | ||
| 27 | #and use that as the step size. | ||
| 28 | my $step = undef; | ||
| 29 | my %xcoords = map {$_->x => 1} $self->group->data; | ||
| 30 | |||
| 31 | ($x1,$x2) = (undef,undef); | ||
| 32 | foreach my $xcoord (sort {$a <=> $b} keys %xcoords){ | ||
| 33 | |||
| 34 | $x1 = $xcoord and next unless defined($x1); | ||
| 35 | $x2 = $xcoord; | ||
| 36 | |||
| 37 | if(!defined($step)){ | ||
| 38 | $step = $x2 - $x1; | ||
| 39 | } else { | ||
| 40 | $step = $x2 - $x1 < $step ? $x2 - $x1 : $step; | ||
| 41 | } | ||
| 42 | |||
| 43 | $x1 = $x2; | ||
| 44 | } | ||
| 45 | |||
| 46 | foreach my $data ($self->group->data_chunks){ | ||
| 47 | my @newlifts = $self->_glyphs($step,\@lifts,[$data->data]); | ||
| 48 | my $i = 0; | ||
| 49 | foreach my $newlift (@newlifts){ | ||
| 50 | $lifts[$i] += $newlift; | ||
| 51 | $i++; | ||
| 52 | } | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | sub _glyphs { | ||
| 57 | my $self = shift; | ||
| 58 | my($step,$lifts,$data) = @_; | ||
| 59 | |||
| 60 | my @ls = (); | ||
| 61 | my $l; | ||
| 62 | |||
| 63 | #FIXME this logic is fubar, but it keeps things in proper scale. | ||
| 64 | |||
| 65 | my $yscale = $self->ysize / ($self->group->ymax); | ||
| 66 | my $xscale = $self->xsize / (scalar(@$data)); | ||
| 67 | |||
| 68 | my $fontsize = int($self->xsize / (scalar(@$data) * 0.96)); | ||
| 69 | foreach my $datum (sort {$a->x <=> $b->x} @$data){ | ||
| 70 | |||
| 71 | |||
| 72 | my $xsc = 1; | ||
| 73 | my $ysc = $datum->y * $yscale; | ||
| 74 | warn $ysc; | ||
| 75 | my $x1 = (($datum->x - 1) * $fontsize) + $self->xoffset; | ||
| 76 | my $y1 = $self->ysize + $self->yoffset - ($self->yoffset / 8); | ||
| 77 | |||
| 78 | if($datum->y > 0){ | ||
| 79 | $self->canvas->text( | ||
| 80 | x => 0, | ||
| 81 | y => 0, | ||
| 82 | # 'transform' => "scale($xsc,$ysc), translate(". $x1/$xsc .",". $y1/$ysc .")", | ||
| 83 | 'transform' => "scale($xsc,$ysc), translate(". $x1/$xsc .",". 0 .")", | ||
| 84 | 'font-size' => $fontsize, | ||
| 85 | 'font-family' => 'Helvetica', | ||
| 86 | 'text-anchor' => 'right', | ||
| 87 | 'fill' => $self->color->{$datum->label}, | ||
| 88 | )->cdata($datum->label); | ||
| 89 | } | ||
| 90 | } | ||
| 91 | } | ||
| 92 | |||
| 93 | =head2 color | ||
| 94 | |||
| 95 | Title : color | ||
| 96 | Usage : $obj->color($newval) | ||
| 97 | Function: | ||
| 98 | Example : | ||
| 99 | Returns : value of color (a scalar) | ||
| 100 | Args : on set, new value (a scalar or undef, optional) | ||
| 101 | |||
| 102 | |||
| 103 | =cut | ||
| 104 | |||
| 105 | sub color{ | ||
| 106 | my $self = shift; | ||
| 107 | |||
| 108 | return $self->{'color'} = shift if @_; | ||
| 109 | return $self->{'color'}; | ||
| 110 | } | ||
| 111 | |||
| 112 | 1; |