| 1 | ############################################################################## | ||
| 2 | # The Faq-O-Matic is Copyright 1997 by Jon Howell, all rights reserved. # | ||
| 3 | # # | ||
| 4 | # This program is free software; you can redistribute it and/or # | ||
| 5 | # modify it under the terms of the GNU General Public License # | ||
| 6 | # as published by the Free Software Foundation; either version 2 # | ||
| 7 | # of the License, or (at your option) any later version. # | ||
| 8 | # # | ||
| 9 | # This program is distributed in the hope that it will be useful, # | ||
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of # | ||
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # | ||
| 12 | # GNU General Public License for more details. # | ||
| 13 | # # | ||
| 14 | # You should have received a copy of the GNU General Public License # | ||
| 15 | # along with this program; if not, write to the Free Software # | ||
| 16 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.# | ||
| 17 | # # | ||
| 18 | # Jon Howell can be contacted at: # | ||
| 19 | # 6211 Sudikoff Lab, Dartmouth College # | ||
| 20 | # Hanover, NH 03755-3510 # | ||
| 21 | # jonh@cs.dartmouth.edu # | ||
| 22 | # # | ||
| 23 | # An electronic copy of the GPL is available at: # | ||
| 24 | # http://www.gnu.org/copyleft/gpl.html # | ||
| 25 | # # | ||
| 26 | ############################################################################## | ||
| 27 | |||
| 28 | use strict; | ||
| 29 | |||
| 30 | package FAQ::OMatic::statgraph; | ||
| 31 | |||
| 32 | use CGI; | ||
| 33 | use GD; | ||
| 34 | |||
| 35 | my $image_type; | ||
| 36 | { | ||
| 37 | my $image = new GD::Image (10, 10); | ||
| 38 | $image_type = $image->can('gif') ? "gif" : "png"; | ||
| 39 | |||
| 40 | } | ||
| 41 | |||
| 42 | |||
| 43 | use FAQ::OMatic; | ||
| 44 | use FAQ::OMatic::Item; | ||
| 45 | use FAQ::OMatic::Log; | ||
| 46 | |||
| 47 | use vars qw($width $height | ||
| 48 | $minx $rangex $basex $sizex | ||
| 49 | $miny $rangey $basey $sizey); # file scope, for mod_perl | ||
| 50 | |||
| 51 | sub calcx { | ||
| 52 | my ($arg) = shift; | ||
| 53 | return int($basex+min((($arg-($minx))/$rangex)*$sizex,$sizex)); | ||
| 54 | } | ||
| 55 | sub calcy { | ||
| 56 | my ($arg) = shift; | ||
| 57 | return int($basey+$sizey-min((($arg-$miny)/$rangey)*$sizey,$sizey)); | ||
| 58 | } | ||
| 59 | |||
| 60 | sub max { | ||
| 61 | my ($x, $y) = @_; | ||
| 62 | return $x if ($x > $y); | ||
| 63 | return $y; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub min { | ||
| 67 | my ($x, $y) = @_; | ||
| 68 | return $x if ($x < $y); | ||
| 69 | return $y; | ||
| 70 | } | ||
| 71 | |||
| 72 | sub emptygraph { | ||
| 73 | my ($message) = shift; | ||
| 74 | |||
| 75 | my $image = new GD::Image (200, 12); | ||
| 76 | my $white = $image->colorAllocate(255, 255, 255); | ||
| 77 | my $red = $image->colorAllocate(255, 0, 0); | ||
| 78 | $image->filledRectangle(0, 0, $width, $height, $white); | ||
| 79 | $image->string(gdSmallFont, 0, 0, $message, $red); | ||
| 80 | print $image->$image_type(); | ||
| 81 | FAQ::OMatic::myExit(0); | ||
| 82 | } | ||
| 83 | |||
| 84 | #given a range value, returns a nice round interval | ||
| 85 | sub autorange { | ||
| 86 | my ($range) = shift; | ||
| 87 | my ($multiplier) = 1.0; | ||
| 88 | my $interval; | ||
| 89 | |||
| 90 | emptygraph("Range $range<=0.") if ($range <= 0); #that's an error | ||
| 91 | while ($range < 10) { | ||
| 92 | $range *= 10.0; | ||
| 93 | $multiplier *= 0.1; | ||
| 94 | } | ||
| 95 | while ($range >= 100) { | ||
| 96 | $multiplier *= 10.0; | ||
| 97 | $range *= 0.1; | ||
| 98 | } | ||
| 99 | if ($range <= 25.0) { | ||
| 100 | $interval = 2.5; | ||
| 101 | } elsif ($range <= 50.0) { | ||
| 102 | $interval = 5.0; | ||
| 103 | } else { | ||
| 104 | $interval = 10.0; | ||
| 105 | } | ||
| 106 | return $multiplier*$interval; | ||
| 107 | } | ||
| 108 | |||
| 109 | sub autoIntervalDays { | ||
| 110 | my ($range) = shift; | ||
| 111 | my $interval; | ||
| 112 | |||
| 113 | if ($range > 450) { | ||
| 114 | return (365,'Years'); | ||
| 115 | } elsif ($range > 30*3) { | ||
| 116 | return (30,'Months'); | ||
| 117 | } else { | ||
| 118 | return (7,'Weeks'); | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | #given (min,max), return (min,max,range,interval), with a nice interval | ||
| 123 | # and rounded min and max. | ||
| 124 | sub round { | ||
| 125 | my ($arg) = shift; | ||
| 126 | return int($arg+0.5) if ($arg >= 0); | ||
| 127 | return int($arg-0.5); | ||
| 128 | } | ||
| 129 | |||
| 130 | sub rerange { | ||
| 131 | my ($min, $max) = @_; | ||
| 132 | |||
| 133 | my $interval = autorange($max-$min); | ||
| 134 | my $newmin = (round($min/$interval)-1)*$interval; | ||
| 135 | my $newmax = (round($max/$interval)+1)*$interval; | ||
| 136 | return ($newmin, $newmax, $newmax-$newmin, $interval); | ||
| 137 | } | ||
| 138 | |||
| 139 | sub main { | ||
| 140 | my $cgi = FAQ::OMatic::dispatch::cgi(); | ||
| 141 | |||
| 142 | $cgi->cache('NO'); # recommend that netscape not cache this image | ||
| 143 | print FAQ::OMatic::header($cgi, | ||
| 144 | '-type' => "image/$image_type", | ||
| 145 | '-nph' => 1, | ||
| 146 | '-expires' => 0.000000001); | ||
| 147 | |||
| 148 | my $params = FAQ::OMatic::getParams($cgi,'nolog'); | ||
| 149 | my $property = $params->{'property'}; | ||
| 150 | my $duration = $params->{'duration'} || $FAQ::OMatic::Appearance::graphHistory; | ||
| 151 | my $resolution = int($params->{'resolution'} || '1'); | ||
| 152 | my $today = $params->{'today'} || FAQ::OMatic::Log::numericToday(); | ||
| 153 | |||
| 154 | # gotta sanity-check these things, because of stupid infinite loop | ||
| 155 | # in timelocal() | ||
| 156 | my ($yr,$mo,$dy) = split('-',$today); | ||
| 157 | if ($yr<1990 or $yr>2036 or $mo<0 or $mo>11 or $dy<0 or $dy>31) { | ||
| 158 | $today = FAQ::OMatic::Log::numericToday(); | ||
| 159 | } | ||
| 160 | |||
| 161 | $resolution = 1 if ($resolution < 0); | ||
| 162 | $resolution = 14 if ($resolution > 14); | ||
| 163 | |||
| 164 | if ($duration eq 'history') { | ||
| 165 | $duration = FAQ::OMatic::Log::subTwoDays($today,FAQ::OMatic::Log::earliestSmry()); | ||
| 166 | } else { | ||
| 167 | $duration = int($duration); | ||
| 168 | } | ||
| 169 | |||
| 170 | if (($duration/$resolution) > $FAQ::OMatic::Appearance::graphWidth) { | ||
| 171 | # no point in gathering too many data points! | ||
| 172 | $resolution = int($duration / $FAQ::OMatic::Appearance::graphWidth); | ||
| 173 | } | ||
| 174 | # guarantee at least two items in @mydata | ||
| 175 | $duration = $resolution if ($duration <= 0); | ||
| 176 | |||
| 177 | # collect the data of interest. | ||
| 178 | my $day; | ||
| 179 | my $earliestday = FAQ::OMatic::Log::adddays($today, -$duration); | ||
| 180 | my @mydata = (); # The data point itself | ||
| 181 | my @myindex = (); # The day "number" (-$duration .. 0) of that data point | ||
| 182 | # (i.e. where it goes on the graph) | ||
| 183 | my $i; | ||
| 184 | for ($day = $today, $i=0; | ||
| 185 | $day ge $earliestday; | ||
| 186 | $day = FAQ::OMatic::Log::adddays($day, -$resolution), $i-=$resolution) { | ||
| 187 | |||
| 188 | my $item = new FAQ::OMatic::Item($day.".smry", $FAQ::OMatic::Config::metaDir); | ||
| 189 | if (defined $item->{$property}) { | ||
| 190 | unshift @mydata, $item->{$property}; | ||
| 191 | unshift @myindex, $i; | ||
| 192 | } else { | ||
| 193 | unshift @mydata, 0; | ||
| 194 | unshift @myindex, $i; | ||
| 195 | } | ||
| 196 | } | ||
| 197 | |||
| 198 | # days run from negative (history) to zero (today) | ||
| 199 | $minx = $myindex[0]; | ||
| 200 | my $maxx = $myindex[$#myindex]; | ||
| 201 | my $maxy; | ||
| 202 | ($miny, $maxy) = (+10000000, -1000000); | ||
| 203 | foreach my $i (@mydata) { | ||
| 204 | $miny = min($miny, $i); | ||
| 205 | $maxy = max($maxy, $i); | ||
| 206 | } | ||
| 207 | |||
| 208 | # autorange y | ||
| 209 | $miny = 0; #Want bottom of graph to be at 0 | ||
| 210 | $maxy = max($maxy,1); # make sure range doesn't collapse to 0 | ||
| 211 | my $intervaly; | ||
| 212 | ($miny, $maxy, $rangey, $intervaly) = rerange($miny, $maxy); | ||
| 213 | $miny = 0; # autoranging may have nudged miny to -1, so fix it | ||
| 214 | $rangey = $maxy-$miny; # thus fix range, too. | ||
| 215 | |||
| 216 | # get x interval automatically | ||
| 217 | $rangex = $maxx - $minx; | ||
| 218 | my ($intervalx,$unitsx) = autoIntervalDays($rangex); | ||
| 219 | |||
| 220 | $width = $FAQ::OMatic::Appearance::graphWidth; | ||
| 221 | $height = $FAQ::OMatic::Appearance::graphHeight; # from FAQ::OMatic::FaqConfig | ||
| 222 | my $borderx = 30; | ||
| 223 | my $bordery = 10; | ||
| 224 | |||
| 225 | my $image = new GD::Image ($width, $height); | ||
| 226 | my $transparent = $image->colorAllocate(255, 255, 255); | ||
| 227 | my $framefill = $image->colorAllocate(255, 255, 255); | ||
| 228 | my $grid = $image->colorAllocate(192, 192, 192); | ||
| 229 | my $datafill = $image->colorAllocate(0, 0, 132); | ||
| 230 | my $datacolor = $image->colorAllocate(240, 0, 0); | ||
| 231 | my $frame = $image->colorAllocate(128, 0, 0); | ||
| 232 | my $labels = $image->colorAllocate(10, 88, 0); | ||
| 233 | |||
| 234 | # draw the graph in this order, back-to-front: | ||
| 235 | # transparent (whole image background) | ||
| 236 | # framefill | ||
| 237 | # grid | ||
| 238 | # datafill | ||
| 239 | # datacolor | ||
| 240 | # ticks (grid color) | ||
| 241 | # frame | ||
| 242 | # labels | ||
| 243 | |||
| 244 | # transparent | ||
| 245 | $image->filledRectangle(0, 0, $width, $height, $transparent); | ||
| 246 | |||
| 247 | # framefill | ||
| 248 | # note these are globals so calc[xy] can see them | ||
| 249 | ($basex, $basey) = ($borderx, $bordery); | ||
| 250 | ($sizex, $sizey) = ($width-2*$borderx, $height-3*$bordery); | ||
| 251 | $image->filledRectangle($basex, $basey, $basex+$sizex, $basey+$sizey, | ||
| 252 | $framefill); | ||
| 253 | |||
| 254 | # grid | ||
| 255 | for ($i=$miny; $i<=$maxy; $i+=$intervaly) { | ||
| 256 | $image->line(calcx($minx), calcy($i), | ||
| 257 | calcx($miny), calcy($i), $grid); | ||
| 258 | } | ||
| 259 | |||
| 260 | # datafill - plot data region | ||
| 261 | # I use many little polys instead of one big one because GD has | ||
| 262 | # a bug wherein it fails to fill polygons correctly when three points | ||
| 263 | # share the same y value. | ||
| 264 | for ($i=0; $i<scalar(@mydata)-1; $i++) { | ||
| 265 | my $poly = new GD::Polygon; | ||
| 266 | |||
| 267 | $poly->addPt(calcx($myindex[$i]), calcy($miny)); | ||
| 268 | $poly->addPt(calcx($myindex[$i]), calcy($mydata[$i])); | ||
| 269 | $poly->addPt(calcx($myindex[$i+1]), calcy($mydata[$i+1])); | ||
| 270 | $poly->addPt(calcx($myindex[$i+1]), calcy($miny)); | ||
| 271 | |||
| 272 | #$image->filledPolygon($poly, $datafill); | ||
| 273 | } | ||
| 274 | |||
| 275 | # ticks - provide scale for image | ||
| 276 | my $tickwidth = 5; | ||
| 277 | for ($i=$maxx; $i>$minx; $i-=$intervalx) { | ||
| 278 | $image->line(calcx($i), calcy($miny), | ||
| 279 | calcx($i), calcy($miny)-$tickwidth, $grid); | ||
| 280 | my $label = $i/$intervalx; | ||
| 281 | $label = "Today" if ($label == 0); | ||
| 282 | $image->string(gdTinyFont, calcx($i)-2, calcy($miny)+1, | ||
| 283 | $label, $labels); | ||
| 284 | } | ||
| 285 | my $title=$params->{'title'} || $property; | ||
| 286 | $image->string(gdSmallFont, calcx($minx+$rangex*0.5)-length($title)*6/2, | ||
| 287 | calcy($maxy)-12, $title, $labels); | ||
| 288 | for ($i=$miny; $i<=$maxy; $i+=$intervaly) { | ||
| 289 | $image->line(calcx($minx), calcy($i), | ||
| 290 | calcx($minx)+$tickwidth, calcy($i), $grid); | ||
| 291 | $image->line(calcx($maxx), calcy($i), | ||
| 292 | calcx($maxx)-$tickwidth, calcy($i), $grid); | ||
| 293 | my $label = $i; | ||
| 294 | $label =~ s/000$/k/; | ||
| 295 | $image->string(gdTinyFont, $borderx-length($label)*5-2, | ||
| 296 | calcy($i)-3, $label, $labels); | ||
| 297 | } | ||
| 298 | $image->string(gdTinyFont, calcx($minx+$rangex*0.5)-length($unitsx)*5/2, | ||
| 299 | calcy($miny)+10, $unitsx, $labels); | ||
| 300 | |||
| 301 | # datacolor - highlight top edge of data | ||
| 302 | for ($i=0; $i<scalar(@mydata)-1; $i++) { | ||
| 303 | $image->line(calcx($myindex[$i]), calcy($mydata[$i]), | ||
| 304 | calcx($myindex[$i+1]), calcy($mydata[$i+1]), $datacolor); | ||
| 305 | } | ||
| 306 | |||
| 307 | # frame - outline the plot box | ||
| 308 | $image->rectangle($basex, $basey, $basex+$sizex, $basey+$sizey, $frame); | ||
| 309 | |||
| 310 | # announce final value (should show up over ticks) | ||
| 311 | my $value = $mydata[$#mydata]; | ||
| 312 | if (not $value =~ m/\./) { | ||
| 313 | # for big numbers | ||
| 314 | $value =~ s/(?!^|,)(\d\d\d)$/,$1/; # add commas for readability | ||
| 315 | $value =~ s/(?!^|,)(\d\d\d),/,$1,/; # of big numbers. This'll keep you | ||
| 316 | $value =~ s/(?!^|,)(\d\d\d),/,$1,/; # til your trillionth hit. :v) | ||
| 317 | } else { | ||
| 318 | # for little numbers | ||
| 319 | $value = sprintf "%.2f", $value; | ||
| 320 | } | ||
| 321 | my @spots = ( [-1, 0,$framefill], | ||
| 322 | [-1,-1,$framefill], | ||
| 323 | [ 0,-1,$framefill], | ||
| 324 | [ 1,-1,$framefill], | ||
| 325 | [ 1, 0,$framefill], | ||
| 326 | [ 1, 1,$framefill], | ||
| 327 | [ 0, 1,$framefill], | ||
| 328 | [-1, 1,$framefill], | ||
| 329 | [ 0, 0,$datafill] ); | ||
| 330 | foreach my $spot (@spots) { | ||
| 331 | $image->string(gdTinyFont, calcx($maxx)-5*length($value)+18+$spot->[0], | ||
| 332 | max(calcy($mydata[$#mydata])-9+$spot->[1],$basey+2), | ||
| 333 | $value, $spot->[2]); | ||
| 334 | } | ||
| 335 | |||
| 336 | # make image background transparent so it'll look nicer sitting | ||
| 337 | # in the browser | ||
| 338 | $image->transparent($transparent); | ||
| 339 | |||
| 340 | # send image | ||
| 341 | print $image->$image_type(); | ||
| 342 | } | ||
| 343 | |||
| 344 | 1; |