| 1 | ## @file | ||
| 2 | # Implementation of Chart::Split | ||
| 3 | # | ||
| 4 | # written and maintained by the | ||
| 5 | # @author Chart Group at Geodetic Fundamental Station Wettzell (Chart@fs.wettzell.de) | ||
| 6 | # @date 2015-03-01 | ||
| 7 | # @version 2.4.10 | ||
| 8 | # | ||
| 9 | |||
| 10 | ## @class Chart::Split | ||
| 11 | #Split class derived from class Base. | ||
| 12 | # | ||
| 13 | # This class provides all functions which are specific to | ||
| 14 | # splitted plots | ||
| 15 | # | ||
| 16 | |||
| 17 | 2 | ▶ | use v5.12; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 18 | |||
| 19 | package Chart::Split; | ||
| 20 | our @ISA = qw(Chart::Base); | ||
| 21 | our $VERSION = 'v2.403.9'; | ||
| 22 | |||
| 23 | 0 | ▶ | use Chart::Base; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 24 | use GD; | ||
| 25 | use Carp; | ||
| 26 | |||
| 27 | #>>>>>>>>>>>>>>>>>>>>>>>>>># | ||
| 28 | # public methods go here # | ||
| 29 | #<<<<<<<<<<<<<<<<<<<<<<<<<<# | ||
| 30 | |||
| 31 | #>>>>>>>>>>>>>>>>>>>>>>>>>>># | ||
| 32 | # private methods go here # | ||
| 33 | #<<<<<<<<<<<<<<<<<<<<<<<<<<<# | ||
| 34 | |||
| 35 | ## @fn private _draw_x_number_ticks | ||
| 36 | #draw the ticks | ||
| 37 | sub _draw_x_number_ticks | ||
| 38 | { | ||
| 39 | my $self = shift; | ||
| 40 | my $data = $self->{'dataref'}; | ||
| 41 | my $font = $self->{'tick_label_font'}; | ||
| 42 | my $textcolor = $self->_color_role_to_index('text'); | ||
| 43 | my $misccolor = $self->_color_role_to_index('misc'); | ||
| 44 | my $num_points = $self->{'num_datapoints'}; | ||
| 45 | my ( $h, $w, $width, $step, $start, $interval, $label, $stag, @labels ); | ||
| 46 | my ( $x_start, $y_start, $y, $x, $lines, $delta, $ticks ); | ||
| 47 | my $x_label_len = 1; | ||
| 48 | my $y_label_len = 1; | ||
| 49 | my $x_max = -0x80000000; | ||
| 50 | |||
| 51 | $self->{'grid_data'}->{'x'} = []; | ||
| 52 | |||
| 53 | # find the width | ||
| 54 | $width = $self->{'curr_x_max'} - $self->{'curr_x_min'}; | ||
| 55 | $width = 1 if $width == 0; | ||
| 56 | |||
| 57 | # make sure we got a real font | ||
| 58 | unless ( ( ref $font ) eq 'GD::Font' ) | ||
| 59 | { | ||
| 60 | croak "The tick label font you specified isn\'t a GD Font object"; | ||
| 61 | } | ||
| 62 | |||
| 63 | # find out how big the font is | ||
| 64 | ( $w, $h ) = ( $font->width, $font->height ); | ||
| 65 | |||
| 66 | unless ( defined $self->{'start'} && defined $self->{'interval'} ) | ||
| 67 | { | ||
| 68 | croak "I need two values from you to draw a split chart: start and interval!"; | ||
| 69 | } | ||
| 70 | else | ||
| 71 | { | ||
| 72 | $interval = $self->{'interval'}; | ||
| 73 | $start = $self->{'start'}; | ||
| 74 | $ticks = $self->{'interval_ticks'} - 1; | ||
| 75 | $label = $start; | ||
| 76 | } | ||
| 77 | |||
| 78 | #look after devision by zero! | ||
| 79 | if ( $ticks == 0 ) { $ticks = 1; } | ||
| 80 | |||
| 81 | #calculate the step between the ticks | ||
| 82 | $step = $interval / $ticks; | ||
| 83 | |||
| 84 | for ( 0 .. $ticks ) | ||
| 85 | { | ||
| 86 | push @labels, $self->{f_x_tick}->( sprintf( "%." . $self->{'precision'} . "f", $label ) ); | ||
| 87 | $label += $step; | ||
| 88 | } | ||
| 89 | |||
| 90 | #find the biggest x value | ||
| 91 | foreach ( @{ $data->[0] } ) | ||
| 92 | { | ||
| 93 | if ( $_ > $x_max ) | ||
| 94 | { | ||
| 95 | $x_max = $_; | ||
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | #find the length of the x and y labels | ||
| 100 | foreach (@labels) | ||
| 101 | { | ||
| 102 | if ( length($_) > $x_label_len ) | ||
| 103 | { | ||
| 104 | $x_label_len = length($_); | ||
| 105 | } | ||
| 106 | } | ||
| 107 | |||
| 108 | #find the amount of lines | ||
| 109 | $lines = int( ( ( $x_max - $start ) / $interval ) + 0.99999999999 ); | ||
| 110 | $lines = 1 if $lines == 0; | ||
| 111 | |||
| 112 | #find the length, of the label. | ||
| 113 | $y_label_len = length($lines); | ||
| 114 | |||
| 115 | #get the starting point and the width | ||
| 116 | if ( $lines > 1 ) | ||
| 117 | { #if there are y-ticks | ||
| 118 | if ( $self->{'y_axes'} =~ /^right$/i ) | ||
| 119 | { | ||
| 120 | $x_start = $self->{'curr_x_min'}; | ||
| 121 | $width = $self->{'curr_x_max'} - $x_start - $self->{'text_space'} * 2 - $y_label_len * $w - $self->{'tick_len'}; | ||
| 122 | |||
| 123 | } | ||
| 124 | elsif ( $self->{'y_axes'} =~ /^both$/i ) | ||
| 125 | { | ||
| 126 | $x_start = $self->{'curr_x_min'} + ( $w * $y_label_len ) + 2 * $self->{'text_space'} + $self->{'tick_len'}; | ||
| 127 | $width = $self->{'curr_x_max'} - $x_start - ( $w * $y_label_len ) - 2 * $self->{'text_space'} - $self->{'tick_len'}; | ||
| 128 | } | ||
| 129 | else | ||
| 130 | { | ||
| 131 | $x_start = $self->{'curr_x_min'} + ( $w * $y_label_len ) + 3 * $self->{'text_space'}; | ||
| 132 | $width = $self->{'curr_x_max'} - $x_start; | ||
| 133 | } | ||
| 134 | } | ||
| 135 | else | ||
| 136 | { #if there are no y-axes | ||
| 137 | $x_start = $self->{'curr_x_min'}; | ||
| 138 | $width = $self->{'curr_x_max'} - $x_start; | ||
| 139 | } | ||
| 140 | |||
| 141 | #and the y_start value | ||
| 142 | $y_start = $self->{'curr_y_max'} - $h - $self->{'text_space'}; | ||
| 143 | |||
| 144 | #get the delta value | ||
| 145 | $delta = $width / ($ticks); | ||
| 146 | |||
| 147 | if ( !defined( $self->{'skip_x_ticks'} ) ) | ||
| 148 | { | ||
| 149 | $self->{'skip_x_ticks'} = 1; | ||
| 150 | } | ||
| 151 | |||
| 152 | #draw the labels | ||
| 153 | if ( $self->{'x_ticks'} =~ /^normal$/i ) | ||
| 154 | { | ||
| 155 | if ( $self->{'skip_x_ticks'} > 1 ) | ||
| 156 | { #draw a normal tick every nth label | ||
| 157 | for ( 0 .. $#labels - 1 ) | ||
| 158 | { | ||
| 159 | if ( defined( $labels[ $_ * $self->{'skip_x_ticks'} ] ) ) | ||
| 160 | { | ||
| 161 | $x = | ||
| 162 | $x_start + | ||
| 163 | $delta * ( $_ * $self->{'skip_x_ticks'} ) - | ||
| 164 | ( $w * length( $labels[ $_ * $self->{'skip_x_ticks'} ] ) ) / 2; | ||
| 165 | $self->{'gd_obj'}->string( $font, $x, $y_start, $labels[ $_ * $self->{'skip_x_ticks'} ], $textcolor ); | ||
| 166 | } | ||
| 167 | } | ||
| 168 | } | ||
| 169 | elsif ( $self->{'custom_x_ticks'} ) | ||
| 170 | { #draw only the normal ticks they wanted | ||
| 171 | foreach ( @{ $self->{'custom_x_ticks'} } ) | ||
| 172 | { | ||
| 173 | if ( defined $labels[$_] ) | ||
| 174 | { | ||
| 175 | $x = $x_start + $delta * $_ - ( $w * length( $labels[$_] ) ) / 2; | ||
| 176 | $self->{'gd_obj'}->string( $font, $x, $y_start, $labels[$_], $textcolor ); | ||
| 177 | } | ||
| 178 | } | ||
| 179 | } | ||
| 180 | else | ||
| 181 | { | ||
| 182 | for ( 0 .. $#labels ) | ||
| 183 | { #draw all ticks normal | ||
| 184 | if ( defined $labels[$_] ) | ||
| 185 | { | ||
| 186 | $x = $x_start + $delta * ($_) - ( $w * length( $labels[$_] ) ) / 2; | ||
| 187 | $self->{'gd_obj'}->string( $font, $x, $y_start, $labels[$_], $textcolor ); | ||
| 188 | } | ||
| 189 | } | ||
| 190 | } | ||
| 191 | } | ||
| 192 | elsif ( $self->{'x_ticks'} =~ /^staggered$/i ) | ||
| 193 | { | ||
| 194 | $stag = 0; | ||
| 195 | if ( $self->{'skip_x_ticks'} > 1 ) | ||
| 196 | { #draw a staggered tick every nth label | ||
| 197 | for ( 0 .. $#labels - 1 ) | ||
| 198 | { | ||
| 199 | if ( defined( $labels[ $_ * $self->{'skip_x_ticks'} ] ) ) | ||
| 200 | { | ||
| 201 | $x = | ||
| 202 | $x_start + | ||
| 203 | $delta * ( $_ * $self->{'skip_x_ticks'} ) - | ||
| 204 | ( $w * length( $labels[ $_ * $self->{'skip_x_ticks'} ] ) ) / 2; | ||
| 205 | if ( $stag % 2 == 0 ) | ||
| 206 | { | ||
| 207 | $y_start -= $self->{'text_space'} + $h; | ||
| 208 | } | ||
| 209 | $self->{'gd_obj'}->string( $font, $x, $y_start, $labels[ $_ * $self->{'skip_x_ticks'} ], $textcolor ); | ||
| 210 | if ( $stag % 2 == 0 ) | ||
| 211 | { | ||
| 212 | $y_start += $self->{'text_space'} + $h; | ||
| 213 | } | ||
| 214 | $stag++; | ||
| 215 | } | ||
| 216 | } | ||
| 217 | } | ||
| 218 | elsif ( $self->{'custom_x_ticks'} ) | ||
| 219 | { # draw only the wanted ticks staggered | ||
| 220 | foreach ( sort ( @{ $self->{'custom_x_ticks'} } ) ) | ||
| 221 | { | ||
| 222 | if ( defined $labels[$_] ) | ||
| 223 | { | ||
| 224 | $x = $x_start + $delta * $_ - ( $w * ( length( $labels[$_] ) ) ) / 2; | ||
| 225 | if ( $stag % 2 == 0 ) | ||
| 226 | { | ||
| 227 | $y_start -= $self->{'text_space'} + $h; | ||
| 228 | } | ||
| 229 | $self->{'gd_obj'}->string( $font, $x, $y_start, $labels[$_], $textcolor ); | ||
| 230 | if ( $stag % 2 == 0 ) | ||
| 231 | { | ||
| 232 | $y_start += $self->{'text_space'} + $h; | ||
| 233 | } | ||
| 234 | $stag++; | ||
| 235 | } | ||
| 236 | } | ||
| 237 | } | ||
| 238 | else | ||
| 239 | { # draw all ticks staggered | ||
| 240 | for ( 0 .. $#labels ) | ||
| 241 | { | ||
| 242 | if ( defined $labels[$_] ) | ||
| 243 | { | ||
| 244 | $x = $x_start + $delta * $_ - ( $w * ( length( $labels[$_] ) ) ) / 2; | ||
| 245 | if ( $stag % 2 == 0 ) | ||
| 246 | { | ||
| 247 | $y_start -= $self->{'text_space'} + $h; | ||
| 248 | } | ||
| 249 | $self->{'gd_obj'}->string( $font, $x, $y_start, $labels[$_], $textcolor ); | ||
| 250 | if ( $stag % 2 == 0 ) | ||
| 251 | { | ||
| 252 | $y_start += $self->{'text_space'} + $h; | ||
| 253 | } | ||
| 254 | $stag++; | ||
| 255 | } | ||
| 256 | } | ||
| 257 | } | ||
| 258 | } | ||
| 259 | elsif ( $self->{'x_ticks'} =~ /^vertical$/i ) | ||
| 260 | { | ||
| 261 | $y_start = $self->{'curr_y_max'} - $self->{'text_space'}; | ||
| 262 | if ( $self->{'skip_x_ticks'} > 1 ) | ||
| 263 | { #draw every nth tick vertical | ||
| 264 | for ( 0 .. $#labels ) | ||
| 265 | { | ||
| 266 | if ( defined $_ ) | ||
| 267 | { | ||
| 268 | $x = $x_start + $delta * ( $_ * $self->{'skip_x_ticks'} ) - $h / 2; | ||
| 269 | $y = $y_start - ( $x_label_len - length( $labels[ $_ * $self->{'skip_x_ticks'} ] ) ) * $w; | ||
| 270 | $self->{'gd_obj'}->stringUp( $font, $x, $y, $labels[ $_ * $self->{'skip_x_ticks'} ], $textcolor ); | ||
| 271 | } | ||
| 272 | } | ||
| 273 | } | ||
| 274 | elsif ( $self->{'custom_x_ticks'} ) | ||
| 275 | { | ||
| 276 | foreach ( @{ $self->{'custom_x_ticks'} } ) | ||
| 277 | { #draw the ticks they want vertical | ||
| 278 | if ( defined $labels[$_] ) | ||
| 279 | { | ||
| 280 | $x = $x_start + $delta * $_ - $h / 2; | ||
| 281 | $y = $y_start - ( $x_label_len - length( $labels[$_] ) ) * $w; | ||
| 282 | $self->{'gd_obj'}->stringUp( $font, $x, $y, $labels[$_], $textcolor ); | ||
| 283 | } | ||
| 284 | } | ||
| 285 | } | ||
| 286 | else | ||
| 287 | { # draw all ticks vertical | ||
| 288 | for ( 0 .. $#labels ) | ||
| 289 | { | ||
| 290 | if ( defined $labels[$_] ) | ||
| 291 | { | ||
| 292 | $x = $x_start + $delta * $_ - $h / 2; | ||
| 293 | $y = $y_start - ( $x_label_len - length( $labels[$_] ) ) * $w; | ||
| 294 | $self->{'gd_obj'}->stringUp( $font, $x, $y, $labels[$_], $textcolor ); | ||
| 295 | } | ||
| 296 | } | ||
| 297 | } | ||
| 298 | |||
| 299 | } | ||
| 300 | |||
| 301 | #update the borders | ||
| 302 | if ( $self->{'interval_ticks'} > 0 ) | ||
| 303 | { | ||
| 304 | if ( $self->{'x_ticks'} =~ /^normal$/i ) | ||
| 305 | { | ||
| 306 | $self->{'curr_y_max'} -= $h + $self->{'text_space'} * 2; | ||
| 307 | } | ||
| 308 | elsif ( $self->{'x_ticks'} =~ /^staggered$/i ) | ||
| 309 | { | ||
| 310 | $self->{'curr_y_max'} -= 2 * $h + 3 * $self->{'text_space'}; | ||
| 311 | } | ||
| 312 | elsif ( $self->{'x_ticks'} =~ /^vertical$/i ) | ||
| 313 | { | ||
| 314 | $self->{'curr_y_max'} -= $w * $x_label_len + $self->{'text_space'} * 2; | ||
| 315 | } | ||
| 316 | } | ||
| 317 | |||
| 318 | #draw the ticks | ||
| 319 | $y_start = $self->{'curr_y_max'}; | ||
| 320 | $y = $y_start - $self->{'tick_len'}; | ||
| 321 | |||
| 322 | if ( $self->{'skip_x_ticks'} > 1 ) | ||
| 323 | { | ||
| 324 | for ( 0 .. int( ($#labels) / $self->{'skip_x_ticks'} ) ) | ||
| 325 | { | ||
| 326 | $x = $x_start + $delta * ( $_ * $self->{'skip_x_ticks'} ); | ||
| 327 | $self->{'gd_obj'}->line( $x, $y_start, $x, $y, $misccolor ); | ||
| 328 | if ( $self->true( $self->{'grid_lines'} ) | ||
| 329 | or $self->true( $self->{'x_grid_lines'} ) ) | ||
| 330 | { | ||
| 331 | $self->{'grid_data'}->{'x'}->[$_] = $x; | ||
| 332 | } | ||
| 333 | } | ||
| 334 | } | ||
| 335 | elsif ( $self->{'custom_x_ticks'} ) | ||
| 336 | { | ||
| 337 | foreach ( @{ $self->{'custom_x_ticks'} } ) | ||
| 338 | { | ||
| 339 | if ( $_ <= $ticks ) | ||
| 340 | { | ||
| 341 | $x = $x_start + $delta * $_; | ||
| 342 | $self->{'gd_obj'}->line( $x, $y_start, $x, $y, $misccolor ); | ||
| 343 | if ( $self->true( $self->{'grid_lines'} ) | ||
| 344 | or $self->true( $self->{'x_grid_lines'} ) ) | ||
| 345 | { | ||
| 346 | $self->{'grid_data'}->{'x'}->[$_] = $x; | ||
| 347 | } | ||
| 348 | } | ||
| 349 | } | ||
| 350 | } | ||
| 351 | else | ||
| 352 | { | ||
| 353 | for ( 0 .. $#labels ) | ||
| 354 | { | ||
| 355 | $x = $x_start + $_ * $delta; | ||
| 356 | $self->{'gd_obj'}->line( $x, $y_start, $x, $y, $misccolor ); | ||
| 357 | if ( $self->true( $self->{'grid_lines'} ) | ||
| 358 | or $self->true( $self->{'x_grid_lines'} ) ) | ||
| 359 | { | ||
| 360 | $self->{'grid_data'}->{'x'}->[$_] = $x; | ||
| 361 | } | ||
| 362 | } | ||
| 363 | } | ||
| 364 | |||
| 365 | #another update of the borders | ||
| 366 | $self->{'curr_y_max'} -= $self->{'tick_len'} if $self->{'interval_ticks'} > 0; | ||
| 367 | |||
| 368 | #finally return | ||
| 369 | return; | ||
| 370 | } | ||
| 371 | |||
| 372 | ## @fn private _draw_x_ticks | ||
| 373 | # override the function implemented in base | ||
| 374 | sub _draw_x_ticks | ||
| 375 | { | ||
| 376 | my $self = shift; | ||
| 377 | |||
| 378 | #Use always the _draw_x_tick funktion because we always do a xy_plot!!! | ||
| 379 | $self->_draw_x_number_ticks(); | ||
| 380 | |||
| 381 | #and return | ||
| 382 | return 1; | ||
| 383 | } | ||
| 384 | |||
| 385 | ## @fn private _draw_y_ticks | ||
| 386 | # override the function implemented in base | ||
| 387 | sub _draw_y_ticks | ||
| 388 | { | ||
| 389 | my $self = shift; | ||
| 390 | my $side = shift || 'left'; | ||
| 391 | my $data = $self->{'dataref'}; | ||
| 392 | my $font = $self->{'tick_label_font'}; | ||
| 393 | my $textcolor = $self->_color_role_to_index('text'); | ||
| 394 | my $misccolor = $self->_color_role_to_index('misc'); | ||
| 395 | my @labels = @{ $self->{'y_tick_labels'} }; | ||
| 396 | my $num_points = $self->{'num_datapoints'}; | ||
| 397 | my ( $w, $h ); | ||
| 398 | my ( $x_start, $x, $y_start, $y, $start, $interval ); | ||
| 399 | my ( $height, $delta, $label, $lines, $label_len ); | ||
| 400 | my ( $s, $f ); | ||
| 401 | my $x_max = -0x80000000; | ||
| 402 | $self->{grid_data}->{'y'} = []; | ||
| 403 | $self->{grid_data}->{'y2'} = []; | ||
| 404 | |||
| 405 | # find the height | ||
| 406 | $height = $self->{'curr_y_max'} - $self->{'curr_y_min'}; | ||
| 407 | |||
| 408 | # make sure we got a real font | ||
| 409 | unless ( ( ref $font ) eq 'GD::Font' ) | ||
| 410 | { | ||
| 411 | croak "The tick label font you specified isn\'t a GD Font object"; | ||
| 412 | } | ||
| 413 | |||
| 414 | # find out how big the font is | ||
| 415 | ( $w, $h ) = ( $font->width, $font->height ); | ||
| 416 | |||
| 417 | #get the base variables | ||
| 418 | $interval = $self->{'interval'}; | ||
| 419 | $start = $self->{'start'}; | ||
| 420 | |||
| 421 | #find the biggest x value | ||
| 422 | foreach ( @{ $data->[0] } ) | ||
| 423 | { | ||
| 424 | if ( $_ > $x_max ) | ||
| 425 | { | ||
| 426 | $x_max = $_; | ||
| 427 | } | ||
| 428 | } | ||
| 429 | |||
| 430 | #calculate the number of lines and the length | ||
| 431 | $lines = int( ( ( $x_max - $start ) / $interval ) + 0.99999999999 ); | ||
| 432 | $lines = 1 if $lines == 0; | ||
| 433 | $label_len = length($lines); | ||
| 434 | |||
| 435 | #get the space between two lines | ||
| 436 | $delta = $height / $lines; | ||
| 437 | |||
| 438 | #now draw them | ||
| 439 | if ( $lines > 1 ) | ||
| 440 | { | ||
| 441 | if ( $side =~ /^right$/i ) | ||
| 442 | { | ||
| 443 | |||
| 444 | #get the starting point | ||
| 445 | $x_start = $self->{'curr_x_max'}; | ||
| 446 | $y_start = $self->{'curr_y_min'}; | ||
| 447 | |||
| 448 | #draw the labels | ||
| 449 | for $label ( 0 .. $lines - 1 ) | ||
| 450 | { | ||
| 451 | $x = $x_start - $self->{'text_space'} - $label_len * $w; | ||
| 452 | $y = $y_start + $label * $delta + $delta / 2 - $h / 2; | ||
| 453 | $self->{'gd_obj'}->string( $font, $x, $y, $label, $textcolor ); | ||
| 454 | } | ||
| 455 | |||
| 456 | #draw the ticks | ||
| 457 | for $label ( 0 .. $lines ) | ||
| 458 | { | ||
| 459 | $x = $x_start - $self->{'text_space'} * 2 - $label_len * $w - $self->{'tick_len'}; | ||
| 460 | $y = $y_start + $label * $delta; | ||
| 461 | $self->{'gd_obj'}->line( $x_start - $self->{'text_space'}, $y, $x, $y, $misccolor ); | ||
| 462 | |||
| 463 | #add data for grid_lines | ||
| 464 | push @{ $self->{grid_data}->{'y'} }, $y; | ||
| 465 | } | ||
| 466 | |||
| 467 | #update the borders | ||
| 468 | $self->{'curr_x_max'} = $x_start - $self->{'text_space'} * 2 - $label_len * $w - $self->{'tick_len'}; | ||
| 469 | |||
| 470 | } | ||
| 471 | |||
| 472 | elsif ( $side =~ /^both$/i ) | ||
| 473 | { | ||
| 474 | |||
| 475 | #get the starting point | ||
| 476 | $x_start = $self->{'curr_x_min'}; | ||
| 477 | $y_start = $self->{'curr_y_min'}; | ||
| 478 | |||
| 479 | #first the left side | ||
| 480 | #draw the labels | ||
| 481 | for $label ( 0 .. $lines - 1 ) | ||
| 482 | { | ||
| 483 | $x = $self->{'curr_x_min'} + $self->{'text_space'} * 2; | ||
| 484 | $y = $y_start + $label * $delta + $delta / 2 - $h / 2; | ||
| 485 | $self->{'gd_obj'}->string( $font, $x, $y, $self->{'f_y_tick'}->($label), $textcolor ); | ||
| 486 | } | ||
| 487 | |||
| 488 | #draw the ticks | ||
| 489 | for $label ( 0 .. $lines ) | ||
| 490 | { | ||
| 491 | $x = $x_start + $self->{'text_space'} * 2 + $label_len * $w + $self->{'tick_len'}; | ||
| 492 | $y = $y_start + $label * $delta; | ||
| 493 | $self->{'gd_obj'}->line( $x_start + $self->{'text_space'}, $y, $x, $y, $misccolor ); | ||
| 494 | } | ||
| 495 | |||
| 496 | #then the right side | ||
| 497 | #get the starting point | ||
| 498 | $x_start = $self->{'curr_x_max'}; | ||
| 499 | $y_start = $self->{'curr_y_min'}; | ||
| 500 | |||
| 501 | #draw the labels | ||
| 502 | for $label ( 0 .. $lines - 1 ) | ||
| 503 | { | ||
| 504 | $x = $x_start - $self->{'text_space'} - $label_len * $w; | ||
| 505 | $y = $y_start + $label * $delta + $delta / 2 - $h / 2; | ||
| 506 | $self->{'gd_obj'}->string( $font, $x, $y, $self->{'f_y_tick'}->($label), $textcolor ); | ||
| 507 | } | ||
| 508 | |||
| 509 | #draw the ticks | ||
| 510 | for $label ( 0 .. $lines ) | ||
| 511 | { | ||
| 512 | $x = $x_start - $self->{'text_space'} * 2 - $label_len * $w - $self->{'tick_len'}; | ||
| 513 | $y = $y_start + $label * $delta; | ||
| 514 | $self->{'gd_obj'}->line( $x_start - $self->{'text_space'}, $y, $x, $y, $misccolor ); | ||
| 515 | |||
| 516 | #add data for grid_lines | ||
| 517 | push @{ $self->{grid_data}->{'y'} }, $y; | ||
| 518 | } | ||
| 519 | |||
| 520 | #update the borders | ||
| 521 | $self->{'curr_x_min'} += $self->{'text_space'} * 2 + $label_len * $w + $self->{'tick_len'}; | ||
| 522 | $self->{'curr_x_max'} = $x_start - $self->{'text_space'} * 2 - $label_len * $w - $self->{'tick_len'}; | ||
| 523 | |||
| 524 | } | ||
| 525 | else | ||
| 526 | { | ||
| 527 | |||
| 528 | #get the starting point | ||
| 529 | $x_start = $self->{'curr_x_min'}; | ||
| 530 | $y_start = $self->{'curr_y_min'}; | ||
| 531 | |||
| 532 | #draw the labels | ||
| 533 | for $label ( 0 .. $lines - 1 ) | ||
| 534 | { | ||
| 535 | $x = $self->{'curr_x_min'} + $self->{'text_space'} * 2; | ||
| 536 | $y = $y_start + $label * $delta + $delta / 2 - $h / 2; | ||
| 537 | $self->{'gd_obj'}->string( $font, $x, $y, $self->{'f_y_tick'}->($label), $textcolor ); | ||
| 538 | } | ||
| 539 | |||
| 540 | #draw the ticks | ||
| 541 | for $label ( 0 .. $lines ) | ||
| 542 | { | ||
| 543 | $x = $x_start + $label_len * $w + $self->{'tick_len'} + $self->{'text_space'} * 3; | ||
| 544 | $y = $y_start + $label * $delta; | ||
| 545 | $self->{'gd_obj'}->line( $x_start + $self->{'text_space'}, $y, $x, $y, $misccolor ); | ||
| 546 | |||
| 547 | #this is also where we have to draw the grid_lines | ||
| 548 | push @{ $self->{grid_data}->{'y'} }, $y; | ||
| 549 | } | ||
| 550 | |||
| 551 | #update the borders | ||
| 552 | $self->{'curr_x_min'} = $x_start + $self->{'text_space'} * 3 + $label_len * $w; | ||
| 553 | } | ||
| 554 | |||
| 555 | } | ||
| 556 | |||
| 557 | #finally return | ||
| 558 | return 1; | ||
| 559 | } | ||
| 560 | |||
| 561 | ## @fn private _draw_data | ||
| 562 | # plot the data | ||
| 563 | sub _draw_data | ||
| 564 | { | ||
| 565 | my $self = shift; | ||
| 566 | my $data = $self->{'dataref'}; | ||
| 567 | my $misccolor = $self->_color_role_to_index('misc'); | ||
| 568 | my $num_points = $self->{'num_datapoints'}; | ||
| 569 | $num_points = 1 if $num_points == 0; | ||
| 570 | my $num_sets = $self->{'num_datasets'}; | ||
| 571 | $num_sets = 1 if $num_sets == 0; | ||
| 572 | my ( $lines, $split, $width, $height, $delta_lines, $delta_sets, $map, $last_line ); | ||
| 573 | my ( $akt_line, $akt_set, $akt_point, $color, $x_start, $y_start, $x, $y ); | ||
| 574 | my ( $x_last, $y_last, $delta_point, $brush, $mod, $x_interval, $start ); | ||
| 575 | my $i = 0; | ||
| 576 | my $interval = ( $self->{'max_val'} - $self->{'min_val'} ); | ||
| 577 | $interval = 1 if $interval == 0; | ||
| 578 | my $x_max = -0x80000000; | ||
| 579 | |||
| 580 | # find the height and the width | ||
| 581 | $width = $self->{'curr_x_max'} - $self->{'curr_x_min'}; | ||
| 582 | $width = 1 if $width == 0; | ||
| 583 | $height = $self->{'curr_y_max'} - $self->{'curr_y_min'}; | ||
| 584 | $height = 1 if $height == 0; | ||
| 585 | |||
| 586 | # init the imagemap data field if they asked for it | ||
| 587 | if ( $self->true( $self->{'imagemap'} ) ) | ||
| 588 | { | ||
| 589 | $self->{'imagemap_data'} = []; | ||
| 590 | } | ||
| 591 | |||
| 592 | #get the base values | ||
| 593 | $x_interval = $self->{'interval'}; | ||
| 594 | $x_interval = 1 if $x_interval == 0; | ||
| 595 | $start = $self->{'start'}; | ||
| 596 | |||
| 597 | #find the biggest x value | ||
| 598 | foreach ( @{ $data->[0] } ) | ||
| 599 | { | ||
| 600 | if ( $_ > $x_max ) | ||
| 601 | { | ||
| 602 | $x_max = $_; | ||
| 603 | } | ||
| 604 | } | ||
| 605 | |||
| 606 | #calculate the number of lines | ||
| 607 | $lines = int( ( ( $x_max - $start ) / $x_interval ) + 0.99999999999 ); | ||
| 608 | $lines = 1 if $lines == 0; | ||
| 609 | |||
| 610 | #find delta_lines for the space between the lines | ||
| 611 | #and delta_sets for the space of the datasets of one line | ||
| 612 | #and the delta_point for the space between the datapoints | ||
| 613 | $delta_lines = $height / $lines; | ||
| 614 | $delta_sets = $delta_lines / $num_sets; | ||
| 615 | $delta_point = $width / ($x_interval); | ||
| 616 | |||
| 617 | #find $map, for the y values | ||
| 618 | $map = $delta_sets / $interval; | ||
| 619 | |||
| 620 | #find the mod and the y_start value | ||
| 621 | #correct the start value, if scale is set! Otherwise the plot is to high or to low! | ||
| 622 | #The corecction, isn't perfect, but it does a good job in most cases. | ||
| 623 | if ( $self->{'min_val'} >= 0 ) | ||
| 624 | { | ||
| 625 | $mod = $self->{'min_val'}; | ||
| 626 | if ( $self->{'scale'} > 1 ) | ||
| 627 | { | ||
| 628 | $y_start = $self->{'curr_y_min'} + ( $interval * $map / 2 ) * ( $self->{'scale'} - 1 ); | ||
| 629 | } | ||
| 630 | else | ||
| 631 | { | ||
| 632 | $y_start = $self->{'curr_y_min'}; | ||
| 633 | } | ||
| 634 | } | ||
| 635 | elsif ( $self->{'max_val'} <= 0 ) | ||
| 636 | { | ||
| 637 | $mod = $self->{'min_val'}; | ||
| 638 | if ( $self->{'scale'} > 1 ) | ||
| 639 | { | ||
| 640 | $y_start = $self->{'curr_y_min'} + ( $interval * $map / 2 ) * ( $self->{'scale'} - 1 ); | ||
| 641 | } | ||
| 642 | else | ||
| 643 | { | ||
| 644 | $y_start = $self->{'curr_y_min'}; | ||
| 645 | } | ||
| 646 | } | ||
| 647 | else | ||
| 648 | { | ||
| 649 | $y_start = $self->{'curr_y_min'} + ( $map * $self->{'min_val'} ); | ||
| 650 | $mod = 0; | ||
| 651 | } | ||
| 652 | |||
| 653 | #The upper right corner is the point, where we start | ||
| 654 | $x_start = $self->{'curr_x_min'}; | ||
| 655 | |||
| 656 | #draw the lines | ||
| 657 | for $akt_set ( 0 .. $num_sets - 1 ) | ||
| 658 | { | ||
| 659 | for $akt_point ( 0 .. $self->{'num_datapoints'} - 1 ) | ||
| 660 | { | ||
| 661 | |||
| 662 | #get the color for this dataset | ||
| 663 | $color = $self->_color_role_to_index( 'dataset' . $akt_set ); | ||
| 664 | $brush = $self->_prepare_brush( $color, 'line' ); | ||
| 665 | $self->{'gd_obj'}->setBrush($brush); | ||
| 666 | |||
| 667 | #start with the first point at line number zero | ||
| 668 | $last_line = 0; | ||
| 669 | for $akt_line ( $last_line .. $lines - 1 ) | ||
| 670 | { | ||
| 671 | |||
| 672 | #update the last line. That makes it a little bit faster. | ||
| 673 | $last_line = $akt_line; | ||
| 674 | |||
| 675 | #Don't try to draw, if there is no data | ||
| 676 | if ( defined $data->[0][$akt_point] ) | ||
| 677 | { | ||
| 678 | if ( $data->[0][$akt_point] <= ( ( $akt_line + 1 ) * $x_interval + $start ) | ||
| 679 | && $data->[0][$akt_point] >= $akt_line * $x_interval + $start ) | ||
| 680 | { | ||
| 681 | |||
| 682 | #the current point | ||
| 683 | $x = $x_start + ( $data->[0][$akt_point] - ( $akt_line * $x_interval ) - ($start) ) * $delta_point; | ||
| 684 | $y = | ||
| 685 | $y_start + | ||
| 686 | $akt_line * $delta_lines + | ||
| 687 | $akt_set * $delta_sets + | ||
| 688 | $delta_sets - | ||
| 689 | ( $data->[ 1 + $akt_set ][$akt_point] - $mod ) * $map * $self->{'scale'}; | ||
| 690 | |||
| 691 | #draw the line | ||
| 692 | $self->{'gd_obj'}->line( $x_last, $y_last, $x, $y, gdBrushed ) if $akt_point != 0; | ||
| 693 | |||
| 694 | #calculate the start point for the next line | ||
| 695 | #first if the next point is in the same line | ||
| 696 | if ( defined( $data->[0][ $akt_point + 1 ] ) | ||
| 697 | && $data->[0][ $akt_point + 1 ] <= ( ( $akt_line + 1 ) * $x_interval + $start ) | ||
| 698 | && $data->[0][ $akt_point + 1 ] > $akt_line * $x_interval + $start ) | ||
| 699 | { | ||
| 700 | $x_last = $x; | ||
| 701 | $y_last = $y; | ||
| 702 | } | ||
| 703 | |||
| 704 | #second, if the next point is not in the same line | ||
| 705 | else | ||
| 706 | { | ||
| 707 | $x_last = $self->{'curr_x_min'}; | ||
| 708 | $y_last = | ||
| 709 | $y_start + | ||
| 710 | ( $akt_line + 1 ) * $delta_lines + | ||
| 711 | $akt_set * $delta_sets + | ||
| 712 | $delta_sets - | ||
| 713 | ( $data->[ 1 + $akt_set ][$akt_point] - $mod ) * $map * $self->{'scale'}; | ||
| 714 | } | ||
| 715 | |||
| 716 | # store the imagemap data if they asked for it | ||
| 717 | if ( $self->true( $self->{'imagemap'} ) ) | ||
| 718 | { | ||
| 719 | $self->{'imagemap_data'}->[$akt_set][ $akt_point - 1 ] = [ $x_last, $y_last ]; | ||
| 720 | $self->{'imagemap_data'}->[$akt_set][$akt_point] = [ $x, $y ]; | ||
| 721 | } | ||
| 722 | } | ||
| 723 | else | ||
| 724 | { #Go to the next line. Maybe the current point is in that line! | ||
| 725 | next; | ||
| 726 | } | ||
| 727 | } | ||
| 728 | else | ||
| 729 | { | ||
| 730 | if ( $self->true( $self->{'imagemap'} ) ) | ||
| 731 | { | ||
| 732 | $self->{'imagemap_data'}->[$akt_set][ $akt_point - 1 ] = [ undef(), undef() ]; | ||
| 733 | $self->{'imagemap_data'}->[$akt_set][$akt_point] = [ undef(), undef() ]; | ||
| 734 | } | ||
| 735 | } | ||
| 736 | } | ||
| 737 | } | ||
| 738 | } | ||
| 739 | |||
| 740 | $y_start = $self->{'curr_y_min'}; | ||
| 741 | |||
| 742 | #draw some nice little lines | ||
| 743 | for $akt_set ( 0 .. $num_sets - 1 ) | ||
| 744 | { | ||
| 745 | for $akt_line ( 0 .. $lines - 1 ) | ||
| 746 | { | ||
| 747 | |||
| 748 | #draw a line between the sets at the left side of the chart | ||
| 749 | $self->{'gd_obj'}->line( | ||
| 750 | $x_start, | ||
| 751 | $y_start + $akt_line * $delta_lines + $akt_set * $delta_sets, | ||
| 752 | $x_start + $self->{'tick_len'}, | ||
| 753 | $y_start + $akt_line * $delta_lines + $akt_set * $delta_sets, $misccolor | ||
| 754 | ); | ||
| 755 | |||
| 756 | #draw a line between the sets at the right side of the chart | ||
| 757 | $self->{'gd_obj'}->line( | ||
| 758 | $self->{'curr_x_max'}, | ||
| 759 | $y_start + $akt_line * $delta_lines + $akt_set * $delta_sets, | ||
| 760 | $self->{'curr_x_max'} - $self->{'tick_len'}, | ||
| 761 | $y_start + $akt_line * $delta_lines + $akt_set * $delta_sets, $misccolor | ||
| 762 | ); | ||
| 763 | } | ||
| 764 | } | ||
| 765 | |||
| 766 | #Box it off | ||
| 767 | $self->{'gd_obj'} | ||
| 768 | ->rectangle( $self->{'curr_x_min'}, $self->{'curr_y_min'}, $self->{'curr_x_max'}, $self->{'curr_y_max'}, $misccolor ); | ||
| 769 | |||
| 770 | #finally retrun | ||
| 771 | return; | ||
| 772 | } | ||
| 773 | |||
| 774 | #be a good modul and return 1 | ||
| 775 | 1; | ||
| 776 | |||