| 1 | package CGI::Graph::Plot::bars; | ||
| 2 | |||
| 3 | use CGI::Graph::Plot; | ||
| 4 | use GD::Graph::bars; | ||
| 5 | |||
| 6 | @ISA = ("CGI::Graph::Plot"); | ||
| 7 | |||
| 8 | %default = ( y_tick_number => 8, | ||
| 9 | y_label_skip => 2, | ||
| 10 | r_margin => 7, | ||
| 11 | precision => 14, | ||
| 12 | header_size => 15, # default | ||
| 13 | space_size => 18, # values for | ||
| 14 | data_size => 100 # text box | ||
| 15 | ); | ||
| 16 | |||
| 17 | # | ||
| 18 | # calls parent class to intialize values. | ||
| 19 | # | ||
| 20 | |||
| 21 | sub new { | ||
| 22 | my ($pkg, $vars) = @_; | ||
| 23 | my $class = ref($pkg) || $pkg; | ||
| 24 | $vars->{graph_type} = 'bar'; | ||
| 25 | $self=$class->SUPER::new($vars); | ||
| 26 | |||
| 27 | return bless $self, $class; | ||
| 28 | } | ||
| 29 | |||
| 30 | # | ||
| 31 | # determines upper and lower bounds for the grid. | ||
| 32 | # | ||
| 33 | |||
| 34 | sub gridBounds { | ||
| 35 | my $self = shift; | ||
| 36 | |||
| 37 | my ($Xref,$Yref) = $self->count(); | ||
| 38 | my @Y = @$Yref; | ||
| 39 | |||
| 40 | return (0,1,0,(CGI::Graph::Plot::bounds(@Y))[1]); | ||
| 41 | } | ||
| 42 | |||
| 43 | # | ||
| 44 | # determines upper and lower bounds for the main graph image or map. | ||
| 45 | # | ||
| 46 | |||
| 47 | sub graphBounds { | ||
| 48 | my ($self) = @_; | ||
| 49 | |||
| 50 | my ($Xref,$Yref) = $self->count(); | ||
| 51 | |||
| 52 | # Y axis is numerical, use bounds to determine min/max | ||
| 53 | my @Y = @$Yref; | ||
| 54 | my ($y_min,$y_max) = CGI::Graph::Plot::bounds(@Y); | ||
| 55 | |||
| 56 | # use fractions for x min/max | ||
| 57 | my ($xc,$yc,$span) = $self->resize(); | ||
| 58 | |||
| 59 | my $x_min = sprintf("%.$default{precision}f", | ||
| 60 | ($xc-$span/2)/$self->{'divisions'}); | ||
| 61 | my $x_max = sprintf("%.$default{precision}f", | ||
| 62 | ($xc+$span/2)/$self->{'divisions'}); | ||
| 63 | |||
| 64 | $y_max = sprintf("%.$default{precision}f", | ||
| 65 | $y_max*($yc+.5)/$self->{'divisions'}) | ||
| 66 | if ($self->{zoom_type}=~/^unlocked/); | ||
| 67 | |||
| 68 | $y_max = ($y_max>1)?int($y_max+.5):1 | ||
| 69 | if ($self->{zoom_type} eq 'unlocked int'); | ||
| 70 | |||
| 71 | ($self->{x_min},$self->{x_max},$self->{y_min},$self->{y_max}) = | ||
| 72 | ($x_min,$x_max,0,$y_max); | ||
| 73 | } | ||
| 74 | |||
| 75 | # | ||
| 76 | # initializes the graph for the graphMap and drawGraph functions. | ||
| 77 | # | ||
| 78 | |||
| 79 | sub setGraph { | ||
| 80 | my ($self,$graph) = @_; | ||
| 81 | |||
| 82 | $graph->set( | ||
| 83 | x_labels_vertical => 1, | ||
| 84 | x_label_position => .5, | ||
| 85 | y_number_format => \&shorten, | ||
| 86 | x_label => substr($self->{X},2), | ||
| 87 | y_label => "count", | ||
| 88 | y_min_value => $self->{y_min}, | ||
| 89 | y_max_value => $self->{y_max}, | ||
| 90 | r_margin => $default{r_margin}, | ||
| 91 | y_tick_number => $default{y_tick_number}, | ||
| 92 | y_label_skip => $default{y_label_skip} | ||
| 93 | ); | ||
| 94 | |||
| 95 | return $graph; | ||
| 96 | } | ||
| 97 | |||
| 98 | # | ||
| 99 | # generates an image map which allows individual points to be selected. Also | ||
| 100 | # displays properties of the points in a textbox. | ||
| 101 | # | ||
| 102 | |||
| 103 | sub graphMap { | ||
| 104 | my ($self,$name,$mapName) = @_; | ||
| 105 | |||
| 106 | # obtain X and Y values to be drawn | ||
| 107 | my ($fullXref,$Xref,$Yref,$Sref) = $self->valuesInRange(); | ||
| 108 | my @X = @$Xref; | ||
| 109 | my @Y = @$Yref; | ||
| 110 | # array storying number of elements selected for each value of X | ||
| 111 | my @selectDraw = @$Sref; | ||
| 112 | my @dataX = @$fullXref; # X value that has not been shortened | ||
| 113 | |||
| 114 | # set up graph and plot points | ||
| 115 | my $graph = GD::Graph::bars->new($self->{width},$self->{height}); | ||
| 116 | $graph = $self->setGraph($graph); | ||
| 117 | my @data = ([@X],[@Y]); | ||
| 118 | my $gd = $graph->plot(\@data); | ||
| 119 | |||
| 120 | my @setlist = $graph->get_hotspot; | ||
| 121 | |||
| 122 | my $index=0; | ||
| 123 | my $bar_count=1; | ||
| 124 | my $mapInfo = $self->mapInfo(); | ||
| 125 | |||
| 126 | my $final = "\n<map name=\"$mapName\">\n"; | ||
| 127 | |||
| 128 | for $setnum (1.. $#setlist) { | ||
| 129 | foreach $set (@{$setlist[$setnum]}) { | ||
| 130 | my $text = sprintf "%-$default{'space_size'}.". | ||
| 131 | "$default{'header_size'}s",substr($self->{X},2); | ||
| 132 | $text.= sprintf "%-$default{'data_size'}.". | ||
| 133 | "$default{'data_size'}s",$dataX[$bar_count-1]; | ||
| 134 | $text.="\\n"; | ||
| 135 | $text.= sprintf "%-$default{'space_size'}.". | ||
| 136 | "$default{'header_size'}s","count"; | ||
| 137 | $text.= sprintf "%-.$default{'data_size'}s", | ||
| 138 | $Y[$bar_count-1]; | ||
| 139 | |||
| 140 | #escape ' characters | ||
| 141 | $text=~s/\'/\\\'/; | ||
| 142 | |||
| 143 | my $select_fraction = ($Y[$bar_count-1] > 0) ? | ||
| 144 | $selectDraw[$bar_count-1]/$Y[$bar_count-1] : 0; | ||
| 145 | $y_mid = $$set[2]+$select_fraction*($$set[4]-$$set[2]); | ||
| 146 | |||
| 147 | # map area for selected portion of bar | ||
| 148 | $final.= "<area alt=\"$dataX[$bar_count-1]\n"; | ||
| 149 | $final.= "$Y[$bar_count-1]\" "; | ||
| 150 | $final.= "shape=$$set[0] coords=$$set[1],"; | ||
| 151 | $final.= "$$set[2],$$set[3],$y_mid "; | ||
| 152 | $final.= "href=\"$name?select=$bar_count&"; | ||
| 153 | $final.= "select_type=unselect&"; | ||
| 154 | $final.= $mapInfo; | ||
| 155 | $final.= " onMouseOver=\"myform.myarea.value='$text';"; | ||
| 156 | $final.= "return true;\">\n"; | ||
| 157 | |||
| 158 | # map area for unselected portion of bar | ||
| 159 | $final.= "<area alt=\"$dataX[$bar_count-1]\n"; | ||
| 160 | $final.= "$Y[$bar_count-1]\""; | ||
| 161 | $final.= "shape=$$set[0] coords=$$set[1],"; | ||
| 162 | $final.= "$y_mid,$$set[3],$$set[4] "; | ||
| 163 | $final.= "href=\"$name?select=$bar_count&"; | ||
| 164 | $final.= "select_type=select&"; | ||
| 165 | $final.= $mapInfo; | ||
| 166 | $final.= " onMouseOver=\"myform.myarea.value='$text';"; | ||
| 167 | $final.= "return true;\">\n"; | ||
| 168 | |||
| 169 | $bar_count++; | ||
| 170 | $index++; | ||
| 171 | } | ||
| 172 | } | ||
| 173 | |||
| 174 | $final.= "</map>\n"; | ||
| 175 | |||
| 176 | return $final; | ||
| 177 | } | ||
| 178 | |||
| 179 | # | ||
| 180 | # returns a graph image (as a gd object) with selected points highlighted in | ||
| 181 | # green | ||
| 182 | # | ||
| 183 | |||
| 184 | sub drawGraph { | ||
| 185 | my ($self) = @_; | ||
| 186 | |||
| 187 | my (undef,$Xref,$Yref,$Sref) = $self->valuesInRange(); | ||
| 188 | my @X = @$Xref; | ||
| 189 | my @Y = @$Yref; | ||
| 190 | my @selectDraw = @$Sref; | ||
| 191 | |||
| 192 | my $graph = GD::Graph::bars->new($self->{width}, $self->{height}); | ||
| 193 | $graph = $self->setGraph($graph); | ||
| 194 | my @data = ([@X],[@Y]); | ||
| 195 | |||
| 196 | my @dataColor = GD::Graph::colour::hex2rgb($self->{dataColor}); | ||
| 197 | GD::Graph::colour::add_colour(myColor => \@dataColor); | ||
| 198 | $graph->set(dclrs => ["myColor"]); | ||
| 199 | |||
| 200 | my $gd = $graph->plot(\@data); | ||
| 201 | my @setlist = $graph->get_hotspot; | ||
| 202 | |||
| 203 | my @selectColor = GD::Graph::colour::hex2rgb($self->{selectColor}); | ||
| 204 | $selectColor = $gd->colorAllocate(@selectColor); | ||
| 205 | my $index=0; | ||
| 206 | |||
| 207 | # color selected portion of bars | ||
| 208 | for $setnum (1.. $#setlist) { | ||
| 209 | foreach $set (@{$setlist[$setnum]}) { | ||
| 210 | if ($$set[0] eq 'rect' && $selectDraw[$index]) { | ||
| 211 | $select_fraction = | ||
| 212 | $selectDraw[$index]/$Y[$index]; | ||
| 213 | $y_mid = $$set[2] + | ||
| 214 | $select_fraction*($$set[4]-$$set[2]); | ||
| 215 | $gd->rectangle($$set[1],$$set[2],$$set[3], | ||
| 216 | $y_mid,$selectColor); | ||
| 217 | $gd->rectangle($$set[1]+1,$$set[2]+1,$$set[3]-1, | ||
| 218 | $y_mid-1,$selectColor); | ||
| 219 | } | ||
| 220 | |||
| 221 | $index++; | ||
| 222 | } | ||
| 223 | } | ||
| 224 | |||
| 225 | return $gd; | ||
| 226 | } | ||
| 227 | |||
| 228 | # | ||
| 229 | # returns a gd object similar to drawGraph, but with axes hidden | ||
| 230 | # | ||
| 231 | |||
| 232 | sub drawGrid { | ||
| 233 | my ($self,$dataColor,$selectColor,$lineColor,$windowColor) = @_; | ||
| 234 | |||
| 235 | my (undef,$Xref,$Yref,$Sref) = $self->valuesInRange(); | ||
| 236 | my @X = @$Xref; | ||
| 237 | my @Y = @$Yref; | ||
| 238 | my @selectDraw = @$Sref; | ||
| 239 | |||
| 240 | # create a graph that is larger than necessary | ||
| 241 | my $graph = GD::Graph::bars->new(2*$self->{grid_width}, 2*$self->{grid_height}); | ||
| 242 | |||
| 243 | my @data = ([@X],[@Y]); | ||
| 244 | |||
| 245 | $graph->set( | ||
| 246 | fgclr => "white", # hide axes | ||
| 247 | y_min_value => $self->{y_min}, | ||
| 248 | y_max_value => $self->{y_max}, | ||
| 249 | r_margin => $default{r_margin}, | ||
| 250 | y_tick_number => $default{y_tick_number}, | ||
| 251 | y_label_skip => $default{y_label_skip} | ||
| 252 | ); | ||
| 253 | |||
| 254 | my @dataColor = GD::Graph::colour::hex2rgb($self->{dataColor}); | ||
| 255 | GD::Graph::colour::add_colour(myColor => \@dataColor); | ||
| 256 | $graph->set(dclrs => ["myColor"]); | ||
| 257 | |||
| 258 | my $gd = $graph->plot(\@data); | ||
| 259 | my @setlist = $graph->get_hotspot; | ||
| 260 | |||
| 261 | my @selectColor = GD::Graph::colour::hex2rgb($self->{selectColor}); | ||
| 262 | $selectColor = $gd->colorAllocate(@selectColor); | ||
| 263 | |||
| 264 | my $index=0; | ||
| 265 | |||
| 266 | # color selected portion of bars | ||
| 267 | for $setnum (1.. $#setlist) { | ||
| 268 | foreach $set (@{$setlist[$setnum]}) { | ||
| 269 | if ($$set[0] eq 'rect' && $selectDraw[$index]) { | ||
| 270 | $select_fraction = | ||
| 271 | $selectDraw[$index]/$Y[$index]; | ||
| 272 | $y_mid = $$set[2] + | ||
| 273 | $select_fraction*($$set[4]-$$set[2]); | ||
| 274 | $gd->rectangle($$set[1],$$set[2],$$set[3], | ||
| 275 | $y_mid,$selectColor); | ||
| 276 | $gd->rectangle($$set[1]+1,$$set[2]+1,$$set[3]-1, | ||
| 277 | $y_mid-1,$selectColor); | ||
| 278 | } | ||
| 279 | |||
| 280 | $index++; | ||
| 281 | } | ||
| 282 | } | ||
| 283 | |||
| 284 | # create new blank image of correct size | ||
| 285 | my $image = new GD::Image($self->{grid_width},$self->{grid_height}); | ||
| 286 | my $white = $image->colorAllocate(255,255,255); | ||
| 287 | |||
| 288 | # copy only the area of the graph that has data plotted | ||
| 289 | $image->copyResized($gd,0,0,$graph->{left},$graph->{top},$self->{grid_width}, | ||
| 290 | $self->{grid_height},$graph->{right}-$graph->{left}, | ||
| 291 | $graph->{bottom}-$graph->{top}); | ||
| 292 | |||
| 293 | $image = $self->gridLines($image); | ||
| 294 | return $image; | ||
| 295 | } | ||
| 296 | |||
| 297 | sub shorten { | ||
| 298 | my $value = shift; | ||
| 299 | return sprintf ("%g", $value); | ||
| 300 | } | ||
| 301 | |||
| 302 | # | ||
| 303 | # returns the center and span of the current view, relative to the number of | ||
| 304 | # divisions. | ||
| 305 | # | ||
| 306 | |||
| 307 | sub resize { | ||
| 308 | my $self = shift; | ||
| 309 | |||
| 310 | #calculate effects of center, zoom, and layers | ||
| 311 | #fraction of full length | ||
| 312 | my @center = split(/,/, $self->{center}); | ||
| 313 | |||
| 314 | my $span = (2**(5-$self->{zoom})+1); | ||
| 315 | $span = 1 if ($self->{zoom} >= 5); | ||
| 316 | |||
| 317 | my $xc = ($center[0]-.5); | ||
| 318 | my $yc = ($self->{divisions}-$center[1]+.5); | ||
| 319 | |||
| 320 | # shift center to avoid going out of bounds | ||
| 321 | if (($xc-$span/2) < 0) { | ||
| 322 | $xc+=(-1*($xc-$span/2)); | ||
| 323 | } | ||
| 324 | elsif (($xc+$span/2) > $self->{divisions}) { | ||
| 325 | $xc-= $xc+$span/2-$self->{divisions}; | ||
| 326 | } | ||
| 327 | |||
| 328 | $yc = $self->{divisions}/2 unless ($self->{zoom_type}=~/^unlocked/); | ||
| 329 | |||
| 330 | # update center in object | ||
| 331 | my $center = ($xc+.5).",".($self->{divisions}+1-($yc+.5)); | ||
| 332 | $self->{center} = $center; | ||
| 333 | |||
| 334 | return ($xc,$yc,$span); | ||
| 335 | } | ||
| 336 | |||
| 337 | # | ||
| 338 | # adds grid lines to an image, depending on the number of divisions. Also draws | ||
| 339 | # in the blue center marker and view window. | ||
| 340 | # | ||
| 341 | |||
| 342 | sub gridLines { | ||
| 343 | my ($self,$image) = @_; | ||
| 344 | |||
| 345 | my @lineColor = GD::Graph::colour::hex2rgb($self->{lineColor}); | ||
| 346 | $lineColor = $image->colorAllocate(@lineColor); | ||
| 347 | |||
| 348 | my @windowColor = GD::Graph::colour::hex2rgb($self->{windowColor}); | ||
| 349 | $windowColor = $image->colorAllocate(@windowColor); | ||
| 350 | |||
| 351 | #draw horizontal lines (move vertically) | ||
| 352 | my $Vinc = $self->{grid_height}/$self->{divisions}; | ||
| 353 | for (0..$self->{divisions}-1) { | ||
| 354 | $y = $_*$Vinc; | ||
| 355 | $image->line(0,$y,$self->{grid_width}-1,$y,$lineColor); | ||
| 356 | } | ||
| 357 | #draw last line | ||
| 358 | $image->line(0,$self->{grid_height}-1,$self->{grid_width}-1,$self->{grid_height}-1, | ||
| 359 | $lineColor); | ||
| 360 | |||
| 361 | #draw vertical lines (move horizontally) | ||
| 362 | my $Hinc = $self->{grid_width}/$self->{divisions}; | ||
| 363 | for (0..$self->{divisions}-1) { | ||
| 364 | $x = $_*$Hinc; | ||
| 365 | $image->line($x,0,$x,$self->{grid_height}-1,$lineColor); | ||
| 366 | } | ||
| 367 | #draw last line | ||
| 368 | $image->line($self->{grid_width}-1,0,$self->{grid_width}-1,$self->{grid_height}-1, | ||
| 369 | $lineColor); | ||
| 370 | |||
| 371 | # determine center position and size of view window | ||
| 372 | @center = split(/,/, $self->{center}); | ||
| 373 | $span = (2**(5-$self->{zoom})+1); | ||
| 374 | $span = (2**(5-$self->{zoom})) if ($self->{zoom} >= 5); | ||
| 375 | |||
| 376 | #draw center marker | ||
| 377 | $x1 = ($center[0]-1)*$Hinc; | ||
| 378 | $x2 = $center[0]*$Hinc; | ||
| 379 | $y1 = ($center[1]-1)*$Vinc; | ||
| 380 | $y2 = $center[1]*$Vinc; | ||
| 381 | $image->filledRectangle($x1,$y1,$x2,$y2,$windowColor); | ||
| 382 | |||
| 383 | # determine coordinates for view window and draw it | ||
| 384 | $x1 = ($center[0]-.5-$span/2)*$Hinc; | ||
| 385 | $x2 = ($center[0]-.5+$span/2)*$Hinc; | ||
| 386 | |||
| 387 | # bar graph does not zoom in with respect to Y axis | ||
| 388 | $y1 = ($self->{zoom_type}=~/^unlocked/)?($center[1]-1)*$Vinc:0; | ||
| 389 | $y2 = ($self->{grid_height}-1); | ||
| 390 | |||
| 391 | $x2-- if ($x2==$self->{grid_width}); | ||
| 392 | $y2-- if ($y2==$self->{grid_height}); | ||
| 393 | |||
| 394 | $image->rectangle($x1,$y1,$x2,$y2,$windowColor); | ||
| 395 | $image->rectangle($x1+1,$y1+1,$x2-1,$y2-1,$windowColor); | ||
| 396 | |||
| 397 | return $image; | ||
| 398 | } | ||
| 399 | |||
| 400 | 1; |