| 1 | |||
| 2 | # Chart::Base : all other drawing classes ared derived from this | ||
| 3 | # provides all common functions | ||
| 4 | |||
| 5 | 69 | ▶ | use v5.12; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | package Chart::Base; | ||
| 8 | our $VERSION = 'v2.403.9'; | ||
| 9 | |||
| 10 | 69 | ▶ | use FileHandle; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 69 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 0 | ▶ | use GD; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 13 | use GD::Image; | ||
| 14 | use Chart::Property::DataType::Color; | ||
| 15 | #use Chart::Property::DataType::Font; | ||
| 16 | |||
| 17 | #>>>>>>>>>>>>>>>>>>>>>>>>>># | ||
| 18 | # public methods # | ||
| 19 | #<<<<<<<<<<<<<<<<<<<<<<<<<<# | ||
| 20 | |||
| 21 | sub new { # Standard constructor | ||
| 22 | my $proto = shift; | ||
| 23 | my $class = ref($proto) || $proto; | ||
| 24 | my $self = (bless {}, $class) ; | ||
| 25 | |||
| 26 | $self->_init(@_); | ||
| 27 | return $self; | ||
| 28 | } | ||
| 29 | |||
| 30 | |||
| 31 | # set (%options) --> 1 | croak | ||
| 32 | # main method for customizing the chart, by specify parameters | ||
| 33 | # options are saved locally to be able to output them via @see getopts() | ||
| 34 | |||
| 35 | sub set { | ||
| 36 | my $self = shift; | ||
| 37 | my %opts = @_; | ||
| 38 | |||
| 39 | # basic error checking on the options, just warn 'em | ||
| 40 | unless ( $#_ % 2 ) | ||
| 41 | { | ||
| 42 | carp "Whoops, some option to be set didn't have a value.\n", "You might want to look at that.\n"; | ||
| 43 | } | ||
| 44 | |||
| 45 | # set the options | ||
| 46 | for ( keys %opts ) { | ||
| 47 | $self->{$_} = $opts{$_}; | ||
| 48 | $self->{saveopts}->{$_} = $opts{$_}; | ||
| 49 | |||
| 50 | # if someone wants to change the grid_lines color, we should set all | ||
| 51 | # the colors of the grid_lines | ||
| 52 | if ( $_ =~ /^colors$/ ) { | ||
| 53 | my %hash = %{ $opts{$_} }; | ||
| 54 | foreach my $key ( sort keys %hash ) { | ||
| 55 | |||
| 56 | if ( $key =~ /^grid_lines$/ ) { | ||
| 57 | if ( ref( $hash{'grid_lines'} ) eq 'ARRAY' ) | ||
| 58 | { | ||
| 59 | my @aLocal = ( $hash{'grid_lines'}[0], $hash{'grid_lines'}[1], $hash{'grid_lines'}[2] ); | ||
| 60 | $self->{'colors'}{'y_grid_lines'} = [@aLocal]; | ||
| 61 | $self->{'colors'}{'x_grid_lines'} = [@aLocal]; | ||
| 62 | $self->{'colors'}{'y2_grid_lines'} = [@aLocal]; | ||
| 63 | } | ||
| 64 | elsif ( ref( \$hash{'grid_lines'} ) eq 'SCALAR' ) | ||
| 65 | { | ||
| 66 | my $sLocal = $hash{'grid_lines'}; | ||
| 67 | $self->{'colors'}{'y_grid_lines'} = $sLocal; | ||
| 68 | $self->{'colors'}{'x_grid_lines'} = $sLocal; | ||
| 69 | $self->{'colors'}{'y2_grid_lines'} = $sLocal; | ||
| 70 | } | ||
| 71 | else { carp "colors{'grid_lines'} is not SCALAR and not ARRAY\n" } | ||
| 72 | } | ||
| 73 | } | ||
| 74 | } | ||
| 75 | } | ||
| 76 | return 1; | ||
| 77 | } | ||
| 78 | |||
| 79 | ## @method int add_pt(@data) | ||
| 80 | # Graph API\n | ||
| 81 | # Add one dataset (as a list) to the dataref | ||
| 82 | # | ||
| 83 | # @param data Dataset to add | ||
| 84 | |||
| 85 | ## @method add_pt(\\\@data) | ||
| 86 | # Graph API\n | ||
| 87 | # Add one dataset (as a reference to a list) to the dataref | ||
| 88 | # via | ||
| 89 | # <pre> | ||
| 90 | # for ( 0 .. $#data ) | ||
| 91 | # { | ||
| 92 | # push @{ $self->{'dataref'}->[$_] }, $data[$_]; | ||
| 93 | # } | ||
| 94 | # </pre> | ||
| 95 | # | ||
| 96 | # @param data Dataset to add | ||
| 97 | # | ||
| 98 | sub add_pt { | ||
| 99 | my $self = shift; | ||
| 100 | my @data = (); | ||
| 101 | |||
| 102 | if ( ref $_[0] eq 'ARRAY' ) { | ||
| 103 | my $rdata = shift; | ||
| 104 | @data = @$rdata if @$rdata; | ||
| 105 | } | ||
| 106 | elsif ( ( ref \$_[0] ) =~ /^SCALAR/ ) | ||
| 107 | { | ||
| 108 | @data = @_ if defined $_[0]; | ||
| 109 | } else { croak "Not an array or reference to array" } | ||
| 110 | |||
| 111 | # error check the data (carp, don't croak) | ||
| 112 | if ( $self->{'dataref'} && ( $#{ $self->{'dataref'} } != $#data ) ) | ||
| 113 | { | ||
| 114 | carp "New point to be added has an incorrect number of data sets"; | ||
| 115 | return 0; | ||
| 116 | } | ||
| 117 | |||
| 118 | # copy it into the dataref | ||
| 119 | push @{ $self->{'dataref'}->[$_] }, $data[$_] for 0 .. $#data; | ||
| 120 | |||
| 121 | # now return | ||
| 122 | return 1; | ||
| 123 | } | ||
| 124 | |||
| 125 | ## @method int add_dataset(@data) | ||
| 126 | # Graph API\n | ||
| 127 | # Add many datasets (implemented as a list) | ||
| 128 | # to the dataref, | ||
| 129 | # | ||
| 130 | # @param data Dataset (list) to add | ||
| 131 | |||
| 132 | ## @method int add_dataset(\\\@data) | ||
| 133 | # Graph API\n | ||
| 134 | # Add many datasets (implemented as a references to alist) | ||
| 135 | # to the dataref, | ||
| 136 | # | ||
| 137 | # @param data Dataset (reference to a list) to add | ||
| 138 | sub add_dataset { | ||
| 139 | my $self = shift; | ||
| 140 | my @data = (); | ||
| 141 | |||
| 142 | if ( ref $_[0] eq 'ARRAY' ) { | ||
| 143 | my $rdata = shift; | ||
| 144 | @data = @$rdata if @$rdata; | ||
| 145 | } elsif ( ( ref \$_[0] ) =~ /^SCALAR/ ) { | ||
| 146 | @data = @_ if defined $_[0]; | ||
| 147 | } else { | ||
| 148 | croak "Not an array or reference to array"; | ||
| 149 | return; | ||
| 150 | } | ||
| 151 | |||
| 152 | # error check the data (carp, don't croak) | ||
| 153 | if ( $self->{'dataref'} && ( $#{ $self->{'dataref'}->[0] } != $#data ) ) | ||
| 154 | { | ||
| 155 | carp "New data set to be added has an incorrect number of points"; | ||
| 156 | } | ||
| 157 | |||
| 158 | # copy it into the dataref | ||
| 159 | push @{ $self->{'dataref'} }, [@data]; | ||
| 160 | |||
| 161 | # now return | ||
| 162 | return 1; | ||
| 163 | } | ||
| 164 | |||
| 165 | ## @method int add_datafile($filename,$format) | ||
| 166 | # Graph API\n | ||
| 167 | # it's also possible to add a complete datafile\n | ||
| 168 | # Uses | ||
| 169 | # @see add_pt | ||
| 170 | # @see add_dataset | ||
| 171 | # | ||
| 172 | # @param[in] filename Name of file which contents is to be added | ||
| 173 | # @param[in] format 'pt' or 'set' to distiguish between function add_pt() in case of 'pt' | ||
| 174 | # or function add_dataset() in case of 'set' | ||
| 175 | sub add_datafile { | ||
| 176 | my $self = shift; | ||
| 177 | my $filename = shift; | ||
| 178 | my $format = shift // 'set'; | ||
| 179 | my ( $File, @array ); | ||
| 180 | |||
| 181 | carp "Need format for the data file: 'set' (default) or 'pt' \n" unless $format eq 'set' or $format eq 'pt'; | ||
| 182 | |||
| 183 | # do some ugly checking to see if they gave me | ||
| 184 | # a filehandle or a file name | ||
| 185 | if ( ( ref \$filename ) eq 'SCALAR' ) { | ||
| 186 | open( $File, $filename ) or croak "Can't open the datafile: $filename.\n"; | ||
| 187 | } elsif ( ( ref \$filename ) =~ /^(?:REF|GLOB)$/ ) { | ||
| 188 | |||
| 189 | # either a FileHandle object or a regular file handle | ||
| 190 | $File = $filename; | ||
| 191 | } else { | ||
| 192 | carp "I'm not sure what kind of datafile you gave me,\n", "but it wasn't a filename or a filehandle.\n"; | ||
| 193 | } | ||
| 194 | |||
| 195 | while (<$File>) { | ||
| 196 | @array = split /\s*,\s*|\s+/; | ||
| 197 | next unless @array and substr($array[0], 0, 1) ne '#'; | ||
| 198 | if ($format eq 'set'){ $self->add_dataset( @array ) } | ||
| 199 | else { $self->add_pt( @array ) } | ||
| 200 | } | ||
| 201 | close($File); | ||
| 202 | } | ||
| 203 | |||
| 204 | ## @method int clear_data() | ||
| 205 | # Clear Graph API (by undefining 'dataref' | ||
| 206 | # @return Status of function | ||
| 207 | sub clear_data { | ||
| 208 | my $self = shift; | ||
| 209 | |||
| 210 | $self->{'dataref'} = undef; | ||
| 211 | |||
| 212 | return 1; | ||
| 213 | } | ||
| 214 | |||
| 215 | ## @method arrayref get_data() | ||
| 216 | # Get array of data of the last graph | ||
| 217 | # @return Reference to data set of the last graph | ||
| 218 | sub get_data { | ||
| 219 | my $self = shift; | ||
| 220 | my $ref = []; | ||
| 221 | my ( $i, $j ); | ||
| 222 | |||
| 223 | # give them a copy, not a reference into the object | ||
| 224 | for $i ( 0 .. $#{ $self->{'dataref'} } ) { | ||
| 225 | @{ $ref->[$i] } = @{ $self->{'dataref'}->[$i] } | ||
| 226 | ## speedup, compared to... | ||
| 227 | # for $j (0..$#{$self->{'dataref'}->[$i]}) { | ||
| 228 | # $ref->[$i][$j] = $self->{'dataref'}->[$i][$j]; | ||
| 229 | # } | ||
| 230 | } | ||
| 231 | |||
| 232 | # return it | ||
| 233 | return $ref; | ||
| 234 | } | ||
| 235 | |||
| 236 | ## @method int png($file, $dataref) | ||
| 237 | # Produce the graph of options set in png format. | ||
| 238 | # | ||
| 239 | # called after the options are set, this method | ||
| 240 | # invokes all my private methods to actually | ||
| 241 | # draw the chart and plot the data | ||
| 242 | # @see _set_colors | ||
| 243 | # @see _copy_data | ||
| 244 | # @see _check_data | ||
| 245 | # @see _draw | ||
| 246 | # @param[in] file Name of file to write graph to | ||
| 247 | # @param[in] dataref Reference to external data space | ||
| 248 | # @return Status of the plot | ||
| 249 | sub png { | ||
| 250 | my $self = shift; | ||
| 251 | my $file = shift; | ||
| 252 | my $dataref = shift; | ||
| 253 | my $fh; | ||
| 254 | |||
| 255 | # do some ugly checking to see if they gave me | ||
| 256 | # a filehandle or a file name | ||
| 257 | if ( ( ref \$file ) eq 'SCALAR' ) { | ||
| 258 | |||
| 259 | # they gave me a file name | ||
| 260 | # Try to delete an existing file | ||
| 261 | if ( -f $file ) | ||
| 262 | { | ||
| 263 | my $number_deleted_files = unlink $file; | ||
| 264 | if ( $number_deleted_files != 1 ) | ||
| 265 | { | ||
| 266 | croak "Error: File \"$file\" did already exist, but it failed to delete it"; | ||
| 267 | } | ||
| 268 | } | ||
| 269 | $fh = FileHandle->new(">$file"); | ||
| 270 | if ( !defined $fh ) | ||
| 271 | { | ||
| 272 | croak "Error: File \"$file\" could not be created!\n"; | ||
| 273 | } | ||
| 274 | } | ||
| 275 | elsif ( ( ref \$file ) =~ /^(?:REF|GLOB)$/ ) | ||
| 276 | { | ||
| 277 | |||
| 278 | # either a FileHandle object or a regular file handle | ||
| 279 | $fh = $file; | ||
| 280 | } | ||
| 281 | else | ||
| 282 | { | ||
| 283 | croak "I'm not sure what you gave me to write this png to,\n", "but it wasn't a filename or a filehandle.\n"; | ||
| 284 | } | ||
| 285 | |||
| 286 | # allocate the background color | ||
| 287 | $self->_set_colors(); | ||
| 288 | |||
| 289 | # make sure the object has its copy of the data | ||
| 290 | $self->_copy_data($dataref); | ||
| 291 | |||
| 292 | # do a sanity check on the data, and collect some basic facts | ||
| 293 | # about the data | ||
| 294 | $self->_check_data(); | ||
| 295 | |||
| 296 | # pass off the real work to the appropriate subs | ||
| 297 | $self->_draw(); | ||
| 298 | |||
| 299 | # now write it to the file handle, and don't forget | ||
| 300 | # to be nice to the poor ppl using nt | ||
| 301 | binmode $fh; | ||
| 302 | |||
| 303 | print $fh $self->{'gd_obj'}->png(); | ||
| 304 | |||
| 305 | # now exit | ||
| 306 | return 1; | ||
| 307 | } | ||
| 308 | |||
| 309 | ## @method int cgi_png($dataref) | ||
| 310 | # Produce the graph of options set in png format to be directly | ||
| 311 | # written for CGI. | ||
| 312 | # | ||
| 313 | # called after the options are set, this method | ||
| 314 | # invokes all my private methods to actually | ||
| 315 | # draw the chart and plot the data | ||
| 316 | # @param[in] dataref Reference to external data space | ||
| 317 | # @return Status of the plot | ||
| 318 | sub cgi_png { | ||
| 319 | my $self = shift; | ||
| 320 | my $dataref = shift; | ||
| 321 | |||
| 322 | # allocate the background color | ||
| 323 | $self->_set_colors(); | ||
| 324 | |||
| 325 | # make sure the object has its copy of the data | ||
| 326 | $self->_copy_data($dataref); | ||
| 327 | |||
| 328 | # do a sanity check on the data, and collect some basic facts | ||
| 329 | # about the data | ||
| 330 | $self->_check_data(); | ||
| 331 | |||
| 332 | # pass off the real work to the appropriate subs | ||
| 333 | $self->_draw(); | ||
| 334 | |||
| 335 | # print the header (ripped the crlf octal from the CGI module) | ||
| 336 | if ( $self->true( $self->{no_cache} ) ) | ||
| 337 | { | ||
| 338 | print "Content-type: image/png\015\012Pragma: no-cache\015\012\015\012"; | ||
| 339 | } | ||
| 340 | else | ||
| 341 | { | ||
| 342 | print "Content-type: image/png\015\012\015\012"; | ||
| 343 | } | ||
| 344 | |||
| 345 | # now print the png, and binmode it first so Windows-XX likes us | ||
| 346 | binmode STDOUT; | ||
| 347 | print STDOUT $self->{'gd_obj'}->png(); | ||
| 348 | |||
| 349 | # now exit | ||
| 350 | return 1; | ||
| 351 | } | ||
| 352 | |||
| 353 | ## @method int scalar_png($dataref) | ||
| 354 | # Produce the graph of options set in PNG format to be directly returned | ||
| 355 | # | ||
| 356 | # called after the options are set, this method | ||
| 357 | # invokes all my private methods to actually | ||
| 358 | # draw the chart and return the image to the caller | ||
| 359 | # | ||
| 360 | # @param dataref Reference to data | ||
| 361 | # @return returns the png image as a scalar value, so that | ||
| 362 | # the programmer-user can do whatever the heck | ||
| 363 | # s/he wants to with it | ||
| 364 | sub scalar_png | ||
| 365 | { | ||
| 366 | my $self = shift; | ||
| 367 | my $dataref = shift; | ||
| 368 | |||
| 369 | #allocate the background color | ||
| 370 | $self->_set_colors(); | ||
| 371 | |||
| 372 | # make sure the object has its copy of the data | ||
| 373 | $self->_copy_data($dataref); | ||
| 374 | |||
| 375 | # do a sanity check on the data, and collect some basic facts | ||
| 376 | # about the data | ||
| 377 | $self->_check_data(); | ||
| 378 | |||
| 379 | # pass off the real work to the appropriate subs | ||
| 380 | $self->_draw(); | ||
| 381 | |||
| 382 | # returns the png image as a scalar value, so that | ||
| 383 | # the programmer/user can do whatever the she/he wants to with it | ||
| 384 | return $self->{'gd_obj'}->png(); | ||
| 385 | } | ||
| 386 | |||
| 387 | ## @method int jpeg($file,$dataref) | ||
| 388 | # Produce the graph of options set in JPG format to be directly plotted.\n | ||
| 389 | # | ||
| 390 | # Called after the options are set, this method | ||
| 391 | # invokes all my private methods to actually | ||
| 392 | # draw the chart and plot the data. | ||
| 393 | # The output has the jpeg format in opposite to png format produced by | ||
| 394 | # @see png | ||
| 395 | # | ||
| 396 | # Uses the following private functions:\n | ||
| 397 | # @see _set_colors | ||
| 398 | # @see _copy_data | ||
| 399 | # @see _check_data | ||
| 400 | # @see _draw | ||
| 401 | # | ||
| 402 | # @param[in] file Name of file to write graph to | ||
| 403 | # @param[in] dataref Reference to external data space | ||
| 404 | # @return Status of the plot | ||
| 405 | # | ||
| 406 | sub jpeg | ||
| 407 | { | ||
| 408 | my $self = shift; | ||
| 409 | my $file = shift; | ||
| 410 | my $dataref = shift; | ||
| 411 | my $fh; | ||
| 412 | |||
| 413 | # do some ugly checking to see if they gave me | ||
| 414 | # a filehandle or a file name | ||
| 415 | if ( ( ref \$file ) eq 'SCALAR' ) | ||
| 416 | { | ||
| 417 | |||
| 418 | # they gave me a file name | ||
| 419 | # Try to delete an existing file | ||
| 420 | if ( -f $file ) | ||
| 421 | { | ||
| 422 | my $number_deleted_files = unlink $file; | ||
| 423 | if ( $number_deleted_files != 1 ) | ||
| 424 | { | ||
| 425 | croak "Error: File \"$file\" did already exist, but it fails to delete it"; | ||
| 426 | } | ||
| 427 | } | ||
| 428 | $fh = FileHandle->new(">$file"); | ||
| 429 | if ( !defined $fh ) | ||
| 430 | { | ||
| 431 | croak "Error: File \"$file\" could not be created!\n"; | ||
| 432 | } | ||
| 433 | } | ||
| 434 | elsif ( ( ref \$file ) =~ /^(?:REF|GLOB)$/ ) | ||
| 435 | { | ||
| 436 | |||
| 437 | # either a FileHandle object or a regular file handle | ||
| 438 | $fh = $file; | ||
| 439 | } | ||
| 440 | else | ||
| 441 | { | ||
| 442 | croak "I'm not sure what you gave me to write this jpeg to,\n", "but it wasn't a filename or a filehandle.\n"; | ||
| 443 | } | ||
| 444 | |||
| 445 | # allocate the background color | ||
| 446 | $self->_set_colors(); | ||
| 447 | |||
| 448 | # make sure the object has its copy of the data | ||
| 449 | $self->_copy_data($dataref); | ||
| 450 | |||
| 451 | # do a sanity check on the data, and collect some basic facts | ||
| 452 | # about the data | ||
| 453 | $self->_check_data; | ||
| 454 | |||
| 455 | # pass off the real work to the appropriate subs | ||
| 456 | $self->_draw(); | ||
| 457 | |||
| 458 | # now write it to the file handle, and don't forget | ||
| 459 | # to be nice to the poor ppl using Windows-XX | ||
| 460 | binmode $fh; | ||
| 461 | print $fh $self->{'gd_obj'}->jpeg( [100] ); # high quality need | ||
| 462 | |||
| 463 | # now exit | ||
| 464 | return 1; | ||
| 465 | } | ||
| 466 | |||
| 467 | ## @method int cgi_jpeg($dataref) | ||
| 468 | # Produce the graph of options set in JPG format to be directly | ||
| 469 | # for CGI. | ||
| 470 | # | ||
| 471 | # called after the options are set, this method | ||
| 472 | # invokes all my private methods to actually | ||
| 473 | # draw the chart and plot the data | ||
| 474 | # @param[in] dataref Reference to external data space | ||
| 475 | # @return Status of the plot | ||
| 476 | sub cgi_jpeg | ||
| 477 | { | ||
| 478 | my $self = shift; | ||
| 479 | my $dataref = shift; | ||
| 480 | |||
| 481 | # allocate the background color | ||
| 482 | $self->_set_colors(); | ||
| 483 | |||
| 484 | # make sure the object has its copy of the data | ||
| 485 | $self->_copy_data($dataref); | ||
| 486 | |||
| 487 | # do a sanity check on the data, and collect some basic facts | ||
| 488 | # about the data | ||
| 489 | $self->_check_data(); | ||
| 490 | |||
| 491 | # pass off the real work to the appropriate subs | ||
| 492 | $self->_draw(); | ||
| 493 | |||
| 494 | # print the header (ripped the crlf octal from the CGI module) | ||
| 495 | if ( $self->true( $self->{no_cache} ) ) | ||
| 496 | { | ||
| 497 | print "Content-type: image/jpeg\015\012Pragma: no-cache\015\012\015\012"; | ||
| 498 | } | ||
| 499 | else | ||
| 500 | { | ||
| 501 | print "Content-type: image/jpeg\015\012\015\012"; | ||
| 502 | } | ||
| 503 | |||
| 504 | # now print the jpeg, and binmode it first so Windows-XX likes us | ||
| 505 | binmode STDOUT; | ||
| 506 | print STDOUT $self->{'gd_obj'}->jpeg( [100] ); | ||
| 507 | |||
| 508 | # now exit | ||
| 509 | return 1; | ||
| 510 | } | ||
| 511 | |||
| 512 | ## @method int scalar_jpeg($dataref) | ||
| 513 | # Produce the graph of options set in JPG format to be directly returned | ||
| 514 | # | ||
| 515 | # called after the options are set, this method | ||
| 516 | # invokes all my private methods to actually | ||
| 517 | # draw the chart and return the image to the caller | ||
| 518 | # | ||
| 519 | # @param dataref Reference to data area | ||
| 520 | # @return returns the jpeg image as a scalar value, so that | ||
| 521 | # the programmer-user can do whatever the heck | ||
| 522 | # s/he wants to with it | ||
| 523 | sub scalar_jpeg | ||
| 524 | { | ||
| 525 | my $self = shift; | ||
| 526 | my $dataref = shift; | ||
| 527 | |||
| 528 | # allocate the background color | ||
| 529 | $self->_set_colors(); | ||
| 530 | |||
| 531 | # make sure the object has its copy of the data | ||
| 532 | $self->_copy_data($dataref); | ||
| 533 | |||
| 534 | # do a sanity check on the data, and collect some basic facts | ||
| 535 | # about the data | ||
| 536 | $self->_check_data(); | ||
| 537 | |||
| 538 | # pass off the real work to the appropriate subs | ||
| 539 | $self->_draw(); | ||
| 540 | |||
| 541 | # returns the jpeg image as a scalar value, so that | ||
| 542 | # the programmer-user can do whatever the heck | ||
| 543 | # s/he wants to with it | ||
| 544 | $self->{'gd_obj'}->jpeg( [100] ); | ||
| 545 | } | ||
| 546 | |||
| 547 | ## @method int make_gd($dataref) | ||
| 548 | # Produce the graph of options set in GD format to be directly | ||
| 549 | # | ||
| 550 | # called after the options are set, this method | ||
| 551 | # invokes all my private methods to actually | ||
| 552 | # draw the chart and plot the data | ||
| 553 | # @param dataref Reference to data | ||
| 554 | # @return Status of the plot | ||
| 555 | sub make_gd { | ||
| 556 | my $self = shift; | ||
| 557 | my $dataref = shift; | ||
| 558 | |||
| 559 | # allocate the background color | ||
| 560 | $self->_set_colors(); | ||
| 561 | |||
| 562 | # make sure the object has its copy of the data | ||
| 563 | $self->_copy_data($dataref); | ||
| 564 | |||
| 565 | # do a sanity check on the data, and collect some basic facts | ||
| 566 | # about the data | ||
| 567 | $self->_check_data(); | ||
| 568 | |||
| 569 | # pass off the real work to the appropriate subs | ||
| 570 | $self->_draw(); | ||
| 571 | |||
| 572 | # return the GD::Image object that we've drawn into | ||
| 573 | return $self->{'gd_obj'}; | ||
| 574 | } | ||
| 575 | |||
| 576 | ## @method imagemap_dump() | ||
| 577 | # get the information to turn the chart into an imagemap | ||
| 578 | # | ||
| 579 | # @return Reference to an array of the image | ||
| 580 | sub imagemap_dump { | ||
| 581 | my $self = shift; | ||
| 582 | my $ref = []; | ||
| 583 | my ( $i, $j ); | ||
| 584 | |||
| 585 | # croak if they didn't ask me to remember the data, or if they're asking | ||
| 586 | # for the data before I generate it | ||
| 587 | unless ( ( $self->true( $self->{'imagemap'} ) ) && $self->{'imagemap_data'} ) | ||
| 588 | { | ||
| 589 | croak "You need to set the imagemap option to true, and then call the png method, before you can get the imagemap data"; | ||
| 590 | } | ||
| 591 | |||
| 592 | # can't just return a ref to my internal structures... | ||
| 593 | for $i ( 0 .. $#{ $self->{'imagemap_data'} } ) | ||
| 594 | { | ||
| 595 | for $j ( 0 .. $#{ $self->{'imagemap_data'}->[$i] } ) | ||
| 596 | { | ||
| 597 | $ref->[$i][$j] = [ @{ $self->{'imagemap_data'}->[$i][$j] } ]; | ||
| 598 | } | ||
| 599 | } | ||
| 600 | |||
| 601 | # return their copy | ||
| 602 | return $ref; | ||
| 603 | } | ||
| 604 | |||
| 605 | ## @method minimum (@array) | ||
| 606 | # determine minimum of an array of values | ||
| 607 | # @param array List of numerical values (\@array) | ||
| 608 | # @return Minimal value of list of values | ||
| 609 | sub minimum { | ||
| 610 | my $self = shift; | ||
| 611 | my @array = @_; | ||
| 612 | |||
| 613 | return undef if !@array; | ||
| 614 | my $min = $array[0]; | ||
| 615 | for ( my $iIndex = 0 ; $iIndex < scalar @array ; $iIndex++ ) | ||
| 616 | { | ||
| 617 | $min = $array[$iIndex] if ( $min > $array[$iIndex] ); | ||
| 618 | } | ||
| 619 | $min; | ||
| 620 | } | ||
| 621 | |||
| 622 | ## @method maximum(@array) | ||
| 623 | # determine maximum of an array of values | ||
| 624 | # @param array List of numerical values (@array) | ||
| 625 | # @return Maximal value of list of values | ||
| 626 | sub maximum { | ||
| 627 | my $self = shift; | ||
| 628 | my @array = @_; | ||
| 629 | |||
| 630 | return undef if !@array; | ||
| 631 | my $max = $array[0]; | ||
| 632 | for ( my $iIndex = 0 ; $iIndex < scalar @array ; $iIndex++ ) | ||
| 633 | { | ||
| 634 | $max = $array[$iIndex] if ( $max < $array[$iIndex] ); | ||
| 635 | } | ||
| 636 | $max; | ||
| 637 | } | ||
| 638 | |||
| 639 | ## @method arccos($a) | ||
| 640 | # Function arccos(a) | ||
| 641 | # @param a Value | ||
| 642 | # @return arccos(a) | ||
| 643 | sub arccos { | ||
| 644 | my $self = shift; | ||
| 645 | my $a = shift; | ||
| 646 | |||
| 647 | return ( atan2( sqrt( 1 - $a * $a ), $a ) ); | ||
| 648 | } | ||
| 649 | |||
| 650 | ## @method arcsin($a) | ||
| 651 | # Function arcsin(a) | ||
| 652 | # @param a Value | ||
| 653 | # @return arcsin(a) | ||
| 654 | sub arcsin | ||
| 655 | { | ||
| 656 | my $self = shift; | ||
| 657 | my $a = shift; | ||
| 658 | |||
| 659 | return ( atan2( $a, sqrt( 1 - $a * $a ) ) ); | ||
| 660 | } | ||
| 661 | |||
| 662 | ## @method true($arg) | ||
| 663 | # determine true value of argument | ||
| 664 | # @param[in] arg Bool value to check for true | ||
| 665 | # @return 1 if argument is equal to TRUE, true, 1, t, T, and defined | ||
| 666 | sub true { | ||
| 667 | my $pkg = shift; | ||
| 668 | my $arg = shift; | ||
| 669 | |||
| 670 | if ( !defined($arg) ) | ||
| 671 | { | ||
| 672 | return 0; | ||
| 673 | } | ||
| 674 | |||
| 675 | if ( $arg eq 'true' | ||
| 676 | || $arg eq 'TRUE' | ||
| 677 | || $arg eq 't' | ||
| 678 | || $arg eq 'T' | ||
| 679 | || $arg eq '1' ) | ||
| 680 | { | ||
| 681 | return 1; | ||
| 682 | } | ||
| 683 | |||
| 684 | return 0; | ||
| 685 | } | ||
| 686 | |||
| 687 | ## @method false($arg) | ||
| 688 | # determine false value of argument | ||
| 689 | # @param[in] arg Bool value to check for true | ||
| 690 | # @return 1 if argument is equal to false, FALSE, 0, f, F or undefined | ||
| 691 | sub false { | ||
| 692 | my $pkg = shift; | ||
| 693 | my $arg = shift; | ||
| 694 | |||
| 695 | if ( !defined($arg) ) | ||
| 696 | { | ||
| 697 | return 1; | ||
| 698 | } | ||
| 699 | |||
| 700 | if ( $arg eq 'false' | ||
| 701 | || $arg eq 'FALSE' | ||
| 702 | || $arg eq 'f' | ||
| 703 | || $arg eq 'F' | ||
| 704 | || $arg eq '0' | ||
| 705 | || $arg eq 'none' ) | ||
| 706 | { | ||
| 707 | return 1; | ||
| 708 | } | ||
| 709 | |||
| 710 | return 0; | ||
| 711 | } | ||
| 712 | |||
| 713 | ## @method modulo($a,$b) | ||
| 714 | # Calculate float($a % $b) as the internal operator '%' | ||
| 715 | # does only calculate in integers | ||
| 716 | # @param[in] a a in a%b | ||
| 717 | # @param[in] b b in a%b | ||
| 718 | # @return $a % $b in float | ||
| 719 | sub modulo { | ||
| 720 | my $pkg = shift; | ||
| 721 | my $a = shift; | ||
| 722 | my $b = shift; | ||
| 723 | |||
| 724 | my $erg = 0.0; | ||
| 725 | |||
| 726 | if ( !defined($a) || !defined($b) || $b == 0 ) | ||
| 727 | { | ||
| 728 | die "Modulo needs valid parameters!" | ||
| 729 | |||
| 730 | #return $erg; | ||
| 731 | } | ||
| 732 | |||
| 733 | my $div = $a / $b; | ||
| 734 | |||
| 735 | $erg = $a - int($div) * $b; | ||
| 736 | |||
| 737 | return $erg; | ||
| 738 | } | ||
| 739 | |||
| 740 | #>>>>>>>>>>>>>>>>>>>>>>>>>>># | ||
| 741 | # private methods go here # | ||
| 742 | #<<<<<<<<<<<<<<<<<<<<<<<<<<<# | ||
| 743 | |||
| 744 | ## @fn private int _init($x,$y) | ||
| 745 | # Initialize all default options here | ||
| 746 | # @param[in] x Width of the final image in pixels (Default: 400) | ||
| 747 | # @param[in] y Height of the final image in pixels (Default: 300) | ||
| 748 | # | ||
| 749 | sub _init { | ||
| 750 | my $self = shift; | ||
| 751 | my $x = shift || 400; # give them a 400x300 image | ||
| 752 | my $y = shift || 300; # unless they say otherwise | ||
| 753 | |||
| 754 | # get the gd object | ||
| 755 | |||
| 756 | # Reference to new GD::Image | ||
| 757 | $self->{'gd_obj'} = GD::Image->new( $x, $y ); | ||
| 758 | |||
| 759 | # start keeping track of used space | ||
| 760 | # actual current y min Value | ||
| 761 | $self->{'curr_y_min'} = 0; | ||
| 762 | $self->{'curr_y_max'} = $y; # maximum pixel in y direction (down) | ||
| 763 | $self->{'curr_x_min'} = 0; | ||
| 764 | $self->{'curr_x_max'} = $x; # maximum pixel in x direction (right) | ||
| 765 | |||
| 766 | # use a 10 pixel border around the whole png | ||
| 767 | $self->{'png_border'} = 10; | ||
| 768 | |||
| 769 | # leave some space around the text fields | ||
| 770 | $self->{'text_space'} = 2; | ||
| 771 | |||
| 772 | # and leave some more space around the chart itself | ||
| 773 | $self->{'graph_border'} = 10; | ||
| 774 | |||
| 775 | # leave a bit of space inside the legend box | ||
| 776 | $self->{'legend_space'} = 4; | ||
| 777 | |||
| 778 | # set some default fonts | ||
| 779 | $self->{'title_font'} = gdLargeFont, | ||
| 780 | $self->{'sub_title_font'} = gdLargeFont, | ||
| 781 | $self->{'legend_font'} = gdSmallFont, | ||
| 782 | $self->{'label_font'} = gdMediumBoldFont, | ||
| 783 | $self->{'tick_label_font'} = gdSmallFont; | ||
| 784 | |||
| 785 | # put the legend on the bottom of the chart | ||
| 786 | $self->{'legend'} = 'right'; | ||
| 787 | |||
| 788 | # default to an empty list of labels | ||
| 789 | $self->{'legend_labels'} = []; | ||
| 790 | |||
| 791 | # use 20 pixel length example lines in the legend | ||
| 792 | $self->{'legend_example_size'} = 20; | ||
| 793 | |||
| 794 | # Set the maximum & minimum number of ticks to use. | ||
| 795 | $self->{'y_ticks'} = 6, | ||
| 796 | $self->{'min_y_ticks'} = 6, | ||
| 797 | $self->{'max_y_ticks'} = 100, | ||
| 798 | $self->{'x_number_ticks'} = 1, | ||
| 799 | $self->{'min_x_ticks'} = 6, | ||
| 800 | $self->{'max_x_ticks'} = 100; | ||
| 801 | |||
| 802 | # make the ticks 4 pixels long | ||
| 803 | $self->{'tick_len'} = 4; | ||
| 804 | |||
| 805 | # no custom y tick labels | ||
| 806 | $self->{'y_tick_labels'} = undef; | ||
| 807 | |||
| 808 | # no patterns | ||
| 809 | $self->{'patterns'} = undef; | ||
| 810 | |||
| 811 | # let the lines in Chart::Lines be 6 pixels wide | ||
| 812 | $self->{'brush_size'} = 6; | ||
| 813 | |||
| 814 | # let the points in Chart::Points and Chart::LinesPoints be 18 pixels wide | ||
| 815 | $self->{'pt_size'} = 18; | ||
| 816 | |||
| 817 | # use the old non-spaced bars | ||
| 818 | $self->{'spaced_bars'} = 'true'; | ||
| 819 | |||
| 820 | # use the new grey background for the plots | ||
| 821 | $self->{'grey_background'} = 'true'; | ||
| 822 | |||
| 823 | # don't default to transparent | ||
| 824 | $self->{'transparent'} = 'false'; | ||
| 825 | |||
| 826 | # default to "normal" x_tick drawing | ||
| 827 | $self->{'x_ticks'} = 'normal'; | ||
| 828 | |||
| 829 | # we're not a component until Chart::Composite says we are | ||
| 830 | $self->{'component'} = 'false'; | ||
| 831 | |||
| 832 | # don't force the y-axes in a Composite chare to be the same | ||
| 833 | $self->{'same_y_axes'} = 'false'; | ||
| 834 | |||
| 835 | # plot rectangeles in the legend instead of lines in a composite chart | ||
| 836 | $self->{'legend_example_height'} = 'false'; | ||
| 837 | |||
| 838 | # don't force integer y-ticks | ||
| 839 | $self->{'integer_ticks_only'} = 'false'; | ||
| 840 | |||
| 841 | # don't forbid a false zero scale. | ||
| 842 | $self->{'include_zero'} = 'false'; | ||
| 843 | |||
| 844 | # don't waste time/memory by storing imagemap info unless they ask | ||
| 845 | $self->{'imagemap'} = 'false'; | ||
| 846 | |||
| 847 | # default for grid_lines is off | ||
| 848 | $self->{grid_lines} = 'false', | ||
| 849 | $self->{x_grid_lines} = 'false', | ||
| 850 | $self->{y_grid_lines} = 'false', | ||
| 851 | $self->{y2_grid_lines} = 'false'; | ||
| 852 | |||
| 853 | # default for no_cache is false. (it breaks netscape 4.5) | ||
| 854 | $self->{no_cache} = 'false'; | ||
| 855 | |||
| 856 | # default value for skip_y_ticks for the labels | ||
| 857 | $self->{skip_y_ticks} = 1; | ||
| 858 | |||
| 859 | # default value for skip_int_ticks only for integer_ticks_only | ||
| 860 | $self->{skip_int_ticks} = 1; | ||
| 861 | |||
| 862 | # default value for precision | ||
| 863 | $self->{precision} = 3; | ||
| 864 | |||
| 865 | # default value for legend label values in pie charts | ||
| 866 | $self->{legend_label_values} = 'value'; | ||
| 867 | |||
| 868 | # default value for the labels in a pie chart | ||
| 869 | $self->{label_values} = 'percent'; | ||
| 870 | |||
| 871 | # default position for the y-axes | ||
| 872 | $self->{y_axes} = 'left'; | ||
| 873 | |||
| 874 | # copies of the current values at the x-ticks function | ||
| 875 | $self->{temp_x_min} = 0; | ||
| 876 | $self->{temp_x_max} = 0; | ||
| 877 | $self->{temp_y_min} = 0; | ||
| 878 | $self->{temp_y_max} = 0; | ||
| 879 | |||
| 880 | # Instance for a sum | ||
| 881 | $self->{sum} = 0; | ||
| 882 | |||
| 883 | # Don't sort the data unless they ask | ||
| 884 | $self->{'sort'} = 'false'; | ||
| 885 | |||
| 886 | # The Interval for drawing the x-axes in the split module | ||
| 887 | $self->{'interval'} = undef; | ||
| 888 | |||
| 889 | # The start value for the split chart | ||
| 890 | $self->{'start'} = undef; | ||
| 891 | |||
| 892 | # How many ticks do i have to draw at the x-axes in one interval of a split-plot? | ||
| 893 | $self->{'interval_ticks'} = 6; | ||
| 894 | |||
| 895 | # Draw the Lines in the split-chart normal | ||
| 896 | $self->{'scale'} = 1; | ||
| 897 | |||
| 898 | # Make a x-y plot | ||
| 899 | $self->{'xy_plot'} = 'false'; | ||
| 900 | |||
| 901 | # min and max for xy plot | ||
| 902 | $self->{'x_min_val'} = 1; | ||
| 903 | $self->{'x_max_val'} = 1; | ||
| 904 | |||
| 905 | # use the same error value in ErrorBars | ||
| 906 | $self->{'same_error'} = 'false'; | ||
| 907 | |||
| 908 | # Set the minimum and maximum number of circles to draw in a direction chart | ||
| 909 | $self->{'min_circles'} = 4, $self->{'max_circles'} = 100; | ||
| 910 | |||
| 911 | # set the style of a direction diagramm | ||
| 912 | $self->{'point'} = 'true', $self->{'line'} = 'false', $self->{'arrow'} = 'false'; | ||
| 913 | |||
| 914 | # The number of angel axes in a direction Chart | ||
| 915 | $self->{'angle_interval'} = 30; | ||
| 916 | |||
| 917 | # dont use different 'x_axes' in a direction Chart | ||
| 918 | $self->{'pairs'} = 'false'; | ||
| 919 | |||
| 920 | # polarplot for a direction Chart (not yet tested) | ||
| 921 | $self->{'polar'} = 'false'; | ||
| 922 | |||
| 923 | # guiding lines in a Pie Chart | ||
| 924 | $self->{'legend_lines'} = 'false'; | ||
| 925 | |||
| 926 | # Ring Chart instead of Pie | ||
| 927 | $self->{'ring'} = 1; # width of ring; i.e. normal pie | ||
| 928 | |||
| 929 | # stepline for Lines, LinesPoints | ||
| 930 | $self->{'stepline'} = 'false'; | ||
| 931 | $self->{'stepline_mode'} = 'end'; # begin, end | ||
| 932 | |||
| 933 | # used function to transform x- and y-tick labels to strings | ||
| 934 | $self->{f_x_tick} = \&_default_f_tick, $self->{f_y_tick} = \&_default_f_tick, $self->{f_z_tick} = \&_default_f_tick; | ||
| 935 | |||
| 936 | # default color specs for various color roles. | ||
| 937 | # Subclasses should extend as needed. | ||
| 938 | my $d = 0; | ||
| 939 | $self->{'colors_default_spec'} = { | ||
| 940 | background => 'white', | ||
| 941 | misc => 'black', | ||
| 942 | text => 'black', | ||
| 943 | y_label => 'black', | ||
| 944 | y_label2 => 'black', | ||
| 945 | grid_lines => 'black', | ||
| 946 | grey_background => 'gray90', | ||
| 947 | ( | ||
| 948 | map { 'dataset' . $d++ => $_ } | ||
| 949 | qw (flamescarlet forestgreen navy olive lightseagreen purple | ||
| 950 | orangepeel gold2 chartreuse3 cornflowerblue mediumpurple2 deeppink2 | ||
| 951 | galaxyblue hazelnut pottersclay BlueViolet | ||
| 952 | |||
| 953 | PaleGreen1 DarkBlue orange2 chocolate1 LightGreen | ||
| 954 | pink light_purple light_blue plum yellow turquoise light_green brown | ||
| 955 | PaleGreen2 MediumPurple PeachPuff1 orange3 chocolate2 | ||
| 956 | olive light_purple light_blue yellow turquoise light_green brown | ||
| 957 | DarkOrange PaleGreen3 SlateBlue BlueViolet PeachPuff2 orange4 | ||
| 958 | chocolate3 LightGreen light_purple light_blue light_green | ||
| 959 | snow1 honeydew3 SkyBlue1 cyan3 DarkOliveGreen1 IndianRed3 | ||
| 960 | orange1 LightPink3 MediumPurple1 snow3 LavenderBlush1 SkyBlue3 | ||
| 961 | DarkSlateGray1 DarkOliveGreen3 sienna1 orange3 PaleVioletRed1 | ||
| 962 | MediumPurple3 seashell1 LavenderBlush3 LightSkyBlue1) | ||
| 963 | ), | ||
| 964 | }; | ||
| 965 | |||
| 966 | # get default color specs for some color roles from alternate role. | ||
| 967 | # Subclasses should extend as needed. | ||
| 968 | $self->{'colors_default_role'} = { | ||
| 969 | 'x_grid_lines' => 'grid_lines', | ||
| 970 | 'y_grid_lines' => 'grid_lines', | ||
| 971 | 'y2_grid_lines' => 'grid_lines', # should be added by Chart::Composite... | ||
| 972 | }; | ||
| 973 | |||
| 974 | # Define style to plot dots in Points and Lines | ||
| 975 | $self->{'brushStyle'} = 'FilledCircle'; | ||
| 976 | |||
| 977 | # and return | ||
| 978 | return 1; | ||
| 979 | } | ||
| 980 | |||
| 981 | ## @fn private int _copy_data($extern_ref) | ||
| 982 | # Copy external data via a reference to internal memory. | ||
| 983 | # | ||
| 984 | # Remember the external reference.\n | ||
| 985 | # Therefore, this function can anly be called once! | ||
| 986 | # @param extern_ref Reference to external data space | ||
| 987 | sub _copy_data { | ||
| 988 | my $self = shift; | ||
| 989 | my $extern_ref = shift; | ||
| 990 | |||
| 991 | # look to see if they used the other api | ||
| 992 | if ( $self->{'dataref'} ) { | ||
| 993 | return 1; # we've already got a copy, thanks | ||
| 994 | } else { | ||
| 995 | return unless ref $extern_ref eq 'ARRAY'; | ||
| 996 | |||
| 997 | $self->{'dataref'} = [ map { \@$_ } @$extern_ref ]; # clone AoA | ||
| 998 | return 1; | ||
| 999 | } | ||
| 1000 | } | ||
| 1001 | |||
| 1002 | ## @fn private int _check_data | ||
| 1003 | # Check the internal data to be displayed. | ||
| 1004 | # | ||
| 1005 | # Make sure the data isn't really weird | ||
| 1006 | # and collect some basic info about it\n | ||
| 1007 | # Not logical data is 'carp'ed.\n | ||
| 1008 | # @return status of check | ||
| 1009 | sub _check_data { | ||
| 1010 | my $self = shift; | ||
| 1011 | my $length = 0; | ||
| 1012 | |||
| 1013 | # first make sure there's something there | ||
| 1014 | croak "Call me again when you have some data to chart" unless scalar @{ $self->{'dataref'} } >= 2; | ||
| 1015 | |||
| 1016 | # make sure we don't end up dividing by zero if they ask for | ||
| 1017 | # just one y_tick | ||
| 1018 | if ($self->{'y_ticks'} <= 1) { | ||
| 1019 | $self->{'y_ticks'} = 2; | ||
| 1020 | carp "The number of y_ticks displayed must be at least 2"; | ||
| 1021 | } | ||
| 1022 | |||
| 1023 | # remember the number of datasets | ||
| 1024 | $self->{'num_datasets'} = $#{ $self->{'dataref'} }; | ||
| 1025 | |||
| 1026 | # remember the number of points in the largest dataset | ||
| 1027 | $self->{'num_datapoints'} = 0; | ||
| 1028 | for ( 0 .. $self->{'num_datasets'} ) { | ||
| 1029 | if ( scalar( @{ $self->{'dataref'}[$_] } ) > $self->{'num_datapoints'} ) { | ||
| 1030 | $self->{'num_datapoints'} = scalar( @{ $self->{'dataref'}[$_] } ); | ||
| 1031 | } | ||
| 1032 | } | ||
| 1033 | |||
| 1034 | # find good min and max y-values for the plot | ||
| 1035 | $self->_find_y_scale(); | ||
| 1036 | |||
| 1037 | # find the longest x-tick label | ||
| 1038 | $length = 0; | ||
| 1039 | for ( @{ $self->{'dataref'}->[0] } ) { | ||
| 1040 | next if !defined($_); | ||
| 1041 | if ( length( $self->{f_x_tick}->($_) ) > $length ) { | ||
| 1042 | $length = length( $self->{f_x_tick}->($_) ); | ||
| 1043 | } | ||
| 1044 | } | ||
| 1045 | if ( $length <= 0 ) { $length = 1; } # make sure $length is positive and greater 0 | ||
| 1046 | |||
| 1047 | # now store it in the object | ||
| 1048 | $self->{'x_tick_label_length'} = $length; | ||
| 1049 | |||
| 1050 | # find x-scale, if a x-y plot is wanted | ||
| 1051 | # makes only sense for some charts | ||
| 1052 | if ( $self->true( $self->{'xy_plot'} ) && ( $self->isa('Chart::Lines') | ||
| 1053 | || $self->isa('Chart::Points') | ||
| 1054 | || $self->isa('Chart::LinesPoints') | ||
| 1055 | || $self->isa('Chart::Split') | ||
| 1056 | || $self->isa('Chart::ErrorBars') ) ) { | ||
| 1057 | $self->_find_x_scale; | ||
| 1058 | } | ||
| 1059 | |||
| 1060 | return 1; | ||
| 1061 | } | ||
| 1062 | |||
| 1063 | ## @fn private int _draw | ||
| 1064 | # Plot the chart to the gd object\n | ||
| 1065 | # Calls: | ||
| 1066 | # @see _draw_title | ||
| 1067 | # @see _draw_sub_title | ||
| 1068 | # @see _sort_data | ||
| 1069 | # @see _plot | ||
| 1070 | # | ||
| 1071 | # @return status | ||
| 1072 | sub _draw { | ||
| 1073 | my $self = shift; | ||
| 1074 | |||
| 1075 | # leave the appropriate border on the png | ||
| 1076 | $self->{'curr_x_max'} -= $self->{'png_border'}; | ||
| 1077 | $self->{'curr_x_min'} += $self->{'png_border'}; | ||
| 1078 | $self->{'curr_y_max'} -= $self->{'png_border'}; | ||
| 1079 | $self->{'curr_y_min'} += $self->{'png_border'}; | ||
| 1080 | |||
| 1081 | # draw in the title | ||
| 1082 | $self->_draw_title() if $self->{'title'}; | ||
| 1083 | |||
| 1084 | # have to leave this here for backwards compatibility | ||
| 1085 | $self->_draw_sub_title() if $self->{'sub_title'}; | ||
| 1086 | |||
| 1087 | # sort the data if they want to (mainly here to make sure | ||
| 1088 | # pareto charts get sorted) | ||
| 1089 | $self->_sort_data() if ( $self->true( $self->{'sort'} ) ); | ||
| 1090 | |||
| 1091 | # start drawing the data (most methods in this will be | ||
| 1092 | # overridden by the derived classes) | ||
| 1093 | # include _draw_legend() in this to ensure that the legend | ||
| 1094 | # will be flush with the chart | ||
| 1095 | $self->_plot(); | ||
| 1096 | |||
| 1097 | # and return | ||
| 1098 | return 1; | ||
| 1099 | } | ||
| 1100 | |||
| 1101 | ## @fn private int _set_colors | ||
| 1102 | # specify my colors | ||
| 1103 | # @return status | ||
| 1104 | sub _set_colors { | ||
| 1105 | my $self = shift; | ||
| 1106 | |||
| 1107 | my $index = $self->_color_role_to_index('background'); # allocate GD color | ||
| 1108 | if ( $self->true( $self->{'transparent'} ) ) | ||
| 1109 | { | ||
| 1110 | $self->{'gd_obj'}->transparent($index); | ||
| 1111 | } | ||
| 1112 | |||
| 1113 | # all other roles are initialized by calling $self->_color_role_to_index(ROLENAME); | ||
| 1114 | |||
| 1115 | # and return | ||
| 1116 | return 1; | ||
| 1117 | } | ||
| 1118 | |||
| 1119 | ## @fn private int _color_role_to_index | ||
| 1120 | # return a (list of) color index(es) corresponding to the (list of) role(s) | ||
| 1121 | # | ||
| 1122 | # @details wantarray | ||
| 1123 | # is a special keyword which returns a flag indicating | ||
| 1124 | # which context your subroutine has been called in. | ||
| 1125 | # It will return one of three values. | ||
| 1126 | # | ||
| 1127 | # @li true: If your subroutine has been called in list context | ||
| 1128 | # @li false: If your subroutine has been called in scalar context | ||
| 1129 | # @li undef: If your subroutine has been called in void context | ||
| 1130 | # | ||
| 1131 | # @return a (list of) color index(es) corresponding to the (list of) role(s) in \\\@_. | ||
| 1132 | sub _color_role_to_index { | ||
| 1133 | my $self = shift; | ||
| 1134 | |||
| 1135 | # Return a (list of) color index(es) corresponding to the (list of) role(s) in @_. | ||
| 1136 | my @result = map { | ||
| 1137 | my $role = $_; | ||
| 1138 | my $index = $self->{'color_table'}->{$role}; | ||
| 1139 | |||
| 1140 | unless ( defined $index ) { | ||
| 1141 | my $spec = | ||
| 1142 | $self->{'colors'}->{$role} | ||
| 1143 | || $self->{'colors_default_spec'}->{$role} | ||
| 1144 | || $self->{'colors_default_spec'}->{ $self->{'colors_default_role'}->{$role} }; | ||
| 1145 | my @rgb = $self->_color_spec_to_rgb( $role, $spec ); | ||
| 1146 | |||
| 1147 | my $string = sprintf " RGB(%d,%d,%d)", map { $_ + 0 } @rgb; | ||
| 1148 | |||
| 1149 | $index = $self->{'color_table'}->{$string}; | ||
| 1150 | unless ( defined $index ) { | ||
| 1151 | $index = $self->{'gd_obj'}->colorAllocate(@rgb); | ||
| 1152 | $self->{'color_table'}->{$string} = $index; | ||
| 1153 | } | ||
| 1154 | |||
| 1155 | $self->{'color_table'}->{$role} = $index; | ||
| 1156 | } | ||
| 1157 | $index; | ||
| 1158 | } @_; | ||
| 1159 | |||
| 1160 | ( wantarray && @_ > 1 ? @result : $result[0] ); | ||
| 1161 | } | ||
| 1162 | |||
| 1163 | sub _color_spec_to_rgb { | ||
| 1164 | my ($self, $role, $spec) = @_; # color role name (from set) for error msg | ||
| 1165 | my $color = Chart::Property::DataType::Color->new( $spec ); | ||
| 1166 | return croak "Unrecognized color for $role\n" unless ref $color; | ||
| 1167 | $color->rgb; | ||
| 1168 | } | ||
| 1169 | |||
| 1170 | ## @fn private int _brushStyles_of_roles | ||
| 1171 | # return a (list of) brushStyles corresponding to the (list of) role(s) | ||
| 1172 | # | ||
| 1173 | # @param list_of_roles List of roles (\\\@list_of_roles) | ||
| 1174 | # @return (list of) brushStyle(s) corresponding to the (list of) role(s) in \\\@_. | ||
| 1175 | # | ||
| 1176 | sub _brushStyles_of_roles { | ||
| 1177 | my $self = shift; | ||
| 1178 | map {exists $self->{'brushStyles'}{$_} ? $self->{'brushStyles'}{$_} : $self->{'brushStyle'}} @_; | ||
| 1179 | } | ||
| 1180 | |||
| 1181 | ## @fn private int _draw_title | ||
| 1182 | # draw the title for the chart | ||
| 1183 | # | ||
| 1184 | # The title was defined by the user in set('title' => ....)\n | ||
| 1185 | # The user may define some title lines by separating them via character '\\n';\n | ||
| 1186 | # The used font is taken from 'title_font';\n | ||
| 1187 | # The used color is calculated by function '_color_role_to_index' | ||
| 1188 | # based on 'title' or 'text'\n | ||
| 1189 | # @see _color_role_to_index | ||
| 1190 | # @return status | ||
| 1191 | sub draw_text { | ||
| 1192 | my ($self, $text, $font, $color) = @_; | ||
| 1193 | # !!! | ||
| 1194 | 1; | ||
| 1195 | } | ||
| 1196 | |||
| 1197 | sub _draw_title { | ||
| 1198 | my $self = shift; | ||
| 1199 | my $font = $self->{'title_font'}; | ||
| 1200 | my $color; | ||
| 1201 | my ( $h, $w, @lines, $x, $y ); | ||
| 1202 | |||
| 1203 | #get the right color | ||
| 1204 | $color = ( defined $self->{'colors'}{'title'} ) | ||
| 1205 | ? $self->_color_role_to_index('title') | ||
| 1206 | : $self->_color_role_to_index('text'); | ||
| 1207 | |||
| 1208 | # make sure we're actually using a real font | ||
| 1209 | croak "The title font you specified isn\'t a GD Font object" unless ref $font eq 'GD::Font' | ||
| 1210 | or ref $font eq 'Chart::Font'; | ||
| 1211 | |||
| 1212 | # get the height and width of the font | ||
| 1213 | ( $h, $w ) = ( $font->height, $font->width ); | ||
| 1214 | |||
| 1215 | # split the title into lines | ||
| 1216 | @lines = split( /\\n/, $self->{'title'} ); | ||
| 1217 | |||
| 1218 | # write the first line | ||
| 1219 | $x = ( $self->{'curr_x_max'} - $self->{'curr_x_min'} ) / 2 + $self->{'curr_x_min'} - ( length( $lines[0] ) * $w ) / 2; | ||
| 1220 | $y = $self->{'curr_y_min'} + $self->{'text_space'}; | ||
| 1221 | |||
| 1222 | #----------------------------------------------------------- | ||
| 1223 | |||
| 1224 | # Tests for Version 2.5 | ||
| 1225 | # ttf are found in /var/share/fonts/truetype/freefont/ | ||
| 1226 | |||
| 1227 | # /var/share/fonts/truetype | ||
| 1228 | |||
| 1229 | # Sketch for further processing | ||
| 1230 | # if ( $font ~= /^gd/ && ! -f $font ) | ||
| 1231 | # { | ||
| 1232 | # $self->{'gd_obj'}->string( $font, $x, $y, $lines[0], $color ); | ||
| 1233 | # } | ||
| 1234 | # elsif ( -f $font ) | ||
| 1235 | # { | ||
| 1236 | # my $fontname = '/var/share/fonts/truetype/freefont/FreeSerifBoldItalic.ttf'; | ||
| 1237 | # $self->{'gd_obj'}->stringFT( $color, $fontname, 8,0, $x, $y, $lines[0] ); | ||
| 1238 | # } | ||
| 1239 | |||
| 1240 | # my $fontname = '/var/share/fonts/truetype/freefont/FreeSerifBoldItalic.ttf'; | ||
| 1241 | # # size, angle | ||
| 1242 | # $self->{'gd_obj'}->stringFT( $color, $fontname, 12,0, $x, $y, $lines[0] ); | ||
| 1243 | |||
| 1244 | #----------------------------------------------------------------- | ||
| 1245 | $self->{'gd_obj'}->string( $font, $x, $y, $lines[0], $color ); | ||
| 1246 | |||
| 1247 | # now loop through the rest of them | ||
| 1248 | # (the font is decreased in width and height by 1 | ||
| 1249 | if ( $w > 1 ) { $w-- } | ||
| 1250 | if ( $h > 1 ) { $h-- } | ||
| 1251 | for ( 1 .. $#lines ) { | ||
| 1252 | $self->{'curr_y_min'} += $self->{'text_space'} + $h; | ||
| 1253 | $x = ( $self->{'curr_x_max'} - $self->{'curr_x_min'} ) / 2 + $self->{'curr_x_min'} - ( length( $lines[$_] ) * $w ) / 2; | ||
| 1254 | $y = $self->{'curr_y_min'} + $self->{'text_space'}; | ||
| 1255 | $self->{'gd_obj'}->string( $font, $x, $y, $lines[$_], $color ); | ||
| 1256 | } | ||
| 1257 | |||
| 1258 | # mark off that last space | ||
| 1259 | $self->{'curr_y_min'} += 2 * $self->{'text_space'} + $h; | ||
| 1260 | |||
| 1261 | # and return | ||
| 1262 | return 1; | ||
| 1263 | } | ||
| 1264 | |||
| 1265 | ## @fn private int _draw_sub_title() | ||
| 1266 | # draw the sub-title for the chart | ||
| 1267 | # @see _draw_title\n | ||
| 1268 | # _draw_sub_title() is more or less obsolete as _draw_title() does the same | ||
| 1269 | # by writing more than one line as the title. | ||
| 1270 | # Both use decreased width and height of the font by one. | ||
| 1271 | # @return status | ||
| 1272 | sub _draw_sub_title { | ||
| 1273 | my $self = shift; | ||
| 1274 | |||
| 1275 | my $font = $self->{'sub_title_font'}; | ||
| 1276 | my $text = $self->{'sub_title'}; | ||
| 1277 | return 1 if length($text) == 0; # nothing to plot | ||
| 1278 | |||
| 1279 | #get the right color | ||
| 1280 | my $color; | ||
| 1281 | if ( defined $self->{'colors'}{'title'} ) | ||
| 1282 | { | ||
| 1283 | $color = $self->_color_role_to_index('title'); | ||
| 1284 | } | ||
| 1285 | else | ||
| 1286 | { | ||
| 1287 | $color = $self->_color_role_to_index('text'); | ||
| 1288 | } | ||
| 1289 | |||
| 1290 | my ( $h, $w, $x, $y ); | ||
| 1291 | |||
| 1292 | # make sure we're using a real font | ||
| 1293 | unless ( ( ref($font) ) eq 'GD::Font' ) | ||
| 1294 | { | ||
| 1295 | croak "The subtitle font you specified isn\'t a GD Font object"; | ||
| 1296 | } | ||
| 1297 | |||
| 1298 | # get the size of the font | ||
| 1299 | ( $h, $w ) = ( $font->height, $font->width ); | ||
| 1300 | if ( $h > 1 && $w > 1 ) { $h--, $w-- } | ||
| 1301 | |||
| 1302 | # figure out the placement | ||
| 1303 | $x = ( $self->{'curr_x_max'} - $self->{'curr_x_min'} ) / 2 + $self->{'curr_x_min'} - ( length($text) * $w ) / 2; | ||
| 1304 | $y = $self->{'curr_y_min'}; | ||
| 1305 | |||
| 1306 | # now draw the subtitle | ||
| 1307 | $self->{'gd_obj'}->string( $font, $x, $y, $text, $color ); | ||
| 1308 | |||
| 1309 | # Adapt curr_y_min | ||
| 1310 | $self->{'curr_y_min'} += $self->{'text_space'} + $h; | ||
| 1311 | |||
| 1312 | # and return | ||
| 1313 | return 1; | ||
| 1314 | } | ||
| 1315 | |||
| 1316 | ## @fn private int _sort_data() | ||
| 1317 | # sort the data nicely (mostly for the pareto charts and xy-plots) | ||
| 1318 | # @return status | ||
| 1319 | sub _sort_data { | ||
| 1320 | my $self = shift; | ||
| 1321 | my $data_ref = $self->{'dataref'}; | ||
| 1322 | my @data = @{ $self->{'dataref'} }; | ||
| 1323 | my @sort_index; | ||
| 1324 | |||
| 1325 | #sort the data with slices | ||
| 1326 | @sort_index = sort { $data[0][$a] <=> $data[0][$b] } ( 0 .. scalar( @{ $data[1] } ) - 1 ); | ||
| 1327 | for ( 1 .. $#data ) | ||
| 1328 | { | ||
| 1329 | @{ $self->{'dataref'}->[$_] } = @{ $self->{'dataref'}->[$_] }[@sort_index]; | ||
| 1330 | } | ||
| 1331 | @{ $data_ref->[0] } = sort { $a <=> $b } @{ $data_ref->[0] }; | ||
| 1332 | |||
| 1333 | #finally return | ||
| 1334 | return 1; | ||
| 1335 | } | ||
| 1336 | |||
| 1337 | ## @fn private int _find_x_scale() | ||
| 1338 | # For a xy-plot do the same for the x values, as '_find_y_scale' does for the y values! | ||
| 1339 | # @see _find_y_scale | ||
| 1340 | # @return status | ||
| 1341 | sub _find_x_scale { | ||
| 1342 | my $self = shift; | ||
| 1343 | my @data = @{ $self->{'dataref'} }; | ||
| 1344 | my ( $i, $j ); | ||
| 1345 | my ( $d_min, $d_max ); | ||
| 1346 | my ( $p_min, $p_max, $f_min, $f_max ); | ||
| 1347 | my ( $tickInterval, $tickCount, $skip ); | ||
| 1348 | my @tickLabels; | ||
| 1349 | my $maxtickLabelLen = 0; | ||
| 1350 | |||
| 1351 | #look, if we have numbers | ||
| 1352 | #see also if we only have integers | ||
| 1353 | for $i ( 0 .. ( $self->{'num_datasets'} ) ) | ||
| 1354 | { | ||
| 1355 | for $j ( 0 .. ( $self->{'num_datapoints'} - 1 ) ) | ||
| 1356 | { | ||
| 1357 | |||
| 1358 | # the following regular Expression matches all possible numbers, including scientific numbers | ||
| 1359 | # iff data is defined | ||
| 1360 | if ( defined $data[$i][$j] and $data[$i][$j] !~ m/^[\+\-]?((\.\d+)|(\d+\.?\d*))([eE][+-]?\d+)?[fFdD]?$/ ) | ||
| 1361 | { | ||
| 1362 | croak "<$data[$i][$j]> You should give me numbers for drawing a xy plot!\n"; | ||
| 1363 | } | ||
| 1364 | } | ||
| 1365 | } | ||
| 1366 | |||
| 1367 | #find the dataset min and max | ||
| 1368 | ( $d_min, $d_max ) = $self->_find_x_range(); | ||
| 1369 | |||
| 1370 | # Force the inclusion of zero if the user has requested it. | ||
| 1371 | if ( $self->true( $self->{'include_zero'} ) ) | ||
| 1372 | { | ||
| 1373 | if ( ( $d_min * $d_max ) > 0 ) # If both are non zero and of the same sign. | ||
| 1374 | { | ||
| 1375 | if ( $d_min > 0 ) # If the whole scale is positive. | ||
| 1376 | { | ||
| 1377 | $d_min = 0; | ||
| 1378 | } | ||
| 1379 | else # The scale is entirely negative. | ||
| 1380 | { | ||
| 1381 | $d_max = 0; | ||
| 1382 | } | ||
| 1383 | } | ||
| 1384 | } | ||
| 1385 | |||
| 1386 | # Calculate the width of the dataset. (possibly modified by the user) | ||
| 1387 | my $d_width = $d_max - $d_min; | ||
| 1388 | |||
| 1389 | # If the width of the range is zero, forcebly widen it | ||
| 1390 | # (to avoid division by zero errors elsewhere in the code). | ||
| 1391 | if ( 0 == $d_width ) | ||
| 1392 | { | ||
| 1393 | $d_min--, $d_max++, $d_width = 2; | ||
| 1394 | } | ||
| 1395 | |||
| 1396 | # Descale the range by converting the dataset width into | ||
| 1397 | # a floating point exponent & mantisa pair. | ||
| 1398 | my ( $rangeExponent, $rangeMantisa ) = $self->_sepFP($d_width); | ||
| 1399 | my $rangeMuliplier = 10**$rangeExponent; | ||
| 1400 | |||
| 1401 | # Find what tick | ||
| 1402 | # to use & how many ticks to plot, | ||
| 1403 | # round the plot min & max to suatable round numbers. | ||
| 1404 | ( $tickInterval, $tickCount, $p_min, $p_max ) = $self->_calcXTickInterval( | ||
| 1405 | $d_min / $rangeMuliplier, | ||
| 1406 | $d_max / $rangeMuliplier, | ||
| 1407 | $f_min, $f_max, | ||
| 1408 | $self->{'min_x_ticks'}, | ||
| 1409 | $self->{'max_x_ticks'} | ||
| 1410 | ); | ||
| 1411 | |||
| 1412 | # Restore the tickInterval etc to the correct scale | ||
| 1413 | $_ *= $rangeMuliplier foreach ( $tickInterval, $p_min, $p_max ); | ||
| 1414 | |||
| 1415 | #get the precision for the labels | ||
| 1416 | my $precision = $self->{'precision'}; | ||
| 1417 | |||
| 1418 | # Now sort out an array of tick labels. | ||
| 1419 | for ( my $labelNum = $p_min ; $labelNum < $p_max + $tickInterval / 2 ; $labelNum += $tickInterval ) | ||
| 1420 | { | ||
| 1421 | my $labelText; | ||
| 1422 | |||
| 1423 | if ( defined $self->{f_y_tick} ) | ||
| 1424 | { | ||
| 1425 | |||
| 1426 | # Is _default_f_tick function used? | ||
| 1427 | if ( $self->{f_y_tick} == \&_default_f_tick ) | ||
| 1428 | { | ||
| 1429 | $labelText = sprintf( "%." . $precision . "f", $labelNum ); | ||
| 1430 | } | ||
| 1431 | else | ||
| 1432 | { | ||
| 1433 | $labelText = $self->{f_y_tick}->($labelNum); | ||
| 1434 | } | ||
| 1435 | } | ||
| 1436 | else | ||
| 1437 | { | ||
| 1438 | $labelText = sprintf( "%." . $precision . "f", $labelNum ); | ||
| 1439 | } | ||
| 1440 | |||
| 1441 | push @tickLabels, $labelText; | ||
| 1442 | $maxtickLabelLen = length $labelText if $maxtickLabelLen < length $labelText; | ||
| 1443 | } | ||
| 1444 | |||
| 1445 | # Store the calculated data. | ||
| 1446 | $self->{'x_min_val'} = $p_min, | ||
| 1447 | $self->{'x_max_val'} = $p_max, | ||
| 1448 | $self->{'x_tick_labels'} = \@tickLabels, | ||
| 1449 | $self->{'x_tick_label_length'} = $maxtickLabelLen, | ||
| 1450 | $self->{'x_number_ticks'} = $tickCount; | ||
| 1451 | return 1; | ||
| 1452 | } | ||
| 1453 | |||
| 1454 | ## @fn private int _find_y_scale() | ||
| 1455 | # find good values for the minimum and maximum y-value on the chart | ||
| 1456 | # @return status | ||
| 1457 | # | ||
| 1458 | # New version, re-written by David Pottage of Tao Group.\n | ||
| 1459 | # This code is *AS IS* and comes with *NO WARRANTY*\n | ||
| 1460 | # | ||
| 1461 | # This Sub calculates correct values for the following class local variables, | ||
| 1462 | # if they have not been set by the user. | ||
| 1463 | # | ||
| 1464 | # max_val, min_val: The maximum and minimum values for the y axis.\n | ||
| 1465 | # y_ticks: The number of ticks to plot on the y scale, including | ||
| 1466 | # the end points. e.g. If the scale runs from 0 to 50, | ||
| 1467 | # with ticks every 10, y_ticks will have the value of 6.\n | ||
| 1468 | # y_tick_labels: An array of strings, each is a label for the y axis.\n | ||
| 1469 | # y_tick_labels_length: The length to allow for B tick labels. (How long is | ||
| 1470 | # the longest?) | ||
| 1471 | sub _find_y_scale { | ||
| 1472 | my $self = shift; | ||
| 1473 | |||
| 1474 | # Predeclare vars. | ||
| 1475 | my ( $d_min, $d_max ); # Dataset min & max. | ||
| 1476 | my ( $p_min, $p_max ); # Plot min & max. | ||
| 1477 | my ( $tickInterval, $tickCount, $skip ); | ||
| 1478 | my @tickLabels; # List of labels for each tick. | ||
| 1479 | my $maxtickLabelLen = 0; # The length of the longest tick label. | ||
| 1480 | my $prec_test = 0; # Boolean which indicate if precision < |rangeExponent| | ||
| 1481 | my $temp_rangeExponent; | ||
| 1482 | |||
| 1483 | my $flag_all_integers = 1; # assume true | ||
| 1484 | |||
| 1485 | # Find the dataset minimum and maximum. | ||
| 1486 | ( $d_min, $d_max, $flag_all_integers ) = $self->_find_y_range(); | ||
| 1487 | |||
| 1488 | # Force the inclusion of zero if the user has requested it. | ||
| 1489 | if ( $self->true( $self->{'include_zero'} ) ) | ||
| 1490 | { | ||
| 1491 | |||
| 1492 | #print "include_zero = true\n"; | ||
| 1493 | if ( ( $d_min * $d_max ) > 0 ) # If both are non zero and of the same sign. | ||
| 1494 | { | ||
| 1495 | if ( $d_min > 0 ) # If the whole scale is positive. | ||
| 1496 | { | ||
| 1497 | $d_min = 0; | ||
| 1498 | } | ||
| 1499 | else # The scale is entirely negative. | ||
| 1500 | { | ||
| 1501 | $d_max = 0; | ||
| 1502 | } | ||
| 1503 | } | ||
| 1504 | } | ||
| 1505 | |||
| 1506 | if ( $self->true( $self->{'integer_ticks_only'} ) ) { | ||
| 1507 | |||
| 1508 | # Allow the dataset range to be overidden by the user. | ||
| 1509 | # f_min/f_max are booleans which indicate that the min & max should not be modified. | ||
| 1510 | my $f_min = 0; | ||
| 1511 | if ( defined $self->{'min_val'} ) { $f_min = 1; } | ||
| 1512 | $d_min = $self->{'min_val'} if $f_min; | ||
| 1513 | |||
| 1514 | my $f_max = 0; | ||
| 1515 | if ( defined $self->{'max_val'} ) { $f_max = 1; } | ||
| 1516 | $d_max = $self->{'max_val'} if $f_max; | ||
| 1517 | |||
| 1518 | # Assert against defined min and max. | ||
| 1519 | if ( !defined $d_min || !defined $d_max ) | ||
| 1520 | { | ||
| 1521 | croak "No min_val or max_val is defined"; | ||
| 1522 | } | ||
| 1523 | |||
| 1524 | # Assert against the min is larger than the max. | ||
| 1525 | if ( $d_min > $d_max ) | ||
| 1526 | { | ||
| 1527 | croak "The specified 'min_val' & 'max_val' values are reversed (min > max: $d_min>$d_max)"; | ||
| 1528 | } | ||
| 1529 | |||
| 1530 | # The user asked for integer ticks, force the limits to integers. | ||
| 1531 | # & work out the range directly. | ||
| 1532 | #$p_min = $self->_round2Tick($d_min, 1, -1); | ||
| 1533 | #$p_max = $self->_round2Tick($d_max, 1, 1); | ||
| 1534 | |||
| 1535 | $skip = $self->{skip_int_ticks}; | ||
| 1536 | $skip = 1 if $skip < 1; | ||
| 1537 | |||
| 1538 | $p_min = $self->_round2Tick( $d_min, 1, -1 ); | ||
| 1539 | $p_max = $self->_round2Tick( $d_max, 1, 1 ); | ||
| 1540 | if ( ( $p_max - $p_min ) == 0 ) | ||
| 1541 | { | ||
| 1542 | $p_max++ if ( $f_max != 1 ); # p_max is not defined by the user | ||
| 1543 | $p_min-- if ( $f_min != 1 ); # p_min is not defined by the user | ||
| 1544 | $p_max++ if ( ( $p_max - $p_min ) == 0 ); | ||
| 1545 | } | ||
| 1546 | |||
| 1547 | $tickInterval = $skip; | ||
| 1548 | $tickCount = ( $p_max - $p_min ) / $skip + 1; | ||
| 1549 | |||
| 1550 | # Now sort out an array of tick labels. | ||
| 1551 | |||
| 1552 | for ( my $labelNum = $p_min ; $labelNum < $p_max + $tickInterval / 3 ; $labelNum += $tickInterval ) | ||
| 1553 | { | ||
| 1554 | my $labelText; | ||
| 1555 | |||
| 1556 | if ( defined $self->{f_y_tick} ) | ||
| 1557 | { | ||
| 1558 | |||
| 1559 | # Is _default_f_tick function used? | ||
| 1560 | if ( $self->{f_y_tick} == \&_default_f_tick ) | ||
| 1561 | { | ||
| 1562 | $labelText = sprintf( "%d", $labelNum ); | ||
| 1563 | } | ||
| 1564 | else | ||
| 1565 | { | ||
| 1566 | $labelText = $self->{f_y_tick}->($labelNum); | ||
| 1567 | } | ||
| 1568 | } | ||
| 1569 | else | ||
| 1570 | { | ||
| 1571 | $labelText = sprintf( "%d", $labelNum ); | ||
| 1572 | } | ||
| 1573 | |||
| 1574 | push @tickLabels, $labelText; | ||
| 1575 | $maxtickLabelLen = length $labelText if $maxtickLabelLen < length $labelText; | ||
| 1576 | } | ||
| 1577 | } else { | ||
| 1578 | |||
| 1579 | # Allow the dataset range to be overidden by the user. | ||
| 1580 | # f_min/f_max are booleans which indicate that the min & max should not be modified. | ||
| 1581 | my $f_min = 0; | ||
| 1582 | if ( defined $self->{'min_val'} ) { $f_min = 1; } | ||
| 1583 | $d_min = $self->{'min_val'} if $f_min; | ||
| 1584 | |||
| 1585 | my $f_max = 0; | ||
| 1586 | if ( defined $self->{'max_val'} ) { $f_max = 1; } | ||
| 1587 | $d_max = $self->{'max_val'} if $f_max; | ||
| 1588 | |||
| 1589 | # print "fmin $f_min fmax $f_max\n"; | ||
| 1590 | # print "dmin $d_min dmax $d_max\n"; | ||
| 1591 | |||
| 1592 | # Assert against defined min and max. | ||
| 1593 | if ( !defined $d_min || !defined $d_max ) | ||
| 1594 | { | ||
| 1595 | croak "No min_val or max_val is defined"; | ||
| 1596 | } | ||
| 1597 | |||
| 1598 | # Assert against the min is larger than the max. | ||
| 1599 | if ( $d_min > $d_max ) | ||
| 1600 | { | ||
| 1601 | croak "The the specified 'min_val' & 'max_val' values are reversed (min > max: $d_min>$d_max)"; | ||
| 1602 | } | ||
| 1603 | |||
| 1604 | # Calculate the width of the dataset. (possibly modified by the user) | ||
| 1605 | my $d_width = $d_max - $d_min; | ||
| 1606 | |||
| 1607 | # If the width of the range is zero, forcibly widen it | ||
| 1608 | # (to avoid division by zero errors elsewhere in the code). | ||
| 1609 | if ( $d_width == 0 ) | ||
| 1610 | { | ||
| 1611 | $d_min--, $d_max++, $d_width = 2; | ||
| 1612 | } | ||
| 1613 | |||
| 1614 | # Descale the range by converting the dataset width into | ||
| 1615 | # a floating point exponent & mantisa pair. | ||
| 1616 | my ( $rangeExponent, $rangeMantisa ) = $self->_sepFP($d_width); | ||
| 1617 | my $rangeMuliplier = 10**$rangeExponent; | ||
| 1618 | |||
| 1619 | # print "fmin $f_min fmax $f_max\n"; | ||
| 1620 | # print "dmin $d_min dmax $d_max\n"; | ||
| 1621 | |||
| 1622 | # Find what tick | ||
| 1623 | # to use & how many ticks to plot, | ||
| 1624 | # round the plot min & max to suitable round numbers. | ||
| 1625 | ( $tickInterval, $tickCount, $p_min, $p_max ) = $self->_calcTickInterval( | ||
| 1626 | $d_min / $rangeMuliplier, | ||
| 1627 | $d_max / $rangeMuliplier, | ||
| 1628 | $f_min, $f_max, | ||
| 1629 | $self->{'min_y_ticks'}, | ||
| 1630 | $self->{'max_y_ticks'} | ||
| 1631 | ); | ||
| 1632 | |||
| 1633 | # Restore the tickInterval etc to the correct scale | ||
| 1634 | $_ *= $rangeMuliplier foreach ( $tickInterval, $p_min, $p_max ); | ||
| 1635 | |||
| 1636 | # Is precision < |rangeExponent|? | ||
| 1637 | if ( $rangeExponent < 0 ) | ||
| 1638 | { | ||
| 1639 | $temp_rangeExponent = -$rangeExponent; | ||
| 1640 | } | ||
| 1641 | else | ||
| 1642 | { | ||
| 1643 | $temp_rangeExponent = $rangeExponent; | ||
| 1644 | } | ||
| 1645 | |||
| 1646 | # print "pmin $p_min pmax $p_max\n"; | ||
| 1647 | # print "range exponent $rangeExponent\n"; | ||
| 1648 | |||
| 1649 | #get the precision for the labels | ||
| 1650 | my $precision = $self->{'precision'}; | ||
| 1651 | |||
| 1652 | if ( $temp_rangeExponent != 0 | ||
| 1653 | && $rangeExponent < 0 | ||
| 1654 | && $temp_rangeExponent > $precision ) | ||
| 1655 | { | ||
| 1656 | $prec_test = 1; | ||
| 1657 | } | ||
| 1658 | |||
| 1659 | # Now sort out an array of tick labels. | ||
| 1660 | for ( my $labelNum = $p_min ; $labelNum < $p_max + $tickInterval / 2 ; $labelNum += $tickInterval ) | ||
| 1661 | { | ||
| 1662 | my $labelText; | ||
| 1663 | if ( defined $self->{f_y_tick} ) | ||
| 1664 | { | ||
| 1665 | |||
| 1666 | # Is _default_f_tick function used? | ||
| 1667 | if ( ( $self->{f_y_tick} == \&_default_f_tick ) && ( $prec_test == 0 ) ) | ||
| 1668 | { | ||
| 1669 | $labelText = sprintf( "%." . $precision . "f", $labelNum ); | ||
| 1670 | } | ||
| 1671 | |||
| 1672 | # If precision <|rangeExponent| print the labels whith exponents | ||
| 1673 | elsif ( ( $self->{f_y_tick} == \&_default_f_tick ) && ( $prec_test == 1 ) ) | ||
| 1674 | { | ||
| 1675 | $labelText = $self->{f_y_tick}->($labelNum); | ||
| 1676 | |||
| 1677 | # print "precision $precision\n"; | ||
| 1678 | # print "temp range exponent $temp_rangeExponent\n"; | ||
| 1679 | # print "range exponent $rangeExponent\n"; | ||
| 1680 | # print "labelText $labelText\n"; | ||
| 1681 | |||
| 1682 | } | ||
| 1683 | else | ||
| 1684 | { | ||
| 1685 | $labelText = $self->{f_y_tick}->($labelNum); | ||
| 1686 | } | ||
| 1687 | } | ||
| 1688 | else | ||
| 1689 | { | ||
| 1690 | $labelText = sprintf( "%." . $precision . "f", $labelNum ); | ||
| 1691 | } | ||
| 1692 | push @tickLabels, $labelText; | ||
| 1693 | $maxtickLabelLen = length $labelText if $maxtickLabelLen < length $labelText; | ||
| 1694 | } # end for | ||
| 1695 | } | ||
| 1696 | |||
| 1697 | # Store the calculated data. | ||
| 1698 | #### begin debugging output | ||
| 1699 | #if ( defined $self->{'y_ticks'} ) | ||
| 1700 | #{ | ||
| 1701 | # print "_find_y_scale: self->{'y_ticks'}=".$self->{'y_ticks'}."\n"; | ||
| 1702 | #} | ||
| 1703 | #else | ||
| 1704 | #{ | ||
| 1705 | # print "_find_y_scale: self->{'y_ticks'}= NOT DEFINED\n"; | ||
| 1706 | #} | ||
| 1707 | #if ( defined $self->{'min_val'} ) | ||
| 1708 | #{ | ||
| 1709 | # print "_find_y_scale: self->{'min_val'}=".$self->{'min_val'}."\n"; | ||
| 1710 | #} | ||
| 1711 | #else | ||
| 1712 | #{ | ||
| 1713 | # print "_find_y_scale: self->{'min_val'}=NOT DEFINED\n"; | ||
| 1714 | #} | ||
| 1715 | #if ( defined $self->{'max_val'} ) | ||
| 1716 | #{ | ||
| 1717 | # print "_find_y_scale: self->{'max_val'}=".$self->{'max_val'}."\n"; | ||
| 1718 | #} | ||
| 1719 | #else | ||
| 1720 | #{ | ||
| 1721 | # print "_find_y_scale: self->{'max_val'}= NOT DEFINED\n"; | ||
| 1722 | #} | ||
| 1723 | #### end debugging output | ||
| 1724 | |||
| 1725 | $self->{'min_val'} = $p_min, | ||
| 1726 | $self->{'max_val'} = $p_max, | ||
| 1727 | $self->{'y_ticks'} = $tickCount, | ||
| 1728 | $self->{'y_tick_labels'} = \@tickLabels, | ||
| 1729 | $self->{'y_tick_label_length'} = $maxtickLabelLen; | ||
| 1730 | |||
| 1731 | ################## | ||
| 1732 | #print statement is for debug only | ||
| 1733 | #print "_find_y_scale: min_val = $p_min, max_val=$p_max\n"; | ||
| 1734 | ################## | ||
| 1735 | |||
| 1736 | # and return. | ||
| 1737 | return 1; | ||
| 1738 | } | ||
| 1739 | |||
| 1740 | ## @fn private _calcTickInterval($dataset_min, $dataset_max, $flag_fixed_min, $flag_fixed_max, $minTicks, $maxTicks) | ||
| 1741 | # @brief | ||
| 1742 | # Calculate the Interval between ticks in y direction | ||
| 1743 | # | ||
| 1744 | # @details | ||
| 1745 | # Calculate the Interval between ticks in y direction | ||
| 1746 | # and compare the number of ticks to | ||
| 1747 | # the user's given values min_y_ticks, max_y_ticks. | ||
| 1748 | # | ||
| 1749 | # @param[in] dataset_min Minimal value in y direction | ||
| 1750 | # @param[in] dataset_max Maximal value in y direction | ||
| 1751 | # @param[in] flag_fixed_min Indicator whether the dataset_min value is fixed | ||
| 1752 | # @param[in] flag_fixed_max Indicator whether the dataset_max value is fixed | ||
| 1753 | # @param[in] minTicks Minimal number of ticks wanted | ||
| 1754 | # @param[in] maxTicks Maximal number of ticks wanted | ||
| 1755 | # @return Array of ($tickInterval, $tickCount, $pMin, $pMax) | ||
| 1756 | # | ||
| 1757 | sub _calcTickInterval | ||
| 1758 | { | ||
| 1759 | my $self = shift; | ||
| 1760 | |||
| 1761 | my ( | ||
| 1762 | $dataset_min, $dataset_max, # The dataset min & max. | ||
| 1763 | $flag_fixed_min, $flag_fixed_max, # Indicates if those min/max are fixed. | ||
| 1764 | $minTicks, $maxTicks, # The minimum & maximum number of ticks. | ||
| 1765 | ) = @_; | ||
| 1766 | |||
| 1767 | # print "calcTickInterval dataset_min $dataset_min dataset_max $dataset_max flag_fixed_min $flag_fixed_min flag_mixed_max $flag_fixed_max\n"; | ||
| 1768 | |||
| 1769 | # Verify the supplied 'min_y_ticks' & 'max_y_ticks' are sensible. | ||
| 1770 | if ( $minTicks < 2 ) | ||
| 1771 | { | ||
| 1772 | |||
| 1773 | #print STDERR "Chart::Base::_calcTickInterval : Incorrect value for 'min_y_ticks', too small (less than 2).\n"; | ||
| 1774 | $minTicks = 2; | ||
| 1775 | } | ||
| 1776 | |||
| 1777 | if ( $maxTicks < 5 * $minTicks ) | ||
| 1778 | { | ||
| 1779 | |||
| 1780 | #print STDERR "Chart::Base::_calcTickInterval : Incorrect value for 'max_y_ticks', too small (<5*minTicks).\n"; | ||
| 1781 | $maxTicks = 5 * $minTicks; | ||
| 1782 | } | ||
| 1783 | |||
| 1784 | my $width = $dataset_max - $dataset_min; | ||
| 1785 | my @divisorList; | ||
| 1786 | |||
| 1787 | for ( my $baseMul = 1 ; ; $baseMul *= 10 ) | ||
| 1788 | { | ||
| 1789 | TRY: foreach my $tryMul ( 1, 2, 5 ) | ||
| 1790 | { | ||
| 1791 | |||
| 1792 | # Calc a fresh, smaller tick interval. | ||
| 1793 | my $divisor = $baseMul * $tryMul; | ||
| 1794 | |||
| 1795 | # Count the number of ticks. | ||
| 1796 | my ( $tickCount, $pMin, $pMax ) = $self->_countTicks( $dataset_min, $dataset_max, 1 / $divisor ); | ||
| 1797 | |||
| 1798 | # Look a the number of ticks. | ||
| 1799 | if ( $maxTicks < $tickCount ) | ||
| 1800 | { | ||
| 1801 | |||
| 1802 | # If it is to high, Backtrack. | ||
| 1803 | $divisor = pop @divisorList; | ||
| 1804 | |||
| 1805 | # just for security: | ||
| 1806 | if ( !defined($divisor) || $divisor == 0 ) { $divisor = 1; } | ||
| 1807 | ( $tickCount, $pMin, $pMax ) = $self->_countTicks( $dataset_min, $dataset_max, 1 / $divisor ); | ||
| 1808 | |||
| 1809 | #print STDERR "\nChart::Base : Caution: Tick limit of $maxTicks exceeded. Backing of to an interval of ".1/$divisor." which plots $tickCount ticks\n"; | ||
| 1810 | return ( 1 / $divisor, $tickCount, $pMin, $pMax ); | ||
| 1811 | } | ||
| 1812 | elsif ( $minTicks > $tickCount ) | ||
| 1813 | { | ||
| 1814 | |||
| 1815 | # If it is too low, try again. | ||
| 1816 | next TRY; | ||
| 1817 | } | ||
| 1818 | else | ||
| 1819 | { | ||
| 1820 | |||
| 1821 | # Store the divisor for possible later backtracking. | ||
| 1822 | push @divisorList, $divisor; | ||
| 1823 | |||
| 1824 | # if the min or max is fixed, check they will fit in the interval. | ||
| 1825 | next TRY if ( $flag_fixed_min && ( int( $dataset_min * $divisor ) != ( $dataset_min * $divisor ) ) ); | ||
| 1826 | next TRY if ( $flag_fixed_max && ( int( $dataset_max * $divisor ) != ( $dataset_max * $divisor ) ) ); | ||
| 1827 | |||
| 1828 | # If everything passes the tests, return. | ||
| 1829 | return ( 1 / $divisor, $tickCount, $pMin, $pMax ); | ||
| 1830 | } | ||
| 1831 | } | ||
| 1832 | } | ||
| 1833 | |||
| 1834 | die "can't happen!"; | ||
| 1835 | } | ||
| 1836 | |||
| 1837 | ## @fn private int _calcXTickInterval($min,$max,$minF,$maxF,$minTicks,$maxTicks) | ||
| 1838 | # @brief | ||
| 1839 | # Calculate the Interval between ticks in x direction | ||
| 1840 | # | ||
| 1841 | # @details | ||
| 1842 | # Calculate the Interval between ticks in x direction | ||
| 1843 | # and compare the number of ticks to | ||
| 1844 | # the user's given values minTicks, maxTicks. | ||
| 1845 | # | ||
| 1846 | # @param[in] min Minimal value of dataset in x direction | ||
| 1847 | # @param[in] max Maximal value of dataset in x direction | ||
| 1848 | # @param[in] minF Inddicator if those min value is fixed | ||
| 1849 | # @param[in] maxF Inddicator if those max value is fixed | ||
| 1850 | # @param[in] minTicks Minimal number of tick in x direction | ||
| 1851 | # @param[in] maxTicks Maximal number of tick in x direction | ||
| 1852 | # @return $tickInterval, $tickCount, $pMin, $pMax | ||
| 1853 | sub _calcXTickInterval { | ||
| 1854 | my $self = shift; | ||
| 1855 | my ( | ||
| 1856 | $min, $max, # The dataset min & max. | ||
| 1857 | $minF, $maxF, # Indicates if those min/max are fixed. | ||
| 1858 | $minTicks, $maxTicks, # The minimum & maximum number of ticks. | ||
| 1859 | ) = @_; | ||
| 1860 | |||
| 1861 | # Verify the supplied 'min_y_ticks' & 'max_y_ticks' are sensible. | ||
| 1862 | if ( $minTicks < 2 ) | ||
| 1863 | { | ||
| 1864 | |||
| 1865 | #print STDERR "Chart::Base::_calcXTickInterval : Incorrect value for 'min_y_ticks', too small.\n"; | ||
| 1866 | $minTicks = 2; | ||
| 1867 | } | ||
| 1868 | |||
| 1869 | if ( $maxTicks < 5 * $minTicks ) | ||
| 1870 | { | ||
| 1871 | |||
| 1872 | #print STDERR "Chart::Base::_calcXTickInterval : Incorrect value for 'max_y_ticks', to small.\n"; | ||
| 1873 | $maxTicks = 5 * $minTicks; | ||
| 1874 | } | ||
| 1875 | |||
| 1876 | my $width = $max - $min; | ||
| 1877 | my @divisorList; | ||
| 1878 | |||
| 1879 | for ( my $baseMul = 1 ; ; $baseMul *= 10 ) | ||
| 1880 | { | ||
| 1881 | TRY: foreach my $tryMul ( 1, 2, 5 ) | ||
| 1882 | { | ||
| 1883 | |||
| 1884 | # Calc a fresh, smaller tick interval. | ||
| 1885 | my $divisor = $baseMul * $tryMul; | ||
| 1886 | |||
| 1887 | # Count the number of ticks. | ||
| 1888 | my ( $tickCount, $pMin, $pMax ) = $self->_countTicks( $min, $max, 1 / $divisor ); | ||
| 1889 | |||
| 1890 | #print STDERR "Chart::Base::_calcXTickInterval : tickCount = $tickCount, maxTicks = $maxTicks\n"; | ||
| 1891 | # Look a the number of ticks. | ||
| 1892 | if ( $maxTicks < $tickCount ) | ||
| 1893 | { | ||
| 1894 | |||
| 1895 | # If it is to high, Backtrack. | ||
| 1896 | $divisor = pop @divisorList; | ||
| 1897 | |||
| 1898 | # just for security: | ||
| 1899 | if ( !defined($divisor) || $divisor == 0 ) { $divisor = 1; } | ||
| 1900 | ( $tickCount, $pMin, $pMax ) = $self->_countTicks( $min, $max, 1 / $divisor ); | ||
| 1901 | |||
| 1902 | #print STDERR "\nChart::Base : Caution: Tick limit of $maxTicks exceeded. Backing of to an interval of ".1/$divisor." which plots $tickCount ticks\n"; | ||
| 1903 | return ( 1 / $divisor, $tickCount, $pMin, $pMax ); | ||
| 1904 | } | ||
| 1905 | elsif ( $minTicks > $tickCount ) | ||
| 1906 | { | ||
| 1907 | |||
| 1908 | # If it is too low, try again. | ||
| 1909 | next TRY; | ||
| 1910 | } | ||
| 1911 | else | ||
| 1912 | { | ||
| 1913 | |||
| 1914 | # Store the divisor for possible later backtracking. | ||
| 1915 | push @divisorList, $divisor; | ||
| 1916 | |||
| 1917 | # if the min or max is fixed, check they will fit in the interval. | ||
| 1918 | next TRY if ( $minF && ( int( $min * $divisor ) != ( $min * $divisor ) ) ); | ||
| 1919 | next TRY if ( $maxF && ( int( $max * $divisor ) != ( $max * $divisor ) ) ); | ||
| 1920 | |||
| 1921 | # If everything passes the tests, return. | ||
| 1922 | return ( 1 / $divisor, $tickCount, $pMin, $pMax ); | ||
| 1923 | } | ||
| 1924 | } | ||
| 1925 | } | ||
| 1926 | |||
| 1927 | croak "can't happen!"; | ||
| 1928 | } | ||
| 1929 | |||
| 1930 | ## @fn private int _countTicks($min,$max,$interval) | ||
| 1931 | # | ||
| 1932 | # @brief | ||
| 1933 | # Works out how many ticks would be displayed at that interval | ||
| 1934 | # | ||
| 1935 | # @param min Minimal value | ||
| 1936 | # @param max Maximal value | ||
| 1937 | # @param interval value | ||
| 1938 | # @return ($tickCount, $minR, $maxR) | ||
| 1939 | # | ||
| 1940 | # @details | ||
| 1941 | # | ||
| 1942 | # e.g min=2, max=5, interval=1, result is 4 ticks.\n | ||
| 1943 | # written by David Pottage of Tao Group.\n | ||
| 1944 | # $minR = $self->_round2Tick( $min, $interval, -1);\n | ||
| 1945 | # $maxR = $self->_round2Tick( $max, $interval, 1);\n | ||
| 1946 | # $tickCount = ( $maxR/$interval ) - ( $minR/$interval ) +1; | ||
| 1947 | sub _countTicks | ||
| 1948 | { | ||
| 1949 | my $self = shift; | ||
| 1950 | my ( $min, $max, $interval ) = @_; | ||
| 1951 | |||
| 1952 | my $minR = $self->_round2Tick( $min, $interval, -1 ); | ||
| 1953 | my $maxR = $self->_round2Tick( $max, $interval, 1 ); | ||
| 1954 | |||
| 1955 | my $tickCount = ( $maxR / $interval ) - ( $minR / $interval ) + 1; | ||
| 1956 | |||
| 1957 | return ( $tickCount, $minR, $maxR ); | ||
| 1958 | } | ||
| 1959 | |||
| 1960 | ## @fn private int _round2Tick($input, $interval, $roundUP) | ||
| 1961 | # Rounds up or down to the next tick of interval size. | ||
| 1962 | # | ||
| 1963 | # $roundUP can be +1 or -1 to indicate if rounding should be up or down.\n | ||
| 1964 | # written by David Pottage of Tao Group. | ||
| 1965 | # | ||
| 1966 | # @param input | ||
| 1967 | # @param interval | ||
| 1968 | # @param roundUP | ||
| 1969 | # @return retN*interval | ||
| 1970 | sub _round2Tick { | ||
| 1971 | my $self = shift; | ||
| 1972 | my ( $input, $interval, $roundUP ) = @_; | ||
| 1973 | return $input if $interval == 0; | ||
| 1974 | die unless 1 == $roundUP * $roundUP; | ||
| 1975 | |||
| 1976 | my $intN = int( $input / $interval ); | ||
| 1977 | my $fracN = ( $input / $interval ) - $intN; | ||
| 1978 | |||
| 1979 | my $retN = | ||
| 1980 | ( ( 0 == $fracN ) || ( ( $roundUP * $fracN ) < 0 ) ) | ||
| 1981 | ? $intN | ||
| 1982 | : $intN + $roundUP; | ||
| 1983 | |||
| 1984 | return $retN * $interval; | ||
| 1985 | } | ||
| 1986 | |||
| 1987 | ## @fn private array _sepFP($num) | ||
| 1988 | # @brief | ||
| 1989 | # Seperates a number into it's base 10 floating point exponent & mantisa. | ||
| 1990 | # @details | ||
| 1991 | # written by David Pottage of Tao Group. | ||
| 1992 | # | ||
| 1993 | # @param num Floating point number | ||
| 1994 | # @return ( exponent, mantissa) | ||
| 1995 | sub _sepFP { | ||
| 1996 | my $self = shift; | ||
| 1997 | my ($num) = @_; | ||
| 1998 | return ( 0, 0 ) if $num == 0; | ||
| 1999 | |||
| 2000 | my $sign = ( $num > 0 ) ? 1 : -1; | ||
| 2001 | $num *= $sign; | ||
| 2002 | |||
| 2003 | my $exponent = int( log($num) / log(10) ); | ||
| 2004 | my $mantisa = $sign * ( $num / ( 10**$exponent ) ); | ||
| 2005 | |||
| 2006 | return ( $exponent, $mantisa ); | ||
| 2007 | } | ||
| 2008 | |||
| 2009 | ## @fn private array _find_y_range() | ||
| 2010 | # Find minimum and maximum value of y data sets. | ||
| 2011 | # | ||
| 2012 | # @return ( min, max, flag_all_integers ) | ||
| 2013 | sub _find_y_range | ||
| 2014 | { | ||
| 2015 | my $self = shift; | ||
| 2016 | my $data = $self->{'dataref'}; | ||
| 2017 | |||
| 2018 | my $max = undef; | ||
| 2019 | my $min = undef; | ||
| 2020 | my $flag_all_integers = 1; # assume true | ||
| 2021 | |||
| 2022 | for my $dataset ( @$data[ 1 .. $#$data ] ) | ||
| 2023 | { | ||
| 2024 | for my $datum (@$dataset) | ||
| 2025 | { | ||
| 2026 | if ( defined $datum ) | ||
| 2027 | { | ||
| 2028 | |||
| 2029 | #croak "Missing data (dataset)"; | ||
| 2030 | if ($flag_all_integers) | ||
| 2031 | { | ||
| 2032 | |||
| 2033 | # it's worth looking for integers | ||
| 2034 | if ( $datum !~ /^[\-\+]?\d+$/ ) | ||
| 2035 | { | ||
| 2036 | $flag_all_integers = 0; # No | ||
| 2037 | } | ||
| 2038 | } | ||
| 2039 | if ( $datum =~ /^[\-\+]?\s*[\d\.eE\-\+]+/ ) | ||
| 2040 | { | ||
| 2041 | if ( defined $max && $max =~ /^[\-\+]{0,}\s*[\d\.eE\-\+]+/ ) | ||
| 2042 | { | ||
| 2043 | if ( $datum > $max ) { $max = $datum; } | ||
| 2044 | elsif ( !defined $min ) { $min = $datum; } | ||
| 2045 | elsif ( $datum < $min ) { $min = $datum; } | ||
| 2046 | } | ||
| 2047 | else { $min = $max = $datum } | ||
| 2048 | } | ||
| 2049 | } | ||
| 2050 | } | ||
| 2051 | } | ||
| 2052 | |||
| 2053 | # Return: | ||
| 2054 | ( $min, $max, $flag_all_integers ); | ||
| 2055 | } | ||
| 2056 | |||
| 2057 | ## @fn private array _find_x_range() | ||
| 2058 | # Find minimum and maximum value of x data sets | ||
| 2059 | # @return ( min, max ) | ||
| 2060 | sub _find_x_range | ||
| 2061 | { | ||
| 2062 | my $self = shift; | ||
| 2063 | my $data = $self->{'dataref'}; | ||
| 2064 | |||
| 2065 | my $max = undef; | ||
| 2066 | my $min = undef; | ||
| 2067 | |||
| 2068 | for my $datum ( @{ $data->[0] } ) | ||
| 2069 | { | ||
| 2070 | if ( defined $datum && $datum =~ /^[\-\+]{0,1}\s*[\d\.eE\-\+]+/ ) | ||
| 2071 | { | ||
| 2072 | if ( defined $max && $max =~ /^[\-\+]{0,1}\s*[\d\.eE\-\+]+/ ) | ||
| 2073 | { | ||
| 2074 | if ( $datum > $max ) { $max = $datum } | ||
| 2075 | elsif ( $datum < $min ) { $min = $datum } | ||
| 2076 | } | ||
| 2077 | else { $min = $max = $datum } | ||
| 2078 | } | ||
| 2079 | } | ||
| 2080 | |||
| 2081 | return ( $min, $max ); | ||
| 2082 | } | ||
| 2083 | |||
| 2084 | ## @fn private int _plot() | ||
| 2085 | # main sub that controls all the plotting of the actual chart | ||
| 2086 | # @return status | ||
| 2087 | sub _plot | ||
| 2088 | { | ||
| 2089 | my $self = shift; | ||
| 2090 | |||
| 2091 | # draw the legend first | ||
| 2092 | $self->_draw_legend(); | ||
| 2093 | |||
| 2094 | # mark off the graph_border space | ||
| 2095 | $self->{'curr_x_min'} += $self->{'graph_border'}; | ||
| 2096 | $self->{'curr_x_max'} -= $self->{'graph_border'}; | ||
| 2097 | $self->{'curr_y_min'} += $self->{'graph_border'}; | ||
| 2098 | $self->{'curr_y_max'} -= $self->{'graph_border'}; | ||
| 2099 | |||
| 2100 | # draw the x- and y-axis labels | ||
| 2101 | $self->_draw_x_label if $self->{'x_label'}; | ||
| 2102 | $self->_draw_y_label('left') if $self->{'y_label'}; | ||
| 2103 | $self->_draw_y_label('right') if $self->{'y_label2'}; | ||
| 2104 | |||
| 2105 | # draw the ticks and tick labels | ||
| 2106 | $self->_draw_ticks(); | ||
| 2107 | |||
| 2108 | # give the plot a grey background if they want it | ||
| 2109 | $self->_grey_background if ( $self->true( $self->{'grey_background'} ) ); | ||
| 2110 | |||
| 2111 | #draw the ticks again if grey_background has ruined it in a Direction Chart. | ||
| 2112 | if ( $self->true( $self->{'grey_background'} ) && $self->isa("Chart::Direction") ) | ||
| 2113 | { | ||
| 2114 | $self->_draw_ticks; | ||
| 2115 | } | ||
| 2116 | $self->_draw_grid_lines if ( $self->true( $self->{'grid_lines'} ) ); | ||
| 2117 | $self->_draw_x_grid_lines if ( $self->true( $self->{'x_grid_lines'} ) ); | ||
| 2118 | $self->_draw_y_grid_lines if ( $self->true( $self->{'y_grid_lines'} ) ); | ||
| 2119 | $self->_draw_y2_grid_lines if ( $self->true( $self->{'y2_grid_lines'} ) ); | ||
| 2120 | |||
| 2121 | # plot the data | ||
| 2122 | $self->_draw_data(); | ||
| 2123 | |||
| 2124 | # and return | ||
| 2125 | return 1; | ||
| 2126 | } | ||
| 2127 | |||
| 2128 | ## @fn private int _draw_legend() | ||
| 2129 | # let the user know what all the pretty colors mean.\n | ||
| 2130 | # The user define the position of the legend by setting option | ||
| 2131 | # 'legend' to 'top', 'bottom', 'left', 'right' or 'none'. | ||
| 2132 | # The legend is positioned at the defined place, respectively. | ||
| 2133 | # @return status | ||
| 2134 | sub _draw_legend { | ||
| 2135 | my $self = shift; | ||
| 2136 | my $length; | ||
| 2137 | # check to see if legend type is none.. | ||
| 2138 | if ( $self->{'legend'} =~ /^none$/ || length( $self->{'legend'} ) == 0 ) | ||
| 2139 | { | ||
| 2140 | return 1; | ||
| 2141 | } | ||
| 2142 | |||
| 2143 | # check to see if they have as many labels as datasets, | ||
| 2144 | # warn them if not | ||
| 2145 | if ( ( $#{ $self->{'legend_labels'} } >= 0 ) | ||
| 2146 | && ( ( scalar( @{ $self->{'legend_labels'} } ) ) != $self->{'num_datasets'} ) ) | ||
| 2147 | { | ||
| 2148 | carp "The number of legend labels and datasets doesn\'t match"; | ||
| 2149 | } | ||
| 2150 | |||
| 2151 | # init a field to store the length of the longest legend label | ||
| 2152 | unless ( $self->{'max_legend_label'} ) | ||
| 2153 | { | ||
| 2154 | $self->{'max_legend_label'} = 0; | ||
| 2155 | } | ||
| 2156 | |||
| 2157 | # fill in the legend labels, find the longest one | ||
| 2158 | for ( 1 .. $self->{'num_datasets'} ) | ||
| 2159 | { | ||
| 2160 | unless ( $self->{'legend_labels'}[ $_ - 1 ] ) | ||
| 2161 | { | ||
| 2162 | $self->{'legend_labels'}[ $_ - 1 ] = "Dataset $_"; | ||
| 2163 | } | ||
| 2164 | $length = length( $self->{'legend_labels'}[ $_ - 1 ] ); | ||
| 2165 | if ( $length > $self->{'max_legend_label'} ) | ||
| 2166 | { | ||
| 2167 | $self->{'max_legend_label'} = $length; | ||
| 2168 | } | ||
| 2169 | } | ||
| 2170 | |||
| 2171 | # different legend types | ||
| 2172 | if ( $self->{'legend'} eq 'bottom' ) | ||
| 2173 | { | ||
| 2174 | $self->_draw_bottom_legend; | ||
| 2175 | } | ||
| 2176 | elsif ( $self->{'legend'} eq 'right' ) | ||
| 2177 | { | ||
| 2178 | $self->_draw_right_legend; | ||
| 2179 | } | ||
| 2180 | elsif ( $self->{'legend'} eq 'left' ) | ||
| 2181 | { | ||
| 2182 | $self->_draw_left_legend; | ||
| 2183 | } | ||
| 2184 | elsif ( $self->{'legend'} eq 'top' ) | ||
| 2185 | { | ||
| 2186 | $self->_draw_top_legend; | ||
| 2187 | |||
| 2188 | } | ||
| 2189 | elsif ( $self->{'legend'} eq 'none' || length( $self->{'legend'} ) == 0 ) | ||
| 2190 | { | ||
| 2191 | $self->_draw_none_legend; | ||
| 2192 | } | ||
| 2193 | else | ||
| 2194 | { | ||
| 2195 | carp "I can't put a legend there (at " . $self->{'legend'} . ")\n"; | ||
| 2196 | } | ||
| 2197 | |||
| 2198 | # and return | ||
| 2199 | return 1; | ||
| 2200 | } | ||
| 2201 | |||
| 2202 | ## @fn private int _draw_bottom_legend() | ||
| 2203 | # put the legend on the bottom of the chart | ||
| 2204 | # @return status | ||
| 2205 | sub _draw_bottom_legend { | ||
| 2206 | |||
| 2207 | my $self = shift; | ||
| 2208 | |||
| 2209 | my @labels = @{ $self->{'legend_labels'} }; | ||
| 2210 | my ( $x1, $y1, $x2, $x3, $y2 ); | ||
| 2211 | my ( $empty_width, $max_label_width, $cols, $rows, $color, $brush ); | ||
| 2212 | my ( $col_width, $row_height, $r, $c, $index, $x, $y, $w, $h, $axes_space ); | ||
| 2213 | my $font = $self->{'legend_font'}; | ||
| 2214 | |||
| 2215 | # make sure we're using a real font | ||
| 2216 | unless ( ( ref($font) ) eq 'GD::Font' ) | ||
| 2217 | { | ||
| 2218 | croak "The font you specified isn\'t a GD Font object"; | ||
| 2219 | } | ||
| 2220 | |||
| 2221 | # get the size of the font | ||
| 2222 | ( $h, $w ) = ( $font->height, $font->width ); | ||
| 2223 | |||
| 2224 | # find the base x values | ||
| 2225 | $axes_space = | ||
| 2226 | ( $self->{'y_tick_label_length'} * $self->{'tick_label_font'}->width ) + | ||
| 2227 | $self->{'tick_len'} + | ||
| 2228 | ( 3 * $self->{'text_space'} ); | ||
| 2229 | $x1 = $self->{'curr_x_min'} + $self->{'graph_border'}; | ||
| 2230 | $x2 = $self->{'curr_x_max'} - $self->{'graph_border'}; | ||
| 2231 | |||
| 2232 | if ( $self->{'y_axes'} =~ /^right$/i ) | ||
| 2233 | { | ||
| 2234 | $x2 -= $axes_space; | ||
| 2235 | } | ||
| 2236 | elsif ( $self->{'y_axes'} =~ /^both$/i ) | ||
| 2237 | { | ||
| 2238 | $x2 -= $axes_space; | ||
| 2239 | $x1 += $axes_space; | ||
| 2240 | } | ||
| 2241 | |||
| 2242 | if ( $self->{'y_label'} ) | ||
| 2243 | { | ||
| 2244 | $x1 += $self->{'label_font'}->height + 2 * $self->{'text_space'}; | ||
| 2245 | } | ||
| 2246 | if ( $self->{'y_label2'} ) | ||
| 2247 | { | ||
| 2248 | $x2 -= $self->{'label_font'}->height + 2 * $self->{'text_space'}; | ||
| 2249 | } | ||
| 2250 | |||
| 2251 | # figure out how wide the columns need to be, and how many we | ||
| 2252 | # can fit in the space available | ||
| 2253 | $empty_width = ( $x2 - $x1 ) - ( 2 * $self->{'legend_space'} ); | ||
| 2254 | $max_label_width = $self->{'max_legend_label'} * $w + ( 4 * $self->{'text_space'} ) + $self->{'legend_example_size'}; | ||
| 2255 | $cols = int( $empty_width / $max_label_width ); | ||
| 2256 | |||
| 2257 | unless ($cols) | ||
| 2258 | { | ||
| 2259 | $cols = 1; | ||
| 2260 | } | ||
| 2261 | $col_width = $empty_width / $cols; | ||
| 2262 | |||
| 2263 | # figure out how many rows we need, remember how tall they are | ||
| 2264 | $rows = int( $self->{'num_datasets'} / $cols ); | ||
| 2265 | unless ( ( $self->{'num_datasets'} % $cols ) == 0 ) | ||
| 2266 | { | ||
| 2267 | $rows++; | ||
| 2268 | } | ||
| 2269 | unless ($rows) | ||
| 2270 | { | ||
| 2271 | $rows = 1; | ||
| 2272 | } | ||
| 2273 | $row_height = $h + $self->{'text_space'}; | ||
| 2274 | |||
| 2275 | # box the legend off | ||
| 2276 | $y1 = $self->{'curr_y_max'} - $self->{'text_space'} - ( $rows * $row_height ) - ( 2 * $self->{'legend_space'} ); | ||
| 2277 | $y2 = $self->{'curr_y_max'}; | ||
| 2278 | $self->{'gd_obj'}->rectangle( $x1, $y1, $x2, $y2, $self->_color_role_to_index('misc') ); | ||
| 2279 | $x1 += $self->{'legend_space'} + $self->{'text_space'}; | ||
| 2280 | $x2 -= $self->{'legend_space'}; | ||
| 2281 | $y1 += $self->{'legend_space'} + $self->{'text_space'}; | ||
| 2282 | $y2 -= $self->{'legend_space'} + $self->{'text_space'}; | ||
| 2283 | |||
| 2284 | my $text_color = $self->_color_role_to_index( 'text' ); | ||
| 2285 | |||
| 2286 | # draw in the actual legend | ||
| 2287 | for $r ( 0 .. $rows - 1 ) | ||
| 2288 | { | ||
| 2289 | for $c ( 0 .. $cols - 1 ) | ||
| 2290 | { | ||
| 2291 | $index = ( $r * $cols ) + $c; # find the index in the label array | ||
| 2292 | if ( $labels[$index] ) | ||
| 2293 | { | ||
| 2294 | |||
| 2295 | # get the color | ||
| 2296 | $color = $self->_color_role_to_index( 'dataset' . $index ); | ||
| 2297 | |||
| 2298 | # get the x-y coordinate for the start of the example line | ||
| 2299 | $x = $x1 + ( $col_width * $c ); | ||
| 2300 | $y = $y1 + ( $row_height * $r ) + $h / 2; | ||
| 2301 | |||
| 2302 | # now draw the example line | ||
| 2303 | $self->{'gd_obj'}->line( $x, $y, $x + $self->{'legend_example_size'}, $y, $color ); | ||
| 2304 | |||
| 2305 | # reset the brush for points | ||
| 2306 | $brush = $self->_prepare_brush( $color, 'point', 'dataset' . $index ); | ||
| 2307 | $self->{'gd_obj'}->setBrush($brush); | ||
| 2308 | |||
| 2309 | # draw the point | ||
| 2310 | $x3 = int( $x + $self->{'legend_example_size'} / 2 ); | ||
| 2311 | $self->{'gd_obj'}->line( $x3, $y, $x3, $y, gdBrushed ); | ||
| 2312 | |||
| 2313 | # adjust the x-y coordinates for the start of the label | ||
| 2314 | $x += $self->{'legend_example_size'} + ( 2 * $self->{'text_space'} ); | ||
| 2315 | $y = $y1 + ( $row_height * $r ); | ||
| 2316 | |||
| 2317 | # now draw the label | ||
| 2318 | $self->{'gd_obj'}->string( $font, $x, $y, $labels[$index], $text_color ); | ||
| 2319 | } | ||
| 2320 | } | ||
| 2321 | } | ||
| 2322 | # mark off the space used | ||
| 2323 | $self->{'curr_y_max'} -= $rows * $row_height + 2 * $self->{'text_space'} + 2 * $self->{'legend_space'}; | ||
| 2324 | |||
| 2325 | # now return | ||
| 2326 | return 1; | ||
| 2327 | } | ||
| 2328 | |||
| 2329 | ## @fn private int _draw_right_legend() | ||
| 2330 | # put the legend on the right of the chart | ||
| 2331 | # @return status | ||
| 2332 | sub _draw_right_legend { | ||
| 2333 | my $self = shift; | ||
| 2334 | my @labels = @{ $self->{'legend_labels'} }; | ||
| 2335 | my ( $x1, $x2, $x3, $y1, $y2, $width, $color, $misccolor, $w, $h, $brush ); | ||
| 2336 | my $font = $self->{'legend_font'}; | ||
| 2337 | |||
| 2338 | # make sure we're using a real font | ||
| 2339 | unless ( ( ref($font) ) eq 'GD::Font' ) | ||
| 2340 | { | ||
| 2341 | croak "The subtitle font you specified isn\'t a GD Font object"; | ||
| 2342 | } | ||
| 2343 | |||
| 2344 | # get the size of the font | ||
| 2345 | ( $h, $w ) = ( $font->height, $font->width ); | ||
| 2346 | |||
| 2347 | # get the miscellaneous color | ||
| 2348 | $misccolor = $self->_color_role_to_index('misc'); | ||
| 2349 | |||
| 2350 | # find out how wide the largest label is | ||
| 2351 | $width = | ||
| 2352 | ( 2 * $self->{'text_space'} ) + | ||
| 2353 | ( $self->{'max_legend_label'} * $w ) + | ||
| 2354 | $self->{'legend_example_size'} + | ||
| 2355 | ( 2 * $self->{'legend_space'} ); | ||
| 2356 | |||
| 2357 | # get some starting x-y values | ||
| 2358 | $x1 = $self->{'curr_x_max'} - $width; | ||
| 2359 | $x2 = $self->{'curr_x_max'}; | ||
| 2360 | $y1 = $self->{'curr_y_min'} + $self->{'graph_border'}; | ||
| 2361 | $y2 = | ||
| 2362 | $self->{'curr_y_min'} + | ||
| 2363 | $self->{'graph_border'} + | ||
| 2364 | $self->{'text_space'} + | ||
| 2365 | ( $self->{'num_datasets'} * ( $h + $self->{'text_space'} ) ) + | ||
| 2366 | ( 2 * $self->{'legend_space'} ); | ||
| 2367 | |||
| 2368 | # box the legend off | ||
| 2369 | $self->{'gd_obj'}->rectangle( $x1, $y1, $x2, $y2, $misccolor ); | ||
| 2370 | |||
| 2371 | # leave that nice space inside the legend box | ||
| 2372 | $x1 += $self->{'legend_space'}; | ||
| 2373 | $y1 += $self->{'legend_space'} + $self->{'text_space'}; | ||
| 2374 | |||
| 2375 | # now draw the actual legend | ||
| 2376 | for ( 0 .. $#labels ) | ||
| 2377 | { | ||
| 2378 | |||
| 2379 | # get the color | ||
| 2380 | my $c = $self->{'num_datasets'} - $_ - 1; | ||
| 2381 | |||
| 2382 | # color of the datasets in the legend | ||
| 2383 | $color = $self->_color_role_to_index( 'dataset' . $_ ); | ||
| 2384 | |||
| 2385 | # find the x-y coords | ||
| 2386 | $x2 = $x1; | ||
| 2387 | $x3 = $x2 + $self->{'legend_example_size'}; | ||
| 2388 | $y2 = $y1 + ( $_ * ( $self->{'text_space'} + $h ) ) + $h / 2; | ||
| 2389 | |||
| 2390 | # do the line first | ||
| 2391 | $self->{'gd_obj'}->line( $x2, $y2, $x3, $y2, $color ); | ||
| 2392 | |||
| 2393 | # reset the brush for points | ||
| 2394 | my $offset = 0; | ||
| 2395 | ( $brush, $offset ) = $self->_prepare_brush( $color, 'point', 'dataset' . $_ ); | ||
| 2396 | $self->{'gd_obj'}->setBrush($brush); | ||
| 2397 | |||
| 2398 | # draw the point | ||
| 2399 | $self->{'gd_obj'}->line( int( ( $x3 + $x2 ) / 2 ), $y2, int( ( $x3 + $x2 ) / 2 ), $y2, gdBrushed ); | ||
| 2400 | |||
| 2401 | # now the label | ||
| 2402 | $x2 = $x3 + ( 2 * $self->{'text_space'} ); | ||
| 2403 | $y2 -= $h / 2; | ||
| 2404 | |||
| 2405 | # order of the datasets in the legend | ||
| 2406 | $self->{'gd_obj'}->string( $font, $x2, $y2, $labels[$_], $color ); | ||
| 2407 | } | ||
| 2408 | |||
| 2409 | # mark off the used space | ||
| 2410 | $self->{'curr_x_max'} -= $width; | ||
| 2411 | |||
| 2412 | # and return | ||
| 2413 | return 1; | ||
| 2414 | } | ||
| 2415 | |||
| 2416 | ## @fn private int _draw_top_legend() | ||
| 2417 | # put the legend on top of the chart | ||
| 2418 | # @return status | ||
| 2419 | sub _draw_top_legend { | ||
| 2420 | my $self = shift; | ||
| 2421 | my @labels = @{ $self->{'legend_labels'} }; | ||
| 2422 | my ( $x1, $y1, $x2, $x3, $y2, $empty_width, $max_label_width ); | ||
| 2423 | my ( $cols, $rows, $color, $brush ); | ||
| 2424 | my ( $col_width, $row_height, $r, $c, $index, $x, $y, $w, $h, $axes_space ); | ||
| 2425 | my $font = $self->{'legend_font'}; | ||
| 2426 | |||
| 2427 | # make sure we're using a real font | ||
| 2428 | unless ( ( ref($font) ) eq 'GD::Font' ) | ||
| 2429 | { | ||
| 2430 | croak "The subtitle font you specified isn\'t a GD Font object"; | ||
| 2431 | } | ||
| 2432 | |||
| 2433 | # get the size of the font | ||
| 2434 | ( $h, $w ) = ( $font->height, $font->width ); | ||
| 2435 | |||
| 2436 | # find the base x values | ||
| 2437 | $axes_space = | ||
| 2438 | ( $self->{'y_tick_label_length'} * $self->{'tick_label_font'}->width ) + | ||
| 2439 | $self->{'tick_len'} + | ||
| 2440 | ( 3 * $self->{'text_space'} ); | ||
| 2441 | $x1 = $self->{'curr_x_min'} + $self->{'graph_border'}; | ||
| 2442 | $x2 = $self->{'curr_x_max'} - $self->{'graph_border'}; | ||
| 2443 | |||
| 2444 | if ( $self->{'y_axes'} =~ /^right$/i ) | ||
| 2445 | { | ||
| 2446 | $x2 -= $axes_space; | ||
| 2447 | } | ||
| 2448 | elsif ( $self->{'y_axes'} =~ /^both$/i ) | ||
| 2449 | { | ||
| 2450 | $x2 -= $axes_space; | ||
| 2451 | $x1 += $axes_space; | ||
| 2452 | } | ||
| 2453 | |||
| 2454 | # figure out how wide the columns can be, and how many will fit | ||
| 2455 | $empty_width = ( $x2 - $x1 ) - ( 2 * $self->{'legend_space'} ); | ||
| 2456 | $max_label_width = ( 4 * $self->{'text_space'} ) + ( $self->{'max_legend_label'} * $w ) + $self->{'legend_example_size'}; | ||
| 2457 | $cols = int( $empty_width / $max_label_width ); | ||
| 2458 | unless ($cols) | ||
| 2459 | { | ||
| 2460 | $cols = 1; | ||
| 2461 | } | ||
| 2462 | $col_width = $empty_width / $cols; | ||
| 2463 | |||
| 2464 | # figure out how many rows we need and remember how tall they are | ||
| 2465 | $rows = int( $self->{'num_datasets'} / $cols ); | ||
| 2466 | unless ( ( $self->{'num_datasets'} % $cols ) == 0 ) | ||
| 2467 | { | ||
| 2468 | $rows++; | ||
| 2469 | } | ||
| 2470 | unless ($rows) | ||
| 2471 | { | ||
| 2472 | $rows = 1; | ||
| 2473 | } | ||
| 2474 | $row_height = $h + $self->{'text_space'}; | ||
| 2475 | |||
| 2476 | # box the legend off | ||
| 2477 | $y1 = $self->{'curr_y_min'}; | ||
| 2478 | $y2 = $self->{'curr_y_min'} + $self->{'text_space'} + ( $rows * $row_height ) + ( 2 * $self->{'legend_space'} ); | ||
| 2479 | $self->{'gd_obj'}->rectangle( $x1, $y1, $x2, $y2, $self->_color_role_to_index('misc') ); | ||
| 2480 | |||
| 2481 | # leave some space inside the legend | ||
| 2482 | $x1 += $self->{'legend_space'} + $self->{'text_space'}; | ||
| 2483 | $x2 -= $self->{'legend_space'}; | ||
| 2484 | $y1 += $self->{'legend_space'} + $self->{'text_space'}; | ||
| 2485 | $y2 -= $self->{'legend_space'} + $self->{'text_space'}; | ||
| 2486 | |||
| 2487 | # draw in the actual legend | ||
| 2488 | for $r ( 0 .. $rows - 1 ) | ||
| 2489 | { | ||
| 2490 | for $c ( 0 .. $cols - 1 ) | ||
| 2491 | { | ||
| 2492 | $index = ( $r * $cols ) + $c; # find the index in the label array | ||
| 2493 | if ( $labels[$index] ) | ||
| 2494 | { | ||
| 2495 | |||
| 2496 | # get the color | ||
| 2497 | $color = $self->_color_role_to_index( 'dataset' . $index ); | ||
| 2498 | |||
| 2499 | # find the x-y coords | ||
| 2500 | $x = $x1 + ( $col_width * $c ); | ||
| 2501 | $y = $y1 + ( $row_height * $r ) + $h / 2; | ||
| 2502 | |||
| 2503 | # draw the line first | ||
| 2504 | $self->{'gd_obj'}->line( $x, $y, $x + $self->{'legend_example_size'}, $y, $color ); | ||
| 2505 | |||
| 2506 | # reset the brush for points | ||
| 2507 | $brush = $self->_prepare_brush( $color, 'point', 'dataset' . $index ); | ||
| 2508 | $self->{'gd_obj'}->setBrush($brush); | ||
| 2509 | |||
| 2510 | # draw the point | ||
| 2511 | $x3 = int( $x + $self->{'legend_example_size'} / 2 ); | ||
| 2512 | $self->{'gd_obj'}->line( $x3, $y, $x3, $y, gdBrushed ); | ||
| 2513 | |||
| 2514 | # now the label | ||
| 2515 | $x += $self->{'legend_example_size'} + ( 2 * $self->{'text_space'} ); | ||
| 2516 | $y -= $h / 2; | ||
| 2517 | $self->{'gd_obj'}->string( $font, $x, $y, $labels[$index], $color ); | ||
| 2518 | } | ||
| 2519 | } | ||
| 2520 | } | ||
| 2521 | |||
| 2522 | # mark off the space used | ||
| 2523 | $self->{'curr_y_min'} += ( $rows * $row_height ) + $self->{'text_space'} + 2 * $self->{'legend_space'}; | ||
| 2524 | |||
| 2525 | # now return | ||
| 2526 | return 1; | ||
| 2527 | } | ||
| 2528 | |||
| 2529 | ## @fn private int _draw_left_legend() | ||
| 2530 | # put the legend on the left of the chart | ||
| 2531 | # @return status | ||
| 2532 | sub _draw_left_legend | ||
| 2533 | { | ||
| 2534 | my $self = shift; | ||
| 2535 | my @labels = @{ $self->{'legend_labels'} }; | ||
| 2536 | my ( $x1, $x2, $x3, $y1, $y2, $width, $color, $misccolor, $w, $h, $brush ); | ||
| 2537 | my $font = $self->{'legend_font'}; | ||
| 2538 | |||
| 2539 | # make sure we're using a real font | ||
| 2540 | unless ( ( ref($font) ) eq 'GD::Font' ) | ||
| 2541 | { | ||
| 2542 | croak "The subtitle font you specified isn\'t a GD Font object"; | ||
| 2543 | } | ||
| 2544 | |||
| 2545 | # get the size of the font | ||
| 2546 | ( $h, $w ) = ( $font->height, $font->width ); | ||
| 2547 | |||
| 2548 | # get the miscellaneous color | ||
| 2549 | $misccolor = $self->_color_role_to_index('misc'); | ||
| 2550 | |||
| 2551 | # find out how wide the largest label is | ||
| 2552 | $width = | ||
| 2553 | ( 2 * $self->{'text_space'} ) + | ||
| 2554 | ( $self->{'max_legend_label'} * $w ) + | ||
| 2555 | $self->{'legend_example_size'} + | ||
| 2556 | ( 2 * $self->{'legend_space'} ); | ||
| 2557 | |||
| 2558 | # get some base x-y coordinates | ||
| 2559 | $x1 = $self->{'curr_x_min'}; | ||
| 2560 | $x2 = $self->{'curr_x_min'} + $width; | ||
| 2561 | $y1 = $self->{'curr_y_min'} + $self->{'graph_border'}; | ||
| 2562 | $y2 = | ||
| 2563 | $self->{'curr_y_min'} + | ||
| 2564 | $self->{'graph_border'} + | ||
| 2565 | $self->{'text_space'} + | ||
| 2566 | ( $self->{'num_datasets'} * ( $h + $self->{'text_space'} ) ) + | ||
| 2567 | ( 2 * $self->{'legend_space'} ); | ||
| 2568 | |||
| 2569 | # box the legend off | ||
| 2570 | $self->{'gd_obj'}->rectangle( $x1, $y1, $x2, $y2, $misccolor ); | ||
| 2571 | |||
| 2572 | # leave that nice space inside the legend box | ||
| 2573 | $x1 += $self->{'legend_space'}; | ||
| 2574 | $y1 += $self->{'legend_space'} + $self->{'text_space'}; | ||
| 2575 | |||
| 2576 | # now draw the actual legend | ||
| 2577 | for ( 0 .. $#labels ) | ||
| 2578 | { | ||
| 2579 | |||
| 2580 | # get the color | ||
| 2581 | my $c = $self->{'num_datasets'} - $_ - 1; | ||
| 2582 | |||
| 2583 | # color of the datasets in the legend | ||
| 2584 | $color = $self->_color_role_to_index( 'dataset' . $_ ); | ||
| 2585 | |||
| 2586 | # find the x-y coords | ||
| 2587 | $x2 = $x1; | ||
| 2588 | $x3 = $x2 + $self->{'legend_example_size'}; | ||
| 2589 | $y2 = $y1 + ( $_ * ( $self->{'text_space'} + $h ) ) + $h / 2; | ||
| 2590 | |||
| 2591 | # do the line first | ||
| 2592 | $self->{'gd_obj'}->line( $x2, $y2, $x3, $y2, $color ); | ||
| 2593 | |||
| 2594 | # reset the brush for points | ||
| 2595 | $brush = $self->_prepare_brush( $color, 'point', 'dataset' . $_ ); | ||
| 2596 | $self->{'gd_obj'}->setBrush($brush); | ||
| 2597 | |||
| 2598 | # draw the point | ||
| 2599 | $self->{'gd_obj'}->line( int( ( $x3 + $x2 ) / 2 ), $y2, int( ( $x3 + $x2 ) / 2 ), $y2, gdBrushed ); | ||
| 2600 | |||
| 2601 | # now the label | ||
| 2602 | $x2 = $x3 + ( 2 * $self->{'text_space'} ); | ||
| 2603 | $y2 -= $h / 2; | ||
| 2604 | |||
| 2605 | # order of the datasets in the legend | ||
| 2606 | $self->{'gd_obj'}->string( $font, $x2, $y2, $labels[$_], $color ); | ||
| 2607 | } | ||
| 2608 | |||
| 2609 | # mark off the used space | ||
| 2610 | $self->{'curr_x_min'} += $width; | ||
| 2611 | |||
| 2612 | # and return | ||
| 2613 | return 1; | ||
| 2614 | } | ||
| 2615 | |||
| 2616 | ## @fn private int _draw_none_legend() | ||
| 2617 | # no legend to draw.. | ||
| 2618 | # Just return in this case. This routine may be overwritten by | ||
| 2619 | # subclasses. | ||
| 2620 | # @return 1 | ||
| 2621 | sub _draw_none_legend { | ||
| 2622 | my $self = shift; | ||
| 2623 | my $status = 1; | ||
| 2624 | |||
| 2625 | return $status; | ||
| 2626 | } | ||
| 2627 | |||
| 2628 | ## @fn private int _draw_x_label() | ||
| 2629 | # draw the label for the x-axis | ||
| 2630 | # | ||
| 2631 | # Get font for labels\n | ||
| 2632 | # Get the color of x_label or text\n | ||
| 2633 | # Get size of font\n | ||
| 2634 | # and write x-Label | ||
| 2635 | # | ||
| 2636 | # @return status | ||
| 2637 | sub _draw_x_label { | ||
| 2638 | my $self = shift; | ||
| 2639 | my $label = $self->{'x_label'}; | ||
| 2640 | my $font = $self->{'label_font'}; | ||
| 2641 | my $color; | ||
| 2642 | my ( $h, $w, $x, $y ); | ||
| 2643 | |||
| 2644 | #get the right color | ||
| 2645 | if ( defined $self->{'colors'}->{'x_label'} ) | ||
| 2646 | { | ||
| 2647 | $color = $self->_color_role_to_index('x_label'); | ||
| 2648 | } | ||
| 2649 | else | ||
| 2650 | { | ||
| 2651 | $color = $self->_color_role_to_index('text'); | ||
| 2652 | } | ||
| 2653 | |||
| 2654 | # make sure it's a real GD Font object | ||
| 2655 | unless ( ( ref($font) ) eq 'GD::Font' ) | ||
| 2656 | { | ||
| 2657 | croak "The x-axis label font you specified isn\'t a GD Font object"; | ||
| 2658 | } | ||
| 2659 | |||
| 2660 | # get the size of the font | ||
| 2661 | ( $h, $w ) = ( $font->height, $font->width ); | ||
| 2662 | |||
| 2663 | # make sure it goes in the right place | ||
| 2664 | $x = ( $self->{'curr_x_max'} - $self->{'curr_x_min'} ) / 2 + $self->{'curr_x_min'} - ( length($label) * $w ) / 2; | ||
| 2665 | $y = $self->{'curr_y_max'} - ( $self->{'text_space'} + $h ); | ||
| 2666 | |||
| 2667 | # now write it | ||
| 2668 | $self->{'gd_obj'}->string( $font, $x, $y, $label, $color ); | ||
| 2669 | |||
| 2670 | # mark the space written to as used | ||
| 2671 | $self->{'curr_y_max'} -= $h + 2 * $self->{'text_space'}; | ||
| 2672 | |||
| 2673 | # and return | ||
| 2674 | return 1; | ||
| 2675 | } | ||
| 2676 | |||
| 2677 | ## @fn private int _draw_y_label() | ||
| 2678 | # draw the label for the y-axis | ||
| 2679 | # @return status | ||
| 2680 | sub _draw_y_label | ||
| 2681 | { | ||
| 2682 | my $self = shift; | ||
| 2683 | my $side = shift; | ||
| 2684 | my $font = $self->{'label_font'}; | ||
| 2685 | my ( $label, $h, $w, $x, $y, $color ); | ||
| 2686 | |||
| 2687 | # get the label | ||
| 2688 | if ( $side eq 'left' ) | ||
| 2689 | { | ||
| 2690 | $label = $self->{'y_label'}; | ||
| 2691 | $color = $self->_color_role_to_index('y_label'); | ||
| 2692 | } | ||
| 2693 | elsif ( $side eq 'right' ) | ||
| 2694 | { | ||
| 2695 | $label = $self->{'y_label2'}; | ||
| 2696 | $color = $self->_color_role_to_index('y_label2'); | ||
| 2697 | } | ||
| 2698 | |||
| 2699 | # make sure it's a real GD Font object | ||
| 2700 | unless ( ( ref($font) ) eq 'GD::Font' ) | ||
| 2701 | { | ||
| 2702 | croak "The x-axis label font you specified isn\'t a GD Font object"; | ||
| 2703 | } | ||
| 2704 | |||
| 2705 | # get the size of the font | ||
| 2706 | ( $h, $w ) = ( $font->height, $font->width ); | ||
| 2707 | |||
| 2708 | # make sure it goes in the right place | ||
| 2709 | if ( $side eq 'left' ) | ||
| 2710 | { | ||
| 2711 | $x = $self->{'curr_x_min'} + $self->{'text_space'}; | ||
| 2712 | } | ||
| 2713 | elsif ( $side eq 'right' ) | ||
| 2714 | { | ||
| 2715 | $x = $self->{'curr_x_max'} - $self->{'text_space'} - $h; | ||
| 2716 | } | ||
| 2717 | $y = ( $self->{'curr_y_max'} - $self->{'curr_y_min'} ) / 2 + $self->{'curr_y_min'} + ( length($label) * $w ) / 2; | ||
| 2718 | |||
| 2719 | # write it | ||
| 2720 | $self->{'gd_obj'}->stringUp( $font, $x, $y, $label, $color ); | ||
| 2721 | |||
| 2722 | # mark the space written to as used | ||
| 2723 | if ( $side eq 'left' ) | ||
| 2724 | { | ||
| 2725 | $self->{'curr_x_min'} += $h + 2 * $self->{'text_space'}; | ||
| 2726 | } | ||
| 2727 | elsif ( $side eq 'right' ) | ||
| 2728 | { | ||
| 2729 | $self->{'curr_x_max'} -= $h + 2 * $self->{'text_space'}; | ||
| 2730 | } | ||
| 2731 | |||
| 2732 | # now return | ||
| 2733 | return 1; | ||
| 2734 | } | ||
| 2735 | |||
| 2736 | ## @fn private int _draw_ticks() | ||
| 2737 | # draw the ticks and tick labels | ||
| 2738 | # @return status | ||
| 2739 | sub _draw_ticks { | ||
| 2740 | my $self = shift; | ||
| 2741 | |||
| 2742 | #if the user wants an xy_plot, calculate the x-ticks too | ||
| 2743 | if ( | ||
| 2744 | $self->true( $self->{'xy_plot'} ) | ||
| 2745 | && ( $self->isa('Chart::Lines') | ||
| 2746 | || $self->isa('Chart::Points') | ||
| 2747 | || $self->isa('Chart::LinesPoints') | ||
| 2748 | || $self->isa('Chart::Split') | ||
| 2749 | || $self->isa('Chart::ErrorBars') ) | ||
| 2750 | ) | ||
| 2751 | { | ||
| 2752 | $self->_draw_x_number_ticks; | ||
| 2753 | } | ||
| 2754 | else | ||
| 2755 | { # draw the x ticks with strings | ||
| 2756 | $self->_draw_x_ticks; | ||
| 2757 | } | ||
| 2758 | |||
| 2759 | # now the y ticks | ||
| 2760 | $self->_draw_y_ticks( $self->{'y_axes'} ); | ||
| 2761 | |||
| 2762 | # then return | ||
| 2763 | return 1; | ||
| 2764 | } | ||
| 2765 | |||
| 2766 | ## @fn private int _draw_x_number_ticks() | ||
| 2767 | # draw the ticks and tick labels | ||
| 2768 | # @return status | ||
| 2769 | sub _draw_x_number_ticks { | ||
| 2770 | my $self = shift; | ||
| 2771 | my $data = $self->{'dataref'}; | ||
| 2772 | my $font = $self->{'tick_label_font'}; | ||
| 2773 | my $textcolor = $self->_color_role_to_index('text'); | ||
| 2774 | my $misccolor = $self->_color_role_to_index('misc'); | ||
| 2775 | my ( $h, $w, $x1, $y1, $y2, $x2, $delta, $width, $label ); | ||
| 2776 | my @labels = @{ $self->{'x_tick_labels'} }; | ||
| 2777 | |||
| 2778 | $self->{'grid_data'}->{'x'} = []; | ||
| 2779 | |||
| 2780 | #make sure we have a real font | ||
| 2781 | unless ( ( ref $font ) eq 'GD::Font' ) | ||
| 2782 | { | ||
| 2783 | croak "The tick label font you specified isn't a GD font object"; | ||
| 2784 | } | ||
| 2785 | |||
| 2786 | #get height and width of the font | ||
| 2787 | ( $h, $w ) = ( $font->height, $font->width ); | ||
| 2788 | |||
| 2789 | #store actual borders, for a possible later repair | ||
| 2790 | $self->{'temp_x_min'} = $self->{'curr_x_min'}; | ||
| 2791 | $self->{'temp_x_max'} = $self->{'curr_x_max'}; | ||
| 2792 | $self->{'temp_y_max'} = $self->{'curr_y_max'}; | ||
| 2793 | $self->{'temp_y_min'} = $self->{'curr_y_min'}; | ||
| 2794 | |||
| 2795 | #get the right x-value and width | ||
| 2796 | #The one and only way to get the RIGHT x value and the width | ||
| 2797 | if ( $self->{'y_axes'} =~ /^right$/i ) | ||
| 2798 | { | ||
| 2799 | $x1 = $self->{'curr_x_min'}; | ||
| 2800 | $width = | ||
| 2801 | $self->{'curr_x_max'} - | ||
| 2802 | $x1 - | ||
| 2803 | ( $w * $self->{'y_tick_label_length'} ) - | ||
| 2804 | 3 * $self->{'text_space'} - | ||
| 2805 | $self->{'tick_len'}; | ||
| 2806 | } | ||
| 2807 | elsif ( $self->{'y_axes'} =~ /^both$/i ) | ||
| 2808 | { | ||
| 2809 | $x1 = $self->{'curr_x_min'} + ( $w * $self->{'y_tick_label_length'} ) + 3 * $self->{'text_space'} + $self->{'tick_len'}; | ||
| 2810 | $width = | ||
| 2811 | $self->{'curr_x_max'} - | ||
| 2812 | $x1 - | ||
| 2813 | ( $w * $self->{'y_tick_label_length'} ) - | ||
| 2814 | ( 3 * $self->{'text_space'} ) - | ||
| 2815 | $self->{'tick_len'}; | ||
| 2816 | } | ||
| 2817 | else | ||
| 2818 | { | ||
| 2819 | $x1 = $self->{'curr_x_min'} + ( $w * $self->{'y_tick_label_length'} ) + 3 * $self->{'text_space'} + $self->{'tick_len'}; | ||
| 2820 | $width = $self->{'curr_x_max'} - $x1; | ||
| 2821 | } | ||
| 2822 | |||
| 2823 | #get the delta value | ||
| 2824 | $delta = $width / ( $self->{'x_number_ticks'} - 1 ); | ||
| 2825 | |||
| 2826 | #draw the labels | ||
| 2827 | $y2 = $y1; | ||
| 2828 | |||
| 2829 | if ( $self->{'x_ticks'} =~ /^normal/i ) | ||
| 2830 | { #just normal ticks | ||
| 2831 | #get the point for updating later | ||
| 2832 | $y1 = $self->{'curr_y_max'} - 2 * $self->{'text_space'} - $h - $self->{'tick_len'}; | ||
| 2833 | |||
| 2834 | #get the start point | ||
| 2835 | $y2 = $y1 + $self->{'tick_len'} + $self->{'text_space'}; | ||
| 2836 | |||
| 2837 | if ( $self->{'xlabels'} ) | ||
| 2838 | { | ||
| 2839 | unless ( $self->{'xrange'} ) | ||
| 2840 | { | ||
| 2841 | croak "Base.pm: xrange must be specified with xlabels!\n"; | ||
| 2842 | } | ||
| 2843 | my $xmin = $self->{'xrange'}[0]; | ||
| 2844 | my $xmax = $self->{'xrange'}[1]; | ||
| 2845 | my @labels = @{ $self->{'xlabels'}[0] }; | ||
| 2846 | my @vals = @{ $self->{'xlabels'}[1] }; | ||
| 2847 | my $delta = $width / ( $xmax - $xmin ); | ||
| 2848 | |||
| 2849 | for ( 0 .. $#labels ) | ||
| 2850 | { | ||
| 2851 | my $label = $labels[$_]; | ||
| 2852 | my $val = $vals[$_]; | ||
| 2853 | $x2 = $x1 + ( $delta * ( $val - $xmin ) ) - ( 0.5 * $w * length($label) ); | ||
| 2854 | $self->{'gd_obj'}->string( $font, $x2, $y2, $label, $textcolor ); | ||
| 2855 | |||
| 2856 | #print "write x-label '".$label."' at ($x2,$y2)\n"; | ||
| 2857 | } | ||
| 2858 | } | ||
| 2859 | else | ||
| 2860 | { | ||
| 2861 | my $last_x = 'undefined'; | ||
| 2862 | for ( 0 .. $#labels ) | ||
| 2863 | { | ||
| 2864 | $label = $self->{f_x_tick}->( $self->{'x_tick_labels'}[$_] ); | ||
| 2865 | $x2 = $x1 + ( $delta * $_ ) - ( 0.5 * $w * length($label) ); | ||
| 2866 | if ( $last_x eq 'undefined' | ||
| 2867 | or $last_x < $x2 ) | ||
| 2868 | { | ||
| 2869 | $self->{'gd_obj'}->string( $font, $x2, $y2, $label, $textcolor ); | ||
| 2870 | $last_x = $x2 + ( $w * length($label) ); | ||
| 2871 | } | ||
| 2872 | |||
| 2873 | #print "last_x = $last_x, write string '".$label."' at ($x2,$y2) to '$_'\n"; | ||
| 2874 | } | ||
| 2875 | } | ||
| 2876 | } | ||
| 2877 | |||
| 2878 | elsif ( $self->{'x_ticks'} =~ /^staggered/i ) | ||
| 2879 | { #staggered ticks | ||
| 2880 | #get the point for updating later | ||
| 2881 | $y1 = $self->{'curr_y_max'} - 3 * $self->{'text_space'} - 2 * $h - $self->{'tick_len'}; | ||
| 2882 | |||
| 2883 | if ( $self->{'xlabels'} ) | ||
| 2884 | { | ||
| 2885 | unless ( $self->{'xrange'} ) | ||
| 2886 | { | ||
| 2887 | croak "Base.pm: xrange must be specified with xlabels!\n"; | ||
| 2888 | } | ||
| 2889 | my $xmin = $self->{'xrange'}[0]; | ||
| 2890 | my $xmax = $self->{'xrange'}[1]; | ||
| 2891 | my @labels = @{ $self->{'xlabels'}[0] }; | ||
| 2892 | my @vals = @{ $self->{'xlabels'}[1] }; | ||
| 2893 | my $delta = $width / ( $xmax - $xmin ); | ||
| 2894 | |||
| 2895 | for ( 0 .. $#labels ) | ||
| 2896 | { | ||
| 2897 | my $label = $labels[$_]; | ||
| 2898 | my $val = $vals[$_]; | ||
| 2899 | $x2 = $x1 + ( $delta * ( $val - $xmin ) ) - ( 0.5 * $w * length($label) ); | ||
| 2900 | unless ( $_ % 2 ) | ||
| 2901 | { | ||
| 2902 | $y2 = $y1 + $self->{'text_space'} + $self->{'tick_len'}; | ||
| 2903 | } | ||
| 2904 | else | ||
| 2905 | { | ||
| 2906 | $y2 = $y1 + $h + 2 * $self->{'text_space'} + $self->{'tick_len'}; | ||
| 2907 | } | ||
| 2908 | $self->{'gd_obj'}->string( $font, $x2, $y2, $label, $textcolor ); | ||
| 2909 | |||
| 2910 | #print "write x-label '".$label."' at ($x2,$y2)\n"; | ||
| 2911 | } | ||
| 2912 | } | ||
| 2913 | else | ||
| 2914 | { | ||
| 2915 | for ( 0 .. $#labels ) | ||
| 2916 | { | ||
| 2917 | $label = $self->{f_x_tick}->( $self->{'x_tick_labels'}[$_] ); | ||
| 2918 | $x2 = $x1 + ( $delta * $_ ) - ( 0.5 * $w * length($label) ); | ||
| 2919 | unless ( $_ % 2 ) | ||
| 2920 | { | ||
| 2921 | $y2 = $y1 + $self->{'text_space'} + $self->{'tick_len'}; | ||
| 2922 | } | ||
| 2923 | else | ||
| 2924 | { | ||
| 2925 | $y2 = $y1 + $h + 2 * $self->{'text_space'} + $self->{'tick_len'}; | ||
| 2926 | } | ||
| 2927 | $self->{'gd_obj'}->string( $font, $x2, $y2, $label, $textcolor ); | ||
| 2928 | } | ||
| 2929 | } | ||
| 2930 | } | ||
| 2931 | |||
| 2932 | elsif ( $self->{'x_ticks'} =~ /^vertical/i ) | ||
| 2933 | { #vertical ticks | ||
| 2934 | #get the point for updating later | ||
| 2935 | $y1 = $self->{'curr_y_max'} - 2 * $self->{'text_space'} - $w * $self->{'x_tick_label_length'} - $self->{'tick_len'}; | ||
| 2936 | |||
| 2937 | if ( $self->{'xlabels'} ) | ||
| 2938 | { | ||
| 2939 | unless ( $self->{'xrange'} ) | ||
| 2940 | { | ||
| 2941 | croak "Base.pm: xrange must be specified with xlabels!\n"; | ||
| 2942 | } | ||
| 2943 | my $xmin = $self->{'xrange'}[0]; | ||
| 2944 | my $xmax = $self->{'xrange'}[1]; | ||
| 2945 | my @labels = @{ $self->{'xlabels'}[0] }; | ||
| 2946 | my @vals = @{ $self->{'xlabels'}[1] }; | ||
| 2947 | my $delta = $width / ( $xmax - $xmin ); | ||
| 2948 | |||
| 2949 | for ( 0 .. $#labels ) | ||
| 2950 | { | ||
| 2951 | my $label = $labels[$_]; | ||
| 2952 | my $val = $vals[$_]; | ||
| 2953 | $y2 = $y1 + $self->{'tick_len'} + $w * length($label) + $self->{'text_space'}; | ||
| 2954 | $x2 = $x1 + ( $delta * ( $val - $xmin ) ) - ( $h / 2 ); | ||
| 2955 | $self->{'gd_obj'}->stringUp( $font, $x2, $y2, $label, $textcolor ); | ||
| 2956 | |||
| 2957 | #print "write x-label '".$label."' at ($x2,$y2)\n"; | ||
| 2958 | } | ||
| 2959 | } | ||
| 2960 | else | ||
| 2961 | { | ||
| 2962 | |||
| 2963 | for ( 0 .. $#labels ) | ||
| 2964 | { | ||
| 2965 | $label = $self->{f_x_tick}->( $self->{'x_tick_labels'}[$_] ); | ||
| 2966 | |||
| 2967 | #get the start point | ||
| 2968 | $y2 = $y1 + $self->{'tick_len'} + $w * length($label) + $self->{'text_space'}; | ||
| 2969 | $x2 = $x1 + ( $delta * $_ ) - ( $h / 2 ); | ||
| 2970 | $self->{'gd_obj'}->stringUp( $font, $x2, $y2, $label, $textcolor ); | ||
| 2971 | } | ||
| 2972 | } | ||
| 2973 | } | ||
| 2974 | |||
| 2975 | else | ||
| 2976 | { | ||
| 2977 | croak "I don't understand the type of x-ticks you specified\n" | ||
| 2978 | . "x-ticks must be one of 'normal', 'staggered' or 'vertical' but not of '" | ||
| 2979 | . $self->{'x_ticks'} . "'."; | ||
| 2980 | } | ||
| 2981 | |||
| 2982 | #update the curr y max value | ||
| 2983 | $self->{'curr_y_max'} = $y1; | ||
| 2984 | |||
| 2985 | #draw the ticks | ||
| 2986 | $y1 = $self->{'curr_y_max'}; | ||
| 2987 | $y2 = $self->{'curr_y_max'} + $self->{'tick_len'}; | ||
| 2988 | |||
| 2989 | #draw grid lines | ||
| 2990 | if ( $self->{'xlabels'} ) | ||
| 2991 | { | ||
| 2992 | unless ( $self->{'xrange'} ) | ||
| 2993 | { | ||
| 2994 | croak "Base.pm: xrange must be specified with xlabels!\n"; | ||
| 2995 | } | ||
| 2996 | my $xmin = $self->{'xrange'}[0]; | ||
| 2997 | my $xmax = $self->{'xrange'}[1]; | ||
| 2998 | my @vals = @{ $self->{'xlabels'}[1] }; | ||
| 2999 | my $delta = $width / ( $xmax - $xmin ); | ||
| 3000 | |||
| 3001 | for ( 0 .. $#vals ) | ||
| 3002 | { | ||
| 3003 | my $val = $vals[$_]; | ||
| 3004 | $x2 = ($x1) + ( $delta * ( $val - $xmin ) ); | ||
| 3005 | $self->{'gd_obj'}->line( $x2, $y1, $x2, $y2, $misccolor ); | ||
| 3006 | if ( $self->true( $self->{'grid_lines'} ) | ||
| 3007 | or $self->true( $self->{'x_grid_lines'} ) ) | ||
| 3008 | { | ||
| 3009 | $self->{'grid_data'}->{'x'}->[$_] = $x2; | ||
| 3010 | } | ||
| 3011 | } | ||
| 3012 | } | ||
| 3013 | else | ||
| 3014 | { | ||
| 3015 | for ( 0 .. $#labels ) | ||
| 3016 | { | ||
| 3017 | $x2 = $x1 + ( $delta * $_ ); | ||
| 3018 | $self->{'gd_obj'}->line( $x2, $y1, $x2, $y2, $misccolor ); | ||
| 3019 | if ( ( $self->true( $self->{'grid_lines'} ) ) | ||
| 3020 | or ( $self->true( $self->{'x_grid_lines'} ) ) ) | ||
| 3021 | { | ||
| 3022 | $self->{'grid_data'}->{'x'}->[$_] = $x2; | ||
| 3023 | } | ||
| 3024 | } | ||
| 3025 | } | ||
| 3026 | return 1; | ||
| 3027 | } | ||
| 3028 | |||
| 3029 | ## @fn private int _draw_x_ticks() | ||
| 3030 | # draw the x-ticks and their labels | ||
| 3031 | # @return status | ||
| 3032 | sub _draw_x_ticks { | ||
| 3033 | my $self = shift; | ||
| 3034 | my $data = $self->{'dataref'}; | ||
| 3035 | my $font = $self->{'tick_label_font'}; | ||
| 3036 | my $textcolor = $self->_color_role_to_index('text'); | ||
| 3037 | my $misccolor = $self->_color_role_to_index('misc'); | ||
| 3038 | my $label; | ||
| 3039 | my ( $h, $w ); | ||
| 3040 | my ( $x1, $x2, $y1, $y2 ); | ||
| 3041 | my ( $width, $delta ); | ||
| 3042 | my ($stag); | ||
| 3043 | |||
| 3044 | $self->{'grid_data'}->{'x'} = []; | ||
| 3045 | |||
| 3046 | # make sure we got a real font | ||
| 3047 | unless ( ( ref $font ) eq 'GD::Font' ) | ||
| 3048 | { | ||
| 3049 | croak "The tick label font you specified isn\'t a GD Font object"; | ||
| 3050 | } | ||
| 3051 | |||
| 3052 | # get the height and width of the font | ||
| 3053 | ( $h, $w ) = ( $font->height, $font->width ); | ||
| 3054 | |||
| 3055 | # maybe, we need the actual x and y values later for drawing the x-ticks again | ||
| 3056 | # in the draw function in the lines modul. So copy them. | ||
| 3057 | $self->{'temp_x_min'} = $self->{'curr_x_min'}; | ||
| 3058 | $self->{'temp_x_max'} = $self->{'curr_x_max'}; | ||
| 3059 | $self->{'temp_y_min'} = $self->{'curr_y_min'}; | ||
| 3060 | $self->{'temp_y_max'} = $self->{'curr_y_max'}; | ||
| 3061 | |||
| 3062 | # allow for the amount of space the y-ticks will push the | ||
| 3063 | # axes over to the right | ||
| 3064 | ## _draw_y_ticks allows 3 * text_space, not 1 * ; this caused mismatch between | ||
| 3065 | ## the ticks (and grid lines) and the data. | ||
| 3066 | # $x1 = $self->{'curr_x_min'} + ($w * $self->{'y_tick_label_length'}) | ||
| 3067 | # + $self->{'text_space'} + $self->{'tick_len'}; | ||
| 3068 | ## And, what about the right-tick space?? Only affects Composite, I guess.... | ||
| 3069 | |||
| 3070 | #The one and only way to get the RIGHT x value and the width | ||
| 3071 | if ( $self->{'y_axes'} =~ /^right$/i ) { | ||
| 3072 | $x1 = $self->{'curr_x_min'}; | ||
| 3073 | $width = $self->{'curr_x_max'} - $x1 - $self->{'tick_len'} | ||
| 3074 | - ( $w * $self->{'y_tick_label_length'} ) - | ||
| 3075 | - ( 3 * $self->{'text_space'} ); | ||
| 3076 | } | ||
| 3077 | elsif ( $self->{'y_axes'} =~ /^both$/i ) { | ||
| 3078 | $x1 = $self->{'curr_x_min'} + ( $w * $self->{'y_tick_label_length'} ) + 3 * $self->{'text_space'} + $self->{'tick_len'}; | ||
| 3079 | $width = $self->{'curr_x_max'} - $x1 - $self->{'tick_len'} | ||
| 3080 | - ( $w * $self->{'y_tick_label_length'} ) | ||
| 3081 | - 3 * $self->{'text_space'}; | ||
| 3082 | } else { | ||
| 3083 | $x1 = $self->{'curr_x_min'} + ( $w * $self->{'y_tick_label_length'} ) + 3 * $self->{'text_space'} + $self->{'tick_len'}; | ||
| 3084 | $width = $self->{'curr_x_max'} - $x1; | ||
| 3085 | } | ||
| 3086 | |||
| 3087 | #the same for the y value, but not so tricky | ||
| 3088 | $y1 = $self->{'curr_y_max'} - $h - $self->{'text_space'}; | ||
| 3089 | |||
| 3090 | # get the delta value, figure out how to draw the labels | ||
| 3091 | $delta = $width / ( $self->{'num_datapoints'} > 0 ? $self->{'num_datapoints'} : 1 ); | ||
| 3092 | if ( !defined( $self->{'skip_x_ticks'} ) ) { | ||
| 3093 | $self->{'skip_x_ticks'} = 1; | ||
| 3094 | } elsif ( $self->{'skip_x_ticks'} == 0 ) { | ||
| 3095 | $self->{'skip_x_ticks'} = 1; | ||
| 3096 | } | ||
| 3097 | if ( $delta <= ( $self->{'x_tick_label_length'} * $w ) / $self->{'skip_x_ticks'} ) { | ||
| 3098 | if ( $self->{'x_ticks'} =~ /^normal$/i ) { | ||
| 3099 | $self->{'x_ticks'} = 'staggered'; | ||
| 3100 | } | ||
| 3101 | } | ||
| 3102 | |||
| 3103 | # now draw the labels | ||
| 3104 | if ( $self->{'x_ticks'} =~ /^normal$/i ) { # normal ticks | ||
| 3105 | if ( $self->{'skip_x_ticks'} > 1 ) { # draw only every nth tick and label | ||
| 3106 | for ( 0 .. int( ( $self->{'num_datapoints'} - 1 ) / $self->{'skip_x_ticks'} ) ) | ||
| 3107 | { | ||
| 3108 | if ( defined( $data->[0][ $_ * $self->{'skip_x_ticks'} ] ) ) | ||
| 3109 | { | ||
| 3110 | $label = $self->{f_x_tick}->( $data->[0][ $_ * $self->{'skip_x_ticks'} ] ); | ||
| 3111 | $x2 = $x1 + ( $delta / 2 ) + ( $delta * ( $_ * $self->{'skip_x_ticks'} ) ) - ( $w * length($label) ) / 2; | ||
| 3112 | $self->{'gd_obj'}->string( $font, $x2, $y1, $label, $textcolor ); | ||
| 3113 | } | ||
| 3114 | } | ||
| 3115 | } | ||
| 3116 | elsif ( $self->{'custom_x_ticks'} ) | ||
| 3117 | { # draw only the ticks they wanted | ||
| 3118 | for ( @{ $self->{'custom_x_ticks'} } ) | ||
| 3119 | { | ||
| 3120 | if ( defined($_) ) | ||
| 3121 | { | ||
| 3122 | $label = $self->{f_x_tick}->( $data->[0][$_] ); | ||
| 3123 | $x2 = $x1 + ( $delta / 2 ) + ( $delta * $_ ) - ( $w * length($label) ) / 2; | ||
| 3124 | $self->{'gd_obj'}->string( $font, $x2, $y1, $label, $textcolor ); | ||
| 3125 | } | ||
| 3126 | } | ||
| 3127 | } else { | ||
| 3128 | for ( 0 .. $self->{'num_datapoints'} - 1 ) { | ||
| 3129 | if ( defined($_) ) { | ||
| 3130 | $label = $self->{f_x_tick}->( $data->[0][$_] ); | ||
| 3131 | $x2 = $x1 + ( $delta / 2 ) + ( $delta * $_ ) - ( $w * length($label) ) / 2; | ||
| 3132 | $self->{'gd_obj'}->string( $font, $x2, $y1, $label, $textcolor ); | ||
| 3133 | } | ||
| 3134 | } | ||
| 3135 | } | ||
| 3136 | } elsif ( $self->{'x_ticks'} =~ /^staggered$/i ) { # staggered ticks | ||
| 3137 | if ( $self->{'skip_x_ticks'} > 1 ) | ||
| 3138 | { | ||
| 3139 | $stag = 0; | ||
| 3140 | for ( 0 .. int( ( $self->{'num_datapoints'} - 1 ) / $self->{'skip_x_ticks'} ) ) { | ||
| 3141 | if ( defined( $data->[0][ $_ * $self->{'skip_x_ticks'} ] ) ) { | ||
| 3142 | $x2 = | ||
| 3143 | $x1 + | ||
| 3144 | ( $delta / 2 ) + | ||
| 3145 | ( $delta * ( $_ * $self->{'skip_x_ticks'} ) ) - | ||
| 3146 | ( $w * length( $self->{f_x_tick}->( $data->[0][ $_ * $self->{'skip_x_ticks'} ] ) ) ) / 2; | ||
| 3147 | if ( ( $stag % 2 ) == 1 ) { | ||
| 3148 | $y1 -= $self->{'text_space'} + $h; | ||
| 3149 | } | ||
| 3150 | $self->{'gd_obj'} | ||
| 3151 | ->string( $font, $x2, $y1, $self->{f_x_tick}->( $data->[0][ $_ * $self->{'skip_x_ticks'} ] ), | ||
| 3152 | $textcolor ); | ||
| 3153 | $y1 += $self->{'text_space'} + $h if $stag % 2; | ||
| 3154 | $stag++; | ||
| 3155 | } | ||
| 3156 | } | ||
| 3157 | } | ||
| 3158 | elsif ( $self->{'custom_x_ticks'} ) { | ||
| 3159 | $stag = 0; | ||
| 3160 | for ( sort ( @{ $self->{'custom_x_ticks'} } ) ) { # sort to make it look good | ||
| 3161 | if ( defined($_) ) { | ||
| 3162 | $x2 = $x1 + ( $delta / 2 ) + ( $delta * $_ ) - ( $w * length( $self->{f_x_tick}->( $data->[0][$_] ) ) ) / 2; | ||
| 3163 | $y1 -= $self->{'text_space'} + $h if $stag % 2; | ||
| 3164 | $self->{'gd_obj'}->string( $font, $x2, $y1, $self->{f_x_tick}->( $data->[0][$_] ), $textcolor ); | ||
| 3165 | $y1 += $self->{'text_space'} + $h if $stag % 2; | ||
| 3166 | $stag++; | ||
| 3167 | } | ||
| 3168 | } | ||
| 3169 | } else { | ||
| 3170 | for ( 0 .. $self->{'num_datapoints'} - 1 ) { | ||
| 3171 | if ( defined( $self->{f_x_tick}->( $data->[0][$_] ) ) ) { | ||
| 3172 | $x2 = $x1 + ( $delta / 2 ) + ( $delta * $_ ) - ( $w * length( $self->{f_x_tick}->( $data->[0][$_] ) ) ) / 2; | ||
| 3173 | if ( ( $_ % 2 ) == 1 ) | ||
| 3174 | { | ||
| 3175 | $y1 -= $self->{'text_space'} + $h; | ||
| 3176 | } | ||
| 3177 | $self->{'gd_obj'}->string( $font, $x2, $y1, $self->{f_x_tick}->( $data->[0][$_] ), $textcolor ); | ||
| 3178 | if ( ( $_ % 2 ) == 1 ) | ||
| 3179 | { | ||
| 3180 | $y1 += $self->{'text_space'} + $h; | ||
| 3181 | } | ||
| 3182 | } | ||
| 3183 | } | ||
| 3184 | } | ||
| 3185 | } elsif ( $self->{'x_ticks'} =~ /^vertical$/i ) { # vertical ticks | ||
| 3186 | $y1 = $self->{'curr_y_max'} - $self->{'text_space'}; | ||
| 3187 | if ( $self->{'skip_x_ticks'} > 1 ) { | ||
| 3188 | for ( 0 .. int( ( $self->{'num_datapoints'} - 1 ) / $self->{'skip_x_ticks'} ) ) { | ||
| 3189 | if ( defined($_) ) { | ||
| 3190 | $x2 = $x1 + ( $delta / 2 ) + ( $delta * ( $_ * $self->{'skip_x_ticks'} ) ) - $h / 2; | ||
| 3191 | $y2 = $y1 - ( | ||
| 3192 | ( | ||
| 3193 | $self->{'x_tick_label_length'} - | ||
| 3194 | length( $self->{f_x_tick}->( $data->[0][ $_ * $self->{'skip_x_ticks'} ] ) ) | ||
| 3195 | ) * $w | ||
| 3196 | ); | ||
| 3197 | $self->{'gd_obj'} | ||
| 3198 | ->stringUp( $font, $x2, $y2, $self->{f_x_tick}->( $data->[0][ $_ * $self->{'skip_x_ticks'} ] ), | ||
| 3199 | $textcolor ); | ||
| 3200 | } | ||
| 3201 | } | ||
| 3202 | } elsif ( $self->{'custom_x_ticks'} ) { | ||
| 3203 | for ( @{ $self->{'custom_x_ticks'} } ) { | ||
| 3204 | if ( defined($_) ) { | ||
| 3205 | $x2 = $x1 + ( $delta / 2 ) + ( $delta * $_ ) - $h / 2; | ||
| 3206 | $y2 = $y1 - ( ( $self->{'x_tick_label_length'} - length( $self->{f_x_tick}->( $data->[0][$_] ) ) ) * $w ); | ||
| 3207 | $self->{'gd_obj'}->stringUp( $font, $x2, $y2, $self->{f_x_tick}->( $data->[0][$_] ), $textcolor ); | ||
| 3208 | } | ||
| 3209 | } | ||
| 3210 | } else { | ||
| 3211 | for ( 0 .. $self->{'num_datapoints'} - 1 ) { | ||
| 3212 | if ( defined($_) ) { | ||
| 3213 | $x2 = $x1 + ( $delta / 2 ) + ( $delta * $_ ) - $h / 2; | ||
| 3214 | $y2 = $y1 - ( ( $self->{'x_tick_label_length'} - length( $self->{f_x_tick}->( $data->[0][$_] ) ) ) * $w ); | ||
| 3215 | $self->{'gd_obj'}->stringUp( $font, $x2, $y2, $self->{f_x_tick}->( $data->[0][$_] ), $textcolor ); | ||
| 3216 | } | ||
| 3217 | } | ||
| 3218 | } | ||
| 3219 | } | ||
| 3220 | else | ||
| 3221 | { # error time | ||
| 3222 | carp "I don't understand the type of x-ticks you specified"; | ||
| 3223 | } | ||
| 3224 | |||
| 3225 | # update the current y-max value | ||
| 3226 | if ( $self->{'x_ticks'} =~ /^normal$/i ) | ||
| 3227 | { | ||
| 3228 | $self->{'curr_y_max'} -= $h + ( 2 * $self->{'text_space'} ); | ||
| 3229 | } | ||
| 3230 | elsif ( $self->{'x_ticks'} =~ /^staggered$/i ) | ||
| 3231 | { | ||
| 3232 | $self->{'curr_y_max'} -= ( 2 * $h ) + ( 3 * $self->{'text_space'} ); | ||
| 3233 | } | ||
| 3234 | elsif ( $self->{'x_ticks'} =~ /^vertical$/i ) | ||
| 3235 | { | ||
| 3236 | $self->{'curr_y_max'} -= ( $w * $self->{'x_tick_label_length'} ) + ( 2 * $self->{'text_space'} ); | ||
| 3237 | } | ||
| 3238 | |||
| 3239 | # now plot the ticks | ||
| 3240 | $y1 = $self->{'curr_y_max'}; | ||
| 3241 | $y2 = $self->{'curr_y_max'} - $self->{'tick_len'}; | ||
| 3242 | if ( $self->{'skip_x_ticks'} > 1 ) { | ||
| 3243 | for ( 0 .. int( ( $self->{'num_datapoints'} - 1 ) / $self->{'skip_x_ticks'} ) ) { | ||
| 3244 | $x2 = $x1 + ( $delta / 2 ) + ( $delta * ( $_ * $self->{'skip_x_ticks'} ) ); | ||
| 3245 | $self->{'gd_obj'}->line( $x2, $y1, $x2, $y2, $misccolor ); | ||
| 3246 | if ( $self->true( $self->{'grid_lines'} ) | ||
| 3247 | or $self->true( $self->{'x_grid_lines'} ) ) | ||
| 3248 | { | ||
| 3249 | $self->{'grid_data'}->{'x'}->[$_] = $x2; | ||
| 3250 | } | ||
| 3251 | } | ||
| 3252 | } elsif ( $self->{'custom_x_ticks'} ) { | ||
| 3253 | for ( @{ $self->{'custom_x_ticks'} } ) { | ||
| 3254 | $x2 = $x1 + ( $delta / 2 ) + ( $delta * $_ ); | ||
| 3255 | $self->{'gd_obj'}->line( $x2, $y1, $x2, $y2, $misccolor ); | ||
| 3256 | if ( $self->true( $self->{'grid_lines'} ) | ||
| 3257 | or $self->true( $self->{'x_grid_lines'} ) ) | ||
| 3258 | { | ||
| 3259 | $self->{'grid_data'}->{'x'}->[$_] = $x2; | ||
| 3260 | } | ||
| 3261 | } | ||
| 3262 | } else { | ||
| 3263 | for ( 0 .. $self->{'num_datapoints'} - 1 ) { | ||
| 3264 | $x2 = $x1 + ( $delta / 2 ) + ( $delta * $_ ); | ||
| 3265 | $self->{'gd_obj'}->line( $x2, $y1, $x2, $y2, $misccolor ); | ||
| 3266 | if ( $self->true( $self->{'grid_lines'} ) | ||
| 3267 | or $self->true( $self->{'x_grid_lines'} ) ) { | ||
| 3268 | |||
| 3269 | $self->{'grid_data'}->{'x'}->[$_] = $x2; | ||
| 3270 | } | ||
| 3271 | } | ||
| 3272 | } | ||
| 3273 | |||
| 3274 | # update the current y-max value | ||
| 3275 | $self->{'curr_y_max'} -= $self->{'tick_len'}; | ||
| 3276 | } | ||
| 3277 | |||
| 3278 | ## @fn private int _draw_y_ticks() | ||
| 3279 | # draw the y-ticks and their labels | ||
| 3280 | # @return status | ||
| 3281 | sub _draw_y_ticks { | ||
| 3282 | my $self = shift; | ||
| 3283 | my $side = shift || 'left'; | ||
| 3284 | my $data = $self->{'dataref'}; | ||
| 3285 | my $font = $self->{'tick_label_font'}; | ||
| 3286 | my $textcolor = $self->_color_role_to_index('text'); | ||
| 3287 | my $misccolor = $self->_color_role_to_index('misc'); | ||
| 3288 | my @labels = @{ $self->{'y_tick_labels'} }; | ||
| 3289 | my ( $w, $h ); | ||
| 3290 | my ( $x1, $x2, $y1, $y2 ); | ||
| 3291 | my ( $height, $delta, $label ); | ||
| 3292 | my ( $s, $f ); | ||
| 3293 | |||
| 3294 | $self->{grid_data}->{'y'} = []; | ||
| 3295 | $self->{grid_data}->{'y2'} = []; | ||
| 3296 | |||
| 3297 | # make sure we got a real font | ||
| 3298 | croak "The tick label font you specified isn\'t a GD Font object" unless ( ref $font ) eq 'GD::Font'; | ||
| 3299 | |||
| 3300 | # find out how big the font is | ||
| 3301 | ( $w, $h ) = ( $font->width, $font->height ); | ||
| 3302 | |||
| 3303 | # figure out which ticks not to draw | ||
| 3304 | if ( $self->{'min_val'} >= 0 ) { | ||
| 3305 | $s = 1; | ||
| 3306 | $f = $#labels; | ||
| 3307 | } elsif ( $self->{'max_val'} <= 0 ) { | ||
| 3308 | $s = 0; | ||
| 3309 | $f = $#labels; # -1 entfernt | ||
| 3310 | } else { | ||
| 3311 | $s = 0; | ||
| 3312 | $f = $#labels; | ||
| 3313 | } | ||
| 3314 | |||
| 3315 | # now draw them | ||
| 3316 | if ( $side eq 'right' ) | ||
| 3317 | { # put 'em on the right side of the chart | ||
| 3318 | # get the base x-y values, and the delta value | ||
| 3319 | $x1 = | ||
| 3320 | $self->{'curr_x_max'} - $self->{'tick_len'} - ( 3 * $self->{'text_space'} ) - ( $w * $self->{'y_tick_label_length'} ); | ||
| 3321 | $y1 = $self->{'curr_y_max'}; | ||
| 3322 | $height = $self->{'curr_y_max'} - $self->{'curr_y_min'}; | ||
| 3323 | $self->{'y_ticks'} = 2 if $self->{'y_ticks'} < 2; | ||
| 3324 | $delta = $height / ( $self->{'y_ticks'} - 1 ); | ||
| 3325 | |||
| 3326 | # update the curr_x_max value | ||
| 3327 | $self->{'curr_x_max'} = $x1; | ||
| 3328 | |||
| 3329 | # now draw the ticks | ||
| 3330 | $x2 = $x1 + $self->{'tick_len'}; | ||
| 3331 | for ( $s .. $f ) | ||
| 3332 | { | ||
| 3333 | $y2 = $y1 - ( $delta * $_ ); | ||
| 3334 | $self->{'gd_obj'}->line( $x1, $y2, $x2, $y2, $misccolor ); | ||
| 3335 | if ( $self->true( $self->{'grid_lines'} ) | ||
| 3336 | or $self->true( $self->{'y2_grid_lines'} ) ) | ||
| 3337 | { | ||
| 3338 | $self->{'grid_data'}->{'y2'}->[$_] = $y2; | ||
| 3339 | } | ||
| 3340 | } | ||
| 3341 | |||
| 3342 | # update the current x-min value | ||
| 3343 | $x1 += $self->{'tick_len'} + ( 2 * $self->{'text_space'} ); | ||
| 3344 | $y1 -= $h / 2; | ||
| 3345 | |||
| 3346 | # now draw the labels | ||
| 3347 | for ( 0 .. $#labels ) { | ||
| 3348 | $y2 = $y1 - ( $delta * $_ ); | ||
| 3349 | $self->{'gd_obj'}->string( $font, $x1, $y2, $self->{'y_tick_labels'}[$_], $textcolor ); | ||
| 3350 | } | ||
| 3351 | } elsif ( $side eq 'both' ) { | ||
| 3352 | # put the ticks on the both sides, left side first | ||
| 3353 | # get the base x-y values | ||
| 3354 | $x1 = $self->{'curr_x_min'} + $self->{'text_space'}; | ||
| 3355 | $y1 = $self->{'curr_y_max'} - $h / 2; | ||
| 3356 | |||
| 3357 | # now draw the labels | ||
| 3358 | $height = $self->{'curr_y_max'} - $self->{'curr_y_min'}; | ||
| 3359 | $delta = $height / ( $self->{'y_ticks'} - 1 ); | ||
| 3360 | for ( 0 .. $#labels ) { | ||
| 3361 | $label = $self->{'y_tick_labels'}[$_]; | ||
| 3362 | $y2 = $y1 - ( $delta * $_ ); | ||
| 3363 | $x2 = $x1 + ( $w * $self->{'y_tick_label_length'} ) - ( $w * length($label) ); | ||
| 3364 | $self->{'gd_obj'}->string( $font, $x2, $y2, $label, $textcolor ); | ||
| 3365 | } | ||
| 3366 | |||
| 3367 | # and update the current x-min value | ||
| 3368 | $self->{'curr_x_min'} += ( 3 * $self->{'text_space'} ) + ( $w * $self->{'y_tick_label_length'} ); | ||
| 3369 | |||
| 3370 | # now draw the ticks (skipping the one at zero); | ||
| 3371 | $x1 = $self->{'curr_x_min'}; | ||
| 3372 | $x2 = $self->{'curr_x_min'} + $self->{'tick_len'}; | ||
| 3373 | $y1 += $h / 2; | ||
| 3374 | for ( $s .. $f ) { | ||
| 3375 | $y2 = $y1 - ( $delta * $_ ); | ||
| 3376 | $self->{'gd_obj'}->line( $x1, $y2, $x2, $y2, $misccolor ); | ||
| 3377 | if ( $self->true( $self->{grid_lines} ) | ||
| 3378 | or $self->true( $self->{'y_grid_lines'} ) ) { | ||
| 3379 | $self->{'grid_data'}->{'y'}->[$_] = $y2; | ||
| 3380 | } | ||
| 3381 | } | ||
| 3382 | |||
| 3383 | # update the current x-min value | ||
| 3384 | $self->{'curr_x_min'} += $self->{'tick_len'}; | ||
| 3385 | |||
| 3386 | ## now the right side | ||
| 3387 | # get the base x-y values, and the delta value | ||
| 3388 | $x1 = | ||
| 3389 | $self->{'curr_x_max'} - $self->{'tick_len'} - ( 3 * $self->{'text_space'} ) - ( $w * $self->{'y_tick_label_length'} ); | ||
| 3390 | $y1 = $self->{'curr_y_max'}; | ||
| 3391 | $height = $self->{'curr_y_max'} - $self->{'curr_y_min'}; | ||
| 3392 | $delta = $height / ( $self->{'y_ticks'} - 1 ); | ||
| 3393 | |||
| 3394 | # update the curr_x_max value | ||
| 3395 | $self->{'curr_x_max'} = $x1; | ||
| 3396 | |||
| 3397 | # now draw the ticks (skipping the one at zero); | ||
| 3398 | $x2 = $x1 + $self->{'tick_len'}; | ||
| 3399 | |||
| 3400 | for ( $s .. $f ) { | ||
| 3401 | $y2 = $y1 - ( $delta * $_ ); | ||
| 3402 | $self->{'gd_obj'}->line( $x1, $y2, $x2, $y2, $misccolor ); # draw tick_line | ||
| 3403 | if ( $self->true( $self->{grid_lines} ) | ||
| 3404 | or $self->true( $self->{'y2_grid_lines'} ) ) { | ||
| 3405 | $self->{'grid_data'}->{'y2'}->[$_] = $y2; | ||
| 3406 | } | ||
| 3407 | } | ||
| 3408 | |||
| 3409 | # update the current x-min value | ||
| 3410 | $x1 += $self->{'tick_len'} + ( 2 * $self->{'text_space'} ); | ||
| 3411 | $y1 -= $h / 2; | ||
| 3412 | |||
| 3413 | # now draw the labels | ||
| 3414 | for ( 0 .. $#labels ) { | ||
| 3415 | $y2 = $y1 - ( $delta * $_ ); | ||
| 3416 | $self->{'gd_obj'}->string( $font, $x1, $y2, $self->{'y_tick_labels'}[$_], $textcolor ); | ||
| 3417 | } | ||
| 3418 | } else { # just the left side | ||
| 3419 | # get the base x-y values | ||
| 3420 | $x1 = $self->{'curr_x_min'} + $self->{'text_space'}; | ||
| 3421 | $y1 = $self->{'curr_y_max'} - $h / 2; | ||
| 3422 | |||
| 3423 | # now draw the labels | ||
| 3424 | $height = $self->{'curr_y_max'} - $self->{'curr_y_min'}; | ||
| 3425 | $self->{'y_ticks'} = 2 if $self->{'y_ticks'} < 2; | ||
| 3426 | $delta = $height / ( $self->{'y_ticks'} - 1 ); | ||
| 3427 | for ( 0 .. $#labels ) { | ||
| 3428 | $label = $self->{'y_tick_labels'}[$_]; | ||
| 3429 | $y2 = $y1 - ( $delta * $_ ); | ||
| 3430 | $x2 = $x1 + ( $w * $self->{'y_tick_label_length'} ) - ( $w * length($label) ); | ||
| 3431 | $self->{'gd_obj'}->string( $font, $x2, $y2, $label, $textcolor ); | ||
| 3432 | } | ||
| 3433 | |||
| 3434 | # and update the current x-min value | ||
| 3435 | $self->{'curr_x_min'} += ( 3 * $self->{'text_space'} ) + ( $w * $self->{'y_tick_label_length'} ); | ||
| 3436 | |||
| 3437 | # now draw the ticks | ||
| 3438 | $x1 = $self->{'curr_x_min'}; | ||
| 3439 | $x2 = $self->{'curr_x_min'} + $self->{'tick_len'}; | ||
| 3440 | $y1 += $h / 2; | ||
| 3441 | for ( $s .. $f ) { | ||
| 3442 | $y2 = $y1 - ( $delta * $_ ); | ||
| 3443 | $self->{'gd_obj'}->line( $x1, $y2, $x2, $y2, $misccolor ); | ||
| 3444 | if ( $self->true( $self->{'grid_lines'} ) | ||
| 3445 | or $self->true( $self->{'y_grid_lines'} ) ) { | ||
| 3446 | $self->{'grid_data'}->{'y'}->[$_] = $y2; | ||
| 3447 | } | ||
| 3448 | } | ||
| 3449 | |||
| 3450 | # update the current x-min value | ||
| 3451 | $self->{'curr_x_min'} += $self->{'tick_len'}; | ||
| 3452 | } | ||
| 3453 | |||
| 3454 | # and return | ||
| 3455 | return 1; | ||
| 3456 | } | ||
| 3457 | |||
| 3458 | ## @fn private int _grey_background() | ||
| 3459 | # put a grey background on the plot of the data itself | ||
| 3460 | # @return status | ||
| 3461 | sub _grey_background { | ||
| 3462 | my $self = shift; | ||
| 3463 | # draw it | ||
| 3464 | $self->{'gd_obj'} | ||
| 3465 | ->filledRectangle( $self->{'curr_x_min'}, $self->{'curr_y_min'}, $self->{'curr_x_max'}, $self->{'curr_y_max'}, | ||
| 3466 | $self->_color_role_to_index('grey_background') ); | ||
| 3467 | |||
| 3468 | # now return | ||
| 3469 | return 1; | ||
| 3470 | } | ||
| 3471 | |||
| 3472 | ## @fn private int _draw_grid_lines() | ||
| 3473 | # draw grid_lines | ||
| 3474 | # @return status | ||
| 3475 | sub _draw_grid_lines { | ||
| 3476 | my $self = shift; | ||
| 3477 | $self->_draw_x_grid_lines(); | ||
| 3478 | $self->_draw_y_grid_lines(); | ||
| 3479 | $self->_draw_y2_grid_lines(); | ||
| 3480 | return 1; | ||
| 3481 | } | ||
| 3482 | |||
| 3483 | ## @fn private int _draw_x_grid_lines() | ||
| 3484 | # draw grid_lines for x | ||
| 3485 | # @return status | ||
| 3486 | sub _draw_x_grid_lines { | ||
| 3487 | my $self = shift; | ||
| 3488 | my $grid_role = shift || 'x_grid_lines'; | ||
| 3489 | my $gridcolor = $self->_color_role_to_index($grid_role); | ||
| 3490 | my ( $x, $y, $i ); | ||
| 3491 | |||
| 3492 | foreach $x ( @{ $self->{grid_data}->{'x'} } ) { | ||
| 3493 | if ( defined $x ) { | ||
| 3494 | $self->{gd_obj}->line( ( $x, $self->{'curr_y_min'} + 1 ), $x, ( $self->{'curr_y_max'} - 1 ), $gridcolor ); | ||
| 3495 | } | ||
| 3496 | } | ||
| 3497 | return 1; | ||
| 3498 | } | ||
| 3499 | |||
| 3500 | ## @fn private int _draw_y_grid_lines() | ||
| 3501 | # draw grid_lines for y | ||
| 3502 | # @return status | ||
| 3503 | sub _draw_y_grid_lines { | ||
| 3504 | my $self = shift; | ||
| 3505 | my $grid_role = shift || 'y_grid_lines'; | ||
| 3506 | my $gridcolor = $self->_color_role_to_index($grid_role); | ||
| 3507 | my ( $x, $y, $i ); | ||
| 3508 | |||
| 3509 | #Look if I'm an HorizontalBars object | ||
| 3510 | if ( $self->isa('Chart::HorizontalBars') ) { | ||
| 3511 | for ( $i = 0 ; $i < ( $#{ $self->{grid_data}->{'y'} } ) + 1 ; $i++ ) | ||
| 3512 | { | ||
| 3513 | $y = $self->{grid_data}->{'y'}->[$i]; | ||
| 3514 | $self->{gd_obj}->line( ( $self->{'curr_x_min'} + 1 ), $y, ( $self->{'curr_x_max'} - 1 ), $y, $gridcolor ); | ||
| 3515 | } | ||
| 3516 | } else { | ||
| 3517 | |||
| 3518 | # loop for y values is a little different. This is to discard the first | ||
| 3519 | # and last values we were given - the top/bottom of the chart area. | ||
| 3520 | for ( $i = 1 ; $i < ( $#{ $self->{grid_data}->{'y'} } ) + 1 ; $i++ ) | ||
| 3521 | { ### | ||
| 3522 | $y = $self->{grid_data}->{'y'}->[$i]; | ||
| 3523 | $self->{gd_obj}->line( ( $self->{'curr_x_min'} + 1 ), $y, ( $self->{'curr_x_max'} - 1 ), $y, $gridcolor ); | ||
| 3524 | } | ||
| 3525 | } | ||
| 3526 | return 1; | ||
| 3527 | } | ||
| 3528 | |||
| 3529 | ## @fn private int _draw_y2_grid_lines() | ||
| 3530 | # draw grid_lines for y | ||
| 3531 | # @return status | ||
| 3532 | sub _draw_y2_grid_lines { | ||
| 3533 | my $self = shift; | ||
| 3534 | my $grid_role = shift || 'y2_grid_lines'; | ||
| 3535 | my $gridcolor = $self->_color_role_to_index($grid_role); | ||
| 3536 | my ( $x, $y, $i ); | ||
| 3537 | |||
| 3538 | #Look if I'm an HorizontalBars object | ||
| 3539 | if ( $self->isa('Chart::HorizontalBars') ) { | ||
| 3540 | for ( $i = 0 ; $i < ( $#{ $self->{grid_data}->{'y'} } ) + 1 ; $i++ ) { | ||
| 3541 | $y = $self->{grid_data}->{'y'}->[$i]; | ||
| 3542 | $self->{gd_obj}->line( ( $self->{'curr_x_min'} + 1 ), $y, ( $self->{'curr_x_max'} - 1 ), $y, $gridcolor ); | ||
| 3543 | } | ||
| 3544 | } else { | ||
| 3545 | |||
| 3546 | # loop for y2 values is a little different. This is to discard the first | ||
| 3547 | # and last values we were given - the top/bottom of the chart area. | ||
| 3548 | for ( $i = 1 ; $i < $#{ $self->{grid_data}->{'y2'} } ; $i++ ) { | ||
| 3549 | $y = $self->{grid_data}->{'y2'}->[$i]; | ||
| 3550 | $self->{gd_obj}->line( ( $self->{'curr_x_min'} + 1 ), $y, ( $self->{'curr_x_max'} - 1 ), $y, $gridcolor ); | ||
| 3551 | } | ||
| 3552 | } | ||
| 3553 | return 1; | ||
| 3554 | } | ||
| 3555 | |||
| 3556 | ## @fn private int _prepare_brush($color,$type,$role) | ||
| 3557 | # prepare brush | ||
| 3558 | # | ||
| 3559 | # @details | ||
| 3560 | # set the gdBrush object to tick GD into drawing fat lines & points | ||
| 3561 | # of interesting shapes | ||
| 3562 | # Needed by "Lines", "Points" and "LinesPoints" | ||
| 3563 | # All hacked up by Richard Dice <rdice@pobox.com> Sunday 16 May 1999 | ||
| 3564 | # | ||
| 3565 | # @param color | ||
| 3566 | # @param type 'line','point' | ||
| 3567 | # @param role | ||
| 3568 | # | ||
| 3569 | # @return status | ||
| 3570 | sub _prepare_brush { | ||
| 3571 | my $self = shift; | ||
| 3572 | my $color = shift; | ||
| 3573 | my $type = shift; | ||
| 3574 | my $role = shift || 'default'; | ||
| 3575 | |||
| 3576 | my $brushStyle = $self->{'brushStyle'}; | ||
| 3577 | if ( defined $role ) { | ||
| 3578 | my (@brushStyles) = $self->_brushStyles_of_roles($role); | ||
| 3579 | $brushStyle = $brushStyles[0]; | ||
| 3580 | } | ||
| 3581 | |||
| 3582 | #print STDERR "role=$role\n"; | ||
| 3583 | |||
| 3584 | # decide what $type should be in the event that a param isn't | ||
| 3585 | # passed -- this is necessary to preserve backward compatibility | ||
| 3586 | # with apps that use this module prior to putting _prepare_brush | ||
| 3587 | # in with Base.pm | ||
| 3588 | if ( !defined($type) ) { $type = 'point'; } | ||
| 3589 | |||
| 3590 | if ( ( !length($type) ) | ||
| 3591 | || ( !grep { $type eq $_ } ( 'line', 'point' ) ) ) { | ||
| 3592 | $brushStyle = $self->{'brushStyle'}; | ||
| 3593 | $type = 'line' if ref $self eq 'Chart::Lines'; | ||
| 3594 | $type = 'point' if ref $self eq 'Chart::Points'; | ||
| 3595 | } | ||
| 3596 | |||
| 3597 | my ( $radius, @rgb, $brush, $white, $newcolor ); | ||
| 3598 | |||
| 3599 | # get the rgb values for the desired color | ||
| 3600 | @rgb = $self->{'gd_obj'}->rgb($color); | ||
| 3601 | |||
| 3602 | # get the appropriate brush size | ||
| 3603 | if ( $type eq 'line' ) { | ||
| 3604 | $radius = $self->{'brush_size'} / 2; | ||
| 3605 | } elsif ( $type eq 'point' ) { | ||
| 3606 | $radius = $self->{'pt_size'} / 2; | ||
| 3607 | } | ||
| 3608 | |||
| 3609 | # create the new image | ||
| 3610 | $brush = GD::Image->new( $radius * 2, $radius * 2 ); | ||
| 3611 | |||
| 3612 | # get the colors, make the background transparent | ||
| 3613 | $white = $brush->colorAllocate( 255, 255, 255 ); | ||
| 3614 | $newcolor = $brush->colorAllocate(@rgb); | ||
| 3615 | $brush->transparent($white); | ||
| 3616 | |||
| 3617 | # draw the circle | ||
| 3618 | if ( $type eq 'line' ) { | ||
| 3619 | $brush->arc( $radius - 1, $radius - 1, $radius, $radius, 0, 360, $newcolor ); | ||
| 3620 | $brush->fill( $radius - 1, $radius - 1, $newcolor ); | ||
| 3621 | |||
| 3622 | # RLD | ||
| 3623 | # | ||
| 3624 | # Does $brush->fill really have to be here? Dunno... this | ||
| 3625 | # seems to be a relic from earlier code | ||
| 3626 | # | ||
| 3627 | # Note that 'line's don't benefit from a $brushStyle... yet. | ||
| 3628 | # It shouldn't be too tough to hack this in by taking advantage | ||
| 3629 | # of GD's gdStyled facility | ||
| 3630 | |||
| 3631 | } | ||
| 3632 | |||
| 3633 | if ( $type eq 'point' ) { | ||
| 3634 | $brushStyle = $self->{'brushStyle'} | ||
| 3635 | unless grep { $brushStyle eq $_ } ( | ||
| 3636 | 'FilledCircle', 'circle', 'donut', 'OpenCircle', | ||
| 3637 | 'triangle', 'upsidedownTriangle', 'square', 'hollowSquare', | ||
| 3638 | 'OpenRectangle', 'fatPlus', 'Star', 'OpenStar', | ||
| 3639 | 'FilledDiamond', 'OpenDiamond' | ||
| 3640 | ); | ||
| 3641 | |||
| 3642 | my ( $xc, $yc ) = ( $radius, $radius ); | ||
| 3643 | |||
| 3644 | if ( grep { $brushStyle eq $_ } ( 'default', 'circle', 'donut', 'OpenCircle', 'FilledCircle' ) ) { | ||
| 3645 | $brush->arc( $xc, $yc, $radius, $radius, 0, 360, $newcolor ); | ||
| 3646 | $brush->fill( $xc, $yc, $newcolor ); | ||
| 3647 | |||
| 3648 | # draw a white (and therefore transparent) circle in the middle | ||
| 3649 | # of the existing circle to make the "donut", if appropriate | ||
| 3650 | |||
| 3651 | if ( $brushStyle eq 'donut' || $brushStyle eq 'OpenCircle' ) | ||
| 3652 | { | ||
| 3653 | $brush->arc( $xc, $yc, int( $radius / 2 ), int( $radius / 2 ), 0, 360, $white ); | ||
| 3654 | $brush->fill( $xc, $yc, $white ); | ||
| 3655 | } | ||
| 3656 | } | ||
| 3657 | |||
| 3658 | if ( grep { $brushStyle eq $_ } ( 'triangle', 'upsidedownTriangle' ) ) { | ||
| 3659 | my $poly = new GD::Polygon; | ||
| 3660 | my $sign = ( $brushStyle eq 'triangle' ) ? 1 : (-1); | ||
| 3661 | my $z = int( 0.8 * $radius ); # scaling factor | ||
| 3662 | |||
| 3663 | # co-ords are chosen to make an equilateral triangle | ||
| 3664 | |||
| 3665 | $poly->addPt( $xc, $yc - ( $z * $sign ) ); | ||
| 3666 | $poly->addPt( $xc + int( ( sqrt(3) * $z ) / 2 ), $yc + ( int( $z / 2 ) * $sign ) ); | ||
| 3667 | $poly->addPt( $xc - int( ( sqrt(3) * $z ) / 2 ), $yc + ( int( $z / 2 ) * $sign ) ); | ||
| 3668 | |||
| 3669 | $brush->filledPolygon( $poly, $newcolor ); | ||
| 3670 | } | ||
| 3671 | |||
| 3672 | if ( $brushStyle eq 'fatPlus' ) { | ||
| 3673 | my $poly = new GD::Polygon; | ||
| 3674 | my $z = int( 0.3 * $radius ); | ||
| 3675 | |||
| 3676 | $poly->addPt( $xc + $z, $yc + $z ); | ||
| 3677 | $poly->addPt( $xc + 2 * $z, $yc + $z ); | ||
| 3678 | $poly->addPt( $xc + 2 * $z, $yc - $z ); | ||
| 3679 | |||
| 3680 | $poly->addPt( $xc + $z, $yc - $z ); | ||
| 3681 | $poly->addPt( $xc + $z, $yc - 2 * $z ); | ||
| 3682 | $poly->addPt( $xc - $z, $yc - 2 * $z ); | ||
| 3683 | |||
| 3684 | $poly->addPt( $xc - $z, $yc - $z ); | ||
| 3685 | $poly->addPt( $xc - 2 * $z, $yc - $z ); | ||
| 3686 | $poly->addPt( $xc - 2 * $z, $yc + $z ); | ||
| 3687 | |||
| 3688 | $poly->addPt( $xc - $z, $yc + $z ); | ||
| 3689 | $poly->addPt( $xc - $z, $yc + 2 * $z ); | ||
| 3690 | $poly->addPt( $xc + $z, $yc + 2 * $z ); | ||
| 3691 | $brush->filledPolygon( $poly, $newcolor ); | ||
| 3692 | } | ||
| 3693 | |||
| 3694 | if ( $brushStyle eq 'Star' || $brushStyle eq 'OpenStar' ) { | ||
| 3695 | my $poly = new GD::Polygon; | ||
| 3696 | |||
| 3697 | my $z = int($radius); | ||
| 3698 | my $sz = int( $z / 3 * 1.75 ); # small z | ||
| 3699 | |||
| 3700 | my $x1 = int( $xc + $z ); | ||
| 3701 | my $y1 = int($yc); | ||
| 3702 | my ( $x2, $y2 ); | ||
| 3703 | |||
| 3704 | my $xyRatio = $self->_xyRatio(); | ||
| 3705 | |||
| 3706 | $poly->addPt( $x1, $y1 ); | ||
| 3707 | |||
| 3708 | $x2 = $xc + int( $sz * 0.5 ); | ||
| 3709 | $y2 = $yc - int( $sz * 0.5 ); | ||
| 3710 | $poly->addPt( $x2, $y2 ); | ||
| 3711 | |||
| 3712 | $x2 = $xc; | ||
| 3713 | $y2 = $yc - $z; | ||
| 3714 | $poly->addPt( $x2, $y2 ); | ||
| 3715 | |||
| 3716 | $x2 = $xc - int( $sz * 0.5 ); | ||
| 3717 | $y2 = $yc - int( $sz * 0.5 ); | ||
| 3718 | $poly->addPt( $x2, $y2 ); | ||
| 3719 | |||
| 3720 | $x2 = $xc - $z; | ||
| 3721 | $y2 = $yc; | ||
| 3722 | $poly->addPt( $x2, $y2 ); | ||
| 3723 | |||
| 3724 | $x2 = $xc - int( $sz * 0.5 ); | ||
| 3725 | $y2 = $yc + int( $sz * 0.5 ); | ||
| 3726 | $poly->addPt( $x2, $y2 ); | ||
| 3727 | |||
| 3728 | $x2 = $xc; | ||
| 3729 | $y2 = $yc + $z; | ||
| 3730 | $poly->addPt( $x2, $y2 ); | ||
| 3731 | |||
| 3732 | $x2 = $xc + int( $sz * 0.5 ); | ||
| 3733 | $y2 = $yc + int( $sz * 0.5 ); | ||
| 3734 | $poly->addPt( $x2, $y2 ); | ||
| 3735 | if ( $brushStyle eq 'OpenStar' ) | ||
| 3736 | { | ||
| 3737 | $brush->polygon( $poly, $newcolor ); | ||
| 3738 | } | ||
| 3739 | else | ||
| 3740 | { | ||
| 3741 | $brush->filledPolygon( $poly, $newcolor ); | ||
| 3742 | } | ||
| 3743 | } | ||
| 3744 | |||
| 3745 | if ( grep { $brushStyle eq $_ } ( 'square', 'hollowSquare', 'OpenRectangle' ) ) | ||
| 3746 | { | ||
| 3747 | my $z = int( 0.5 * $radius ); | ||
| 3748 | $brush->filledRectangle( $xc - $z, $yc - $z, $xc + $z, $yc + $z, $newcolor ); | ||
| 3749 | |||
| 3750 | if ( $brushStyle eq 'hollowSquare' || $brushStyle eq 'OpenRectangle' ) { | ||
| 3751 | $z = int( $z / 2 ); | ||
| 3752 | $brush->filledRectangle( $xc - $z, $yc - $z, $xc + $z, $yc + $z, $white ); | ||
| 3753 | } | ||
| 3754 | } | ||
| 3755 | |||
| 3756 | if ( grep { $brushStyle eq $_ } ( 'FilledDiamond', 'OpenDiamond' ) ) { | ||
| 3757 | my $z = int( 0.75 * $radius ); | ||
| 3758 | |||
| 3759 | $brush->line( $xc + $z, $yc, $xc, $yc + $z, $newcolor ); | ||
| 3760 | $brush->line( $xc, $yc + $z, $xc - $z, $yc, $newcolor ); | ||
| 3761 | $brush->line( $xc - $z, $yc, $xc, $yc - $z, $newcolor ); | ||
| 3762 | $brush->line( $xc, $yc - $z, $xc + $z, $yc, $newcolor ); | ||
| 3763 | |||
| 3764 | if ( $brushStyle eq 'FilledDiamond' ) { | ||
| 3765 | # and fill it | ||
| 3766 | $brush->fill( $radius - 1, $radius - 1, $newcolor ); | ||
| 3767 | } | ||
| 3768 | } | ||
| 3769 | |||
| 3770 | } | ||
| 3771 | # set the new image as the main object's brush | ||
| 3772 | return $brush; | ||
| 3773 | } | ||
| 3774 | |||
| 3775 | ## @fn private int _default_f_tick | ||
| 3776 | # default tick conversion function | ||
| 3777 | # This function is pointed to be $self->{f_x_tick} resp. $self->{f_y_tick} | ||
| 3778 | # if the user does not provide another function | ||
| 3779 | # | ||
| 3780 | # @return status | ||
| 3781 | sub _default_f_tick { | ||
| 3782 | my $label = shift; | ||
| 3783 | |||
| 3784 | return $label; | ||
| 3785 | } | ||
| 3786 | |||
| 3787 | ## @fn private float _xyRatio | ||
| 3788 | # Get ratio width_x/width_y | ||
| 3789 | # | ||
| 3790 | # @return ratio width_x and width_y | ||
| 3791 | sub _xyRatio { | ||
| 3792 | my $self = shift; | ||
| 3793 | my $width_x = $self->{'curr_x_max'} - $self->{'curr_x_min'} + 1; | ||
| 3794 | my $width_y = $self->{'curr_y_max'} - $self->{'curr_y_min'} + 1; | ||
| 3795 | |||
| 3796 | return $width_x / $width_y; | ||
| 3797 | } | ||
| 3798 | |||
| 3799 | ## @fn private float _xPixelInReal | ||
| 3800 | # Get width of one Pixel in real coordinates in x-direction | ||
| 3801 | # | ||
| 3802 | # @return width(interval) of reality in x direction | ||
| 3803 | # | ||
| 3804 | sub _xPixelInReal { | ||
| 3805 | my $self = shift; | ||
| 3806 | my $width_x = $self->{'curr_x_max'} - $self->{'curr_x_min'} + 1; | ||
| 3807 | my ( $min, $max ) = $self->_find_x_range(); | ||
| 3808 | my $xRealWidth = $max - $min; | ||
| 3809 | |||
| 3810 | return $xRealWidth / $width_x; | ||
| 3811 | } | ||
| 3812 | |||
| 3813 | ## @fn private float _yPixelInReal | ||
| 3814 | # Get width of one Pixel in real coordinates in y-direction | ||
| 3815 | # | ||
| 3816 | # @return width(interval) of reality in y direction | ||
| 3817 | # | ||
| 3818 | sub _yPixelInReal { | ||
| 3819 | my $self = shift; | ||
| 3820 | my $width_y = $self->{'curr_y_max'} - $self->{'curr_y_min'} + 1; | ||
| 3821 | my ( $min, $max, $flag_all_integers ) = $self->_find_y_range(); | ||
| 3822 | my $yRealWidth = $max - $min; | ||
| 3823 | |||
| 3824 | return $yRealWidth / $width_y; | ||
| 3825 | } | ||
| 3826 | |||
| 3827 | 1; # be a good module and return positive | ||