| 1 | 4 | ▶ | use v5.12; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 2 | |||
| 3 | package Chart::StackedBars; | ||
| 4 | our @ISA = qw(Chart::Base); | ||
| 5 | our $VERSION = 'v2.403.9'; | ||
| 6 | |||
| 7 | 0 | ▶ | use Chart::Base; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 8 | use GD; | ||
| 9 | use Carp; | ||
| 10 | |||
| 11 | #>>>>>>>>>>>>>>>>>>>>>>>>>># | ||
| 12 | # public methods go here # | ||
| 13 | #<<<<<<<<<<<<<<<<<<<<<<<<<<# | ||
| 14 | |||
| 15 | #>>>>>>>>>>>>>>>>>>>>>>>>>>># | ||
| 16 | # private methods go here # | ||
| 17 | #<<<<<<<<<<<<<<<<<<<<<<<<<<<# | ||
| 18 | |||
| 19 | ## @fn private _check_data | ||
| 20 | # override check_data to make sure we don't get datasets with positive | ||
| 21 | # and negative values mixed | ||
| 22 | sub _check_data | ||
| 23 | { | ||
| 24 | my $self = shift; | ||
| 25 | my $data = $self->{'dataref'}; | ||
| 26 | my $length = 0; | ||
| 27 | my ( $i, $j, $posneg ); | ||
| 28 | my $composite; | ||
| 29 | |||
| 30 | # remember the number of datasets | ||
| 31 | if ( defined $self->{'composite_info'} ) | ||
| 32 | { | ||
| 33 | if ( $self->{'composite_info'}[0][0] =~ /^StackedBars$/i ) | ||
| 34 | { | ||
| 35 | $composite = 0; | ||
| 36 | } | ||
| 37 | if ( $self->{'composite_info'}[1][0] =~ /^StackedBars$/i ) | ||
| 38 | { | ||
| 39 | $composite = 1; | ||
| 40 | } | ||
| 41 | |||
| 42 | # $self->{'num_datasets'} = $#{$data}; ### | ||
| 43 | |||
| 44 | $self->{'num_datasets'} = ( $#{ $self->{'composite_info'}[$composite][1] } ) + 1; | ||
| 45 | } | ||
| 46 | else | ||
| 47 | { | ||
| 48 | $self->{'num_datasets'} = $#{$data}; | ||
| 49 | } | ||
| 50 | |||
| 51 | # remember the number of points in the largest dataset | ||
| 52 | $self->{'num_datapoints'} = 0; | ||
| 53 | for ( 0 .. $self->{'num_datasets'} ) | ||
| 54 | { | ||
| 55 | if ( scalar( @{ $data->[$_] } ) > $self->{'num_datapoints'} ) | ||
| 56 | { | ||
| 57 | $self->{'num_datapoints'} = scalar( @{ $data->[$_] } ); | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | # make sure the datasets don't mix pos and neg values | ||
| 62 | for $i ( 0 .. $self->{'num_datapoints'} - 1 ) | ||
| 63 | { | ||
| 64 | $posneg = ''; | ||
| 65 | for $j ( 1 .. $self->{'num_datasets'} ) | ||
| 66 | { | ||
| 67 | if ( $data->[$j][$i] > 0 ) | ||
| 68 | { | ||
| 69 | if ( $posneg eq 'neg' ) | ||
| 70 | { | ||
| 71 | croak "The values for a Chart::StackedBars data point must either be all positive or all negative"; | ||
| 72 | } | ||
| 73 | else | ||
| 74 | { | ||
| 75 | $posneg = 'pos'; | ||
| 76 | } | ||
| 77 | } | ||
| 78 | elsif ( $data->[$j][$i] < 0 ) | ||
| 79 | { | ||
| 80 | if ( $posneg eq 'pos' ) | ||
| 81 | { | ||
| 82 | croak "The values for a Chart::StackedBars data point must either be all positive or all negative"; | ||
| 83 | } | ||
| 84 | else | ||
| 85 | { | ||
| 86 | $posneg = 'neg'; | ||
| 87 | } | ||
| 88 | } | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | # find good min and max y-values for the plot | ||
| 93 | $self->_find_y_scale; | ||
| 94 | |||
| 95 | # find the longest x-tick label | ||
| 96 | for ( @{ $data->[0] } ) | ||
| 97 | { | ||
| 98 | if ( length($_) > $length ) | ||
| 99 | { | ||
| 100 | $length = length($_); | ||
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 104 | # now store it in the object | ||
| 105 | $self->{'x_tick_label_length'} = $length; | ||
| 106 | |||
| 107 | return; | ||
| 108 | } | ||
| 109 | |||
| 110 | ## @fn private _find_y_range | ||
| 111 | sub _find_y_range | ||
| 112 | { | ||
| 113 | my $self = shift; | ||
| 114 | |||
| 115 | # This finds the minimum and maximum point-sum over all x points, | ||
| 116 | # where the point-sum is the sum of the dataset values for that point. | ||
| 117 | # If the y value in any dataset is undef for a given x, it simply | ||
| 118 | # adds nothing to the sum. | ||
| 119 | |||
| 120 | my $data = $self->{'dataref'}; | ||
| 121 | my $max = undef; | ||
| 122 | my $min = undef; | ||
| 123 | for my $i ( 0 .. $#{ $data->[0] } ) | ||
| 124 | { # data point | ||
| 125 | my $sum = $data->[1]->[$i] || 0; | ||
| 126 | for my $dataset ( @$data[ 2 .. $#$data ] ) | ||
| 127 | { # order not important | ||
| 128 | my $datum = $dataset->[$i]; | ||
| 129 | $sum += $datum if defined $datum; | ||
| 130 | } | ||
| 131 | if ( defined $max ) | ||
| 132 | { | ||
| 133 | if ( $sum > $max ) { $max = $sum } | ||
| 134 | elsif ( $sum < $min ) { $min = $sum } | ||
| 135 | } | ||
| 136 | else { $min = $max = $sum } | ||
| 137 | } | ||
| 138 | |||
| 139 | # make sure all-positive or all-negative charts get anchored at | ||
| 140 | # zero so that we don't cut out some parts of the bars | ||
| 141 | if ( ( $max > 0 ) && ( $min > 0 ) ) | ||
| 142 | { | ||
| 143 | $min = 0; | ||
| 144 | } | ||
| 145 | if ( ( $min < 0 ) && ( $max < 0 ) ) | ||
| 146 | { | ||
| 147 | $max = 0; | ||
| 148 | } | ||
| 149 | |||
| 150 | ( $min, $max ); | ||
| 151 | } | ||
| 152 | |||
| 153 | # ## override _find_y_scale to account for stacked bars | ||
| 154 | # sub _find_y_scale { | ||
| 155 | # my $self = shift; | ||
| 156 | # my $raw = $self->{'dataref'}; | ||
| 157 | # my $data = [@{$raw->[1]}]; | ||
| 158 | # my ($i, $j, $max, $min); | ||
| 159 | # my ($order, $mult, $tmp); | ||
| 160 | # my ($range, $delta, @dec, $y_ticks); | ||
| 161 | # my $labels = []; | ||
| 162 | # my $length = 0; | ||
| 163 | # | ||
| 164 | # # use realy weird max and min values | ||
| 165 | # $max = -999999999999; | ||
| 166 | # $min = 999999999999; | ||
| 167 | # | ||
| 168 | # # go through and stack them | ||
| 169 | # for $i (0..$self->{'num_datapoints'}-1) { | ||
| 170 | # for $j (2..$self->{'num_datasets'}) { | ||
| 171 | # $data->[$i] += $raw->[$j][$i]; | ||
| 172 | # } | ||
| 173 | # } | ||
| 174 | # | ||
| 175 | # # get max and min values | ||
| 176 | # for $i (0..$self->{'num_datapoints'}-1) { | ||
| 177 | # if ($data->[$i] > $max) { | ||
| 178 | # $max = $data->[$i]; | ||
| 179 | # } | ||
| 180 | # if ($data->[$i] < $min) { | ||
| 181 | # $min = $data->[$i]; | ||
| 182 | # } | ||
| 183 | # } | ||
| 184 | # | ||
| 185 | # # make sure all-positive or all-negative charts get anchored at | ||
| 186 | # # zero so that we don't cut out some parts of the bars | ||
| 187 | # if (($max > 0) && ($min > 0)) { | ||
| 188 | # $min = 0; | ||
| 189 | # } | ||
| 190 | # if (($min < 0) && ($max < 0)) { | ||
| 191 | # $max = 0; | ||
| 192 | # } | ||
| 193 | # | ||
| 194 | # # calculate good max value | ||
| 195 | # if ($max < -10) { | ||
| 196 | # $tmp = -$max; | ||
| 197 | # $order = int((log $tmp) / (log 10)); | ||
| 198 | # $mult = int ($tmp / (10 ** $order)); | ||
| 199 | # $tmp = ($mult - 1) * (10 ** $order); | ||
| 200 | # $max = -$tmp; | ||
| 201 | # } | ||
| 202 | # elsif ($max < 0) { | ||
| 203 | # $max = 0; | ||
| 204 | # } | ||
| 205 | # elsif ($max > 10) { | ||
| 206 | # $order = int((log $max) / (log 10)); | ||
| 207 | # $mult = int ($max / (10 ** $order)); | ||
| 208 | # $max = ($mult + 1) * (10 ** $order); | ||
| 209 | # } | ||
| 210 | # elsif ($max >= 0) { | ||
| 211 | # $max = 10; | ||
| 212 | # } | ||
| 213 | # | ||
| 214 | # # now go for a good min | ||
| 215 | # if ($min < -10) { | ||
| 216 | # $tmp = -$min; | ||
| 217 | # $order = int((log $tmp) / (log 10)); | ||
| 218 | # $mult = int ($tmp / (10 ** $order)); | ||
| 219 | # $tmp = ($mult + 1) * (10 ** $order); | ||
| 220 | # $min = -$tmp; | ||
| 221 | # } | ||
| 222 | # elsif ($min < 0) { | ||
| 223 | # $min = -10; | ||
| 224 | # } | ||
| 225 | # elsif ($min > 10) { | ||
| 226 | # $order = int ((log $min) / (log 10)); | ||
| 227 | # $mult = int ($min / (10 ** $order)); | ||
| 228 | # $min = $mult * (10 ** $order); | ||
| 229 | # } | ||
| 230 | # elsif ($min >= 0) { | ||
| 231 | # $min = 0; | ||
| 232 | # } | ||
| 233 | # | ||
| 234 | # # put the appropriate min and max values into the object if necessary | ||
| 235 | # unless (defined ($self->{'max_val'})) { | ||
| 236 | # $self->{'max_val'} = $max; | ||
| 237 | # } | ||
| 238 | # unless (defined ($self->{'min_val'})) { | ||
| 239 | # $self->{'min_val'} = $min; | ||
| 240 | # } | ||
| 241 | # | ||
| 242 | # # generate the y_tick labels, store them in the object | ||
| 243 | # # figure out which one is going to be the longest | ||
| 244 | # $range = $self->{'max_val'} - $self->{'min_val'}; | ||
| 245 | # $y_ticks = $self->{'y_ticks'} - 1; | ||
| 246 | # ## Don't adjust y_ticks if the user specified custom labels | ||
| 247 | # if ($self->{'integer_ticks_only'} =~ /^true$/i && ! $self->{'y_tick_labels'}) { | ||
| 248 | # unless (($range % $y_ticks) == 0) { | ||
| 249 | # while (($range % $y_ticks) != 0) { | ||
| 250 | # $y_ticks++; | ||
| 251 | # } | ||
| 252 | # $self->{'y_ticks'} = $y_ticks + 1; | ||
| 253 | # } | ||
| 254 | # } | ||
| 255 | # | ||
| 256 | # $delta = $range / $y_ticks; | ||
| 257 | # for (0..$y_ticks) { | ||
| 258 | # $tmp = $self->{'min_val'} + ($delta * $_); | ||
| 259 | # @dec = split /\./, $tmp; | ||
| 260 | # if ($dec[1] && (length($dec[1]) > 3)) { | ||
| 261 | # $tmp = sprintf("%.3f", $tmp); | ||
| 262 | # } | ||
| 263 | # $labels->[$_] = $tmp; | ||
| 264 | # if (length($tmp) > $length) { | ||
| 265 | # $length = length($tmp); | ||
| 266 | # } | ||
| 267 | # } | ||
| 268 | # | ||
| 269 | # # store it in the object | ||
| 270 | # $self->{'y_tick_labels'} = $labels; | ||
| 271 | # $self->{'y_tick_label_length'} = $length; | ||
| 272 | # | ||
| 273 | # # and return | ||
| 274 | # return; | ||
| 275 | # } | ||
| 276 | |||
| 277 | ## @fn private _draw_data | ||
| 278 | # finally get around to plotting the data | ||
| 279 | sub _draw_data | ||
| 280 | { | ||
| 281 | my $self = shift; | ||
| 282 | my $raw = $self->{'dataref'}; | ||
| 283 | my $data = []; | ||
| 284 | my $misccolor = $self->_color_role_to_index('misc'); | ||
| 285 | my ( $width, $height, $delta, $map, $mod ); | ||
| 286 | my ( $x1, $y1, $x2, $y2, $x3, $y3, $i, $j, $color, $cut ); | ||
| 287 | my $pink = $self->{'gd_obj'}->colorAllocate( 255, 0, 255 ); | ||
| 288 | |||
| 289 | # init the imagemap data field if they want it | ||
| 290 | if ( $self->true( $self->{'imagemap'} ) ) | ||
| 291 | { | ||
| 292 | $self->{'imagemap_data'} = []; | ||
| 293 | } | ||
| 294 | |||
| 295 | # width and height of remaining area, delta for width of bars, mapping value | ||
| 296 | $width = $self->{'curr_x_max'} - $self->{'curr_x_min'}; | ||
| 297 | |||
| 298 | if ( $self->true( $self->{'spaced_bars'} ) ) | ||
| 299 | { | ||
| 300 | $delta = ( $width / ( $self->{'num_datapoints'} * 2 ) ); | ||
| 301 | } | ||
| 302 | else | ||
| 303 | { | ||
| 304 | $delta = $width / $self->{'num_datapoints'}; | ||
| 305 | } | ||
| 306 | $height = $self->{'curr_y_max'} - $self->{'curr_y_min'}; | ||
| 307 | $map = $height / ( $self->{'max_val'} - $self->{'min_val'} ); | ||
| 308 | |||
| 309 | # get the base x and y values | ||
| 310 | $x1 = $self->{'curr_x_min'}; | ||
| 311 | if ( $self->{'min_val'} >= 0 ) | ||
| 312 | { | ||
| 313 | $y1 = $self->{'curr_y_max'}; | ||
| 314 | $mod = $self->{'min_val'}; | ||
| 315 | } | ||
| 316 | elsif ( $self->{'max_val'} <= 0 ) | ||
| 317 | { | ||
| 318 | $y1 = $self->{'curr_y_min'}; | ||
| 319 | $mod = $self->{'max_val'}; | ||
| 320 | } | ||
| 321 | else | ||
| 322 | { | ||
| 323 | $y1 = $self->{'curr_y_min'} + ( $map * $self->{'max_val'} ); | ||
| 324 | $mod = 0; | ||
| 325 | $self->{'gd_obj'}->line( $self->{'curr_x_min'}, $y1, $self->{'curr_x_max'}, $y1, $misccolor ); | ||
| 326 | } | ||
| 327 | |||
| 328 | # create another copy of the data, but stacked | ||
| 329 | $data->[1] = [ @{ $raw->[1] } ]; | ||
| 330 | for $i ( 0 .. $self->{'num_datapoints'} - 1 ) | ||
| 331 | { | ||
| 332 | for $j ( 2 .. $self->{'num_datasets'} ) | ||
| 333 | { | ||
| 334 | $data->[$j][$i] = $data->[ $j - 1 ][$i] + $raw->[$j][$i]; | ||
| 335 | } | ||
| 336 | } | ||
| 337 | |||
| 338 | # draw the damn bars | ||
| 339 | for $i ( 0 .. $self->{'num_datapoints'} - 1 ) | ||
| 340 | { | ||
| 341 | |||
| 342 | # init the y values for this datapoint | ||
| 343 | $y2 = $y1; | ||
| 344 | |||
| 345 | for $j ( 1 .. $self->{'num_datasets'} ) | ||
| 346 | { | ||
| 347 | |||
| 348 | # get the color | ||
| 349 | $color = $self->_color_role_to_index( 'dataset' . ( $j - 1 ) ); | ||
| 350 | |||
| 351 | # set up the geometry for the bar | ||
| 352 | if ( $self->true( $self->{'spaced_bars'} ) ) | ||
| 353 | { | ||
| 354 | $x2 = $x1 + ( 2 * $i * $delta ) + ( $delta / 2 ); | ||
| 355 | $x3 = $x2 + $delta; | ||
| 356 | |||
| 357 | } | ||
| 358 | else | ||
| 359 | { | ||
| 360 | $x2 = $x1 + ( $i * $delta ); | ||
| 361 | $x3 = $x2 + $delta; | ||
| 362 | } | ||
| 363 | $y3 = $y1 - ( ( $data->[$j][$i] - $mod ) * $map ); | ||
| 364 | |||
| 365 | #cut the bars off, if needed | ||
| 366 | if ( $data->[$j][$i] > $self->{'max_val'} ) | ||
| 367 | { | ||
| 368 | $y3 = $y1 - ( ( $self->{'max_val'} - $mod ) * $map ); | ||
| 369 | $cut = 1; | ||
| 370 | } | ||
| 371 | elsif ( $data->[$j][$i] < $self->{'min_val'} ) | ||
| 372 | { | ||
| 373 | $y3 = $y1 - ( ( $self->{'min_val'} - $mod ) * $map ); | ||
| 374 | $cut = 1; | ||
| 375 | } | ||
| 376 | else | ||
| 377 | { | ||
| 378 | $cut = 0; | ||
| 379 | } | ||
| 380 | |||
| 381 | # draw the bar | ||
| 382 | ## y2 and y3 are reversed in some cases because GD's fill | ||
| 383 | ## algorithm is lame | ||
| 384 | if ( $data->[$j][$i] > 0 ) | ||
| 385 | { | ||
| 386 | $self->{'gd_obj'}->filledRectangle( $x2, $y3, $x3, $y2, $color ); | ||
| 387 | if ( $self->true( $self->{'imagemap'} ) ) | ||
| 388 | { | ||
| 389 | $self->{'imagemap_data'}->[$j][$i] = [ $x2, $y3, $x3, $y2 ]; | ||
| 390 | } | ||
| 391 | } | ||
| 392 | else | ||
| 393 | { | ||
| 394 | $self->{'gd_obj'}->filledRectangle( $x2, $y2, $x3, $y3, $color ); | ||
| 395 | if ( $self->true( $self->{'imagemap'} ) ) | ||
| 396 | { | ||
| 397 | $self->{'imagemap_data'}->[$j][$i] = [ $x2, $y2, $x3, $y3 ]; | ||
| 398 | } | ||
| 399 | } | ||
| 400 | |||
| 401 | # now outline it. outline red if the bar had been cut off | ||
| 402 | unless ($cut) | ||
| 403 | { | ||
| 404 | $self->{'gd_obj'}->rectangle( $x2, $y2, $x3, $y3, $misccolor ); | ||
| 405 | } | ||
| 406 | else | ||
| 407 | { | ||
| 408 | $self->{'gd_obj'}->rectangle( $x2, $y2, $x3, $y3, $misccolor ); | ||
| 409 | $self->{'gd_obj'}->rectangle( $x2, $y1, $x3, $y3, $pink ); | ||
| 410 | } | ||
| 411 | |||
| 412 | # now bootstrap the y values | ||
| 413 | $y2 = $y3; | ||
| 414 | } | ||
| 415 | } | ||
| 416 | |||
| 417 | # and finaly box it off | ||
| 418 | $self->{'gd_obj'} | ||
| 419 | ->rectangle( $self->{'curr_x_min'}, $self->{'curr_y_min'}, $self->{'curr_x_max'}, $self->{'curr_y_max'}, $misccolor ); | ||
| 420 | return; | ||
| 421 | |||
| 422 | } | ||
| 423 | |||
| 424 | ## @fn private _draw_left_legend | ||
| 425 | sub _draw_left_legend | ||
| 426 | { | ||
| 427 | my $self = shift; | ||
| 428 | my @labels = @{ $self->{'legend_labels'} }; | ||
| 429 | my ( $x1, $x2, $x3, $y1, $y2, $width, $color, $misccolor, $w, $h, $brush ); | ||
| 430 | my $font = $self->{'legend_font'}; | ||
| 431 | |||
| 432 | # make sure we're using a real font | ||
| 433 | unless ( ( ref($font) ) eq 'GD::Font' ) | ||
| 434 | { | ||
| 435 | croak "The subtitle font you specified isn\'t a GD Font object"; | ||
| 436 | } | ||
| 437 | |||
| 438 | # get the size of the font | ||
| 439 | ( $h, $w ) = ( $font->height, $font->width ); | ||
| 440 | |||
| 441 | # get the miscellaneous color | ||
| 442 | $misccolor = $self->_color_role_to_index('misc'); | ||
| 443 | |||
| 444 | # find out how wide the largest label is | ||
| 445 | $width = | ||
| 446 | ( 2 * $self->{'text_space'} ) + | ||
| 447 | ( $self->{'max_legend_label'} * $w ) + | ||
| 448 | $self->{'legend_example_size'} + | ||
| 449 | ( 2 * $self->{'legend_space'} ); | ||
| 450 | |||
| 451 | # get some base x-y coordinates | ||
| 452 | $x1 = $self->{'curr_x_min'}; | ||
| 453 | $x2 = $self->{'curr_x_min'} + $width; | ||
| 454 | $y1 = $self->{'curr_y_min'} + $self->{'graph_border'}; | ||
| 455 | $y2 = | ||
| 456 | $self->{'curr_y_min'} + | ||
| 457 | $self->{'graph_border'} + | ||
| 458 | $self->{'text_space'} + | ||
| 459 | ( $self->{'num_datasets'} * ( $h + $self->{'text_space'} ) ) + | ||
| 460 | ( 2 * $self->{'legend_space'} ); | ||
| 461 | |||
| 462 | # box the legend off | ||
| 463 | $self->{'gd_obj'}->rectangle( $x1, $y1, $x2, $y2, $misccolor ); | ||
| 464 | |||
| 465 | # leave that nice space inside the legend box | ||
| 466 | $x1 += $self->{'legend_space'}; | ||
| 467 | $y1 += $self->{'legend_space'} + $self->{'text_space'}; | ||
| 468 | |||
| 469 | # now draw the actual legend | ||
| 470 | for ( 0 .. $#labels ) | ||
| 471 | { | ||
| 472 | |||
| 473 | # get the color | ||
| 474 | my $c = $self->{'num_datasets'} - $_ - 1; | ||
| 475 | |||
| 476 | # color of the datasets in the legend | ||
| 477 | if ( $self->{'dataref'}[1][0] < 0 ) | ||
| 478 | { | ||
| 479 | $color = $self->_color_role_to_index( 'dataset' . $_ ); | ||
| 480 | } | ||
| 481 | else | ||
| 482 | { | ||
| 483 | $color = $self->_color_role_to_index( 'dataset' . $c ); | ||
| 484 | } | ||
| 485 | |||
| 486 | # find the x-y coords | ||
| 487 | $x2 = $x1; | ||
| 488 | $x3 = $x2 + $self->{'legend_example_size'}; | ||
| 489 | $y2 = $y1 + ( $_ * ( $self->{'text_space'} + $h ) ) + $h / 2; | ||
| 490 | |||
| 491 | # do the line first | ||
| 492 | $self->{'gd_obj'}->line( $x2, $y2, $x3, $y2, $color ); | ||
| 493 | |||
| 494 | # reset the brush for points | ||
| 495 | $brush = $self->_prepare_brush( $color, 'point', $self->{ 'pointStyle' . $_ } ); | ||
| 496 | $self->{'gd_obj'}->setBrush($brush); | ||
| 497 | |||
| 498 | # draw the point | ||
| 499 | $self->{'gd_obj'}->line( int( ( $x3 + $x2 ) / 2 ), $y2, int( ( $x3 + $x2 ) / 2 ), $y2, gdBrushed ); | ||
| 500 | |||
| 501 | # now the label | ||
| 502 | $x2 = $x3 + ( 2 * $self->{'text_space'} ); | ||
| 503 | $y2 -= $h / 2; | ||
| 504 | |||
| 505 | # order of the datasets in the legend | ||
| 506 | if ( $self->{'dataref'}[1][0] < 0 ) | ||
| 507 | { | ||
| 508 | $self->{'gd_obj'}->string( $font, $x2, $y2, $labels[$_], $color ); | ||
| 509 | } | ||
| 510 | else | ||
| 511 | { | ||
| 512 | $self->{'gd_obj'}->string( $font, $x2, $y2, $labels[$c], $color ); | ||
| 513 | } | ||
| 514 | } | ||
| 515 | |||
| 516 | # mark off the used space | ||
| 517 | $self->{'curr_x_min'} += $width; | ||
| 518 | |||
| 519 | # and return | ||
| 520 | return 1; | ||
| 521 | } | ||
| 522 | |||
| 523 | ## @fn private _draw_right_legend | ||
| 524 | sub _draw_right_legend | ||
| 525 | { | ||
| 526 | my $self = shift; | ||
| 527 | my @labels = @{ $self->{'legend_labels'} }; | ||
| 528 | my ( $x1, $x2, $x3, $y1, $y2, $width, $color, $misccolor, $w, $h, $brush ); | ||
| 529 | my $font = $self->{'legend_font'}; | ||
| 530 | |||
| 531 | # make sure we're using a real font | ||
| 532 | unless ( ( ref($font) ) eq 'GD::Font' ) | ||
| 533 | { | ||
| 534 | croak "The subtitle font you specified isn\'t a GD Font object"; | ||
| 535 | } | ||
| 536 | |||
| 537 | # get the size of the font | ||
| 538 | ( $h, $w ) = ( $font->height, $font->width ); | ||
| 539 | |||
| 540 | # get the miscellaneous color | ||
| 541 | $misccolor = $self->_color_role_to_index('misc'); | ||
| 542 | |||
| 543 | # find out how wide the largest label is | ||
| 544 | $width = | ||
| 545 | ( 2 * $self->{'text_space'} ) + | ||
| 546 | ( $self->{'max_legend_label'} * $w ) + | ||
| 547 | $self->{'legend_example_size'} + | ||
| 548 | ( 2 * $self->{'legend_space'} ); | ||
| 549 | |||
| 550 | # get some starting x-y values | ||
| 551 | $x1 = $self->{'curr_x_max'} - $width; | ||
| 552 | $x2 = $self->{'curr_x_max'}; | ||
| 553 | $y1 = $self->{'curr_y_min'} + $self->{'graph_border'}; | ||
| 554 | $y2 = | ||
| 555 | $self->{'curr_y_min'} + | ||
| 556 | $self->{'graph_border'} + | ||
| 557 | $self->{'text_space'} + | ||
| 558 | ( $self->{'num_datasets'} * ( $h + $self->{'text_space'} ) ) + | ||
| 559 | ( 2 * $self->{'legend_space'} ); | ||
| 560 | |||
| 561 | # box the legend off | ||
| 562 | $self->{'gd_obj'}->rectangle( $x1, $y1, $x2, $y2, $misccolor ); | ||
| 563 | |||
| 564 | # leave that nice space inside the legend box | ||
| 565 | $x1 += $self->{'legend_space'}; | ||
| 566 | $y1 += $self->{'legend_space'} + $self->{'text_space'}; | ||
| 567 | |||
| 568 | # now draw the actual legend | ||
| 569 | for ( 0 .. $#labels ) | ||
| 570 | { | ||
| 571 | |||
| 572 | # get the color | ||
| 573 | my $c = $self->{'num_datasets'} - $_ - 1; | ||
| 574 | |||
| 575 | # color of the datasets in the legend | ||
| 576 | |||
| 577 | if ( $self->{'dataref'}[1][0] < 0 ) | ||
| 578 | { | ||
| 579 | $color = $self->_color_role_to_index( 'dataset' . $_ ); | ||
| 580 | } | ||
| 581 | else | ||
| 582 | { | ||
| 583 | $color = $self->_color_role_to_index( 'dataset' . $c ); | ||
| 584 | } | ||
| 585 | |||
| 586 | # find the x-y coords | ||
| 587 | $x2 = $x1; | ||
| 588 | $x3 = $x2 + $self->{'legend_example_size'}; | ||
| 589 | $y2 = $y1 + ( $_ * ( $self->{'text_space'} + $h ) ) + $h / 2; | ||
| 590 | |||
| 591 | # do the line first | ||
| 592 | $self->{'gd_obj'}->line( $x2, $y2, $x3, $y2, $color ); | ||
| 593 | |||
| 594 | # reset the brush for points | ||
| 595 | $brush = $self->_prepare_brush( $color, 'point', $self->{ 'pointStyle' . $_ } ); | ||
| 596 | $self->{'gd_obj'}->setBrush($brush); | ||
| 597 | |||
| 598 | # draw the point | ||
| 599 | $self->{'gd_obj'}->line( int( ( $x3 + $x2 ) / 2 ), $y2, int( ( $x3 + $x2 ) / 2 ), $y2, gdBrushed ); | ||
| 600 | |||
| 601 | # now the label | ||
| 602 | $x2 = $x3 + ( 2 * $self->{'text_space'} ); | ||
| 603 | $y2 -= $h / 2; | ||
| 604 | |||
| 605 | # order of the datasets in the legend | ||
| 606 | if ( $self->{'dataref'}[1][0] < 0 ) | ||
| 607 | { | ||
| 608 | $self->{'gd_obj'}->string( $font, $x2, $y2, $labels[$_], $color ); | ||
| 609 | } | ||
| 610 | else | ||
| 611 | { | ||
| 612 | $self->{'gd_obj'}->string( $font, $x2, $y2, $labels[$c], $color ); | ||
| 613 | } | ||
| 614 | } | ||
| 615 | |||
| 616 | # mark off the used space | ||
| 617 | $self->{'curr_x_max'} -= $width; | ||
| 618 | |||
| 619 | # and return | ||
| 620 | return 1; | ||
| 621 | } | ||
| 622 | |||
| 623 | ## be a good module and return 1 | ||
| 624 | 1; | ||