File: bar.pm

package info (click to toggle)
libsvg-graph-perl 0.02-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 616 kB
  • sloc: perl: 2,793; makefile: 2
file content (131 lines) | stat: -rw-r--r-- 2,816 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package SVG::Graph::Glyph::bar;

use base SVG::Graph::Glyph;
use strict;
use Data::Dumper;

our @lifts;

=head2 draw

 Title   : draw
 Usage   :
 Function:
 Example :
 Returns : 
 Args    :


=cut

sub draw{
  my ($self,@args) = @_;

  my($x1,$x2,$y1,$y2) = (undef,undef,undef,undef);
  #this will be the width of a bar, we need to calculate the
  #minimum distance between any two values in the data,
  #and use that as the step size.
  my $step = undef;
  my %xcoords = map {$_->x => 1} $self->group->data;

  ($x1,$x2) = (undef,undef);
  foreach my $xcoord (sort {$a <=> $b} keys %xcoords){

	$x1 = $xcoord and next unless defined($x1);
	$x2 = $xcoord;
	
	if(!defined($step)){
	  $step = $x2 - $x1;
	} else {
	  $step = $x2 - $x1 < $step ? $x2 - $x1 : $step;
	}

	$x1 = $x2;
  }

  if($self->group->stack){
	foreach my $data ($self->group->data_chunks){
	  my @newlifts = $self->_bars($step,\@lifts,[$data->data]);
	  my $i = 0;
	  foreach my $newlift (@newlifts){
		$lifts[$i] += $newlift;
		$i++;
	  }
	}
  } else {
	$self->_bars($step,\@lifts,[$self->group->data]);
  }
}

sub _bars {
  my $self = shift;
  my($step,$lifts,$data) = @_;

  my @ls = ();
  my $l;

  #FIXME this logic is fubar, but it keeps things in proper scale.
  my $yscale;
  if($self->group->stack){
	$yscale = $self->ysize / ($self->group->_parent_group->ymax);
  } else {
	$yscale = $self->yscale;
  }

  #AD: umm... why?
  #xscale is ++'d for fencepost error
  my $w = ($self->xsize - ($step * ($self->xscale))) / ($self->group->xrange); #+1

  my $lift;

  my ($x1,$x2,$y1,$y2) = (
						  (($self->group->xmin - $self->group->xmin) * ($self->xscale)) + $self->xoffset, #+1
						  undef,
						  ($self->ysize - ($self->group->ymin - $self->group->ymin) * $yscale) + $self->yoffset,
						  undef,
						 );

  foreach my $datum (sort {$a->x <=> $b->x} @$data){

	$lift = shift @$lifts;
	push @ls, ($self->ysize - $y1 + $self->yoffset) + $lift;

	if(!defined($x1) and !defined($y1)){
	  $x1 = (($datum->x - $self->group->xmin) * $w) + $self->xoffset;
	  $y1 = ($self->ysize - ($datum->y - $self->group->ymin) * $yscale) + $self->yoffset;
	  next;
	}


	$x2 = (($datum->x - $self->group->xmin) * $w) + $self->xoffset;
	$y2 = ($self->ysize - ($datum->y - $self->group->ymin) * $yscale) + $self->yoffset;

	$self->canvas->rectangle(
							 x=>$x1,
							 y=>$y1 - $lift,
							 width => $w,
							 height=>$self->ysize - $y1 + $self->yoffset,
							 style=>{$self->_style}
							);


	$x1 = $x2;
	$y1 = $y2;
  }

  $lift = shift @$lifts;
  push @ls, ($self->ysize - $y1 + $self->yoffset) + $lift;

  #plot the last data bar
  $self->canvas->rectangle(
						   x=>$x1,
						   y=>$y1 - $lift,
						   width=>$w,
						   height=>$self->ysize - $y1 + $self->yoffset,
						   style=>{$self->_style}
						  );

  return @ls;
}

1;