File: bars.pm

package info (click to toggle)
libgifgraph-perl 1.10-2
  • links: PTS
  • area: contrib
  • in suites: potato
  • size: 556 kB
  • ctags: 118
  • sloc: perl: 3,738; makefile: 23
file content (176 lines) | stat: -rw-r--r-- 3,551 bytes parent folder | download
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#==========================================================================
#			   Copyright (c) 1995-1998 Martien Verbruggen
#--------------------------------------------------------------------------
#
#	Name:
#		GIFgraph::bars.pm
#
# $Id: bars.pm,v 2.5 1998/08/25 04:22:15 mgjv Exp $
#
#==========================================================================
 
package GIFgraph::bars;

use strict qw(vars refs subs);

use GIFgraph::axestype;
use GIFgraph::utils qw(:all);

@GIFgraph::bars::ISA = qw( GIFgraph::axestype );

my %Defaults = (
	
	# Spacing between the bars
	bar_spacing 	=> 0,
);

{
	sub initialise()
	{
		my $self = shift;

		$self->SUPER::initialise();

		my $key;
		foreach $key (keys %Defaults)
		{
			$self->set( $key => $Defaults{$key} );
		}
	}
	
	# PRIVATE
	sub draw_data{

		my $s = shift;
		my $g = shift;
		my $d = shift;

		if ( $s->{overwrite} ) 
		{
			$s->draw_data_overwrite($g, $d);
		} 
		else 
		{
			$s->SUPER::draw_data($g, $d);
		}

		# redraw the 'zero' axis
		$g->line( 
			$s->{left}, $s->{zeropoint}, 
			$s->{right}, $s->{zeropoint}, 
			$s->{fgci} );
	}
 
	# Draws the bars on top of each other
 
	sub draw_data_overwrite {

		my $s = shift;
		my $g = shift;
		my $d = shift;
		my $bar_s = _round($s->{bar_spacing}/2);

		my $zero = $s->{zeropoint};

		my $i;
		for $i (0 .. $s->{numpoints}) 
		{
			my $bottom = $zero;
			my ($xp, $t);

			my $j;
			for $j (1 .. $s->{numsets}) 
			{
				next unless (defined $d->[$j][$i]);

				# get data colour
				my $dsci = $s->set_clr( $g, $s->pick_data_clr($j) );

				# get coordinates of top and center of bar
				($xp, $t) = $s->val_to_pixel($i + 1, $d->[$j][$i], $j);

				# calculate left and right of bar
				my $l = $xp - _round($s->{x_step}/2) + $bar_s;
				my $r = $xp + _round($s->{x_step}/2) - $bar_s;

				# calculate new top
				$t -= ($zero - $bottom) if ($s->{overwrite} == 2);

				# draw the bar
				if ($d->[$j][$i] >= 0)
				{
					# positive value
					$g->filledRectangle( $l, $t, $r, $bottom, $dsci );
					$g->rectangle( $l, $t, $r, $bottom, $s->{acci} );
				}
				else
				{
					# negative value
					$g->filledRectangle( $l, $bottom, $r, $t, $dsci );
					$g->rectangle( $l, $bottom, $r, $t, $s->{acci} );
				}

				# reset $bottom to the top
				$bottom = $t if ($s->{overwrite} == 2);
			}
		}
	 }

	sub draw_data_set($$$)
	{
		my $s = shift;
		my $g = shift;
		my $d = shift;
		my $ds = shift;
		my $bar_s = _round($s->{bar_spacing}/2);

		# Pick a data colour
		my $dsci = $s->set_clr( $g, $s->pick_data_clr($ds) );

		my $i;
		for $i (0 .. $s->{numpoints}) 
		{
			next unless (defined $d->[$i]);

			# get coordinates of top and center of bar
			my ($xp, $t) = $s->val_to_pixel($i + 1, $d->[$i], $ds);

			# calculate left and right of bar
			my ($l, $r);

			if ($s->{mixed})
			{
				$l = $xp - _round($s->{x_step}/2) + $bar_s;
				$r = $xp + _round($s->{x_step}/2) - $bar_s;
			}
			else
			{
				$l = $xp 
					- _round($s->{x_step}/2)
					+ _round(($ds - 1) * $s->{x_step}/$s->{numsets})
					+ $bar_s;
				$r = $xp 
					- _round($s->{x_step}/2)
					+ _round($ds * $s->{x_step}/$s->{numsets})
					- $bar_s;
			}

			# draw the bar
			if ($d->[$i] >= 0)
			{
				# positive value
				$g->filledRectangle( $l, $t, $r, $s->{zeropoint}, $dsci );
				$g->rectangle( $l, $t, $r, $s->{zeropoint}, $s->{acci} );
			}
			else
			{
				# negative value
				$g->filledRectangle( $l, $s->{zeropoint}, $r, $t, $dsci );
				$g->rectangle( $l, $s->{zeropoint}, $r, $t, $s->{acci} );
			}
		}
	}
 
} # End of package GIFgraph::bars

1;