Graph/Plot.pm untested

filter: Statementstmt 0.0% 0 / 171 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 267 CC 27 SCAR 48.9
CC
27
Cov
0%
new
306.0
resize
42.0
gridLines
20.0
CRAP 132.9
1 #
2 # Plot
3 #
4 # Parent class containing methods that all graphs have in common.
5 #
6  
7 package CGI::Graph::Plot;
8 use Data::Table;
9 use CGI;
10 use GD;
11 use GD::Graph;
12  
13 my $directory = ".";
14  
15 my %default = ( divisions => 17,
16 center => '9,9',
17 zoom => 1,
18 grid_height => 200,
19 grid_width => 250,
20 height => 400,
21 width => 500,
22 dataColor => "#ff0000",
23 selectColor => "#00ff00",
24 lineColor => "#000000",
25 windowColor => "#0000ff"
26 );
27  
28 #
29 # new creates a object and initializes necessary values if they do not
30 # already exist. a table reference to a sorted table with index column
31 # is added, as is a scalar value that holds selection values.
32 #
33  
34 sub new {
35 my ($pkg, $vars) = @_;
36 my $class = ref($pkg) || $pkg;
37 my $self = $vars;
38  
39 # create table
40 my $table = Data::Table::fromCSV($self->{source});
41 my @header = $table->header;
42  
43 # default values
44 $self->{X} = $header[0] unless $self->{X};
45 $self->{Y} = $header[0] unless $self->{Y};
46 $self->{divisions} = $default{divisions} unless ($self->{divisions} && $self->{divisions} > 0);
47 $self->{center} = $default{center} unless $self->{center};
48 $self->{zoom} = $default{zoom} unless $self->{zoom};
49 $self->{grid_height} = $default{grid_height} unless $self->{grid_height};
50 $self->{grid_width} = $default{grid_width} unless $self->{grid_width};
51 $self->{height} = $default{height} unless $self->{height};
52 $self->{width} = $default{width} unless $self->{width};
53  
54 foreach ("dataColor","selectColor","lineColor","windowColor") {
55 if ($self->{$_}) {
56 # check if array reference
57 if (ref($self->{$_})) {
58 my $ref = $self->{$_};
59 my @rgb = @$ref;
60 $self->{$_} = GD::Graph::colour::rgb2hex(@rgb);
61 }
62 # if not hex, then check for color name
63 elsif (!($self->{$_}=~/^#[0-f]{6}$/)) {
64 if (grep/^$self->{$_}$/,GD::Graph::colour::colour_list) {
65 my @rgb = GD::Graph::colour::_rgb($self->{$_});
66 $self->{$_} = GD::Graph::colour::rgb2hex(@rgb);
67 }
68 else {
69 $self->{$_} = $default{$_};
70 }
71 }
72 #otherwise, it is hex, which is ok
73 }
74 else {
75 $self->{$_} = $default{$_};
76 }
77 }
78  
79 # set up table
80 my @indices= (1..$table->nofRow);
81 $table->addCol(\@indices,'_row');
82 $table->sort($self->{X},($self->{X}=~/^[ir]_/)?0:1,0,$self->{Y},0,0);
83 $self->{table} = $table;
84  
85 # check file that holds selections
86 my $select;
87 if (open INPUT, "$directory/".$self->{myFile}) {
88 $select = <INPUT>;
89 close INPUT;
90
91 # if the number of selections in the file is different than
92 # the number of rows, re-initialize
93 if (length($select) != $table->nofRow) {
94 all_values($self->{myFile},$table->nofRow,0);
95 $select = "0"x$table->nofRow;
96 }
97 }
98 # file does not exist, so initialize it all to 0
99 else {
100 all_values($self->{myFile},$table->nofRow,0);
101 $select = "0"x$table->nofRow;
102 }
103  
104 $self->{selected} = $select;
105 return bless $self, $class;
106 }
107  
108 #
109 # returns a string containing the values necessary to create a graph image.
110 # a random value is added so that the browser will not use a cached image.
111 #
112  
113 sub graphParams {
114 my $self = shift;
115  
116 my @keys = qw (X Y x_min x_max y_min y_max source myFile graph_type
117 histogram_type zoom_type height width dataColor selectColor);
118 my $final = getValues($self,@keys);
119 $final.= "rand=".rand(localtime);
120 return $final;
121 }
122  
123 #
124 # similar to graphParams, gridParams returns a string used for a grid image.
125 #
126  
127 sub gridParams {
128 my $self = shift;
129 my %vars = %$self; #make copy
130  
131 ($vars{x_min},$vars{x_max},$vars{y_min},$vars{y_max}) =
132 $self->gridBounds();
133  
134 my @keys = qw(X Y x_min x_max y_min y_max source myFile graph_type
135 histogram_type zoom_type zoom center divisions grid_width grid_height
136 dataColor selectColor lineColor windowColor);
137 my $final = getValues(\%vars,@keys);
138  
139 $final.= "rand=".rand(localtime);
140 $final.= "&grid=on";
141  
142 return $final;
143 }
144  
145 #
146 # gridMap returns an image map used on the grid image. The grid is divided into
147 # divisions * divisions rectangles and the mapInfo string is used to preserve
148 # the attributes of the current object except for the center value.
149 #
150  
151 sub gridMap{
152 my ($self,$name,$mapName) = @_;
153
154 my $mapInfo = $self->mapInfo();
155
156 my $Vinc = $self->{grid_height}/$self->{divisions};
157 my $Hinc = $self->{grid_width}/$self->{divisions};
158 my $final = "\n<map name = \"$mapName\">\n";
159
160 for $V (1..$self->{divisions}) {
161  
162 for $H (1..$self->{divisions}) {
163 $mapInfo=~s/center=\d+%2C\d+/center=$H%2C$V/;
164 my $x1 = ($H-1)*$Hinc;
165 my $x2 = $H * $Hinc;
166 my $y1 = ($V-1)*$Vinc;
167 my $y2 = $V * $Vinc;
168  
169 $final.= "<area alt=\"$H , $V\" ";
170 $final.= "shape=rect coords=$x1,$y1,$x2,$y2 ";
171 $final.= "href=\"$name?$mapInfo\">\n";
172 }
173 }
174  
175 $final.="</map>\n";
176 return $final;
177 }
178  
179 #
180 # mapInfo returns a string which represents the attributes of the current
181 # object.
182 #
183  
184 sub mapInfo {
185 my $self = shift;
186 my $final;
187
188 my @keys = qw(X Y source myFile refresh_display center zoom graph_type
189 divisions histogram_type zoom_type width height grid_width grid_height
190 dataColor selectColor lineColor windowColor);
191 $final = getValues($self,@keys);
192  
193 chop ($final);
194 $final.="\"";
195 return $final;
196 }
197  
198 #
199 # calculates a "nice" upper and lower bound for the numerical array passed in.
200 #
201  
202 sub bounds {
203 my @sorted = sort {$a <=> $b} @_;
204 my $x_min = $sorted[0];
205 my $x_max = $sorted[-1];
206 my $delta = $x_max - $x_min;
207 my $ndigit = ($delta <=0)?0: int(log10($delta)+1)-1;
208 my $ub = int($x_max/(10**$ndigit)+1)*10**$ndigit;
209 my $lb = int($x_min/(10**$ndigit)-1)*10**$ndigit;
210 #changed to -1 from -.5 !!!!!!
211 return ($lb,$ub);
212 }
213  
214 #
215 # writes the current selection values to the file specified by the object
216 #
217  
218 sub write_selected {
219 my $self = shift;
220 open OUTPUT, ">$directory/".$self->{myFile} or die "Cannot write to $self->{myFile}\n";
221 print OUTPUT $self->{selected};
222 close OUTPUT;
223 }
224  
225 #
226 # returns the center and span of the current view, relative to the number of
227 # divisions.
228 #
229  
230 sub resize {
231 my $self = shift;
232  
233 #calculate effects of center, zoom, and layers
234 #fraction of full length
235 my @center = split(/,/, $self->{center});
236
237 my $span = (2**(5-$self->{zoom})+1);
238 $span = 1 if ($self->{zoom} >= 5);
239
240 my $xc = ($center[0]-.5);
241 my $yc = ($self->{divisions}-$center[1]+.5);
242  
243 # shift center to avoid going out of bounds
244 if (($xc-$span/2) < 0) {
245 $xc+=(-1*($xc-$span/2));
246 }
247 elsif (($xc+$span/2) > $self->{divisions}) {
248 $xc-= $xc+$span/2-$self->{divisions};
249 }
250  
251 if (($yc-$span/2) < 0) {
252 $yc+=(-1*($yc-$span/2));
253 }
254 elsif (($yc+$span/2) > $self->{divisions}) {
255 $yc-= $yc+$span/2-$self->{divisions};
256 }
257  
258 # update center in object
259 my $center = ($xc+.5).",".($self->{divisions}+1-($yc+.5));
260 $self->{center} = $center;
261  
262 return ($xc,$yc,$span);
263 }
264  
265 #
266 # adds grid lines to an image, depending on the number of divisions. Also draws
267 # in the center marker and view window.
268 #
269  
270 sub gridLines {
271 my ($self,$image) = @_;
272  
273 my @lineColor = GD::Graph::colour::hex2rgb($self->{lineColor});
274 $lineColor = $image->colorAllocate(@lineColor);
275  
276 my @windowColor = GD::Graph::colour::hex2rgb($self->{windowColor});
277 $windowColor = $image->colorAllocate(@windowColor);
278  
279 #draw horizontal lines (move vertically)
280 my $Vinc = $self->{grid_height}/$self->{divisions};
281 for (0..$self->{divisions}-1) {
282 $y = $_*$Vinc;
283 $image->line(0,$y,$self->{grid_width}-1,$y,$lineColor);
284 }
285 #draw last line
286 $image->line(0,$self->{grid_height}-1,$self->{grid_width}-1,$self->{grid_height}-1,
287 $lineColor);
288  
289 #draw vertical lines (move horizontally)
290 my $Hinc = $self->{grid_width}/$self->{divisions};
291 for (0..$self->{divisions}-1) {
292 $x = $_*$Hinc;
293 $image->line($x,0,$x,$self->{grid_height}-1,$lineColor);
294 }
295 #draw last line
296 $image->line($self->{grid_width}-1,0,$self->{grid_width}-1,$self->{grid_height}-1,
297 $lineColor);
298  
299 # determine center position and size of view window
300 @center = split(/,/, $self->{center});
301 $span = (2**(5-$self->{zoom})+1);
302 $span = (2**(5-$self->{zoom})) if ($self->{zoom} >= 5);
303  
304 #draw center marker
305 $x1 = ($center[0]-1)*$Hinc;
306 $x2 = $center[0]*$Hinc;
307 $y1 = ($center[1]-1)*$Vinc;
308 $y2 = $center[1]*$Vinc;
309 $image->filledRectangle($x1,$y1,$x2,$y2,$windowColor);
310  
311 # determine coordinates for view window and draw it
312 $x1 = ($center[0]-.5-$span/2)*$Hinc;
313 $x2 = ($center[0]-.5+$span/2)*$Hinc;
314  
315 # bar graph does not zoom in with respect to Y axis
316 $y1 = ($center[1]-.5-$span/2)*$Vinc;
317 $y2 = ($center[1]-.5+$span/2)*$Vinc;
318
319 $x2-- if ($x2==$self->{grid_width});
320 $y2-- if ($y2==$self->{grid_height});
321  
322 $image->rectangle($x1,$y1,$x2,$y2,$windowColor);
323 $image->rectangle($x1+1,$y1+1,$x2-1,$y2-1,$windowColor);
324  
325 return $image;
326 }
327  
328 #
329 # given a list of keys and a hash reference, will return the keys and values in
330 # a string.
331 #
332  
333 sub getValues {
334 my $vars = shift;
335 my @keys = @_;
336 my $final = '';
337  
338 foreach my $key (@keys) {
339 my $value = CGI::escape($vars->{$key});
340 $final.= "$key=$value&";
341 }
342 return $final;
343 }
344  
345 #
346 # returns the base 10 logarithm of a number. Cannot handle 0 or negative
347 # numbers.
348 #
349  
350 sub log10 {
351 my $in = shift;
352 return undef if ($in <= 0);
353 return log($in)/log(10);
354 }
355  
356 #
357 # $value is written $size number of times to the filename specified
358 # (used to intialize all values to 0 or 1)
359 #
360  
361 sub all_values {
362 my ($filename,$size,$value) = @_;
363 my $select = "$value"x($size);
364 open OUT, ">$directory/$filename" or die "Cannot write to $filename\n";
365 print OUT "$select";
366 close OUT;
367 }
368  
369 1;
370  
371  
372 __END__;