| 1 | package SVG::Graph::Glyph::bar; | ||
| 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 | if($self->group->stack){ | ||
| 47 | foreach my $data ($self->group->data_chunks){ | ||
| 48 | my @newlifts = $self->_bars($step,\@lifts,[$data->data]); | ||
| 49 | my $i = 0; | ||
| 50 | foreach my $newlift (@newlifts){ | ||
| 51 | $lifts[$i] += $newlift; | ||
| 52 | $i++; | ||
| 53 | } | ||
| 54 | } | ||
| 55 | } else { | ||
| 56 | $self->_bars($step,\@lifts,[$self->group->data]); | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | sub _bars { | ||
| 61 | my $self = shift; | ||
| 62 | my($step,$lifts,$data) = @_; | ||
| 63 | |||
| 64 | my @ls = (); | ||
| 65 | my $l; | ||
| 66 | |||
| 67 | #FIXME this logic is fubar, but it keeps things in proper scale. | ||
| 68 | my $yscale; | ||
| 69 | if($self->group->stack){ | ||
| 70 | $yscale = $self->ysize / ($self->group->_parent_group->ymax); | ||
| 71 | } else { | ||
| 72 | $yscale = $self->yscale; | ||
| 73 | } | ||
| 74 | |||
| 75 | #AD: umm... why? | ||
| 76 | #xscale is ++'d for fencepost error | ||
| 77 | my $w = ($self->xsize - ($step * ($self->xscale))) / ($self->group->xrange); #+1 | ||
| 78 | |||
| 79 | my $lift; | ||
| 80 | |||
| 81 | my ($x1,$x2,$y1,$y2) = ( | ||
| 82 | (($self->group->xmin - $self->group->xmin) * ($self->xscale)) + $self->xoffset, #+1 | ||
| 83 | undef, | ||
| 84 | ($self->ysize - ($self->group->ymin - $self->group->ymin) * $yscale) + $self->yoffset, | ||
| 85 | undef, | ||
| 86 | ); | ||
| 87 | |||
| 88 | foreach my $datum (sort {$a->x <=> $b->x} @$data){ | ||
| 89 | |||
| 90 | $lift = shift @$lifts; | ||
| 91 | push @ls, ($self->ysize - $y1 + $self->yoffset) + $lift; | ||
| 92 | |||
| 93 | if(!defined($x1) and !defined($y1)){ | ||
| 94 | $x1 = (($datum->x - $self->group->xmin) * $w) + $self->xoffset; | ||
| 95 | $y1 = ($self->ysize - ($datum->y - $self->group->ymin) * $yscale) + $self->yoffset; | ||
| 96 | next; | ||
| 97 | } | ||
| 98 | |||
| 99 | |||
| 100 | $x2 = (($datum->x - $self->group->xmin) * $w) + $self->xoffset; | ||
| 101 | $y2 = ($self->ysize - ($datum->y - $self->group->ymin) * $yscale) + $self->yoffset; | ||
| 102 | |||
| 103 | $self->canvas->rectangle( | ||
| 104 | x=>$x1, | ||
| 105 | y=>$y1 - $lift, | ||
| 106 | width => $w, | ||
| 107 | height=>$self->ysize - $y1 + $self->yoffset, | ||
| 108 | style=>{$self->_style} | ||
| 109 | ); | ||
| 110 | |||
| 111 | |||
| 112 | $x1 = $x2; | ||
| 113 | $y1 = $y2; | ||
| 114 | } | ||
| 115 | |||
| 116 | $lift = shift @$lifts; | ||
| 117 | push @ls, ($self->ysize - $y1 + $self->yoffset) + $lift; | ||
| 118 | |||
| 119 | #plot the last data bar | ||
| 120 | $self->canvas->rectangle( | ||
| 121 | x=>$x1, | ||
| 122 | y=>$y1 - $lift, | ||
| 123 | width=>$w, | ||
| 124 | height=>$self->ysize - $y1 + $self->yoffset, | ||
| 125 | style=>{$self->_style} | ||
| 126 | ); | ||
| 127 | |||
| 128 | return @ls; | ||
| 129 | } | ||
| 130 | |||
| 131 | 1; |