| 1 | # -*- perl -*- | ||
| 2 | |||
| 3 | # Copyright (c) 2002 by Jeff Weisberg | ||
| 4 | # Author: Jeff Weisberg <jaw+chart-strip @ tcp4me.com> | ||
| 5 | # Date: 2002-Nov-01 16:11 (EST) | ||
| 6 | # Function: draw strip charts | ||
| 7 | # | ||
| 8 | # $Id: Strip.pm,v 1.22 2011/11/11 00:38:11 jaw Exp $ | ||
| 9 | |||
| 10 | $Chart::Strip::VERSION = "1.08"; | ||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | Chart::Strip - Draw strip chart type graphs. | ||
| 15 | |||
| 16 | =head1 SYNOPSIS | ||
| 17 | |||
| 18 | use Chart::Strip; | ||
| 19 | |||
| 20 | my $ch = Chart::Strip->new( | ||
| 21 | title => 'Happiness of our Group', | ||
| 22 | # other options ... | ||
| 23 | ); | ||
| 24 | |||
| 25 | $ch->add_data( $davey_data, { style => 'line', | ||
| 26 | color => 'FF0000', | ||
| 27 | label => 'Davey' } ); | ||
| 28 | |||
| 29 | $ch->add_data( $jenna_data, { style => 'line', | ||
| 30 | color => '00FF88', | ||
| 31 | label => 'Jenna' } ); | ||
| 32 | |||
| 33 | print $ch->png(); | ||
| 34 | |||
| 35 | =head1 DESCRIPTION | ||
| 36 | |||
| 37 | The Chart::Strip package plots data values versus time graphs, such as | ||
| 38 | used for seismographs, EKGs, or network usage reports. | ||
| 39 | |||
| 40 | It can plot multiple data sets on one graph. It offers several | ||
| 41 | styles of plots. It automatically determines the proper ranges | ||
| 42 | and labels for both axii. | ||
| 43 | |||
| 44 | =head1 USAGE | ||
| 45 | |||
| 46 | =head2 Create the Chart | ||
| 47 | |||
| 48 | $chart = Chart::Strip->new(); | ||
| 49 | $chart = Chart::Strip->new( | ||
| 50 | option1 => value, | ||
| 51 | option2 => value, | ||
| 52 | ); | ||
| 53 | |||
| 54 | If no options are specified, sensible default values will be used. | ||
| 55 | The following options are recognized: | ||
| 56 | |||
| 57 | =over 4 | ||
| 58 | |||
| 59 | =item C<width> | ||
| 60 | |||
| 61 | The width of the image | ||
| 62 | |||
| 63 | =item C<height> | ||
| 64 | |||
| 65 | The height of the image. | ||
| 66 | |||
| 67 | =item C<title> | ||
| 68 | |||
| 69 | The title of the graph. Will be placed centered at the top. | ||
| 70 | |||
| 71 | =item C<x_label> | ||
| 72 | |||
| 73 | The label for the x axis. Will be placed centered at the bottom. | ||
| 74 | |||
| 75 | =item C<y_label> | ||
| 76 | |||
| 77 | The label for the y axis. Will be placed vertically along the left side. | ||
| 78 | |||
| 79 | =item C<draw_grid> | ||
| 80 | |||
| 81 | Should a grid be drawn on the graph? | ||
| 82 | |||
| 83 | =item C<draw_border> | ||
| 84 | |||
| 85 | Should a border be drawn around the edge of the image? | ||
| 86 | |||
| 87 | =item C<draw_tic_labels> | ||
| 88 | |||
| 89 | Should value labels be shown? | ||
| 90 | |||
| 91 | =item C<draw_data_labels> | ||
| 92 | |||
| 93 | Should each data set be labeled? | ||
| 94 | |||
| 95 | =item C<transparent> | ||
| 96 | |||
| 97 | Should the background be transparent? | ||
| 98 | |||
| 99 | =item C<grid_on_top> | ||
| 100 | |||
| 101 | Should the grid be drawn over the data (1) or below the data (0)? | ||
| 102 | |||
| 103 | =item C<binary> | ||
| 104 | |||
| 105 | Use powers of 2 instead of powers of 10 for the y axis labels. | ||
| 106 | |||
| 107 | =item C<data_label_style> | ||
| 108 | |||
| 109 | Style for drawing the graph labels. C<text> or C<box> | ||
| 110 | |||
| 111 | =item C<thickness> | ||
| 112 | |||
| 113 | Thickness of lines in pixels. (Requires GD newer than $VERSION). | ||
| 114 | |||
| 115 | =item C<skip_undefined> | ||
| 116 | |||
| 117 | Don\'t draw a line into or out of a datapoint whose value is undefined. | ||
| 118 | If false, undefined values are treated as though they were 0. | ||
| 119 | |||
| 120 | =item C<boxwidth> | ||
| 121 | |||
| 122 | Width of boxes for box style graphs. The width may also be specified as C<width> | ||
| 123 | in the data options or per point. If no width is specified a reasonable default is used. | ||
| 124 | |||
| 125 | =back | ||
| 126 | |||
| 127 | =head2 Adding Data | ||
| 128 | |||
| 129 | $chart->add_data( $data, $options ); | ||
| 130 | |||
| 131 | The data should be an array ref of data points. Each data point | ||
| 132 | should be a hash ref containing: | ||
| 133 | |||
| 134 | { | ||
| 135 | time => $time_t, # must be a unix time_t | ||
| 136 | value => $value, # the data value | ||
| 137 | color => $color, # optional, used for this one point | ||
| 138 | } | ||
| 139 | |||
| 140 | or, range style graphs should contain: | ||
| 141 | |||
| 142 | { | ||
| 143 | time => $time_t, # must be a unix time_t | ||
| 144 | min => $low, # the minimum data value | ||
| 145 | max => $high, # the maximum data value | ||
| 146 | color => $color, # optional, used for this one point | ||
| 147 | } | ||
| 148 | |||
| 149 | and the options may contain: | ||
| 150 | |||
| 151 | { | ||
| 152 | style => 'line', # graph style: line, filled, range, points, box | ||
| 153 | color => 'FF00FF', # color used for the graph | ||
| 154 | label => 'New England', # name of the data set | ||
| 155 | } | ||
| 156 | |||
| 157 | points style graphs may specify the point diameter, as C<diam> | ||
| 158 | |||
| 159 | box style graphs may specify the box width, as C<width> | ||
| 160 | |||
| 161 | line and filled graphs may specify a C<smooth> parameter, to connect | ||
| 162 | points using smooth curves instead of straight lines. A value of C<1> | ||
| 163 | is recommended, larger values will be less smooth. | ||
| 164 | |||
| 165 | line, points, box, and filled graphs may specify a drop shadow, | ||
| 166 | consisting of a hashref containing C<dx>, C<dy>, C<dw>, and optionally, C<color> | ||
| 167 | |||
| 168 | shadow => { dx => 3, dy => 3, dw => 3, color => 'CCCCCC' } | ||
| 169 | |||
| 170 | |||
| 171 | =head2 Outputing The Image | ||
| 172 | |||
| 173 | =over 4 | ||
| 174 | |||
| 175 | =item $chart->png() | ||
| 176 | |||
| 177 | Will return the PNG image | ||
| 178 | |||
| 179 | =item $chart->jpeg() | ||
| 180 | |||
| 181 | Will return the jpeg image | ||
| 182 | |||
| 183 | =item $chart->gd() | ||
| 184 | |||
| 185 | Will return the underlying GD object. | ||
| 186 | |||
| 187 | =back | ||
| 188 | |||
| 189 | =cut | ||
| 190 | ; | ||
| 191 | |||
| 192 | package Chart::Strip; | ||
| 193 | 0 | ▶ | use GD; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 194 | use Carp; | ||
| 195 | use POSIX; | ||
| 196 | use strict; | ||
| 197 | |||
| 198 | my $LT_HM = 1; # time | ||
| 199 | my $LT_HR = 2; # time/day | ||
| 200 | my $LT_DW = 3; # day/date | ||
| 201 | my $LT_DM = 4; # date/yr | ||
| 202 | my $LT_YR = 5; # year | ||
| 203 | |||
| 204 | my $MT_NO = 0; # none | ||
| 205 | my $MT_HR = 1; # hrs | ||
| 206 | my $MT_MN = 2; # midnight | ||
| 207 | my $MT_SU = 3; # sunday | ||
| 208 | my $MT_M1 = 4; # 1st | ||
| 209 | my $MT_Y1 = 5; # new years | ||
| 210 | |||
| 211 | sub new { | ||
| 212 | my $class = shift; | ||
| 213 | my %param = @_; | ||
| 214 | |||
| 215 | my $me = bless { | ||
| 216 | width => 640, | ||
| 217 | height => 192, | ||
| 218 | margin_left => 8, | ||
| 219 | margin_bottom => 8, | ||
| 220 | margin_right => 8, | ||
| 221 | margin_top => 8, | ||
| 222 | n_y_tics => 4, # aprox. | ||
| 223 | |||
| 224 | transparent => 1, | ||
| 225 | grid_on_top => 1, | ||
| 226 | draw_grid => 1, | ||
| 227 | draw_border => 1, | ||
| 228 | draw_tic_labels => 1, | ||
| 229 | draw_data_labels => 1, | ||
| 230 | limit_factor => 0, | ||
| 231 | data_label_style => 'text', # or 'box' | ||
| 232 | thickness => 1, | ||
| 233 | tm_time => \&POSIX::localtime, # or gmtime, or... | ||
| 234 | shadow_color => '#CCCCCC', | ||
| 235 | # GD can only antialias on a truecolor image, and only if thickness==1 | ||
| 236 | # yes, I know what the GD documentation says. I also know what the src says... | ||
| 237 | antialias => 0, | ||
| 238 | truecolor => 0, | ||
| 239 | |||
| 240 | # title | ||
| 241 | # x_label | ||
| 242 | # y_label | ||
| 243 | |||
| 244 | # user specified params override defaults | ||
| 245 | %param, | ||
| 246 | |||
| 247 | }, $class; | ||
| 248 | |||
| 249 | $me->adjust(); | ||
| 250 | |||
| 251 | my $im = GD::Image->new( $me->{width}, $me->{height}, $me->{truecolor} ); | ||
| 252 | $me->{img} = $im; | ||
| 253 | |||
| 254 | # Nor long the sun his daily course withheld, | ||
| 255 | # But added colors to the world reveal'd: | ||
| 256 | # When early Turnus, wak'ning with the light, | ||
| 257 | # -- Virgil, Aeneid | ||
| 258 | # allocate some useful colors, 1st is used for bkg | ||
| 259 | my $bkg = $me->color({ color => $me->{background_color} }) if $me->{background_color}; | ||
| 260 | $me->{color}{white} = $im->colorAllocate(255,255,255); | ||
| 261 | $me->{color}{black} = $im->colorAllocate(0,0,0); | ||
| 262 | $me->{color}{blue} = $im->colorAllocate(0, 0, 255); | ||
| 263 | $me->{color}{red} = $im->colorAllocate(255, 0, 0); | ||
| 264 | $me->{color}{green} = $im->colorAllocate(0, 255, 0); | ||
| 265 | $me->{color}{gray} = $im->colorAllocate(128, 128, 128); | ||
| 266 | |||
| 267 | # style for grid lines | ||
| 268 | $im->setStyle(gdTransparent, $me->{color}{gray}, gdTransparent, gdTransparent); | ||
| 269 | |||
| 270 | $im->interlaced('true'); | ||
| 271 | $im->transparent($me->{color}{white}) | ||
| 272 | if $me->{transparent}; | ||
| 273 | |||
| 274 | $im->filledRectangle( 0, 0, $me->{width}-1, $me->{height}-1, ($bkg || $me->{color}{white})); | ||
| 275 | |||
| 276 | my $bc = $me->{border_color} ? $me->img_color($me->{border_color}) : $me->{color}{black}; | ||
| 277 | |||
| 278 | $im->rectangle(0, 0, $me->{width}-1, $me->{height}-1, $bc ) | ||
| 279 | if $me->{draw_border}; | ||
| 280 | |||
| 281 | $me; | ||
| 282 | } | ||
| 283 | |||
| 284 | sub add_data { | ||
| 285 | my $me = shift; | ||
| 286 | my $data = shift; | ||
| 287 | my $opts = shift; | ||
| 288 | |||
| 289 | $me->analyze( $data, $opts ); | ||
| 290 | |||
| 291 | unless( $opts->{style} ){ | ||
| 292 | $opts->{style} = defined $data->[0]{min} ? 'range' : 'line'; | ||
| 293 | } | ||
| 294 | |||
| 295 | push @{$me->{data}}, {data => $data, opts => $opts}; | ||
| 296 | $me->{has_shadow} = 1 if $opts->{shadow}; | ||
| 297 | |||
| 298 | $me; | ||
| 299 | } | ||
| 300 | |||
| 301 | # A plot shall show us all a merry day. | ||
| 302 | # -- Shakespeare, King Richard II | ||
| 303 | sub plot { | ||
| 304 | my $me = shift; | ||
| 305 | |||
| 306 | return unless $me->{data}; | ||
| 307 | return if $me->{all_done}; | ||
| 308 | |||
| 309 | $me->adjust(); | ||
| 310 | $me->clabels(); | ||
| 311 | $me->xlabel(); | ||
| 312 | $me->ylabel(); | ||
| 313 | $me->title(); | ||
| 314 | |||
| 315 | if( $me->{draw_tic_labels} ){ | ||
| 316 | # move margin for xtics before we do ytics | ||
| 317 | $me->{margin_bottom} += 12; | ||
| 318 | $me->adjust(); | ||
| 319 | } | ||
| 320 | |||
| 321 | $me->ytics(); | ||
| 322 | $me->xtics(); | ||
| 323 | |||
| 324 | # draw shadows | ||
| 325 | foreach my $d ( @{$me->{data}} ){ | ||
| 326 | next unless $d->{opts}{shadow}; | ||
| 327 | $me->plot_data( $d->{data}, $d->{opts}, $d->{opts}{shadow} ); | ||
| 328 | } | ||
| 329 | |||
| 330 | $me->axii(); | ||
| 331 | $me->drawgrid() unless $me->{grid_on_top}; | ||
| 332 | |||
| 333 | # plot | ||
| 334 | foreach my $d ( @{$me->{data}} ){ | ||
| 335 | $me->plot_data( $d->{data}, $d->{opts}, undef ); | ||
| 336 | } | ||
| 337 | |||
| 338 | $me->drawgrid() if $me->{grid_on_top}; | ||
| 339 | |||
| 340 | $me->{all_done} = 1; | ||
| 341 | $me; | ||
| 342 | } | ||
| 343 | |||
| 344 | |||
| 345 | # The axis of the earth sticks out visibly through the centre of each and every town or city. | ||
| 346 | # -- Oliver Wendell Holmes, The Autocrat of the Breakfast-Table | ||
| 347 | sub axii { | ||
| 348 | my $me = shift; | ||
| 349 | my $im = $me->{img}; | ||
| 350 | |||
| 351 | # draw axii | ||
| 352 | $im->line( $me->xpt(-1), $me->ypt(-1), $me->xpt(-1), $me->ypt($me->{ymax}), $me->{color}{black}); | ||
| 353 | $im->line( $me->xpt(-1), $me->ypt(-1), $me->xpt($me->{xmax}), $me->ypt(-1), $me->{color}{black}); | ||
| 354 | |||
| 355 | # 'Talking of axes,' said the Duchess, 'chop off her head!' | ||
| 356 | # -- Alice in Wonderland | ||
| 357 | $me; | ||
| 358 | } | ||
| 359 | |||
| 360 | sub set_thickness { | ||
| 361 | my $me = shift; | ||
| 362 | my $tk = shift; | ||
| 363 | my $im = $me->{img}; | ||
| 364 | |||
| 365 | # not available until gd 2.07 | ||
| 366 | return unless $im->can('setThickness'); | ||
| 367 | $im->setThickness($tk); | ||
| 368 | } | ||
| 369 | |||
| 370 | sub gd { | ||
| 371 | my $me = shift; | ||
| 372 | |||
| 373 | $me->plot(); | ||
| 374 | $me->{img}; | ||
| 375 | } | ||
| 376 | |||
| 377 | sub png { | ||
| 378 | my $me = shift; | ||
| 379 | |||
| 380 | $me->plot(); | ||
| 381 | $me->{img}->png( @_ ); | ||
| 382 | } | ||
| 383 | |||
| 384 | sub jpeg { | ||
| 385 | my $me = shift; | ||
| 386 | |||
| 387 | $me->plot(); | ||
| 388 | $me->{img}->jpeg( @_ ); | ||
| 389 | } | ||
| 390 | |||
| 391 | |||
| 392 | # xpt, ypt - convert graph space => image space | ||
| 393 | sub xpt { | ||
| 394 | my $me = shift; | ||
| 395 | my $pt = shift; | ||
| 396 | |||
| 397 | $pt + $me->{margin_left}; | ||
| 398 | } | ||
| 399 | |||
| 400 | sub ypt { | ||
| 401 | my $me = shift; | ||
| 402 | my $pt = shift; | ||
| 403 | |||
| 404 | # make 0 bottom | ||
| 405 | $me->{height} - $pt - $me->{margin_bottom}; | ||
| 406 | } | ||
| 407 | |||
| 408 | # xdatapt, ydatapt - convert data space => image space | ||
| 409 | sub xdatapt { | ||
| 410 | my $me = shift; | ||
| 411 | my $pt = shift; | ||
| 412 | |||
| 413 | $me->xpt( ($pt - $me->{xd_min}) * $me->{xd_scale} ); | ||
| 414 | } | ||
| 415 | |||
| 416 | sub ydatapt { | ||
| 417 | my $me = shift; | ||
| 418 | my $pt = shift; | ||
| 419 | |||
| 420 | $pt = $pt < $me->{yd_min} ? $me->{yd_min} : $pt; | ||
| 421 | $pt = $pt > $me->{yd_max} ? $me->{yd_max} : $pt; | ||
| 422 | |||
| 423 | $me->ypt( ($pt - $me->{yd_min}) * $me->{yd_scale} ); | ||
| 424 | } | ||
| 425 | |||
| 426 | sub adjust { | ||
| 427 | my $me = shift; | ||
| 428 | |||
| 429 | # I have touched the highest point of all my greatness; | ||
| 430 | # -- Shakespeare, King Henry VIII | ||
| 431 | $me->{xmax} = $me->{width} - $me->{margin_right} - $me->{margin_left}; | ||
| 432 | $me->{ymax} = $me->{height} - $me->{margin_bottom} - $me->{margin_top} ; | ||
| 433 | |||
| 434 | if( $me->{data} ){ | ||
| 435 | $me->{xd_scale} = ($me->{xd_min} == $me->{xd_max}) ? 1 | ||
| 436 | : $me->{xmax} / ($me->{xd_max} - $me->{xd_min}); | ||
| 437 | |||
| 438 | $me->{yd_scale} = ($me->{yd_min} == $me->{yd_max}) ? 1 | ||
| 439 | : $me->{ymax} / ($me->{yd_max} - $me->{yd_min}); | ||
| 440 | } | ||
| 441 | |||
| 442 | $me; | ||
| 443 | } | ||
| 444 | |||
| 445 | sub analyze { | ||
| 446 | my $me = shift; | ||
| 447 | my $data = shift; | ||
| 448 | my $opts = shift; | ||
| 449 | my( $st, $et, $pt, $min, $max ); | ||
| 450 | |||
| 451 | $st = $data->[0]{time}; # start time | ||
| 452 | $et = $data->[-1]{time}; # end time | ||
| 453 | $pt = $st; | ||
| 454 | |||
| 455 | foreach my $s (@$data){ | ||
| 456 | croak "data point out of order" if $s->{time} < $pt; | ||
| 457 | my $a = defined $s->{min} ? $s->{min} : $s->{value}; | ||
| 458 | my $b = defined $s->{max} ? $s->{max} : $s->{value}; | ||
| 459 | $a ||= 0 unless $me->{skip_undefined} || $opts->{skip_undefined}; | ||
| 460 | $b ||= 0 unless $me->{skip_undefined} || $opts->{skip_undefined}; | ||
| 461 | ($a, $b) = ($b, $a) if $a > $b; | ||
| 462 | |||
| 463 | $min = $a if defined($a) && ( !defined($min) || $a < $min ); | ||
| 464 | $max = $b if defined($b) && ( !defined($max) || $b > $max ); | ||
| 465 | $pt = $s->{time}; | ||
| 466 | } | ||
| 467 | |||
| 468 | if( $opts->{style} eq 'box' ){ | ||
| 469 | # stretch x axis if drawing wide boxes | ||
| 470 | my $defwid = def_box_width($st, $et, scalar(@$data)); | ||
| 471 | my $w = $data->[0]{width} || $opts->{width} || $me->{boxwidth} || $defwid; | ||
| 472 | $st -= $w/2; | ||
| 473 | |||
| 474 | $w = $data->[-1]{width} || $opts->{width} || $me->{boxwidth} || $defwid; | ||
| 475 | $et += $w/2; | ||
| 476 | |||
| 477 | # boxes are drawn from y=0 | ||
| 478 | $min = 0 if $min > 0; | ||
| 479 | } | ||
| 480 | |||
| 481 | if( $opts->{smooth} || $me->{smooth} ){ | ||
| 482 | # calculate derivative at each point (which may or may not be evenly spaced) | ||
| 483 | for my $i (0 .. @$data-1){ | ||
| 484 | my $here = $data->[$i]; | ||
| 485 | my $left = $i ? $data->[$i-1] : $data->[$i]; | ||
| 486 | my $right = ($i!=@$data-1) ? $data->[$i+1] : $data->[$i]; | ||
| 487 | |||
| 488 | my $dxl = $here->{time} - $left->{time}; | ||
| 489 | my $dxr = $right->{time} - $here->{time}; | ||
| 490 | my $dyl = $here->{value} - $left->{value}; | ||
| 491 | my $dyr = $right->{value} - $here->{value}; | ||
| 492 | |||
| 493 | if( $dxr && $dxl ){ | ||
| 494 | my $dl = $dyl / $dxl; | ||
| 495 | my $dr = $dyr / $dxr; | ||
| 496 | if( $dl < 0 && $dr > 0 || $dl > 0 && $dr < 0 ){ | ||
| 497 | # local extrema | ||
| 498 | $data->[$i]{dydx} = 0; | ||
| 499 | }else{ | ||
| 500 | my $dm = ( $dl * $dxr + $dr * $dxl ) / ($dxr + $dxl); | ||
| 501 | # mathematicaly, $dm is the best estimate of the derivative, and gives the smoothest curve | ||
| 502 | # but, this way looks nicer... | ||
| 503 | my $d = (sort { abs($a) <=> abs($b) } ($dl, $dr, $dm))[0]; | ||
| 504 | $data->[$i]{dydx} = ($d + $dm) / 2; | ||
| 505 | } | ||
| 506 | }elsif($dxr){ | ||
| 507 | $data->[$i]{dydx} = $dyr / $dxr; | ||
| 508 | }elsif($dxl){ | ||
| 509 | $data->[$i]{dydx} = $dyl / $dxl; | ||
| 510 | } | ||
| 511 | } | ||
| 512 | } | ||
| 513 | |||
| 514 | $me->{xd_min} = $st if $st && (!defined($me->{xd_min}) || $st < $me->{xd_min}); | ||
| 515 | $me->{xd_max} = $et if $et && (!defined($me->{xd_max}) || $et > $me->{xd_max}); | ||
| 516 | $me->{yd_min} = $min if !defined($me->{yd_min}) || $min < $me->{yd_min}; | ||
| 517 | $me->{yd_max} = $max if !defined($me->{yd_max}) || $max > $me->{yd_max}; | ||
| 518 | |||
| 519 | } | ||
| 520 | |||
| 521 | # I hear beyond the range of sound, | ||
| 522 | # I see beyond the range of sight, | ||
| 523 | # New earths and skies and seas around, | ||
| 524 | # And in my day the sun doth pale his light. | ||
| 525 | # -- Thoreau, Inspiration | ||
| 526 | sub set_y_range { | ||
| 527 | my $me = shift; | ||
| 528 | my $l = shift; | ||
| 529 | my $h = shift; | ||
| 530 | |||
| 531 | $me->{yd_min} = $l if defined($l) && $l ne ''; | ||
| 532 | $me->{yd_max} = $h if defined($h) && $h ne ''; | ||
| 533 | $me->adjust(); | ||
| 534 | } | ||
| 535 | |||
| 536 | sub set_x_range { | ||
| 537 | my $me = shift; | ||
| 538 | my $l = shift; | ||
| 539 | my $h = shift; | ||
| 540 | |||
| 541 | $me->{xd_min} = $l if defined($l) && $l ne ''; | ||
| 542 | $me->{xd_max} = $h if defined($h) && $h ne ''; | ||
| 543 | $me->adjust(); | ||
| 544 | } | ||
| 545 | |||
| 546 | sub img_color { | ||
| 547 | my $me = shift; | ||
| 548 | my $color = shift; | ||
| 549 | |||
| 550 | $color =~ s/^#//; | ||
| 551 | $color =~ s/\s//g; | ||
| 552 | |||
| 553 | return $me->{color}{$color} if $me->{color}{$color}; | ||
| 554 | my($r,$g,$b) = map {hex} unpack('a2 a2 a2', $color); | ||
| 555 | my $i = $me->{img}->colorAllocate( $r, $g, $b ); | ||
| 556 | $me->{color}{$color} = $i; | ||
| 557 | |||
| 558 | return $i; | ||
| 559 | } | ||
| 560 | |||
| 561 | # choose proper color for plot | ||
| 562 | sub color { | ||
| 563 | my $me = shift; | ||
| 564 | my $data = shift; | ||
| 565 | my $opts = shift; | ||
| 566 | |||
| 567 | # What is your favorite color? | ||
| 568 | # Blue. No yel-- Auuuuuuuugh! | ||
| 569 | # -- Monty Python, Holy Grail | ||
| 570 | my $c = $data->{color} || $opts->{color}; | ||
| 571 | if( $c ){ | ||
| 572 | return $me->img_color( $c ); | ||
| 573 | } | ||
| 574 | |||
| 575 | return $me->{color}{green}; | ||
| 576 | } | ||
| 577 | |||
| 578 | # Titles are marks of honest men, and wise; | ||
| 579 | # The fool or knave that wears a title lies. | ||
| 580 | # -- Edward Young, Love of Fame | ||
| 581 | sub title { | ||
| 582 | my $me = shift; | ||
| 583 | my( $loc ); | ||
| 584 | |||
| 585 | return unless $me->{title}; | ||
| 586 | $me->{margin_top} += 16; | ||
| 587 | $me->adjust(); | ||
| 588 | # center title | ||
| 589 | $loc = ($me->{width} - length($me->{title}) * 7) / 2; | ||
| 590 | $me->{img}->string(gdMediumBoldFont, $loc, 2, $me->{title}, $me->{color}{black}); | ||
| 591 | } | ||
| 592 | |||
| 593 | # when I waked, I found This label on my bosom | ||
| 594 | # -- Shakespeare, Cymbeline | ||
| 595 | sub xlabel { | ||
| 596 | my $me = shift; | ||
| 597 | my( $loc, $y ); | ||
| 598 | |||
| 599 | return unless $me->{x_label}; | ||
| 600 | $me->{margin_bottom} += 16; | ||
| 601 | $me->adjust(); | ||
| 602 | $loc = ($me->{width} - length($me->{x_label}) * 6) / 2; | ||
| 603 | $y = $me->{height} - $me->{margin_bottom} + 8; | ||
| 604 | $me->{img}->string(gdSmallFont, $loc, $y, $me->{x_label}, $me->{color}{black}); | ||
| 605 | } | ||
| 606 | |||
| 607 | sub ylabel { | ||
| 608 | my $me = shift; | ||
| 609 | my( $loc ); | ||
| 610 | |||
| 611 | return unless $me->{y_label}; | ||
| 612 | $me->{margin_left} += 12; | ||
| 613 | $me->adjust(); | ||
| 614 | my $m = ($me->{height} - $me->{margin_top} - $me->{margin_bottom}) / 2 + $me->{margin_top}; | ||
| 615 | $loc = $m + length($me->{y_label}) * 6 / 2; | ||
| 616 | $me->{img}->stringUp(gdSmallFont, 2, $loc, $me->{y_label}, $me->{color}{black}); | ||
| 617 | # small => 12,6; tiny => 10,5 | ||
| 618 | } | ||
| 619 | |||
| 620 | # It must be a very pretty dance | ||
| 621 | # -- Alice in Wonderland | ||
| 622 | # make tic numbers pretty | ||
| 623 | sub pretty { | ||
| 624 | my $me = shift; | ||
| 625 | my $y = shift; | ||
| 626 | my $st = shift; | ||
| 627 | my( $ay, $sc, $b, $prec ); | ||
| 628 | |||
| 629 | return $me->{fmt_value}->($y) if $me->{fmt_value}; | ||
| 630 | $sc = ''; | ||
| 631 | $ay = abs($y); | ||
| 632 | $b = $me->{binary} ? 1024 : 1000; | ||
| 633 | |||
| 634 | if( $ay < 1 ){ | ||
| 635 | if( $ay < 1/$b**3 ){ | ||
| 636 | return "0"; | ||
| 637 | } | ||
| 638 | elsif( $ay < 1/$b**2 ){ | ||
| 639 | $y *= $b ** 3; $st *= $b ** 3; | ||
| 640 | $sc = 'n'; | ||
| 641 | } | ||
| 642 | elsif( $ay < 1/$b ){ | ||
| 643 | $y *= $b**2; $st *= $b**2; | ||
| 644 | $sc = 'u'; | ||
| 645 | } | ||
| 646 | elsif( $ay < 100/$b ){ # QQQ | ||
| 647 | $y *= $b; $st *= $b; | ||
| 648 | $sc = 'm'; | ||
| 649 | } | ||
| 650 | }else{ | ||
| 651 | if( $ay >= $b**4 ){ | ||
| 652 | $y /= $b**4; $st /= $b**4; | ||
| 653 | $sc = 'T'; | ||
| 654 | } | ||
| 655 | elsif( $ay >= $b**3 ){ | ||
| 656 | $y /= $b**3; $st /= $b**3; | ||
| 657 | $sc = 'G'; | ||
| 658 | } | ||
| 659 | elsif( $ay >= $b**2 ){ | ||
| 660 | $y /= $b**2; $st /= $b**2; | ||
| 661 | $sc = 'M'; | ||
| 662 | } | ||
| 663 | elsif( $ay >= $b ){ | ||
| 664 | $y /= $b; $st /= $b; | ||
| 665 | $sc = 'k'; | ||
| 666 | } | ||
| 667 | } | ||
| 668 | $sc .= 'i' if $sc && $me->{binary}; # as per IEC 60027-2 | ||
| 669 | if( $st > 1 ){ | ||
| 670 | $prec = 0; | ||
| 671 | }else{ | ||
| 672 | $prec = abs(floor(log($st)/log(10))); | ||
| 673 | } | ||
| 674 | |||
| 675 | sprintf "%.${prec}f$sc", $y; | ||
| 676 | } | ||
| 677 | |||
| 678 | sub ytics { | ||
| 679 | my $me = shift; | ||
| 680 | my( $min, $max, $tp, $st, $is, $low, $maxw, @tics ); | ||
| 681 | |||
| 682 | $min = $me->{yd_min}; | ||
| 683 | $max = $me->{yd_max}; | ||
| 684 | $maxw = 0; | ||
| 685 | |||
| 686 | if( $min == $max ){ | ||
| 687 | # not a very interesting graph... | ||
| 688 | my $lb = $me->pretty($min, 1); # QQQ | ||
| 689 | my $w = length($lb) * 5 + 6; | ||
| 690 | push @tics, [$me->ydatapt($min), $lb, $w]; | ||
| 691 | $maxw = $w; | ||
| 692 | }else{ | ||
| 693 | $tp = ($max - $min) / $me->{n_y_tics}; # approx spacing of tics | ||
| 694 | if( $me->{binary} ){ | ||
| 695 | $is = 2 ** floor( log($tp)/log(2) ); | ||
| 696 | }else{ | ||
| 697 | $is = 10 ** floor( log($tp)/log(10) ); | ||
| 698 | } | ||
| 699 | $st = floor( $tp / $is ) * $is; # -> 4 - 8, ceil -> 2 - 4 | ||
| 700 | # mathematically, tp/is cannot be less than 1 | ||
| 701 | # but due to floating-point lossage, in rare cases, it might | ||
| 702 | $st ||= $is; | ||
| 703 | $low = int( $min / $st ) * $st; | ||
| 704 | for my $i ( 0 .. (2 * $me->{n_y_tics} + 2) ){ | ||
| 705 | my $y = $low + $i * $st; | ||
| 706 | next if $y < $min; | ||
| 707 | last if $y > $max; | ||
| 708 | my $yy = $me->ydatapt($y); | ||
| 709 | my $label = $me->pretty($y, $st); | ||
| 710 | my $w = 5 * length($label) + 6; | ||
| 711 | $maxw = $w if $w > $maxw; | ||
| 712 | |||
| 713 | push @tics, [$yy, $label, $w]; | ||
| 714 | } | ||
| 715 | } | ||
| 716 | |||
| 717 | if( $me->{draw_tic_labels} ){ | ||
| 718 | # move margin | ||
| 719 | $me->{margin_left} += $maxw; | ||
| 720 | $me->adjust(); | ||
| 721 | } | ||
| 722 | |||
| 723 | $me->{grid}{y} = \@tics; | ||
| 724 | } | ||
| 725 | |||
| 726 | sub drawgrid { | ||
| 727 | my $me = shift; | ||
| 728 | my $im = $me->{img}; | ||
| 729 | |||
| 730 | foreach my $tic (@{$me->{grid}{y}}){ | ||
| 731 | # ytics + horiz lines | ||
| 732 | my $yy = $tic->[0]; | ||
| 733 | $im->line($me->xpt(-1), $yy, $me->xpt(-4), $yy, | ||
| 734 | $me->{color}{black}); | ||
| 735 | $im->line($me->xpt(0), $yy, $me->{width} - $me->{margin_right}, $yy, | ||
| 736 | gdStyled) if $me->{draw_grid}; | ||
| 737 | |||
| 738 | if( $me->{draw_tic_labels} ){ | ||
| 739 | my $label = $tic->[1]; | ||
| 740 | my $w = $tic->[2]; | ||
| 741 | $im->string(gdTinyFont, $me->xpt(-$w), $yy-4, | ||
| 742 | $label, | ||
| 743 | $me->{color}{black}); | ||
| 744 | } | ||
| 745 | } | ||
| 746 | |||
| 747 | foreach my $tic (@{$me->{grid}{x}}){ | ||
| 748 | # xtics + vert lines | ||
| 749 | my( $t, $ll, $label ) = @$tic; | ||
| 750 | |||
| 751 | # supress solid line if adjacent to axis | ||
| 752 | if( $ll && ($t != $me->{xd_min}) ){ | ||
| 753 | # solid line, red label | ||
| 754 | $im->line($me->xdatapt($t), $me->{margin_top}, | ||
| 755 | $me->xdatapt($t), $me->ypt(-4), | ||
| 756 | $me->{color}{black} ); | ||
| 757 | }else{ | ||
| 758 | # tic and grid | ||
| 759 | $im->line($me->xdatapt($t), $me->ypt(-1), | ||
| 760 | $me->xdatapt($t), $me->ypt(-4), | ||
| 761 | $me->{color}{black} ); | ||
| 762 | $im->line($me->xdatapt($t), $me->{margin_top}, | ||
| 763 | $me->xdatapt($t), $me->ypt(0), | ||
| 764 | gdStyled ) if $me->{draw_grid}; | ||
| 765 | } | ||
| 766 | |||
| 767 | if( $me->{draw_tic_labels} ){ | ||
| 768 | my $a = length($label) * 6 / 4; # it looks better not quite centered | ||
| 769 | if( length($label)*6 * 3/4 + $me->xdatapt($t) > $me->{width} ){ | ||
| 770 | # too close to edge, shift | ||
| 771 | $a = $me->xdatapt($t) - $me->{width} + length($label) * 6 + 2; | ||
| 772 | } | ||
| 773 | |||
| 774 | $im->string(gdSmallFont, $me->xdatapt($t)-$a, $me->ypt(-6), | ||
| 775 | $label, $ll ? $me->{color}{red} : $me->{color}{black} ); | ||
| 776 | } | ||
| 777 | } | ||
| 778 | } | ||
| 779 | |||
| 780 | sub xtic_range_data { | ||
| 781 | my $me = shift; # not used | ||
| 782 | my $range = shift; | ||
| 783 | |||
| 784 | my $range_hrs = $range / 3600; | ||
| 785 | my $range_days = $range_hrs / 24; | ||
| 786 | |||
| 787 | # return: step, labeltype, marktype, lti, tmod | ||
| 788 | |||
| 789 | if( $range < 720 ){ | ||
| 790 | (60, $LT_HM, $MT_HR, 1, 1); # tics: 1 min | ||
| 791 | } | ||
| 792 | elsif( $range < 1800 ){ | ||
| 793 | (300, $LT_HM, $MT_HR, 1, 5); # tics: 5 min | ||
| 794 | } | ||
| 795 | elsif( $range_hrs < 2 ){ | ||
| 796 | (600, $LT_HM, $MT_HR, 1, 10); # tics: 10 min | ||
| 797 | } | ||
| 798 | elsif( $range_hrs < 6 ){ | ||
| 799 | (1800, $LT_HR, $MT_MN, 1, 30); # tics: 30 min | ||
| 800 | } | ||
| 801 | elsif( $range_hrs < 13 ){ | ||
| 802 | (3600, $LT_HR, $MT_MN, 2, 1); # tics: 1 hr | ||
| 803 | } | ||
| 804 | elsif( $range_hrs < 25 ){ | ||
| 805 | (3600, $LT_HR, $MT_MN, 2, 2); # tics: 2 hrs | ||
| 806 | } | ||
| 807 | elsif( $range_hrs < 50 ){ | ||
| 808 | (3600, $LT_HR, $MT_MN, 2, 4); # tics: 4 hrs | ||
| 809 | } | ||
| 810 | elsif( $range_hrs < 75 ){ | ||
| 811 | (3600, $LT_HR, $MT_MN, 2, 6); # tics: 6 hrs | ||
| 812 | } | ||
| 813 | |||
| 814 | # NB: days shorter or longer than 24 hours are corrected for below | ||
| 815 | elsif( $range_days < 15 ){ | ||
| 816 | (3600*24, $LT_DW, $MT_SU, 3, 1); # tics 1 day | ||
| 817 | } | ||
| 818 | elsif( $range_days < 22 ){ | ||
| 819 | (3600*24, $LT_DM, $MT_M1, 3, 2); # tics: 2 days | ||
| 820 | } | ||
| 821 | elsif( $range_days < 80 ){ | ||
| 822 | (3600*24, $LT_DM, $MT_M1, 3, 7); # tics: 7 days | ||
| 823 | } | ||
| 824 | elsif( $range_days < 168 ){ | ||
| 825 | (3600*24, $LT_DM, $MT_Y1, 3, 14); # tics: 14 days | ||
| 826 | } | ||
| 827 | # NB: months shorter than 31 days are corrected for below | ||
| 828 | elsif( $range_days < 370 ){ | ||
| 829 | (3600*24*31, $LT_DM, $MT_Y1, 4, 1); # tics: 1 month | ||
| 830 | } | ||
| 831 | elsif( $range_days < 500 ){ | ||
| 832 | (3600*24*31, $LT_DM, $MT_Y1, 4, 2); # tics: 2 month | ||
| 833 | } | ||
| 834 | elsif( $range_days < 1000 ){ | ||
| 835 | (3600*24*31, $LT_DM, $MT_Y1, 4, 3); # tics: 3 month | ||
| 836 | } | ||
| 837 | elsif( $range_days < 2000 ){ | ||
| 838 | (3600*24*31, $LT_DM, $MT_NO, 4, 6); # tics: 6 month | ||
| 839 | } | ||
| 840 | |||
| 841 | else{ | ||
| 842 | # NB: years less than 366 days are corrected for below | ||
| 843 | (3600*24*366, $LT_YR, $MT_NO, 4, 12); # tics: 1 yr | ||
| 844 | } | ||
| 845 | } | ||
| 846 | |||
| 847 | sub xtic_align_initial { | ||
| 848 | my $me = shift; | ||
| 849 | my $step = shift; | ||
| 850 | |||
| 851 | my $t = ($step < 3600) ? (int($me->{xd_min} / $step) * $step) | ||
| 852 | : (int($me->{xd_min} / 3600) * 3600); | ||
| 853 | |||
| 854 | if( $step >= 3600*24*365 ){ | ||
| 855 | while(1){ | ||
| 856 | # search for 1jan | ||
| 857 | my @lt = $me->{tm_time}($t); | ||
| 858 | last if $lt[4] == 0 && $lt[3] == 1 && $lt[2] == 0; | ||
| 859 | # jump fwd: 1M, 1D, or 1H | ||
| 860 | my $dt = ($lt[4] != 11) ? 24*30 : ($lt[3] < 30) ? 24 : 1; | ||
| 861 | $t += $dt * 3600; | ||
| 862 | } | ||
| 863 | } | ||
| 864 | elsif( $step >= 3600*24*31 ){ | ||
| 865 | while(1){ | ||
| 866 | # find 1st of mon | ||
| 867 | my @lt = $me->{tm_time}($t); | ||
| 868 | last if $lt[3] == 1 && $lt[2] == 0; | ||
| 869 | my $dt = ($lt[3] < 28) ? 24 : 1; | ||
| 870 | $t += $dt * 3600; | ||
| 871 | } | ||
| 872 | } | ||
| 873 | elsif( $step >= 3600*24 ){ | ||
| 874 | while(1){ | ||
| 875 | # search for midnight | ||
| 876 | my @lt = $me->{tm_time}($t); | ||
| 877 | last unless $lt[2]; | ||
| 878 | $t += 3600; | ||
| 879 | } | ||
| 880 | } | ||
| 881 | |||
| 882 | $t; | ||
| 883 | } | ||
| 884 | |||
| 885 | sub xtics { | ||
| 886 | my $me = shift; | ||
| 887 | my @tics; | ||
| 888 | |||
| 889 | # this is good for (roughly) 10 mins - 10 yrs | ||
| 890 | return if $me->{xd_max} == $me->{xd_min}; | ||
| 891 | |||
| 892 | my $range = $me->{xd_max} - $me->{xd_min}; | ||
| 893 | my $range_hrs = $range / 3600; | ||
| 894 | my $range_days = $range_hrs / 24; | ||
| 895 | |||
| 896 | my ($step, $labtyp, $marktyp, $lti, $tmod) = $me->xtic_range_data( $range ); | ||
| 897 | my $t = $me->xtic_align_initial( $step ); | ||
| 898 | |||
| 899 | # print "days: $range_days, lt: $labtyp, lti: $lti, tmod: $tmod, st: $step\n"; | ||
| 900 | # print STDERR "t: $t ", scalar(localtime $t), "\n"; | ||
| 901 | |||
| 902 | for( ; $t<$me->{xd_max}; $t+=$step ){ | ||
| 903 | my $redmark = 0; | ||
| 904 | next if $t < $me->{xd_min}; | ||
| 905 | my @lt = $me->{tm_time}($t); | ||
| 906 | my @rlt = @lt; | ||
| 907 | # months go from 0. days from 1. absurd! | ||
| 908 | $lt[3]--; | ||
| 909 | # mathematically, 28 is divisible by 7. but that just looks silly. | ||
| 910 | $lt[3] = 22 if $lt[3] > 22 && $lti==3 && $tmod >= 7; | ||
| 911 | |||
| 912 | if( $step >= 3600*24 && $lt[2] ){ | ||
| 913 | # handle daylight saving time changes - resync to midnight | ||
| 914 | my $dt = ($lt[2] > 12 ? $lt[2] - 24 : $lt[2]) * 3600; | ||
| 915 | $dt += $lt[1] * 60; | ||
| 916 | $t -= $dt; | ||
| 917 | redo; | ||
| 918 | } | ||
| 919 | if( $step >= 3600*24*31 && $lt[3] ){ | ||
| 920 | # some months are not 31 days! | ||
| 921 | # also corrects years that do not leap | ||
| 922 | my $dt = $lt[3] * 3600*24; | ||
| 923 | $t -= $dt; | ||
| 924 | redo; | ||
| 925 | } | ||
| 926 | |||
| 927 | next if $lt[$lti] % $tmod; | ||
| 928 | next if $lt[3] && $lti > 3; | ||
| 929 | next if $lt[2] && $lti > 2; | ||
| 930 | next if $lt[1] && $lti > 1; | ||
| 931 | next if $lt[0] && $lti > 0; | ||
| 932 | |||
| 933 | |||
| 934 | $redmark = 1 if $marktyp == $MT_HR && !$lt[1]; # on the hour | ||
| 935 | $redmark = 1 if $marktyp == $MT_MN && !$lt[2] && !$lt[1]; # midnight | ||
| 936 | $redmark = 1 if $marktyp == $MT_SU && !$lt[6]; # sunday | ||
| 937 | $redmark = 1 if $marktyp == $MT_M1 && !$lt[3]; # 1st of month | ||
| 938 | $redmark = 1 if $marktyp == $MT_Y1 && !$lt[3] && !$lt[4]; # 1 jan | ||
| 939 | |||
| 940 | my $label; | ||
| 941 | # NB: strftime obeys LC_TIME for localized day/month names | ||
| 942 | # (if locales are supported in the OS and perl) | ||
| 943 | if( $labtyp == $LT_HM ){ | ||
| 944 | $label = sprintf "%d:%0.2d", $rlt[2], $rlt[1]; # time | ||
| 945 | } | ||
| 946 | if( $labtyp == $LT_HR ){ | ||
| 947 | if( $redmark ){ | ||
| 948 | $label = strftime("%d/%b", @rlt); # date DD/Mon | ||
| 949 | }else{ | ||
| 950 | $label = sprintf "%d:%0.2d", $rlt[2], $rlt[1]; # time | ||
| 951 | } | ||
| 952 | } | ||
| 953 | if( $labtyp == $LT_DW ){ | ||
| 954 | if( $redmark ){ | ||
| 955 | $label = strftime("%d/%b", @rlt); # date DD/Mon | ||
| 956 | }else{ | ||
| 957 | $label = strftime("%a", @rlt); # day of week | ||
| 958 | } | ||
| 959 | } | ||
| 960 | if( $labtyp == $LT_DM ){ | ||
| 961 | if( !$lt[3] && !$lt[4] ){ | ||
| 962 | $label = $rlt[5] + 1900; # year | ||
| 963 | }else{ | ||
| 964 | $label = strftime("%d/%b", @rlt); # date DD/Mon | ||
| 965 | } | ||
| 966 | } | ||
| 967 | if( $labtyp == $LT_YR ){ | ||
| 968 | $label = $rlt[5] + 1900; # year | ||
| 969 | } | ||
| 970 | push @tics, [$t, $redmark, $label]; | ||
| 971 | } | ||
| 972 | $me->{grid}{x} = \@tics; | ||
| 973 | |||
| 974 | } | ||
| 975 | |||
| 976 | # it shall be inventoried, and every particle and utensil | ||
| 977 | # labelled to my will: as, item, two lips, | ||
| 978 | # indifferent red; item, two grey eyes, with lids to | ||
| 979 | # them; item, one neck, one chin, and so forth. Were | ||
| 980 | # you sent hither to praise me? | ||
| 981 | # -- Shakespeare, Twelfth Night | ||
| 982 | sub clabels { | ||
| 983 | my $me = shift; | ||
| 984 | |||
| 985 | return unless $me->{draw_data_labels}; | ||
| 986 | |||
| 987 | my $rs = 0; | ||
| 988 | my $rm = 0; | ||
| 989 | if( $me->{data_label_style} eq 'box' ){ | ||
| 990 | $rs = 6; | ||
| 991 | $rm = 3; | ||
| 992 | } | ||
| 993 | |||
| 994 | my( $tw, $r, @cl, @cx ); | ||
| 995 | $tw = $r = 0; | ||
| 996 | # round the neck of the bottle was a paper label, with the | ||
| 997 | # words 'DRINK ME' beautifully printed on it in large letters | ||
| 998 | # -- Alice in Wonderland | ||
| 999 | foreach my $d (@{$me->{data}}){ | ||
| 1000 | my $l = $d->{opts}{label}; | ||
| 1001 | my $c = $d->{opts}{color}; | ||
| 1002 | next unless $l; | ||
| 1003 | my $w = length($l) * 5 + 6; | ||
| 1004 | $w += $rm + $rs; | ||
| 1005 | |||
| 1006 | if( $tw + $w > $me->{width} - $me->{margin_left} - $me->{margin_right} ){ | ||
| 1007 | $r ++; | ||
| 1008 | $tw = 0; | ||
| 1009 | } | ||
| 1010 | push @cx, [$l, $tw, $r, $c]; | ||
| 1011 | $tw += $w; | ||
| 1012 | } | ||
| 1013 | |||
| 1014 | my $i = 0; | ||
| 1015 | foreach my $x (@cx){ | ||
| 1016 | my $xx = $x->[1] + $me->{margin_left}; | ||
| 1017 | my $y = $me->{height} - ($r - $x->[2] + 1) * 10; | ||
| 1018 | my $c = $x->[3]; | ||
| 1019 | if( $rs ){ | ||
| 1020 | $me->{img}->filledRectangle($xx, $y+1, $xx+$rs, $y+$rs+1, $me->color({color => $c})); | ||
| 1021 | $me->{img}->rectangle($xx, $y+1, $xx+$rs, $y+$rs+1, $me->{color}{black}); | ||
| 1022 | $me->{img}->string(gdTinyFont, $xx+$rs+$rm, $y, $x->[0], $me->{color}{black}); | ||
| 1023 | }else{ | ||
| 1024 | $me->{img}->string(gdTinyFont, $xx, $y, $x->[0], $me->color({color => $c})); | ||
| 1025 | } | ||
| 1026 | } | ||
| 1027 | if( @cx ){ | ||
| 1028 | $me->{margin_bottom} += ($r + 1) * 10; | ||
| 1029 | $me->adjust(); | ||
| 1030 | } | ||
| 1031 | } | ||
| 1032 | |||
| 1033 | sub plot_data { | ||
| 1034 | my $me = shift; | ||
| 1035 | my $data = shift; | ||
| 1036 | my $opts = shift; | ||
| 1037 | my $shadow = shift; | ||
| 1038 | |||
| 1039 | return unless $data && @$data; | ||
| 1040 | |||
| 1041 | # 'What did they draw?' said Alice, quite forgetting her promise. | ||
| 1042 | # -- Alice in Wonderland | ||
| 1043 | if( $opts->{style} eq 'line' ){ | ||
| 1044 | # 'You can draw water out of a water-well,' said the Hatter | ||
| 1045 | # -- Alice in Wonderland | ||
| 1046 | $me->draw_line( $data, $opts, $shadow ); | ||
| 1047 | } | ||
| 1048 | elsif( $opts->{style} eq 'filled' ){ | ||
| 1049 | # I should think you could draw treacle out of a treacle-well | ||
| 1050 | # -- Alice in Wonderland | ||
| 1051 | $me->draw_filled( $data, $opts, $shadow ); | ||
| 1052 | } | ||
| 1053 | elsif( $opts->{style} eq 'range' ){ | ||
| 1054 | # did you ever see such a thing as a drawing of a muchness? | ||
| 1055 | # -- Alice in Wonderland | ||
| 1056 | $me->draw_range( $data, $opts, $shadow ); | ||
| 1057 | }elsif( $opts->{style} eq 'points' ){ | ||
| 1058 | # and they drew all manner of things--everything that begins with an M--' | ||
| 1059 | # -- Alice in Wonderland | ||
| 1060 | $me->draw_points( $data, $opts, $shadow ); | ||
| 1061 | }elsif( $opts->{style} eq 'box' ){ | ||
| 1062 | $me->draw_boxes( $data, $opts, $shadow ); | ||
| 1063 | }else{ | ||
| 1064 | croak "unknown graph style--cannot draw"; | ||
| 1065 | } | ||
| 1066 | } | ||
| 1067 | |||
| 1068 | # A flattering painter, who made it his care | ||
| 1069 | # To draw men as they ought to be, not as they are. | ||
| 1070 | # -- Oliver Goldsmith, Retaliation | ||
| 1071 | |||
| 1072 | sub draw_filled { | ||
| 1073 | my $me = shift; | ||
| 1074 | my $data = shift; | ||
| 1075 | my $opts = shift; | ||
| 1076 | my $shadow = shift; | ||
| 1077 | |||
| 1078 | my $im = $me->{img}; | ||
| 1079 | my $limit = $me->{limit_factor} * ($me->{xd_max} - $me->{xd_min}) / @$data; | ||
| 1080 | my $skipundef = $opts->{skip_undefined} || $me->{skip_undefined}; | ||
| 1081 | my $thick = $opts->{thickness} || $me->{thickness}; | ||
| 1082 | my $smooth = $opts->{smooth} || $me->{smooth}; | ||
| 1083 | my $shcolor = $shadow ? $me->img_color($shadow->{color} || $me->{shadow_color} ) : undef; | ||
| 1084 | my($px, $py, $pxdpt, $pydpt, $pdydx); | ||
| 1085 | my $ypt0 = $me->ypt(0); | ||
| 1086 | |||
| 1087 | $thick += $shadow->{dw} if $shadow; | ||
| 1088 | $me->set_thickness( $thick ) if $thick; | ||
| 1089 | |||
| 1090 | foreach my $s ( @$data ){ | ||
| 1091 | my $x = $s->{time}; | ||
| 1092 | my $y = $s->{value}; | ||
| 1093 | |||
| 1094 | next if $x < $me->{xd_min} || $x > $me->{xd_max}; | ||
| 1095 | |||
| 1096 | my $xdpt = $me->xdatapt($x); | ||
| 1097 | my $ydpt = $me->ydatapt($y); | ||
| 1098 | my $dydx; | ||
| 1099 | |||
| 1100 | if( $shadow ){ | ||
| 1101 | $xdpt += $shadow->{dx}; | ||
| 1102 | $ydpt += $shadow->{dy}; | ||
| 1103 | } | ||
| 1104 | |||
| 1105 | if( defined($y) || !$skipundef ){ | ||
| 1106 | my $color = $shadow ? $shcolor : $me->color($s, $opts); | ||
| 1107 | |||
| 1108 | if( defined($px) && ($xdpt - $pxdpt > 1) && (!$limit || $x - $px <= $limit) ){ | ||
| 1109 | if( $smooth ){ | ||
| 1110 | next unless defined $s->{dydx}; | ||
| 1111 | $dydx = - $s->{dydx} * $me->{yd_scale} / $me->{xd_scale}; | ||
| 1112 | $me->curve($pxdpt, $pydpt, $pdydx, | ||
| 1113 | $xdpt, $ydpt, $dydx, | ||
| 1114 | $smooth, \&curve_filled, [$color, $ypt0]); | ||
| 1115 | }else{ | ||
| 1116 | my $poly = GD::Polygon->new; | ||
| 1117 | $poly->addPt($pxdpt, $ypt0); | ||
| 1118 | $poly->addPt($pxdpt, $pydpt); | ||
| 1119 | $poly->addPt($xdpt, $ydpt); | ||
| 1120 | $poly->addPt($xdpt, $ypt0); | ||
| 1121 | $im->filledPolygon($poly, $color); | ||
| 1122 | } | ||
| 1123 | }else{ | ||
| 1124 | $im->line( $xdpt, $ypt0, | ||
| 1125 | $xdpt, $ydpt, | ||
| 1126 | $color); | ||
| 1127 | } | ||
| 1128 | $px = $x; $pxdpt = $xdpt; | ||
| 1129 | $py = $y; $pydpt = $ydpt; | ||
| 1130 | $pdydx = $dydx; | ||
| 1131 | }else{ | ||
| 1132 | $px = undef; | ||
| 1133 | } | ||
| 1134 | } | ||
| 1135 | $me->set_thickness( 1 ) if $thick; | ||
| 1136 | } | ||
| 1137 | |||
| 1138 | sub draw_line { | ||
| 1139 | my $me = shift; | ||
| 1140 | my $data = shift; | ||
| 1141 | my $opts = shift; | ||
| 1142 | my $shadow = shift; | ||
| 1143 | |||
| 1144 | my $im = $me->{img}; | ||
| 1145 | my $limit = $me->{limit_factor} * ($me->{xd_max} - $me->{xd_min}) / @$data; | ||
| 1146 | my $thick = $opts->{thickness} || $me->{thickness}; | ||
| 1147 | my $skipundef = $opts->{skip_undefined} || $me->{skip_undefined}; | ||
| 1148 | my $smooth = $opts->{smooth} || $me->{smooth}; | ||
| 1149 | my($px, $py, $pxdpt, $pydpt, $pdydx); | ||
| 1150 | |||
| 1151 | $thick += $shadow->{dw} if $shadow; | ||
| 1152 | $me->set_thickness( $thick ) if $thick; | ||
| 1153 | |||
| 1154 | my $shcolor = $shadow ? $me->img_color($shadow->{color} || $me->{shadow_color} ) : undef; | ||
| 1155 | |||
| 1156 | foreach my $s ( @$data ){ | ||
| 1157 | my $x = $s->{time}; | ||
| 1158 | my $y = $s->{value}; | ||
| 1159 | |||
| 1160 | next if $x < $me->{xd_min} || $x > $me->{xd_max}; | ||
| 1161 | |||
| 1162 | my $xdpt = $me->xdatapt($x); | ||
| 1163 | my $ydpt = $me->ydatapt($y); | ||
| 1164 | my $dydx = $smooth ? - $s->{dydx} * $me->{yd_scale} / $me->{xd_scale} : undef; | ||
| 1165 | |||
| 1166 | if( $shadow ){ | ||
| 1167 | $xdpt += $shadow->{dx}; | ||
| 1168 | $ydpt += $shadow->{dy}; | ||
| 1169 | } | ||
| 1170 | |||
| 1171 | if( defined($y) || !$skipundef ){ | ||
| 1172 | my $color = $shadow ? $shcolor : $me->color($s, $opts); | ||
| 1173 | |||
| 1174 | if( $me->{antialias} && $thick == 1 ){ | ||
| 1175 | # GD cannot antialias a thick line | ||
| 1176 | $im->setAntiAliased($color); | ||
| 1177 | $color = gdAntiAliased; | ||
| 1178 | } | ||
| 1179 | |||
| 1180 | if( defined($px) && (!$limit || $x - $px <= $limit) ){ | ||
| 1181 | if( $smooth ){ | ||
| 1182 | next unless defined $s->{dydx}; | ||
| 1183 | $me->curve($pxdpt, $pydpt, $pdydx, | ||
| 1184 | $xdpt, $ydpt, $dydx, | ||
| 1185 | $smooth, \&curve_line, [$color]); | ||
| 1186 | }else{ | ||
| 1187 | $im->line( $pxdpt, $pydpt, | ||
| 1188 | $xdpt, $ydpt, | ||
| 1189 | $color ); | ||
| 1190 | } | ||
| 1191 | }else{ | ||
| 1192 | $im->setPixel($xdpt, $ydpt, | ||
| 1193 | $color ); | ||
| 1194 | } | ||
| 1195 | $px = $x; $pxdpt = $xdpt; | ||
| 1196 | $py = $y; $pydpt = $ydpt; | ||
| 1197 | $pdydx = $dydx; | ||
| 1198 | }else{ | ||
| 1199 | $px = undef; | ||
| 1200 | } | ||
| 1201 | } | ||
| 1202 | $me->set_thickness( 1 ) if $thick; | ||
| 1203 | } | ||
| 1204 | |||
| 1205 | # GD has only circular arcs, not bezier or cubic splines | ||
| 1206 | # bezier math is easier than trying to use circular arcs | ||
| 1207 | sub curve { | ||
| 1208 | my $me = shift; | ||
| 1209 | my( $x0, $y0, $dydx0, | ||
| 1210 | $x1, $y1, $dydx1, | ||
| 1211 | $smooth, $fnc, $args ) = @_; | ||
| 1212 | |||
| 1213 | # pick bezier control points | ||
| 1214 | # smooth = (.5 - 1) gives nice curves | ||
| 1215 | # smooth > 1 gives straighter segments | ||
| 1216 | # smooth <= .5 takes the graph on a drug trip | ||
| 1217 | my $dxt = ($x1 - $x0) / ($smooth * 3); | ||
| 1218 | my $cx0 = $x0 + $dxt; | ||
| 1219 | my $cx1 = $x1 - $dxt; | ||
| 1220 | my $cy0 = $y0 + $dydx0 * $dxt; | ||
| 1221 | my $cy1 = $y1 - $dydx1 * $dxt; | ||
| 1222 | |||
| 1223 | # bezier coefficients | ||
| 1224 | my $ax = - $x0 + 3 * $cx0 - 3 * $cx1 + $x1; | ||
| 1225 | my $ay = - $y0 + 3 * $cy0 - 3 * $cy1 + $y1; | ||
| 1226 | my $bx = 3 * $x0 - 6 * $cx0 + 3 * $cx1; | ||
| 1227 | my $by = 3 * $y0 - 6 * $cy0 + 3 * $cy1; | ||
| 1228 | my $cx = - 3 * $x0 + 3 * $cx0; | ||
| 1229 | my $cy = - 3 * $y0 + 3 * $cy0; | ||
| 1230 | my $dx = $x0; | ||
| 1231 | my $dy = $y0; | ||
| 1232 | |||
| 1233 | # draw bezier curve | ||
| 1234 | my $px = $x0; | ||
| 1235 | my $py = $y0; | ||
| 1236 | |||
| 1237 | # my $im = $me->{img}; | ||
| 1238 | # $im->line($x0,$y0, $cx0,$cy0, $me->img_color('00ff00')); | ||
| 1239 | # $im->line($x1,$y1, $cx1,$cy1, $me->img_color('00ff00')); | ||
| 1240 | # $im->line($cx0,$cy0, $cx1,$cy1, $me->img_color('0000ff')); | ||
| 1241 | |||
| 1242 | my $ymax = $me->{height} - $me->{margin_bottom}; | ||
| 1243 | my $ymin = $me->{margin_top}; | ||
| 1244 | |||
| 1245 | my $T = ($x1 - $x0) + abs($y1 - $y0); | ||
| 1246 | for my $tt (1 .. $T){ | ||
| 1247 | my $t = $tt / $T; | ||
| 1248 | my $x = $ax * $t**3 + $bx * $t**2 + $cx * $t + $dx; | ||
| 1249 | my $y = $ay * $t**3 + $by * $t**2 + $cy * $t + $dy; | ||
| 1250 | |||
| 1251 | # QQQ - handle out-of-bounds segments how? | ||
| 1252 | if( $y >= $ymin && $y <= $ymax && $py >= $ymin && $py <= $ymax ){ | ||
| 1253 | $fnc->($me, $px,$py, $x,$y, 0, @$args); | ||
| 1254 | }else{ | ||
| 1255 | $fnc->($me, $px,$py, $x,$y, [$ymin, $ymax], @$args); | ||
| 1256 | } | ||
| 1257 | $px = $x; $py = $y; | ||
| 1258 | } | ||
| 1259 | } | ||
| 1260 | |||
| 1261 | sub curve_line { | ||
| 1262 | my $me = shift; | ||
| 1263 | my ($px, $py, $x, $y, $oob, $color) = @_; | ||
| 1264 | |||
| 1265 | return if $oob; | ||
| 1266 | $me->{img}->line($px,$py, $x,$y, $color); | ||
| 1267 | } | ||
| 1268 | |||
| 1269 | sub curve_filled { | ||
| 1270 | my $me = shift; | ||
| 1271 | my ($px, $py, $x, $y, $oob, $color, $y0) = @_; | ||
| 1272 | |||
| 1273 | if( $oob ){ | ||
| 1274 | my($ymin, $ymax) = @$oob; | ||
| 1275 | $y = $ymin if $y < $ymin; | ||
| 1276 | $py = $ymin if $py < $ymin; | ||
| 1277 | $y = $ymax if $y > $ymax; | ||
| 1278 | $py = $ymax if $py > $ymax; | ||
| 1279 | } | ||
| 1280 | |||
| 1281 | my $poly = GD::Polygon->new; | ||
| 1282 | $poly->addPt($px, $y0); | ||
| 1283 | $poly->addPt($px, $py); | ||
| 1284 | $poly->addPt($x, $y); | ||
| 1285 | $poly->addPt($x, $y0); | ||
| 1286 | $me->{img}->filledPolygon($poly, $color); | ||
| 1287 | } | ||
| 1288 | |||
| 1289 | |||
| 1290 | sub draw_range { | ||
| 1291 | my $me = shift; | ||
| 1292 | my $data = shift; | ||
| 1293 | my $opts = shift; | ||
| 1294 | my $shadow = shift; | ||
| 1295 | |||
| 1296 | return if $shadow; | ||
| 1297 | my $im = $me->{img}; | ||
| 1298 | my $limit = $me->{limit_factor} * ($me->{xd_max} - $me->{xd_min}) / @$data; | ||
| 1299 | my $skipundef = $opts->{skip_undefined} || $me->{skip_undefined}; | ||
| 1300 | my($px, $pn, $pm, $pxdpt); | ||
| 1301 | |||
| 1302 | foreach my $s ( @$data ){ | ||
| 1303 | my $x = $s->{time}; | ||
| 1304 | my $a = defined $s->{min} ? $s->{min} : $s->{value}; | ||
| 1305 | my $b = defined $s->{max} ? $s->{max} : $s->{value}; | ||
| 1306 | my $xdpt = $me->xdatapt($x); | ||
| 1307 | |||
| 1308 | next if $x < $me->{xd_min} || $x > $me->{xd_max}; | ||
| 1309 | |||
| 1310 | $a = $b if !defined($a) && $skipundef; | ||
| 1311 | $b = $a if !defined($b) && $skipundef; | ||
| 1312 | |||
| 1313 | if( defined($a) || !$skipundef ){ | ||
| 1314 | |||
| 1315 | if( defined($px) && ($xdpt - $pxdpt > 1) && (!$limit || $x - $px <= $limit) ){ | ||
| 1316 | my $poly = GD::Polygon->new; | ||
| 1317 | $poly->addPt($pxdpt, $me->ydatapt($pn)); | ||
| 1318 | $poly->addPt($pxdpt, $me->ydatapt($pm)); | ||
| 1319 | $poly->addPt($xdpt, $me->ydatapt($b)); | ||
| 1320 | $poly->addPt($xdpt, $me->ydatapt($a)); | ||
| 1321 | $im->filledPolygon($poly, $me->color($s, $opts)); | ||
| 1322 | }else{ | ||
| 1323 | $im->line( $xdpt, $me->ydatapt($b), | ||
| 1324 | $xdpt, $me->ydatapt($a), | ||
| 1325 | $me->color($s, $opts) ); | ||
| 1326 | } | ||
| 1327 | $px = $x; $pn = $a; $pm = $b; | ||
| 1328 | $pxdpt = $xdpt; | ||
| 1329 | }else{ | ||
| 1330 | $px = undef; | ||
| 1331 | } | ||
| 1332 | } | ||
| 1333 | } | ||
| 1334 | |||
| 1335 | sub draw_points { | ||
| 1336 | my $me = shift; | ||
| 1337 | my $data = shift; | ||
| 1338 | my $opts = shift; | ||
| 1339 | my $shadow = shift; | ||
| 1340 | |||
| 1341 | my $im = $me->{img}; | ||
| 1342 | my $skipundef = $opts->{skip_undefined} || $me->{skip_undefined}; | ||
| 1343 | my $shcolor = $shadow ? $me->img_color($shadow->{color} || $me->{shadow_color} ) : undef; | ||
| 1344 | |||
| 1345 | foreach my $s ( @$data ){ | ||
| 1346 | my $x = $s->{time}; | ||
| 1347 | my $y = $s->{value}; | ||
| 1348 | my $d = $s->{diam} || $opts->{diam} || 4; | ||
| 1349 | my $c = $shadow ? $shcolor : $me->color($s, $opts); | ||
| 1350 | |||
| 1351 | next if $x < $me->{xd_min} || $x > $me->{xd_max}; | ||
| 1352 | next if !defined($y) && $skipundef; | ||
| 1353 | my $xdpt = $me->xdatapt($x); | ||
| 1354 | my $ydpt = $me->ydatapt($y); | ||
| 1355 | |||
| 1356 | if( $shadow ){ | ||
| 1357 | $d += $shadow->{dw}; | ||
| 1358 | $xdpt += $shadow->{dx}; | ||
| 1359 | $ydpt += $shadow->{dy}; | ||
| 1360 | } | ||
| 1361 | |||
| 1362 | while( $d > 0 ){ | ||
| 1363 | $im->arc( $xdpt, $ydpt, | ||
| 1364 | $d, $d, 0, 360, | ||
| 1365 | $c ); | ||
| 1366 | $d -= 2; | ||
| 1367 | } | ||
| 1368 | } | ||
| 1369 | } | ||
| 1370 | |||
| 1371 | sub def_box_width { | ||
| 1372 | my $ta = shift; | ||
| 1373 | my $tb = shift; | ||
| 1374 | my $nd = shift; | ||
| 1375 | |||
| 1376 | return ($tb - $ta) / ($nd - 1) if $nd > 1; | ||
| 1377 | return ($tb - $ta) / $nd if $nd; | ||
| 1378 | 1; | ||
| 1379 | } | ||
| 1380 | |||
| 1381 | sub draw_boxes { | ||
| 1382 | my $me = shift; | ||
| 1383 | my $data = shift; | ||
| 1384 | my $opts = shift; | ||
| 1385 | my $shadow = shift; | ||
| 1386 | |||
| 1387 | my $im = $me->{img}; | ||
| 1388 | my $defwid = def_box_width($data->[0]{time}, $data->[-1]{time}, scalar(@$data)); | ||
| 1389 | my $thick = $opts->{thickness} || $me->{thickness}; | ||
| 1390 | my $skipundef = $opts->{skip_undefined} || $me->{skip_undefined}; | ||
| 1391 | my $shcolor = $shadow ? $me->img_color($shadow->{color} || $me->{shadow_color} ) : undef; | ||
| 1392 | |||
| 1393 | $thick += $shadow->{dw} if $shadow; | ||
| 1394 | $me->set_thickness( $thick ) if $thick; | ||
| 1395 | |||
| 1396 | foreach my $s ( @$data ){ | ||
| 1397 | my $x = $s->{time}; | ||
| 1398 | my $y = $s->{value}; | ||
| 1399 | my $w = $s->{width} || $opts->{width} || $me->{boxwidth} || $defwid; | ||
| 1400 | my $y0 = $opts->{boxbase} || $me->{boxbase} || 0; | ||
| 1401 | my $c = $shadow ? $shcolor : $me->color($s, $opts); | ||
| 1402 | |||
| 1403 | next if $x < $me->{xd_min} || $x > $me->{xd_max}; | ||
| 1404 | next if !defined($y) && $skipundef; | ||
| 1405 | |||
| 1406 | # because GD cares... | ||
| 1407 | my $ya = $me->ydatapt($y > $y0 ? $y : $y0); | ||
| 1408 | my $yb = $me->ydatapt($y > $y0 ? $y0 : $y); | ||
| 1409 | my $xa = $me->xdatapt($x - $w/2); | ||
| 1410 | my $xb = $me->xdatapt($x + $w/2); | ||
| 1411 | |||
| 1412 | if( $shadow ){ | ||
| 1413 | $xa += $shadow->{dx}; | ||
| 1414 | $xb += $shadow->{dx}; | ||
| 1415 | $ya += $shadow->{dy}; | ||
| 1416 | $yb += $shadow->{dy}; | ||
| 1417 | } | ||
| 1418 | |||
| 1419 | if( $opts->{filled} || $s->{filled} ){ | ||
| 1420 | $im->filledRectangle( $xa, $ya, $xb, $yb, $c); | ||
| 1421 | }else{ | ||
| 1422 | $im->rectangle( $xa, $ya, $xb, $yb, $c); | ||
| 1423 | } | ||
| 1424 | } | ||
| 1425 | |||
| 1426 | $me->set_thickness( 1 ) if $thick; | ||
| 1427 | } | ||
| 1428 | |||
| 1429 | |||
| 1430 | =head1 EXAMPLE IMAGES | ||
| 1431 | |||
| 1432 | http://argus.tcp4me.com/shots.html | ||
| 1433 | http://search.cpan.org/src/JAW/Chart-Strip-1.07/eg/index.html | ||
| 1434 | |||
| 1435 | =head1 LICENSE | ||
| 1436 | |||
| 1437 | This software may be copied and distributed under the terms | ||
| 1438 | found in the Perl "Artistic License". | ||
| 1439 | |||
| 1440 | A copy of the "Artistic License" may be found in the standard | ||
| 1441 | Perl distribution. | ||
| 1442 | |||
| 1443 | =head1 BUGS | ||
| 1444 | |||
| 1445 | There are no known bugs in the module. | ||
| 1446 | |||
| 1447 | =head1 SEE ALSO | ||
| 1448 | |||
| 1449 | Yellowstone National Park. | ||
| 1450 | |||
| 1451 | =head1 AUTHOR | ||
| 1452 | |||
| 1453 | Jeff Weisberg - http://www.tcp4me.com | ||
| 1454 | |||
| 1455 | =cut | ||
| 1456 | ; | ||
| 1457 | |||
| 1458 | 1; | ||