| 1 | #========================================================================== | ||
| 2 | # Copyright (c) 1995-1998 Martien Verbruggen | ||
| 3 | #-------------------------------------------------------------------------- | ||
| 4 | # | ||
| 5 | # Name: | ||
| 6 | # GD::Graph::axestype.pm | ||
| 7 | # | ||
| 8 | # $Id: axestype.pm,v 1.46 2007/04/27 05:26:47 ben Exp $ | ||
| 9 | # | ||
| 10 | #========================================================================== | ||
| 11 | |||
| 12 | package GD::Graph::axestype; | ||
| 13 | |||
| 14 | ($GD::Graph::axestype::VERSION) = '$Revision: 1.46 $' =~ /\s([\d.]+)/; | ||
| 15 | |||
| 16 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 17 | |||
| 18 | 1 | ▶ | use GD::Graph; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 19 | use GD::Graph::utils qw(:all); | ||
| 20 | use Carp; | ||
| 21 | |||
| 22 | @GD::Graph::axestype::ISA = qw(GD::Graph); | ||
| 23 | |||
| 24 | use constant PI => 4 * atan2(1,1); | ||
| 25 | |||
| 26 | my %Defaults = ( | ||
| 27 | |||
| 28 | # Set the length for the 'short' ticks on the axes. | ||
| 29 | x_tick_length => 4, | ||
| 30 | y_tick_length => 4, | ||
| 31 | |||
| 32 | # Do you want ticks to span the entire width of the graph? | ||
| 33 | x_long_ticks => 0, | ||
| 34 | y_long_ticks => 0, | ||
| 35 | |||
| 36 | # Number of ticks for the y axis | ||
| 37 | y_tick_number => 5, | ||
| 38 | x_tick_number => undef, # CONTRIB Scott Prahl | ||
| 39 | x_tick_offset => 0, # CONTRIB Damon Brodi | ||
| 40 | |||
| 41 | # Skip every nth label. if 1 will print every label on the axes, | ||
| 42 | # if 2 will print every second, etc.. | ||
| 43 | x_label_skip => 1, | ||
| 44 | y_label_skip => 1, | ||
| 45 | |||
| 46 | # Do we want ticks on the x axis? | ||
| 47 | x_ticks => 1, | ||
| 48 | x_all_ticks => 0, | ||
| 49 | |||
| 50 | # Where to place the x and y labels | ||
| 51 | x_label_position => 3/4, | ||
| 52 | y_label_position => 1/2, | ||
| 53 | |||
| 54 | # vertical printing of x labels | ||
| 55 | x_labels_vertical => 0, | ||
| 56 | |||
| 57 | # Draw axes as a box? (otherwise just left and bottom) | ||
| 58 | box_axis => 1, | ||
| 59 | |||
| 60 | # Disable axes? | ||
| 61 | # undef -> all axes, 0 -> Only line for bars, other -> no axes at all. | ||
| 62 | no_axes => undef, | ||
| 63 | |||
| 64 | # Use two different axes for the first and second dataset. The first | ||
| 65 | # will be displayed using the left axis, the second using the right | ||
| 66 | # axis. You cannot use more than two datasets when this option is on. | ||
| 67 | two_axes => 0, | ||
| 68 | |||
| 69 | # Which axis to use for each dataset. This only is in effect when | ||
| 70 | # two_axes is true. The axis number will wrap around, just like | ||
| 71 | # the dclrs array. | ||
| 72 | use_axis => [1, 2], | ||
| 73 | |||
| 74 | # Print values on the axes? | ||
| 75 | x_plot_values => 1, | ||
| 76 | y_plot_values => 1, | ||
| 77 | |||
| 78 | # Space between axis and text | ||
| 79 | axis_space => 4, | ||
| 80 | |||
| 81 | # Do you want bars to be drawn on top of each other, or side by side? | ||
| 82 | overwrite => 0, | ||
| 83 | |||
| 84 | # This will replace 'overwrite = 2'. For now, it is hardcoded to set | ||
| 85 | # overwrite to 2 | ||
| 86 | cumulate => 0, | ||
| 87 | |||
| 88 | # Do you want me to correct the width of the graph, so that bars are | ||
| 89 | # always drawn with a nice integer number of pixels? | ||
| 90 | # | ||
| 91 | # The GD::Graph::bars::initialise sub will switch this on. | ||
| 92 | # Do not set this to anything else than undef! | ||
| 93 | correct_width => undef, | ||
| 94 | |||
| 95 | # XXX The following two need to get better defaults. Maybe computed. | ||
| 96 | # Draw the zero axis in the graph in case there are negative values | ||
| 97 | zero_axis => 0, | ||
| 98 | |||
| 99 | # Draw the zero axis, but do not draw the bottom axis, in case | ||
| 100 | # box-axis == 0 | ||
| 101 | # This also moves the x axis labels to the zero axis | ||
| 102 | zero_axis_only => 0, | ||
| 103 | |||
| 104 | # Size of the legend markers | ||
| 105 | legend_marker_height => 8, | ||
| 106 | legend_marker_width => 12, | ||
| 107 | legend_spacing => 4, | ||
| 108 | legend_placement => 'BC', # '[BR][LCR]' | ||
| 109 | lg_cols => undef, | ||
| 110 | |||
| 111 | # Display the y values above the bar or point in the graph. | ||
| 112 | show_values => undef, | ||
| 113 | values_vertical => undef, # vertical? | ||
| 114 | values_space => 4, # extra spacing | ||
| 115 | values_format => undef, # how to format the value | ||
| 116 | |||
| 117 | # Draw the X axis left and the y1 axis at the bottom (y2 at top) | ||
| 118 | rotate_chart => undef, | ||
| 119 | |||
| 120 | # CONTRIB Edwin Hildebrand | ||
| 121 | # How narrow is a dataset allowed to become before we drop the | ||
| 122 | # accents? | ||
| 123 | accent_treshold => 4, | ||
| 124 | |||
| 125 | # Format of the numbers on the x and y axis | ||
| 126 | y_number_format => undef, | ||
| 127 | y1_number_format => undef, # CONTRIB Andrew OBrien | ||
| 128 | y2_number_format => undef, # CONTRIB Andrew OBrien | ||
| 129 | x_number_format => undef, # CONTRIB Scott Prahl | ||
| 130 | |||
| 131 | # and some attributes without default values | ||
| 132 | x_label => undef, | ||
| 133 | y_label => undef, | ||
| 134 | y1_label => undef, | ||
| 135 | y2_label => undef, | ||
| 136 | x_min_value => undef, | ||
| 137 | x_max_value => undef, | ||
| 138 | y_min_value => undef, | ||
| 139 | y1_min_value => undef, | ||
| 140 | y2_min_value => undef, | ||
| 141 | y_max_value => undef, | ||
| 142 | y1_max_value => undef, | ||
| 143 | y2_max_value => undef, | ||
| 144 | y_min_range => undef, # CONTRIB Ben Tilly | ||
| 145 | y1_min_range => undef, | ||
| 146 | y2_min_range => undef, | ||
| 147 | |||
| 148 | borderclrs => undef, | ||
| 149 | |||
| 150 | aa => 0, | ||
| 151 | alpha => 0, | ||
| 152 | |||
| 153 | # XXX | ||
| 154 | # Multiple inheritance (linespoints and mixed) finally bit me. The | ||
| 155 | # _has_defaults and set methods can only work correctly when the | ||
| 156 | # spot where the defaults are kept are in a mutual parent, which | ||
| 157 | # would be this. The odd implementation of SUPER doesn't help | ||
| 158 | |||
| 159 | # XXX points | ||
| 160 | # The size of the marker to use in the points and linespoints graphs | ||
| 161 | # in pixels | ||
| 162 | marker_size => 4, | ||
| 163 | |||
| 164 | # attributes with no default | ||
| 165 | markers => undef, | ||
| 166 | |||
| 167 | # XXX lines | ||
| 168 | # The width of the line to use in the lines and linespoints graphs | ||
| 169 | # in pixels | ||
| 170 | line_width => 1, | ||
| 171 | |||
| 172 | # Set the scale of the line types | ||
| 173 | line_type_scale => 8, | ||
| 174 | |||
| 175 | # Which line types to use | ||
| 176 | line_types => [1], | ||
| 177 | |||
| 178 | # Skip undefined values, and don't draw them at all | ||
| 179 | skip_undef => 0, | ||
| 180 | |||
| 181 | # XXX bars | ||
| 182 | # Spacing between the bars and groups of bars | ||
| 183 | bar_width => undef, | ||
| 184 | bar_spacing => 0, | ||
| 185 | bargroup_spacing=> 0, # CONTRIB Grant McLean | ||
| 186 | |||
| 187 | # cycle through colours per data point, not set | ||
| 188 | cycle_clrs => 0, | ||
| 189 | |||
| 190 | # colour of the shadow | ||
| 191 | shadowclr => 'dgray', | ||
| 192 | shadow_depth => 0, | ||
| 193 | |||
| 194 | # XXX mixed | ||
| 195 | default_type => 'lines', | ||
| 196 | types => undef, | ||
| 197 | ); | ||
| 198 | |||
| 199 | sub _has_default { | ||
| 200 | my $self = shift; | ||
| 201 | my $attr = shift || return; | ||
| 202 | exists $Defaults{$attr} || $self->SUPER::_has_default($attr); | ||
| 203 | } | ||
| 204 | |||
| 205 | sub initialise | ||
| 206 | { | ||
| 207 | my $self = shift; | ||
| 208 | |||
| 209 | $self->SUPER::initialise(); | ||
| 210 | |||
| 211 | while (my($key, $val) = each %Defaults) | ||
| 212 | { $self->{$key} = $val } | ||
| 213 | |||
| 214 | $self->set_x_label_font(GD::gdSmallFont); | ||
| 215 | $self->set_y_label_font(GD::gdSmallFont); | ||
| 216 | $self->set_x_axis_font(GD::gdTinyFont); | ||
| 217 | $self->set_y_axis_font(GD::gdTinyFont); | ||
| 218 | $self->set_legend_font(GD::gdTinyFont); | ||
| 219 | $self->set_values_font(GD::gdTinyFont); | ||
| 220 | } | ||
| 221 | |||
| 222 | # PUBLIC | ||
| 223 | sub plot | ||
| 224 | { | ||
| 225 | my $self = shift; | ||
| 226 | my $data = shift; | ||
| 227 | |||
| 228 | $self->check_data($data) or return; | ||
| 229 | $self->init_graph() or return; | ||
| 230 | $self->setup_text() or return; | ||
| 231 | $self->setup_legend(); | ||
| 232 | $self->setup_coords() or return; | ||
| 233 | $self->draw_text(); | ||
| 234 | unless (defined $self->{no_axes}) | ||
| 235 | { | ||
| 236 | $self->draw_axes(); | ||
| 237 | $self->draw_ticks() or return; | ||
| 238 | } | ||
| 239 | $self->draw_data() or return; | ||
| 240 | $self->draw_values() or return; | ||
| 241 | $self->draw_legend(); | ||
| 242 | |||
| 243 | return $self->{graph} | ||
| 244 | } | ||
| 245 | |||
| 246 | sub set | ||
| 247 | { | ||
| 248 | my $self = shift; | ||
| 249 | my %args = @_; | ||
| 250 | |||
| 251 | for (keys %args) | ||
| 252 | { | ||
| 253 | /^tick_length$/ and do | ||
| 254 | { | ||
| 255 | $self->{x_tick_length} = | ||
| 256 | $self->{y_tick_length} = $args{$_}; | ||
| 257 | delete $args{$_}; | ||
| 258 | next; | ||
| 259 | }; | ||
| 260 | /^long_ticks$/ and do | ||
| 261 | { | ||
| 262 | $self->{x_long_ticks} = | ||
| 263 | $self->{y_long_ticks} = $args{$_}; | ||
| 264 | delete $args{$_}; | ||
| 265 | next; | ||
| 266 | }; | ||
| 267 | /^overwrite$/ and do | ||
| 268 | { | ||
| 269 | $self->{cumulate} = 1 if $args{$_} == 2; | ||
| 270 | $self->{overwrite} = $args{$_}; | ||
| 271 | delete $args{$_}; | ||
| 272 | next; | ||
| 273 | }; | ||
| 274 | /^cumulate$/ and do | ||
| 275 | { | ||
| 276 | $self->{cumulate} = $args{$_}; | ||
| 277 | # XXX And for now | ||
| 278 | $self->{overwrite} = 2 if $args{$_}; | ||
| 279 | delete $args{$_}; | ||
| 280 | next; | ||
| 281 | }; | ||
| 282 | } | ||
| 283 | |||
| 284 | return $self->SUPER::set(%args); | ||
| 285 | } | ||
| 286 | |||
| 287 | sub setup_text | ||
| 288 | { | ||
| 289 | my $self = shift; | ||
| 290 | |||
| 291 | $self->{gdta_x_label}->set(colour => $self->{lci}); | ||
| 292 | $self->{gdta_y_label}->set(colour => $self->{lci}); | ||
| 293 | $self->{xlfh} = $self->{gdta_x_label}->get('height'); | ||
| 294 | $self->{ylfh} = $self->{gdta_y_label}->get('height'); | ||
| 295 | |||
| 296 | $self->{gdta_x_axis}->set(colour => $self->{alci}); | ||
| 297 | $self->{gdta_y_axis}->set(colour => $self->{alci}); | ||
| 298 | $self->{xafh} = $self->{gdta_x_axis}->get('height'); | ||
| 299 | $self->{yafh} = $self->{gdta_x_axis}->get('height'); | ||
| 300 | |||
| 301 | $self->{gdta_title}->set(colour => $self->{tci}); | ||
| 302 | $self->{gdta_title}->set_align('top', 'center'); | ||
| 303 | $self->{tfh} = $self->{gdta_title}->get('height'); | ||
| 304 | |||
| 305 | $self->{gdta_legend}->set(colour => $self->{legendci}); | ||
| 306 | $self->{gdta_legend}->set_align('top', 'left'); | ||
| 307 | $self->{lgfh} = $self->{gdta_legend}->get('height'); | ||
| 308 | |||
| 309 | $self->{gdta_values}->set(colour => $self->{valuesci}); | ||
| 310 | unless ($self->{rotate_chart}) | ||
| 311 | { | ||
| 312 | if ($self->{values_vertical}) | ||
| 313 | { | ||
| 314 | $self->{gdta_values}->set_align('center', 'left'); | ||
| 315 | } | ||
| 316 | else | ||
| 317 | { | ||
| 318 | $self->{gdta_values}->set_align('bottom', 'center'); | ||
| 319 | } | ||
| 320 | } | ||
| 321 | else | ||
| 322 | { | ||
| 323 | if ($self->{values_vertical}) | ||
| 324 | { | ||
| 325 | $self->{gdta_values}->set_align('top', 'center'); | ||
| 326 | } | ||
| 327 | else | ||
| 328 | { | ||
| 329 | $self->{gdta_values}->set_align('center', 'left'); | ||
| 330 | } | ||
| 331 | } | ||
| 332 | |||
| 333 | return $self; | ||
| 334 | } | ||
| 335 | |||
| 336 | sub set_x_label_font # (fontname) | ||
| 337 | { | ||
| 338 | my $self = shift; | ||
| 339 | $self->_set_font('gdta_x_label', @_); | ||
| 340 | } | ||
| 341 | sub set_y_label_font # (fontname) | ||
| 342 | { | ||
| 343 | my $self = shift; | ||
| 344 | $self->_set_font('gdta_y_label', @_); | ||
| 345 | } | ||
| 346 | sub set_x_axis_font # (fontname) | ||
| 347 | { | ||
| 348 | my $self = shift; | ||
| 349 | $self->_set_font('gdta_x_axis', @_); | ||
| 350 | } | ||
| 351 | |||
| 352 | sub set_y_axis_font # (fontname) | ||
| 353 | { | ||
| 354 | my $self = shift; | ||
| 355 | $self->_set_font('gdta_y_axis', @_); | ||
| 356 | } | ||
| 357 | |||
| 358 | sub set_values_font | ||
| 359 | { | ||
| 360 | my $self = shift; | ||
| 361 | $self->_set_font('gdta_values', @_); | ||
| 362 | } | ||
| 363 | |||
| 364 | sub set_legend # List of legend keys | ||
| 365 | { | ||
| 366 | my $self = shift; | ||
| 367 | $self->{legend} = [@_]; | ||
| 368 | } | ||
| 369 | |||
| 370 | sub set_legend_font # (font name) | ||
| 371 | { | ||
| 372 | my $self = shift; | ||
| 373 | $self->_set_font('gdta_legend', @_); | ||
| 374 | } | ||
| 375 | |||
| 376 | sub get_hotspot | ||
| 377 | { | ||
| 378 | my $self = shift; | ||
| 379 | my $ds = shift; # Which data set | ||
| 380 | my $np = shift; # Which data point? | ||
| 381 | |||
| 382 | if (defined $np && defined $ds) | ||
| 383 | { | ||
| 384 | return @{$self->{_hotspots}->[$ds]->[$np]}; | ||
| 385 | } | ||
| 386 | elsif (defined $ds) | ||
| 387 | { | ||
| 388 | return @{$self->{_hotspots}->[$ds]}; | ||
| 389 | } | ||
| 390 | else | ||
| 391 | { | ||
| 392 | return @{$self->{_hotspots}}; | ||
| 393 | } | ||
| 394 | } | ||
| 395 | |||
| 396 | sub _set_feature_coords | ||
| 397 | { | ||
| 398 | my $self = shift; | ||
| 399 | my $feature = shift; | ||
| 400 | my $type = shift; | ||
| 401 | $self->{_feat_coords}->{$feature} = [ $type, @_ ]; | ||
| 402 | } | ||
| 403 | |||
| 404 | sub _set_text_feature_coords | ||
| 405 | { | ||
| 406 | my $self = shift; | ||
| 407 | my $feature = shift; | ||
| 408 | $self->_set_feature_coords($feature, "rect", @_[0,1,4,5]); | ||
| 409 | } | ||
| 410 | |||
| 411 | sub get_feature_coordinates | ||
| 412 | { | ||
| 413 | my $self = shift; | ||
| 414 | my $feature = shift; | ||
| 415 | if ($feature) | ||
| 416 | { | ||
| 417 | $self->{_feat_coords}->{$feature}; | ||
| 418 | } | ||
| 419 | else | ||
| 420 | { | ||
| 421 | $self->{_feat_coords}; | ||
| 422 | } | ||
| 423 | } | ||
| 424 | |||
| 425 | # PRIVATE | ||
| 426 | |||
| 427 | # inherit check_data from GD::Graph | ||
| 428 | |||
| 429 | # | ||
| 430 | # calculate the bottom of the bounding box for the graph | ||
| 431 | # | ||
| 432 | sub setup_bottom_boundary | ||
| 433 | { | ||
| 434 | my $self = shift; | ||
| 435 | $self->{bottom} = $self->{height} - $self->{b_margin} - 1; | ||
| 436 | if (! $self->{rotate_chart}) | ||
| 437 | { | ||
| 438 | # X label | ||
| 439 | $self->{bottom} -= $self->{xlfh} + $self->{text_space} | ||
| 440 | if $self->{xlfh}; | ||
| 441 | # X axis tick labels | ||
| 442 | $self->{bottom} -= $self->{x_label_height} + $self->{axis_space} | ||
| 443 | if $self->{xafh}; | ||
| 444 | } | ||
| 445 | else | ||
| 446 | { | ||
| 447 | # Y1 label | ||
| 448 | $self->{bottom} -= $self->{ylfh} + $self->{text_space} | ||
| 449 | if $self->{y1_label}; | ||
| 450 | # Y1 axis labels | ||
| 451 | $self->{bottom} -= $self->{y_label_height}[1] + $self->{axis_space} | ||
| 452 | if $self->{y_label_height}[1]; | ||
| 453 | } | ||
| 454 | } | ||
| 455 | # | ||
| 456 | # Calculate the top of the bounding box for the graph | ||
| 457 | # | ||
| 458 | sub setup_top_boundary | ||
| 459 | { | ||
| 460 | my $self = shift; | ||
| 461 | |||
| 462 | $self->{top} = $self->{t_margin}; | ||
| 463 | # Chart title | ||
| 464 | $self->{top} += $self->{tfh} + $self->{text_space} if $self->{tfh}; | ||
| 465 | if (! $self->{rotate_chart}) | ||
| 466 | { | ||
| 467 | # Make sure the text for the y axis tick markers fits on the canvas | ||
| 468 | $self->{top} = $self->{yafh}/2 if $self->{top} == 0; | ||
| 469 | } | ||
| 470 | else | ||
| 471 | { | ||
| 472 | if ($self->{two_axes}) | ||
| 473 | { | ||
| 474 | # Y2 label | ||
| 475 | $self->{top} += $self->{ylfh} + $self->{text_space} | ||
| 476 | if $self->{y2_label}; | ||
| 477 | # Y2 axis labels | ||
| 478 | $self->{top} += $self->{y_label_height}[2] + $self->{axis_space} | ||
| 479 | if $self->{y_label_height}[2]; | ||
| 480 | } | ||
| 481 | } | ||
| 482 | } | ||
| 483 | # | ||
| 484 | # calculate the left of the bounding box for the graph | ||
| 485 | # | ||
| 486 | sub setup_left_boundary | ||
| 487 | { | ||
| 488 | my $self = shift; | ||
| 489 | |||
| 490 | $self->{left} = $self->{l_margin}; | ||
| 491 | if (! $self->{rotate_chart}) | ||
| 492 | { | ||
| 493 | # Y1 label | ||
| 494 | $self->{left} += $self->{ylfh} + $self->{text_space} | ||
| 495 | if $self->{y1_label}; | ||
| 496 | # Y1 axis labels | ||
| 497 | $self->{left} += $self->{y_label_len}[1] + $self->{axis_space} | ||
| 498 | if $self->{y_label_len}[1]; | ||
| 499 | } | ||
| 500 | else | ||
| 501 | { | ||
| 502 | # X label | ||
| 503 | $self->{left} += $self->{xlfh} + $self->{text_space} | ||
| 504 | if $self->{x_label}; | ||
| 505 | # X axis labels | ||
| 506 | $self->{left} += $self->{x_label_width} + $self->{axis_space} | ||
| 507 | if $self->{x_label_width}; | ||
| 508 | } | ||
| 509 | } | ||
| 510 | # | ||
| 511 | # calculate the right of the bounding box for the graph | ||
| 512 | # | ||
| 513 | sub setup_right_boundary | ||
| 514 | { | ||
| 515 | my $self = shift; | ||
| 516 | $self->{right} = $self->{width} - $self->{r_margin} - 1; | ||
| 517 | |||
| 518 | if (! $self->{rotate_chart}) | ||
| 519 | { | ||
| 520 | if ($self->{two_axes}) | ||
| 521 | { | ||
| 522 | # Y2 label | ||
| 523 | $self->{right} -= $self->{ylfh} + $self->{text_space} | ||
| 524 | if $self->{y2_label}; | ||
| 525 | # Y2 axis label | ||
| 526 | $self->{right} -= $self->{y_label_len}[2] + $self->{axis_space} | ||
| 527 | if $self->{y_label_len}[2]; | ||
| 528 | } | ||
| 529 | } | ||
| 530 | else | ||
| 531 | { | ||
| 532 | # Adjust right margin to allow last label of y axes. Only do | ||
| 533 | # this when the right margin doesn't have enough space | ||
| 534 | # already. | ||
| 535 | # | ||
| 536 | # TODO Don't assume rightmost label is the same as the | ||
| 537 | # longest label (stored in y_label_len) The worst that can | ||
| 538 | # happen now is that we reserve too much space. | ||
| 539 | |||
| 540 | my $max_len = $self->{y_label_len}[1]; | ||
| 541 | if ($self->{two_axes}) | ||
| 542 | { | ||
| 543 | $max_len = $self->{y_label_len}[2] if | ||
| 544 | $self->{y_label_len}[2] > $max_len; | ||
| 545 | } | ||
| 546 | $max_len = int ($max_len/2); | ||
| 547 | |||
| 548 | if ($self->{right} + $max_len >= $self->{width} - $self->{r_margin}) | ||
| 549 | { | ||
| 550 | $self->{right} -= $max_len; | ||
| 551 | } | ||
| 552 | } | ||
| 553 | } | ||
| 554 | |||
| 555 | sub _setup_boundaries | ||
| 556 | { | ||
| 557 | my $self = shift; | ||
| 558 | |||
| 559 | $self->setup_bottom_boundary(); | ||
| 560 | $self->setup_top_boundary(); | ||
| 561 | $self->setup_left_boundary(); | ||
| 562 | $self->setup_right_boundary(); | ||
| 563 | |||
| 564 | if ($self->correct_width && !$self->{x_tick_number}) | ||
| 565 | { | ||
| 566 | if (! $self->{rotate_chart}) | ||
| 567 | { | ||
| 568 | # Make sure we have a nice integer number of pixels | ||
| 569 | $self->{r_margin} += ($self->{right} - $self->{left}) % | ||
| 570 | ($self->{_data}->num_points + 1); | ||
| 571 | |||
| 572 | $self->setup_right_boundary(); | ||
| 573 | } | ||
| 574 | else | ||
| 575 | { | ||
| 576 | # Make sure we have a nice integer number of pixels | ||
| 577 | $self->{b_margin} += ($self->{bottom} - $self->{top}) % | ||
| 578 | ($self->{_data}->num_points + 1); | ||
| 579 | |||
| 580 | $self->setup_bottom_boundary(); | ||
| 581 | } | ||
| 582 | } | ||
| 583 | |||
| 584 | return $self->_set_error('Vertical size too small') | ||
| 585 | if $self->{bottom} <= $self->{top}; | ||
| 586 | return $self->_set_error('Horizontal size too small') | ||
| 587 | if $self->{right} <= $self->{left}; | ||
| 588 | |||
| 589 | return $self; | ||
| 590 | } | ||
| 591 | |||
| 592 | # This method should return 1 if the width of the graph needs to be | ||
| 593 | # corrected to whole integers, and 0 if not. The default behaviour is to | ||
| 594 | # not correct the width. Individual classes should override this by | ||
| 595 | # setting the $self->{correct_width} attribute in their initialise | ||
| 596 | # method. Only in complex cases (see mixed.pm) should this method be | ||
| 597 | # overridden | ||
| 598 | sub correct_width { $_[0]->{correct_width} } | ||
| 599 | |||
| 600 | sub setup_x_step_size_v | ||
| 601 | { | ||
| 602 | my $s = shift; | ||
| 603 | |||
| 604 | # calculate the step size for x data | ||
| 605 | # CONTRIB Changes by Scott Prahl | ||
| 606 | if (defined $s->{x_tick_number}) | ||
| 607 | { | ||
| 608 | my $delta = ($s->{right} - $s->{left})/($s->{x_max} - $s->{x_min}); | ||
| 609 | # 'True' numerical X axis addition # From: Gary Deschaines | ||
| 610 | if (defined($s->{x_min_value}) && defined($s->{x_max_value})) | ||
| 611 | { | ||
| 612 | $s->{x_offset} = $s->{left}; | ||
| 613 | $s->{x_step} = $delta; | ||
| 614 | } | ||
| 615 | else | ||
| 616 | { | ||
| 617 | $s->{x_offset} = | ||
| 618 | ($s->{true_x_min} - $s->{x_min}) * $delta + $s->{left}; | ||
| 619 | $s->{x_step} = | ||
| 620 | ($s->{true_x_max} - $s->{true_x_min}) * | ||
| 621 | $delta/($s->{_data}->num_points - 1); | ||
| 622 | } | ||
| 623 | } | ||
| 624 | else | ||
| 625 | { | ||
| 626 | $s->{x_step} = ($s->{right} - $s->{left})/($s->{_data}->num_points + 1); | ||
| 627 | $s->{x_offset} = $s->{left}; | ||
| 628 | } | ||
| 629 | } | ||
| 630 | |||
| 631 | sub setup_x_step_size_h | ||
| 632 | { | ||
| 633 | my $s = shift; | ||
| 634 | |||
| 635 | # calculate the step size for x data | ||
| 636 | # CONTRIB Changes by Scott Prahl | ||
| 637 | if (defined $s->{x_tick_number}) | ||
| 638 | { | ||
| 639 | my $delta = ($s->{bottom} - $s->{top})/($s->{x_max} - $s->{x_min}); | ||
| 640 | # 'True' numerical X axis addition # From: Gary Deschaines | ||
| 641 | if (defined($s->{x_min_value}) && defined($s->{x_max_value})) | ||
| 642 | { | ||
| 643 | $s->{x_offset} = $s->{top}; | ||
| 644 | $s->{x_step} = $delta; | ||
| 645 | } | ||
| 646 | else | ||
| 647 | { | ||
| 648 | $s->{x_offset} = | ||
| 649 | ($s->{true_x_min} - $s->{x_min}) * $delta + $s->{top}; | ||
| 650 | $s->{x_step} = | ||
| 651 | ($s->{true_x_max} - $s->{true_x_min}) * | ||
| 652 | $delta/($s->{_data}->num_points - 1); | ||
| 653 | } | ||
| 654 | } | ||
| 655 | else | ||
| 656 | { | ||
| 657 | $s->{x_step} = ($s->{bottom} - $s->{top})/($s->{_data}->num_points + 1); | ||
| 658 | $s->{x_offset} = $s->{top}; | ||
| 659 | } | ||
| 660 | } | ||
| 661 | |||
| 662 | sub setup_coords | ||
| 663 | { | ||
| 664 | my $s = shift; | ||
| 665 | |||
| 666 | # Do some sanity checks | ||
| 667 | $s->{two_axes} = 0 if $s->{_data}->num_sets < 2 || $s->{two_axes} < 0; | ||
| 668 | $s->{two_axes} = 1 if $s->{two_axes} > 1; | ||
| 669 | |||
| 670 | delete $s->{y_label2} unless $s->{two_axes}; | ||
| 671 | |||
| 672 | # Set some heights for text | ||
| 673 | $s->{tfh} = 0 unless $s->{title}; | ||
| 674 | $s->{xlfh} = 0 unless $s->{x_label}; | ||
| 675 | |||
| 676 | # Make sure the y1 axis has a label if there is one set for y in | ||
| 677 | # general | ||
| 678 | $s->{y1_label} = $s->{y_label} if !$s->{y1_label} && $s->{y_label}; | ||
| 679 | |||
| 680 | # Set axis tick text heights and widths to 0 if they don't need to | ||
| 681 | # be plotted. | ||
| 682 | $s->{xafh} = 0, $s->{xafw} = 0 unless $s->{x_plot_values}; | ||
| 683 | $s->{yafh} = 0, $s->{yafw} = 0 unless $s->{y_plot_values}; | ||
| 684 | |||
| 685 | # Calculate minima and maxima for the axes | ||
| 686 | $s->set_max_min() or return; | ||
| 687 | |||
| 688 | # Create the labels for the axes, and calculate the max length | ||
| 689 | $s->create_y_labels(); | ||
| 690 | $s->create_x_labels(); # CONTRIB Scott Prahl | ||
| 691 | |||
| 692 | # Calculate the boundaries of the chart | ||
| 693 | $s->_setup_boundaries() or return; | ||
| 694 | |||
| 695 | # CONTRIB Scott Prahl | ||
| 696 | # make sure that we can generate valid x tick marks | ||
| 697 | undef($s->{x_tick_number}) if $s->{_data}->num_points < 3; | ||
| 698 | undef($s->{x_tick_number}) if | ||
| 699 | !defined $s->{x_max} || | ||
| 700 | !defined $s->{x_min} || | ||
| 701 | $s->{x_max} == $s->{x_min}; | ||
| 702 | |||
| 703 | $s->{rotate_chart} ? $s->setup_x_step_size_h() : | ||
| 704 | $s->setup_x_step_size_v(); | ||
| 705 | |||
| 706 | # get the zero axis level | ||
| 707 | my ($zl, $zb) = $s->val_to_pixel(0, 0, 1); | ||
| 708 | my ($min,$val,$max) = $s->{rotate_chart} | ||
| 709 | ? ( $s->{left}, $zl, $s->{right} ) | ||
| 710 | : ( $s->{top}, $zb, $s->{bottom} ); | ||
| 711 | |||
| 712 | $s->{zeropoint} = $min > $val ? $min : $max < $val ? $max : $val; | ||
| 713 | |||
| 714 | # More sanity checks | ||
| 715 | $s->{x_label_skip} = 1 if $s->{x_label_skip} < 1; | ||
| 716 | $s->{y_label_skip} = 1 if $s->{y_label_skip} < 1; | ||
| 717 | $s->{y_tick_number} = 1 if $s->{y_tick_number} < 1; | ||
| 718 | |||
| 719 | return $s; | ||
| 720 | } | ||
| 721 | |||
| 722 | sub create_y_labels | ||
| 723 | { | ||
| 724 | my $self = shift; | ||
| 725 | |||
| 726 | # XXX This should really be y_label_width | ||
| 727 | $self->{y_label_len}[$_] = 0 for 1, 2; | ||
| 728 | $self->{y_label_height}[$_] = 0 for 1, 2; | ||
| 729 | |||
| 730 | for my $t (0 .. $self->{y_tick_number}) | ||
| 731 | { | ||
| 732 | # XXX Ugh, why did I ever do it this way? How bloody obscure. | ||
| 733 | for my $axis (1 .. ($self->{two_axes} + 1)) | ||
| 734 | { | ||
| 735 | my $label = $self->{y_min}[$axis] + | ||
| 736 | $t * ($self->{y_max}[$axis] - $self->{y_min}[$axis]) / | ||
| 737 | $self->{y_tick_number}; | ||
| 738 | |||
| 739 | $self->{y_values}[$axis][$t] = $label; | ||
| 740 | |||
| 741 | if (my ($fmt) = grep defined, map($self->{"y${_}_number_format"},$axis,'') ) | ||
| 742 | { | ||
| 743 | $label = ref $fmt eq 'CODE' ? | ||
| 744 | $fmt->($label) : | ||
| 745 | sprintf($fmt, $label); | ||
| 746 | } | ||
| 747 | |||
| 748 | $self->{gdta_y_axis}->set_text($label); | ||
| 749 | my $len = $self->{gdta_y_axis}->get('width'); | ||
| 750 | |||
| 751 | $self->{y_labels}[$axis][$t] = $label; | ||
| 752 | |||
| 753 | # TODO Allow vertical y labels | ||
| 754 | $self->{y_label_len}[$axis] = $len | ||
| 755 | if $len > $self->{y_label_len}[$axis]; | ||
| 756 | $self->{y_label_height}[$axis] = $self->{yafh}; | ||
| 757 | } | ||
| 758 | } | ||
| 759 | } | ||
| 760 | |||
| 761 | sub get_x_axis_label_length | ||
| 762 | { | ||
| 763 | my $self = shift; | ||
| 764 | |||
| 765 | my @values = $self->{x_tick_number} ? | ||
| 766 | @{$self->{x_values}} : | ||
| 767 | $self->{_data}->x_values; | ||
| 768 | |||
| 769 | my $maxlen = 0; | ||
| 770 | foreach my $label (@values) | ||
| 771 | { | ||
| 772 | $self->{gdta_x_axis}->set_text($label); | ||
| 773 | my $len = $self->{gdta_x_axis}->get('width'); | ||
| 774 | $maxlen = $len if $maxlen < $len; | ||
| 775 | } | ||
| 776 | |||
| 777 | return $maxlen; | ||
| 778 | } | ||
| 779 | |||
| 780 | # CONTRIB Scott Prahl | ||
| 781 | sub create_x_labels | ||
| 782 | { | ||
| 783 | my $self = shift; | ||
| 784 | my $maxlen = 0; | ||
| 785 | |||
| 786 | $self->{x_label_height} = 0; | ||
| 787 | $self->{x_label_width} = 0; | ||
| 788 | |||
| 789 | if (defined $self->{x_tick_number}) | ||
| 790 | { | ||
| 791 | # We want to emulate numerical x axes | ||
| 792 | foreach my $t (0..$self->{x_tick_number}) | ||
| 793 | { | ||
| 794 | my $label = | ||
| 795 | $self->{x_min} + | ||
| 796 | $t * ($self->{x_max} - $self->{x_min})/$self->{x_tick_number}; | ||
| 797 | |||
| 798 | $self->{x_values}[$t] = $label; | ||
| 799 | |||
| 800 | if (defined $self->{x_number_format}) | ||
| 801 | { | ||
| 802 | $label = ref $self->{x_number_format} eq 'CODE' ? | ||
| 803 | &{$self->{x_number_format}}($label) : | ||
| 804 | sprintf($self->{x_number_format}, $label); | ||
| 805 | } | ||
| 806 | |||
| 807 | $self->{gdta_x_label}->set_text($label); | ||
| 808 | my $len = $self->{gdta_x_label}->get('width'); | ||
| 809 | |||
| 810 | $self->{x_labels}[$t] = $label; | ||
| 811 | $maxlen = $len | ||
| 812 | if $len > $self->{x_label_height}; | ||
| 813 | } | ||
| 814 | } | ||
| 815 | else | ||
| 816 | { | ||
| 817 | $maxlen = $self->get_x_axis_label_length; | ||
| 818 | } | ||
| 819 | |||
| 820 | $self->{x_label_height} = $self->{x_labels_vertical} ? | ||
| 821 | $maxlen : $self->{xafh}; | ||
| 822 | $self->{x_label_width} = $self->{x_labels_vertical} ? | ||
| 823 | $self->{xafh} : $maxlen; | ||
| 824 | } | ||
| 825 | |||
| 826 | # | ||
| 827 | # The drawing of labels for the axes. This is split up in the four | ||
| 828 | # positions a label can appear in, depending on a few settings. These | ||
| 829 | # settings are all dealt with in the draw_x_labels and draw_y_labels | ||
| 830 | # subroutines, which in turn call the approriate directional label | ||
| 831 | # drawer | ||
| 832 | # | ||
| 833 | sub draw_left_label | ||
| 834 | { | ||
| 835 | my ($self, $label, $align) = @_; | ||
| 836 | |||
| 837 | $label->set_align('top', 'left'); | ||
| 838 | my $tx = $self->{l_margin}; | ||
| 839 | my $ty = $self->{bottom} - $align * ($self->{bottom} - $self->{top}) + | ||
| 840 | $align * $label->get('width'); | ||
| 841 | $label->draw($tx, $ty, PI/2); | ||
| 842 | } | ||
| 843 | |||
| 844 | sub draw_bottom_label | ||
| 845 | { | ||
| 846 | my ($self, $label, $align) = @_; | ||
| 847 | |||
| 848 | $label->set_align('bottom', 'left'); | ||
| 849 | my $tx = $self->{left} + $align * ($self->{right} - $self->{left}) - | ||
| 850 | $align * $label->get('width'); | ||
| 851 | my $ty = $self->{height} - $self->{b_margin}; | ||
| 852 | $label->draw($tx, $ty, 0); | ||
| 853 | } | ||
| 854 | |||
| 855 | sub draw_top_label | ||
| 856 | { | ||
| 857 | my ($self, $label, $align) = @_; | ||
| 858 | |||
| 859 | $label->set_align('top', 'left'); | ||
| 860 | my $tx = $self->{left} + $align * ($self->{right} - $self->{left}) - | ||
| 861 | $align * $label->get('width'); | ||
| 862 | my $ty = $self->{t_margin}; | ||
| 863 | $ty += $self->{tfh} + $self->{text_space} if $self->{tfh}; | ||
| 864 | $label->draw($tx, $ty, 0); | ||
| 865 | } | ||
| 866 | |||
| 867 | sub draw_right_label | ||
| 868 | { | ||
| 869 | my ($self, $label, $align) = @_; | ||
| 870 | |||
| 871 | $label->set_align('bottom', 'left'); | ||
| 872 | my $tx = $self->{width} - $self->{r_margin}; | ||
| 873 | my $ty = $self->{bottom} - $align * ($self->{bottom} - $self->{top}) + | ||
| 874 | $align * $label->get('width'); | ||
| 875 | $label->draw($tx, $ty, PI/2); | ||
| 876 | } | ||
| 877 | |||
| 878 | sub draw_x_label | ||
| 879 | { | ||
| 880 | my $self = shift; | ||
| 881 | my ($tx, $ty, $a); | ||
| 882 | |||
| 883 | my @coords; # coordinates of the label drawn | ||
| 884 | |||
| 885 | return unless $self->{x_label}; | ||
| 886 | |||
| 887 | $self->{gdta_x_label}->set_text($self->{x_label}); | ||
| 888 | if ($self->{rotate_chart}) | ||
| 889 | { | ||
| 890 | @coords = $self->draw_left_label($self->{gdta_x_label}, | ||
| 891 | $self->{x_label_position}); | ||
| 892 | } | ||
| 893 | else | ||
| 894 | { | ||
| 895 | @coords = $self->draw_bottom_label($self->{gdta_x_label}, | ||
| 896 | $self->{x_label_position}); | ||
| 897 | } | ||
| 898 | $self->_set_text_feature_coords("x_label", @coords); | ||
| 899 | } | ||
| 900 | |||
| 901 | sub draw_y_labels | ||
| 902 | { | ||
| 903 | my $self = shift; | ||
| 904 | |||
| 905 | my @coords; # coordinates of the labels drawn | ||
| 906 | |||
| 907 | if (defined $self->{y1_label}) | ||
| 908 | { | ||
| 909 | $self->{gdta_y_label}->set_text($self->{y1_label}); | ||
| 910 | if ($self->{rotate_chart}) | ||
| 911 | { | ||
| 912 | @coords = $self->draw_bottom_label($self->{gdta_y_label}, | ||
| 913 | $self->{y_label_position}); | ||
| 914 | } | ||
| 915 | else | ||
| 916 | { | ||
| 917 | @coords = $self->draw_left_label($self->{gdta_y_label}, | ||
| 918 | $self->{y_label_position}); | ||
| 919 | } | ||
| 920 | $self->_set_text_feature_coords("y1_label", @coords); | ||
| 921 | $self->_set_text_feature_coords("y_label", @coords); | ||
| 922 | } | ||
| 923 | if ( $self->{two_axes} && defined $self->{y2_label} ) | ||
| 924 | { | ||
| 925 | $self->{gdta_y_label}->set_text($self->{y2_label}); | ||
| 926 | if ($self->{rotate_chart}) | ||
| 927 | { | ||
| 928 | @coords = $self->draw_top_label($self->{gdta_y_label}, | ||
| 929 | $self->{y_label_position}); | ||
| 930 | } | ||
| 931 | else | ||
| 932 | { | ||
| 933 | @coords = $self->draw_right_label($self->{gdta_y_label}, | ||
| 934 | $self->{y_label_position}); | ||
| 935 | } | ||
| 936 | $self->_set_text_feature_coords("y2_label", @coords); | ||
| 937 | } | ||
| 938 | } | ||
| 939 | |||
| 940 | sub draw_text | ||
| 941 | { | ||
| 942 | my $self = shift; | ||
| 943 | |||
| 944 | if ($self->{title}) | ||
| 945 | { | ||
| 946 | my $xc = $self->{left} + ($self->{right} - $self->{left})/2; | ||
| 947 | $self->{gdta_title}->set_align('top', 'center'); | ||
| 948 | $self->{gdta_title}->set_text($self->{title}); | ||
| 949 | my @coords = $self->{gdta_title}->draw($xc, $self->{t_margin}); | ||
| 950 | $self->_set_text_feature_coords("title", @coords); | ||
| 951 | } | ||
| 952 | |||
| 953 | $self->draw_x_label(); | ||
| 954 | $self->draw_y_labels(); | ||
| 955 | } | ||
| 956 | |||
| 957 | sub draw_axes | ||
| 958 | { | ||
| 959 | my $self = shift; | ||
| 960 | |||
| 961 | my ($l, $r, $b, $t) = | ||
| 962 | ( $self->{left}, $self->{right}, $self->{bottom}, $self->{top} ); | ||
| 963 | |||
| 964 | # Sanity check for zero_axis and zero_axis_only | ||
| 965 | unless ($self->{y_min}[1] < 0 && $self->{y_max}[1] > 0) | ||
| 966 | { | ||
| 967 | $self->{zero_axis} = 0; | ||
| 968 | $self->{zero_axis_only} = 0; | ||
| 969 | } | ||
| 970 | |||
| 971 | if ( $self->{box_axis} ) | ||
| 972 | { | ||
| 973 | $self->{graph}->filledRectangle($l+1, $t+1, $r-1, $b-1, $self->{boxci}) | ||
| 974 | if $self->{boxci}; | ||
| 975 | |||
| 976 | $self->{graph}->rectangle($l, $t, $r, $b, $self->{fgci}); | ||
| 977 | } | ||
| 978 | else | ||
| 979 | { | ||
| 980 | $self->{graph}->line($l, $t, $l, $b, $self->{fgci}); | ||
| 981 | $self->{graph}->line($l, $b, $r, $b, $self->{fgci}) | ||
| 982 | unless ($self->{zero_axis_only}); | ||
| 983 | $self->{graph}->line($r, $b, $r, $t, $self->{fgci}) | ||
| 984 | if ($self->{two_axes}); | ||
| 985 | } | ||
| 986 | |||
| 987 | if ($self->{zero_axis} or $self->{zero_axis_only}) | ||
| 988 | { | ||
| 989 | my ($x, $y) = $self->val_to_pixel(0, 0, 1); | ||
| 990 | $self->{graph}->line($l, $y, $r, $y, $self->{fgci}); | ||
| 991 | } | ||
| 992 | |||
| 993 | $self->_set_feature_coords("axes", "rect", $l, $b, $r, $t); | ||
| 994 | } | ||
| 995 | |||
| 996 | # | ||
| 997 | # Ticks and values for y axes | ||
| 998 | # | ||
| 999 | sub draw_y_ticks_h | ||
| 1000 | { | ||
| 1001 | my $self = shift; | ||
| 1002 | |||
| 1003 | for my $t (0 .. $self->{y_tick_number}) | ||
| 1004 | { | ||
| 1005 | for my $axis (1 .. ($self->{two_axes} + 1)) | ||
| 1006 | { | ||
| 1007 | my $value = $self->{y_values}[$axis][$t]; | ||
| 1008 | my $label = $self->{y_labels}[$axis][$t]; | ||
| 1009 | |||
| 1010 | my ($x, $y) = $self->val_to_pixel(0, $value, -$axis); | ||
| 1011 | $y = ($axis == 1) ? $self->{bottom} : $self->{top}; | ||
| 1012 | |||
| 1013 | if ($self->{y_long_ticks}) | ||
| 1014 | { | ||
| 1015 | $self->{graph}->line( | ||
| 1016 | $x, $self->{bottom}, | ||
| 1017 | $x, $self->{top}, | ||
| 1018 | $self->{fgci} | ||
| 1019 | ) unless ($axis-1); | ||
| 1020 | } | ||
| 1021 | else | ||
| 1022 | { | ||
| 1023 | $self->{graph}->line( | ||
| 1024 | $x, $y, | ||
| 1025 | $x, $y - $self->{y_tick_length}, | ||
| 1026 | $self->{fgci} | ||
| 1027 | ); | ||
| 1028 | } | ||
| 1029 | |||
| 1030 | next | ||
| 1031 | if $t % ($self->{y_label_skip}) || ! $self->{y_plot_values}; | ||
| 1032 | |||
| 1033 | $self->{gdta_y_axis}->set_text($label); | ||
| 1034 | if ($axis == 1) | ||
| 1035 | { | ||
| 1036 | $self->{gdta_y_axis}->set_align('top', 'center'); | ||
| 1037 | $y += $self->{axis_space}; | ||
| 1038 | } | ||
| 1039 | else | ||
| 1040 | { | ||
| 1041 | $self->{gdta_y_axis}->set_align('bottom', 'center'); | ||
| 1042 | $y -= $self->{axis_space}; | ||
| 1043 | } | ||
| 1044 | $self->{gdta_y_axis}->draw($x, $y); | ||
| 1045 | } | ||
| 1046 | } | ||
| 1047 | |||
| 1048 | return $self; | ||
| 1049 | } | ||
| 1050 | |||
| 1051 | sub draw_y_ticks_v | ||
| 1052 | { | ||
| 1053 | my $self = shift; | ||
| 1054 | |||
| 1055 | for my $t (0 .. $self->{y_tick_number}) | ||
| 1056 | { | ||
| 1057 | # XXX Ugh, why did I ever do it this way? How bloody obscure. | ||
| 1058 | for my $axis (1 .. ($self->{two_axes} + 1)) | ||
| 1059 | { | ||
| 1060 | my $value = $self->{y_values}[$axis][$t]; | ||
| 1061 | my $label = $self->{y_labels}[$axis][$t]; | ||
| 1062 | |||
| 1063 | my ($x, $y) = $self->val_to_pixel(0, $value, -$axis); | ||
| 1064 | $x = ($axis == 1) ? $self->{left} : $self->{right}; | ||
| 1065 | |||
| 1066 | if ($self->{y_long_ticks}) | ||
| 1067 | { | ||
| 1068 | $self->{graph}->line( | ||
| 1069 | $x, $y, | ||
| 1070 | $x + $self->{right} - $self->{left}, $y, | ||
| 1071 | $self->{fgci} | ||
| 1072 | ) unless ($axis-1); | ||
| 1073 | } | ||
| 1074 | else | ||
| 1075 | { | ||
| 1076 | $self->{graph}->line( | ||
| 1077 | $x, $y, | ||
| 1078 | $x + (3 - 2 * $axis) * $self->{y_tick_length}, $y, | ||
| 1079 | $self->{fgci} | ||
| 1080 | ); | ||
| 1081 | } | ||
| 1082 | |||
| 1083 | next | ||
| 1084 | if $t % ($self->{y_label_skip}) || ! $self->{y_plot_values}; | ||
| 1085 | |||
| 1086 | $self->{gdta_y_axis}->set_text($label); | ||
| 1087 | if ($axis == 1) | ||
| 1088 | { | ||
| 1089 | $self->{gdta_y_axis}->set_align('center', 'right'); | ||
| 1090 | $x -= $self->{axis_space}; | ||
| 1091 | } | ||
| 1092 | else | ||
| 1093 | { | ||
| 1094 | $self->{gdta_y_axis}->set_align('center', 'left'); | ||
| 1095 | $x += $self->{axis_space}; | ||
| 1096 | } | ||
| 1097 | $self->{gdta_y_axis}->draw($x, $y); | ||
| 1098 | } | ||
| 1099 | } | ||
| 1100 | |||
| 1101 | return $self; | ||
| 1102 | } | ||
| 1103 | |||
| 1104 | sub draw_y_ticks | ||
| 1105 | { | ||
| 1106 | #TODO Clean this up! | ||
| 1107 | $_[0]->{rotate_chart} ? goto &draw_y_ticks_h : goto &draw_y_ticks_v; | ||
| 1108 | } | ||
| 1109 | |||
| 1110 | |||
| 1111 | # | ||
| 1112 | # Ticks and values for x axes | ||
| 1113 | # | ||
| 1114 | sub draw_x_ticks_h | ||
| 1115 | { | ||
| 1116 | my $self = shift; | ||
| 1117 | |||
| 1118 | for (my $i = 0; $i < $self->{_data}->num_points; $i++) | ||
| 1119 | { | ||
| 1120 | my ($x, $y) = $self->val_to_pixel($i + 1, 0, 1); | ||
| 1121 | |||
| 1122 | $x = $self->{left} unless $self->{zero_axis_only}; | ||
| 1123 | |||
| 1124 | # CONTRIB Damon Brodie for x_tick_offset | ||
| 1125 | next if (!$self->{x_all_ticks} and | ||
| 1126 | ($i - $self->{x_tick_offset}) % $self->{x_label_skip} and | ||
| 1127 | $i != $self->{_data}->num_points - 1 | ||
| 1128 | ); | ||
| 1129 | |||
| 1130 | if ($self->{x_ticks}) | ||
| 1131 | { | ||
| 1132 | if ($self->{x_long_ticks}) | ||
| 1133 | { | ||
| 1134 | $self->{graph}->line($self->{left}, $y, $self->{right}, $y, | ||
| 1135 | $self->{fgci}); | ||
| 1136 | } | ||
| 1137 | else | ||
| 1138 | { | ||
| 1139 | $self->{graph}->line( $x, $y, $x + $self->{x_tick_length}, $y, | ||
| 1140 | $self->{fgci}); | ||
| 1141 | } | ||
| 1142 | } | ||
| 1143 | |||
| 1144 | # CONTRIB Damon Brodie for x_tick_offset | ||
| 1145 | next if | ||
| 1146 | ($i - $self->{x_tick_offset}) % ($self->{x_label_skip}) and | ||
| 1147 | $i != $self->{_data}->num_points - 1; | ||
| 1148 | |||
| 1149 | my $text = $self->{_data}->get_x($i); | ||
| 1150 | if (defined $text) | ||
| 1151 | { | ||
| 1152 | $self->{gdta_x_axis}->set_text($text); | ||
| 1153 | |||
| 1154 | my $angle = 0; | ||
| 1155 | if ($self->{x_labels_vertical}) | ||
| 1156 | { | ||
| 1157 | $self->{gdta_x_axis}->set_align('bottom', 'center'); | ||
| 1158 | $angle = PI/2; | ||
| 1159 | } | ||
| 1160 | else | ||
| 1161 | { | ||
| 1162 | $self->{gdta_x_axis}->set_align('center', 'right'); | ||
| 1163 | } | ||
| 1164 | $self->{gdta_x_axis}->draw($x - $self->{axis_space}, $y, $angle); | ||
| 1165 | } | ||
| 1166 | elsif ($INC{'warnings.pm'} && warnings::enabled('uninitialized') || $^W ) | ||
| 1167 | { | ||
| 1168 | carp("Uninitialized label value at index $i"); | ||
| 1169 | } | ||
| 1170 | } | ||
| 1171 | |||
| 1172 | return $self; | ||
| 1173 | } | ||
| 1174 | |||
| 1175 | sub draw_x_ticks_v | ||
| 1176 | { | ||
| 1177 | my $self = shift; | ||
| 1178 | |||
| 1179 | for (my $i = 0; $i < $self->{_data}->num_points; $i++) | ||
| 1180 | { | ||
| 1181 | my ($x, $y) = $self->val_to_pixel($i + 1, 0, 1); | ||
| 1182 | |||
| 1183 | $y = $self->{bottom} unless $self->{zero_axis_only}; | ||
| 1184 | |||
| 1185 | # CONTRIB Damon Brodie for x_tick_offset | ||
| 1186 | next if (!$self->{x_all_ticks} and | ||
| 1187 | ($i - $self->{x_tick_offset}) % $self->{x_label_skip} and | ||
| 1188 | $i != $self->{_data}->num_points - 1 | ||
| 1189 | ); | ||
| 1190 | |||
| 1191 | if ($self->{x_ticks}) | ||
| 1192 | { | ||
| 1193 | if ($self->{x_long_ticks}) | ||
| 1194 | { | ||
| 1195 | $self->{graph}->line($x, $self->{bottom}, $x, $self->{top}, | ||
| 1196 | $self->{fgci}); | ||
| 1197 | } | ||
| 1198 | else | ||
| 1199 | { | ||
| 1200 | $self->{graph}->line($x, $y, $x, $y - $self->{x_tick_length}, | ||
| 1201 | $self->{fgci}); | ||
| 1202 | } | ||
| 1203 | } | ||
| 1204 | |||
| 1205 | # CONTRIB Damon Brodie for x_tick_offset | ||
| 1206 | next if | ||
| 1207 | ($i - $self->{x_tick_offset}) % ($self->{x_label_skip}) and | ||
| 1208 | $i != $self->{_data}->num_points - 1; | ||
| 1209 | |||
| 1210 | my $text = $self->{_data}->get_x($i); | ||
| 1211 | if (defined $text) | ||
| 1212 | { | ||
| 1213 | $self->{gdta_x_axis}->set_text($text); | ||
| 1214 | |||
| 1215 | my $angle = 0; | ||
| 1216 | if ($self->{x_labels_vertical}) | ||
| 1217 | { | ||
| 1218 | $self->{gdta_x_axis}->set_align('center', 'right'); | ||
| 1219 | $angle = PI/2; | ||
| 1220 | } | ||
| 1221 | else | ||
| 1222 | { | ||
| 1223 | $self->{gdta_x_axis}->set_align('top', 'center'); | ||
| 1224 | } | ||
| 1225 | $self->{gdta_x_axis}->draw($x, $y + $self->{axis_space}, $angle); | ||
| 1226 | } | ||
| 1227 | elsif ($INC{'warnings.pm'} && warnings::enabled('uninitialized') || $^W ) | ||
| 1228 | { | ||
| 1229 | carp("Uninitialized label value at index $i"); | ||
| 1230 | } | ||
| 1231 | } | ||
| 1232 | |||
| 1233 | return $self; | ||
| 1234 | } | ||
| 1235 | |||
| 1236 | sub draw_x_ticks | ||
| 1237 | { | ||
| 1238 | #TODO Clean this up! | ||
| 1239 | $_[0]->{rotate_chart} ? goto &draw_x_ticks_h : goto &draw_x_ticks_v; | ||
| 1240 | } | ||
| 1241 | |||
| 1242 | # CONTRIB Scott Prahl | ||
| 1243 | # Assume x array contains equally spaced x-values | ||
| 1244 | # and generate an appropriate axis | ||
| 1245 | # | ||
| 1246 | #### | ||
| 1247 | # 'True' numerical X axis addition | ||
| 1248 | # From: Gary Deschaines | ||
| 1249 | # | ||
| 1250 | # These modification to draw_x_ticks_number pass x-tick values to the | ||
| 1251 | # val_to_pixel subroutine instead of x-tick indices when ture numerical | ||
| 1252 | # x-axis mode is detected. Also, x_tick_offset and x_label_skip are | ||
| 1253 | # processed differently when true numerical x-axis mode is detected to | ||
| 1254 | # allow labeled major x-tick marks and un-labeled minor x-tick marks. | ||
| 1255 | # | ||
| 1256 | # For example: | ||
| 1257 | # | ||
| 1258 | # x_tick_number => 14, | ||
| 1259 | # x_ticks => 1, | ||
| 1260 | # x_long_ticks => 1, | ||
| 1261 | # x_tick_length => -4, | ||
| 1262 | # x_min_value => 100, | ||
| 1263 | # x_max_value => 800, | ||
| 1264 | # x_tick_offset => 2, | ||
| 1265 | # x_label_skip => 2, | ||
| 1266 | # | ||
| 1267 | # | ||
| 1268 | # ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ | ||
| 1269 | # | | | | | | | | | | | | | | ||
| 1270 | # 1 -| | | | | | | | | | | | | | ||
| 1271 | # | | | | | | | | | | | | | | ||
| 1272 | # 0 _|_________|____|____|____|____|____|____|____|____|____|____|_________| | ||
| 1273 | # | | | | | | | | | | | | ||
| 1274 | # 200 300 400 500 600 700 | ||
| 1275 | sub draw_x_ticks_number | ||
| 1276 | { | ||
| 1277 | my $self = shift; | ||
| 1278 | |||
| 1279 | for my $i (0 .. $self->{x_tick_number}) | ||
| 1280 | { | ||
| 1281 | my ($value, $x, $y); | ||
| 1282 | |||
| 1283 | if (defined($self->{x_min_value}) && defined($self->{x_max_value})) | ||
| 1284 | { | ||
| 1285 | next if ($i - $self->{x_tick_offset}) < 0; | ||
| 1286 | next if ($i + $self->{x_tick_offset}) > $self->{x_tick_number}; | ||
| 1287 | $value = $self->{x_values}[$i]; | ||
| 1288 | ($x, $y) = $self->val_to_pixel($value, 0, 1); | ||
| 1289 | } | ||
| 1290 | else | ||
| 1291 | { | ||
| 1292 | $value = ($self->{_data}->num_points - 1) | ||
| 1293 | * ($self->{x_values}[$i] - $self->{true_x_min}) | ||
| 1294 | / ($self->{true_x_max} - $self->{true_x_min}); | ||
| 1295 | ($x, $y) = $self->val_to_pixel($value + 1, 0, 1); | ||
| 1296 | } | ||
| 1297 | |||
| 1298 | $y = $self->{bottom} unless $self->{zero_axis_only}; | ||
| 1299 | |||
| 1300 | if ($self->{x_ticks}) | ||
| 1301 | { | ||
| 1302 | if ($self->{x_long_ticks}) | ||
| 1303 | { | ||
| 1304 | # XXX This mod needs to be done everywhere ticks are | ||
| 1305 | # drawn | ||
| 1306 | if ( $self->{x_tick_length} >= 0 ) | ||
| 1307 | { | ||
| 1308 | $self->{graph}->line($x, $self->{bottom}, | ||
| 1309 | $x, $self->{top}, $self->{fgci}); | ||
| 1310 | } | ||
| 1311 | else | ||
| 1312 | { | ||
| 1313 | $self->{graph}->line( | ||
| 1314 | $x, $self->{bottom} - $self->{x_tick_length}, | ||
| 1315 | $x, $self->{top}, $self->{fgci}); | ||
| 1316 | } | ||
| 1317 | } | ||
| 1318 | else | ||
| 1319 | { | ||
| 1320 | $self->{graph}->line($x, $y, | ||
| 1321 | $x, $y - $self->{x_tick_length}, $self->{fgci} ); | ||
| 1322 | } | ||
| 1323 | } | ||
| 1324 | |||
| 1325 | # If we have to skip labels, we'll do it here. | ||
| 1326 | # Make sure to always draw the last one. | ||
| 1327 | next if $i % $self->{x_label_skip} && $i != $self->{x_tick_number}; | ||
| 1328 | |||
| 1329 | $self->{gdta_x_axis}->set_text($self->{x_labels}[$i]); | ||
| 1330 | |||
| 1331 | if ($self->{x_labels_vertical}) | ||
| 1332 | { | ||
| 1333 | $self->{gdta_x_axis}->set_align('center', 'right'); | ||
| 1334 | my $yt = $y + $self->{text_space}/2; | ||
| 1335 | $self->{gdta_x_axis}->draw($x, $yt, PI/2); | ||
| 1336 | } | ||
| 1337 | else | ||
| 1338 | { | ||
| 1339 | $self->{gdta_x_axis}->set_align('top', 'center'); | ||
| 1340 | my $yt = $y + $self->{text_space}/2; | ||
| 1341 | $self->{gdta_x_axis}->draw($x, $yt); | ||
| 1342 | } | ||
| 1343 | } | ||
| 1344 | |||
| 1345 | return $self; | ||
| 1346 | } | ||
| 1347 | |||
| 1348 | sub draw_ticks | ||
| 1349 | { | ||
| 1350 | my $self = shift; | ||
| 1351 | |||
| 1352 | $self->draw_y_ticks() or return; | ||
| 1353 | |||
| 1354 | return $self | ||
| 1355 | unless $self->{x_plot_values}; | ||
| 1356 | |||
| 1357 | if (defined $self->{x_tick_number}) | ||
| 1358 | { | ||
| 1359 | $self->draw_x_ticks_number() or return; | ||
| 1360 | } | ||
| 1361 | else | ||
| 1362 | { | ||
| 1363 | $self->draw_x_ticks() or return; | ||
| 1364 | } | ||
| 1365 | |||
| 1366 | return $self; | ||
| 1367 | } | ||
| 1368 | |||
| 1369 | sub draw_data | ||
| 1370 | { | ||
| 1371 | my $self = shift; | ||
| 1372 | |||
| 1373 | # Calculate bar_spacing from bar_width | ||
| 1374 | if ($self->{bar_width}) | ||
| 1375 | { | ||
| 1376 | my $chart_width = !$self->{rotate_chart} ? | ||
| 1377 | $self->{right} - $self->{left} : | ||
| 1378 | $self->{bottom} - $self->{top}; | ||
| 1379 | my $n_bars = $self->{_data}->num_points; | ||
| 1380 | my $n_sets = $self->{_data}->num_sets; | ||
| 1381 | my $bar_space = $chart_width/($n_bars + 1) / | ||
| 1382 | ($self->{overwrite} ? 1 : $n_sets); | ||
| 1383 | $self->{bar_spacing} = $bar_space - $self->{bar_width}; | ||
| 1384 | $self->{bar_spacing} = 0 if $self->{bar_spacing} < 0; | ||
| 1385 | } | ||
| 1386 | |||
| 1387 | # XXX is this comment still pertinent? | ||
| 1388 | # The drawing of 'cumulated' sets needs to be done in reverse, | ||
| 1389 | # for area and bar charts. This is mainly because of backward | ||
| 1390 | # compatibility | ||
| 1391 | |||
| 1392 | for (my $dsn = 1; $dsn <= $self->{_data}->num_sets; $dsn++) | ||
| 1393 | { | ||
| 1394 | $self->draw_data_set($dsn) or return; | ||
| 1395 | } | ||
| 1396 | |||
| 1397 | return $self | ||
| 1398 | } | ||
| 1399 | |||
| 1400 | # | ||
| 1401 | # Draw the values of the data point with the bars, lines or markers | ||
| 1402 | sub draw_values | ||
| 1403 | { | ||
| 1404 | my $self = shift; | ||
| 1405 | |||
| 1406 | return $self unless $self->{show_values}; | ||
| 1407 | |||
| 1408 | my $text_angle = $self->{values_vertical} ? PI/2 : 0; | ||
| 1409 | my (@bars,@others); | ||
| 1410 | |||
| 1411 | if ($self->isa('GD::Graph::mixed') ) { | ||
| 1412 | # 1-indexed, like data-sets themselves | ||
| 1413 | my @types = $self->types; | ||
| 1414 | push @{'bars' eq $types[$_ - 1] ? \@bars : \@others}, $_ for 1 .. @types; | ||
| 1415 | $self->GD::Graph::bars::draw_values(@bars) if @bars; | ||
| 1416 | } else { | ||
| 1417 | @others = 1 .. $self->{_data}->num_sets; | ||
| 1418 | } | ||
| 1419 | |||
| 1420 | foreach my $dsn ( @others ) | ||
| 1421 | { | ||
| 1422 | my @values = $self->{_data}->y_values($dsn) or | ||
| 1423 | return $self->_set_error("Impossible illegal data set: $dsn", | ||
| 1424 | $self->{_data}->error); | ||
| 1425 | my @display = $self->{show_values}->y_values($dsn) or next; | ||
| 1426 | |||
| 1427 | for (my $i = 0; $i < @values; $i++) | ||
| 1428 | { | ||
| 1429 | next unless defined $display[$i]; | ||
| 1430 | my ($xp, $yp); | ||
| 1431 | if (defined($self->{x_min_value}) && defined($self->{x_max_value})) | ||
| 1432 | { | ||
| 1433 | ($xp, $yp) = $self->val_to_pixel( | ||
| 1434 | $self->{_data}->get_x($i), $values[$i], $dsn); | ||
| 1435 | } | ||
| 1436 | else | ||
| 1437 | { | ||
| 1438 | ($xp, $yp) = $self->val_to_pixel($i+1, $values[$i], $dsn); | ||
| 1439 | } | ||
| 1440 | $yp -= $self->{values_space}; | ||
| 1441 | |||
| 1442 | my $value = $display[$i]; | ||
| 1443 | if (defined $self->{values_format}) | ||
| 1444 | { | ||
| 1445 | $value = ref $self->{values_format} eq 'CODE' ? | ||
| 1446 | &{$self->{values_format}}($value) : | ||
| 1447 | sprintf($self->{values_format}, $value); | ||
| 1448 | } | ||
| 1449 | |||
| 1450 | $self->{gdta_values}->set_text($value); | ||
| 1451 | $self->{gdta_values}->draw($xp, $yp, $text_angle); | ||
| 1452 | } | ||
| 1453 | } | ||
| 1454 | |||
| 1455 | return $self | ||
| 1456 | } | ||
| 1457 | |||
| 1458 | # | ||
| 1459 | # draw_data_set is in sub classes | ||
| 1460 | # | ||
| 1461 | sub draw_data_set | ||
| 1462 | { | ||
| 1463 | # ABSTRACT | ||
| 1464 | my $self = shift; | ||
| 1465 | $self->die_abstract( "sub draw_data missing, ") | ||
| 1466 | } | ||
| 1467 | |||
| 1468 | # | ||
| 1469 | # This method corrects the minimum and maximum y values for chart | ||
| 1470 | # types that need to always include a zero point. | ||
| 1471 | # This is supposed to be called before the methods that pick | ||
| 1472 | # good-looking values. | ||
| 1473 | # | ||
| 1474 | # Input: current minimum and maximum. | ||
| 1475 | # Output: new minimum and maximum. | ||
| 1476 | # | ||
| 1477 | sub _correct_y_min_max | ||
| 1478 | { | ||
| 1479 | my $self = shift; | ||
| 1480 | my ($min, $max) = @_; | ||
| 1481 | |||
| 1482 | # Make sure bars and area always have a zero offset | ||
| 1483 | # Only bars and areas need | ||
| 1484 | return ($min, $max) | ||
| 1485 | unless $self->isa("GD::Graph::bars") or $self->isa("GD::Graph::area"); | ||
| 1486 | |||
| 1487 | # If either $min or $max are 0, we can return | ||
| 1488 | return ($min, $max) if $max == 0 or $min == 0; | ||
| 1489 | |||
| 1490 | # If $min and $max on opposite end of zero axis, no work needed | ||
| 1491 | return ($min, $max) unless $min/$max > 0; | ||
| 1492 | |||
| 1493 | if ($min > 0) | ||
| 1494 | { | ||
| 1495 | $min = 0; | ||
| 1496 | } | ||
| 1497 | else | ||
| 1498 | { | ||
| 1499 | $max = 0; | ||
| 1500 | } | ||
| 1501 | |||
| 1502 | return ($min, $max); | ||
| 1503 | } | ||
| 1504 | |||
| 1505 | # | ||
| 1506 | # Figure out the maximum values for the vertical exes, and calculate | ||
| 1507 | # a more or less sensible number for the tops. | ||
| 1508 | # | ||
| 1509 | sub set_max_min | ||
| 1510 | { | ||
| 1511 | my $self = shift; | ||
| 1512 | |||
| 1513 | # XXX fix to calculate min and max for each data set | ||
| 1514 | # independently, and store in an array. Then, based on use_axis, | ||
| 1515 | # pick the minimust and maximust for each axis, and use those. | ||
| 1516 | |||
| 1517 | # First, calculate some decent values | ||
| 1518 | if ( $self->{two_axes} ) | ||
| 1519 | { # XXX this is almost certainly a bug: this key is not set anywhere | ||
| 1520 | my $min_range_1 = defined($self->{min_range_1}) | ||
| 1521 | ? $self->{min_range_1} | ||
| 1522 | : $self->{min_range}; | ||
| 1523 | my $min_range_2 = defined($self->{min_range_2}) | ||
| 1524 | ? $self->{min_range_2} | ||
| 1525 | : $self->{min_range}; | ||
| 1526 | |||
| 1527 | my(@y_min, @y_max); | ||
| 1528 | for my $nd (1 .. $self->{_data}->num_sets) | ||
| 1529 | { | ||
| 1530 | my $axis = $self->{use_axis}->[$nd - 1]; | ||
| 1531 | my($y_min, $y_max) = $self->{_data}->get_min_max_y($nd); | ||
| 1532 | if (!defined $y_min[$axis] || $y_min[$axis] > $y_min) | ||
| 1533 | { | ||
| 1534 | $y_min[$axis] = $y_min; | ||
| 1535 | } | ||
| 1536 | if (!defined $y_max[$axis] || $y_max[$axis] < $y_max) | ||
| 1537 | { | ||
| 1538 | $y_max[$axis] = $y_max; | ||
| 1539 | } | ||
| 1540 | } | ||
| 1541 | |||
| 1542 | ( | ||
| 1543 | $self->{y_min}[1], $self->{y_max}[1], | ||
| 1544 | $self->{y_min}[2], $self->{y_max}[2], | ||
| 1545 | $self->{y_tick_number} | ||
| 1546 | ) = _best_dual_ends( | ||
| 1547 | $self->_correct_y_min_max($y_min[1], $y_max[1]), | ||
| 1548 | $min_range_1, | ||
| 1549 | $self->_correct_y_min_max($y_min[2], $y_max[2]), | ||
| 1550 | $min_range_2, | ||
| 1551 | $self->{y_tick_number} | ||
| 1552 | ); | ||
| 1553 | } | ||
| 1554 | else | ||
| 1555 | { | ||
| 1556 | my ($y_min, $y_max); | ||
| 1557 | if ($self->{cumulate}) | ||
| 1558 | { | ||
| 1559 | my $data_set = $self->{_data}->copy(); | ||
| 1560 | $data_set->cumulate; | ||
| 1561 | ($y_min, $y_max) = $data_set->get_min_max_y($data_set->num_sets); | ||
| 1562 | } | ||
| 1563 | else | ||
| 1564 | { | ||
| 1565 | ($y_min, $y_max) = $self->{_data}->get_min_max_y_all; | ||
| 1566 | } | ||
| 1567 | ($y_min, $y_max) = $self->_correct_y_min_max($y_min, $y_max); | ||
| 1568 | ($self->{y_min}[1], $self->{y_max}[1], $self->{y_tick_number}) = | ||
| 1569 | _best_ends($y_min, $y_max, @$self{'y_tick_number','y_min_range'}); | ||
| 1570 | } | ||
| 1571 | |||
| 1572 | if (defined($self->{x_tick_number})) | ||
| 1573 | { | ||
| 1574 | if (defined($self->{x_min_value}) && defined($self->{x_max_value})) | ||
| 1575 | { | ||
| 1576 | $self->{true_x_min} = $self->{x_min_value}; | ||
| 1577 | $self->{true_x_max} = $self->{x_max_value}; | ||
| 1578 | } | ||
| 1579 | else | ||
| 1580 | { | ||
| 1581 | ($self->{true_x_min}, $self->{true_x_max}) = | ||
| 1582 | $self->{_data}->get_min_max_x; | ||
| 1583 | ($self->{x_min}, $self->{x_max}, $self->{x_tick_number}) = | ||
| 1584 | _best_ends($self->{true_x_min}, $self->{true_x_max}, | ||
| 1585 | @$self{'x_tick_number','x_min_range'}); | ||
| 1586 | |||
| 1587 | } | ||
| 1588 | } | ||
| 1589 | |||
| 1590 | # Overwrite these with any user supplied ones | ||
| 1591 | $self->{y_min}[1] = $self->{y_min_value} if defined $self->{y_min_value}; | ||
| 1592 | $self->{y_min}[2] = $self->{y_min_value} if defined $self->{y_min_value}; | ||
| 1593 | |||
| 1594 | $self->{y_max}[1] = $self->{y_max_value} if defined $self->{y_max_value}; | ||
| 1595 | $self->{y_max}[2] = $self->{y_max_value} if defined $self->{y_max_value}; | ||
| 1596 | |||
| 1597 | $self->{y_min}[1] = $self->{y1_min_value} if defined $self->{y1_min_value}; | ||
| 1598 | $self->{y_max}[1] = $self->{y1_max_value} if defined $self->{y1_max_value}; | ||
| 1599 | |||
| 1600 | $self->{y_min}[2] = $self->{y2_min_value} if defined $self->{y2_min_value}; | ||
| 1601 | $self->{y_max}[2] = $self->{y2_max_value} if defined $self->{y2_max_value}; | ||
| 1602 | |||
| 1603 | $self->{x_min} = $self->{x_min_value} if defined $self->{x_min_value}; | ||
| 1604 | $self->{x_max} = $self->{x_max_value} if defined $self->{x_max_value}; | ||
| 1605 | |||
| 1606 | if ($self->{two_axes}) | ||
| 1607 | { | ||
| 1608 | # If we have two axes, we need to make sure that the zero is at | ||
| 1609 | # the same spot. | ||
| 1610 | # And we need to change the number of ticks on the axes | ||
| 1611 | |||
| 1612 | my $l_range = $self->{y_max}[1] - $self->{y_min}[1]; | ||
| 1613 | my $r_range = $self->{y_max}[2] - $self->{y_min}[2]; | ||
| 1614 | |||
| 1615 | my $l_top = $self->{y_max}[1]/$l_range; | ||
| 1616 | my $r_top = $self->{y_max}[2]/$r_range; | ||
| 1617 | my $l_bot = $self->{y_min}[1]/$l_range; | ||
| 1618 | my $r_bot = $self->{y_min}[2]/$r_range; | ||
| 1619 | |||
| 1620 | if ($l_top > $r_top) | ||
| 1621 | { | ||
| 1622 | $self->{y_max}[2] = $l_top * $r_range; | ||
| 1623 | $self->{y_min}[1] = $r_bot * $l_range; | ||
| 1624 | $self->{y_tick_number} *= 1 + abs $r_bot - $l_bot; | ||
| 1625 | } | ||
| 1626 | else | ||
| 1627 | { | ||
| 1628 | $self->{y_max}[1] = $r_top * $l_range; | ||
| 1629 | $self->{y_min}[2] = $l_bot * $r_range; | ||
| 1630 | $self->{y_tick_number} *= 1 + abs $r_top - $l_top; | ||
| 1631 | } | ||
| 1632 | } | ||
| 1633 | |||
| 1634 | # Check to see if we have sensible values | ||
| 1635 | if ($self->{two_axes}) | ||
| 1636 | { | ||
| 1637 | for my $i (1 .. $self->{_data}->num_sets) | ||
| 1638 | { | ||
| 1639 | my ($min, $max) = $self->{_data}->get_min_max_y($i); | ||
| 1640 | return $self->_set_error("Minimum for y" . $i . " too large") | ||
| 1641 | if $self->{y_min}[$self->{use_axis}[$i-1]] > $min; | ||
| 1642 | return $self->_set_error("Maximum for y" . $i . " too small") | ||
| 1643 | if $self->{y_max}[$self->{use_axis}[$i-1]] < $max; | ||
| 1644 | } | ||
| 1645 | } | ||
| 1646 | |||
| 1647 | return $self; | ||
| 1648 | } | ||
| 1649 | |||
| 1650 | # CONTRIB Scott Prahl | ||
| 1651 | # | ||
| 1652 | # Calculate best endpoints and number of intervals for an axis and | ||
| 1653 | # returns ($nice_min, $nice_max, $n), where $n is the number of | ||
| 1654 | # intervals and | ||
| 1655 | # | ||
| 1656 | # $nice_min <= $min < $max <= $nice_max | ||
| 1657 | # | ||
| 1658 | # Usage: | ||
| 1659 | # ($nmin,$nmax,$nint) = _best_ends(247, 508); | ||
| 1660 | # ($nmin,$nmax) = _best_ends(247, 508, 5); | ||
| 1661 | # use 5 intervals | ||
| 1662 | # ($nmin,$nmax,$nint) = _best_ends(247, 508, [4..7]); | ||
| 1663 | # best of 4,5,6,7 intervals | ||
| 1664 | # ($nmin,$nmax,$nint) = _best_ends(247, 508, 'auto'); | ||
| 1665 | # best of 3,4,5,6 intervals | ||
| 1666 | # ($nmin,$nmax,$nint) = _best_ends(247, 508, [2..5]); | ||
| 1667 | # best of 2,3,4,5 intervals | ||
| 1668 | sub _best_ends | ||
| 1669 | { | ||
| 1670 | my ($min, $max, $n_ref, $min_range) = @_; | ||
| 1671 | |||
| 1672 | # Adjust for the min range if need be | ||
| 1673 | ($min, $max) = _fit_vals_range($min, $max, $min_range); | ||
| 1674 | |||
| 1675 | my ($best_min, $best_max, $best_num) = ($min, $max, 1); | ||
| 1676 | |||
| 1677 | # Check that min and max are not the same, and not 0 | ||
| 1678 | ($min, $max) = ($min) ? ($min * 0.5, $min * 1.5) : (-1,1) | ||
| 1679 | if ($max == $min); | ||
| 1680 | |||
| 1681 | # mgjv - Sometimes, for odd values, and only one data set, this will be | ||
| 1682 | # necessary _after_ the previous step, not before. Data sets of one | ||
| 1683 | # long with negative values were causing infinite loops later on. | ||
| 1684 | ($min, $max) = ($max, $min) if ($min > $max); | ||
| 1685 | |||
| 1686 | my @n = ref($n_ref) ? @$n_ref : $n_ref; | ||
| 1687 | |||
| 1688 | if (@n <= 0) | ||
| 1689 | { | ||
| 1690 | @n = (3..6); | ||
| 1691 | } | ||
| 1692 | else | ||
| 1693 | { | ||
| 1694 | @n = map { ref($_) ? @$_ : /(\d+)/i ? $1 : (3..6) } @n; | ||
| 1695 | } | ||
| 1696 | |||
| 1697 | my $best_fit = 1e30; | ||
| 1698 | my $range = $max - $min; | ||
| 1699 | |||
| 1700 | # create array of interval sizes | ||
| 1701 | my $s = 1; | ||
| 1702 | while ($s < $range) { $s *= 10 } | ||
| 1703 | while ($s > $range) { $s /= 10 } | ||
| 1704 | my @step = map {$_ * $s} (0.2, 0.5, 1, 2, 5); | ||
| 1705 | |||
| 1706 | for my $n (@n) | ||
| 1707 | { | ||
| 1708 | # Try all numbers of intervals | ||
| 1709 | next if ($n < 1); | ||
| 1710 | |||
| 1711 | for my $step (@step) | ||
| 1712 | { | ||
| 1713 | next if ($n != 1) and ($step < $range/$n) || ($step <= 0); | ||
| 1714 | # $step too small | ||
| 1715 | |||
| 1716 | my ($nice_min, $nice_max, $fit) | ||
| 1717 | = _fit_interval($min, $max, $n, $step); | ||
| 1718 | |||
| 1719 | next if $best_fit <= $fit; | ||
| 1720 | |||
| 1721 | $best_min = $nice_min; | ||
| 1722 | $best_max = $nice_max; | ||
| 1723 | $best_fit = $fit; | ||
| 1724 | $best_num = $n; | ||
| 1725 | } | ||
| 1726 | } | ||
| 1727 | return ($best_min, $best_max, $best_num) | ||
| 1728 | } | ||
| 1729 | |||
| 1730 | # CONTRIB Ben Tilly | ||
| 1731 | # | ||
| 1732 | # Calculate best endpoints and number of intervals for a pair of axes | ||
| 1733 | # where it is trying to line up the scale of the two intervals. It | ||
| 1734 | # returns ($nice_min_1, $nice_max_1, $nice_min_2, $nice_max_2, $n), | ||
| 1735 | # where $n is the number of intervals and | ||
| 1736 | # | ||
| 1737 | # $nice_min_1 <= $min_1 < $max_1 <= $nice_max_1 | ||
| 1738 | # $nice_min_2 <= $min_2 < $max_2 <= $nice_max_2 | ||
| 1739 | # | ||
| 1740 | # and 0 will appear at the same point on both axes. | ||
| 1741 | # | ||
| 1742 | # Usage: | ||
| 1743 | # ($nmin_1,$nmax_1,$nmin_2,$nmax_2,$nint) = _best_dual_ends(247, 508, undef, -1, 5, undef, [2..5]); | ||
| 1744 | # etc. (The usage of the last arguments just parallels _best_ends.) | ||
| 1745 | # | ||
| 1746 | sub _best_dual_ends | ||
| 1747 | { | ||
| 1748 | my ($min_1, $max_1) = _fit_vals_range(splice @_, 0, 3); | ||
| 1749 | my ($min_2, $max_2) = _fit_vals_range(splice @_, 0, 3); | ||
| 1750 | my @rem_args = @_; | ||
| 1751 | |||
| 1752 | # Fix the situation where both min_1 and max_1 are 0, which makes it | ||
| 1753 | # loop forever | ||
| 1754 | ($min_1, $max_1) = (0, 1) unless $min_1 or $max_1; | ||
| 1755 | |||
| 1756 | my $scale_1 = _max(abs($min_1), abs($max_1)); | ||
| 1757 | my $scale_2 = _max(abs($min_2), abs($max_2)); | ||
| 1758 | |||
| 1759 | $scale_1 = defined($scale_2) ? $scale_2 : 1 unless defined($scale_1); | ||
| 1760 | $scale_2 = $scale_1 unless defined($scale_2); | ||
| 1761 | |||
| 1762 | my $ratio = $scale_1 / ($scale_2 || 1); | ||
| 1763 | my $fact_1 = my $fact_2 = 1; | ||
| 1764 | |||
| 1765 | while ($ratio < sqrt(0.1)) | ||
| 1766 | { | ||
| 1767 | $ratio *= 10; | ||
| 1768 | $fact_2 *= 10; | ||
| 1769 | } | ||
| 1770 | while ($ratio > sqrt(10)) | ||
| 1771 | { | ||
| 1772 | $ratio /= 10; | ||
| 1773 | $fact_1 *= 10; | ||
| 1774 | } | ||
| 1775 | |||
| 1776 | my ($best_min_1, $best_max_1, $best_min_2, $best_max_2, $best_n, $best_fit) | ||
| 1777 | = ($min_1, $max_1, $min_2, $max_2, 1, 1e10); | ||
| 1778 | |||
| 1779 | # Now try all of the ratios of "simple numbers" in the right size-range | ||
| 1780 | foreach my $frac | ||
| 1781 | ( | ||
| 1782 | [1,1], [1,2], [1,3], [2,1], [2,3], [2,5], | ||
| 1783 | [3,1], [3,2], [3,4], [3,5], [3,8], [3,10], | ||
| 1784 | [4,3], [4,5], [5,2], [5,3], [5,4], [5,6], | ||
| 1785 | [5,8], [6,5], [8,3], [8,5], [10,3] | ||
| 1786 | ) | ||
| 1787 | { | ||
| 1788 | my $bfact_1 = $frac->[0] * $fact_1; | ||
| 1789 | my $bfact_2 = $frac->[1] * $fact_2; | ||
| 1790 | |||
| 1791 | my $min = _min( $min_1/$bfact_1, $min_2/$bfact_2 ); | ||
| 1792 | my $max = _max( $max_1/$bfact_1, $max_2/$bfact_2 ); | ||
| 1793 | |||
| 1794 | my ($bmin, $bmax, $n) = _best_ends($min, $max, @rem_args); | ||
| 1795 | my ($bmin_1, $bmax_1) = ($bfact_1*$bmin, $bfact_1*$bmax); | ||
| 1796 | my ($bmin_2, $bmax_2) = ($bfact_2*$bmin, $bfact_2*$bmax); | ||
| 1797 | |||
| 1798 | my $fit = _measure_interval_fit($bmin_1, $min_1, $max_1, $bmax_1) | ||
| 1799 | + _measure_interval_fit($bmin_2, $min_2, $max_2, $bmax_2); | ||
| 1800 | |||
| 1801 | next if $best_fit < $fit; | ||
| 1802 | |||
| 1803 | ( | ||
| 1804 | $best_min_1, $best_max_1, $best_min_2, $best_max_2, | ||
| 1805 | $best_n, $best_fit | ||
| 1806 | ) = ( | ||
| 1807 | $bmin_1, $bmax_1, $bmin_2, $bmax_2, | ||
| 1808 | $n, $fit | ||
| 1809 | ); | ||
| 1810 | } | ||
| 1811 | |||
| 1812 | return ($best_min_1, $best_max_1, $best_min_2, $best_max_2, $best_n); | ||
| 1813 | } | ||
| 1814 | |||
| 1815 | # Takes $min, $max, $step_count, $step_size. Assumes $min <= $max and both | ||
| 1816 | # $step_count and $step_size are positive. Returns the fitted $min, $max, | ||
| 1817 | # and a $fit statistic (where smaller is better). Failure to fit the | ||
| 1818 | # interval results in a poor fit statistic. :-) | ||
| 1819 | sub _fit_interval | ||
| 1820 | { | ||
| 1821 | my ($min, $max, $step_count, $step_size) = @_; | ||
| 1822 | |||
| 1823 | my $nice_min = $step_size * int($min/$step_size); | ||
| 1824 | $nice_min -= $step_size if ($nice_min > $min); | ||
| 1825 | my $nice_max = ($step_count == 1) | ||
| 1826 | ? $step_size * int($max/$step_size + 1) | ||
| 1827 | : $nice_min + $step_count * $step_size; | ||
| 1828 | |||
| 1829 | my $fit = _measure_interval_fit($nice_min, $min, $max, $nice_max); | ||
| 1830 | |||
| 1831 | # Prevent division by zero errors further up | ||
| 1832 | return ($min, $max, 0) if ($step_size == 0); | ||
| 1833 | return ($nice_min, $nice_max, $fit); | ||
| 1834 | } | ||
| 1835 | |||
| 1836 | # Takes 2 values and a minimum range. Returns a min and max which holds | ||
| 1837 | # both values and is at least that minimum size | ||
| 1838 | sub _fit_vals_range | ||
| 1839 | { | ||
| 1840 | my ($min, $max, $min_range) = @_; | ||
| 1841 | |||
| 1842 | ($min, $max) = ($max, $min) if $max < $min; | ||
| 1843 | |||
| 1844 | if (defined($min_range) and $min_range > $max - $min) | ||
| 1845 | { | ||
| 1846 | my $nice_min = $min_range * int($min/$min_range); | ||
| 1847 | $nice_min = $nice_min - $min_range if $min < $nice_min; | ||
| 1848 | my $nice_max = $max < $nice_min + $min_range | ||
| 1849 | ? $nice_min + $min_range | ||
| 1850 | : $max; | ||
| 1851 | ($min, $max) = ($nice_min, $nice_max); | ||
| 1852 | } | ||
| 1853 | return (0+$min, 0+$max); | ||
| 1854 | } | ||
| 1855 | |||
| 1856 | # Takes $bmin, $min, $max, $bmax and returns a fit statistic for how well | ||
| 1857 | # ($bmin, $bmax) encloses the interval ($min, $max). Smaller is better, | ||
| 1858 | # and failure to fit will be a very bad fit. Assumes that $min <= $max | ||
| 1859 | # and $bmin < $bmax. | ||
| 1860 | sub _measure_interval_fit | ||
| 1861 | { | ||
| 1862 | my ($bmin, $min, $max, $bmax) = @_; | ||
| 1863 | return 1000 if $bmin > $min or $bmax < $max; | ||
| 1864 | |||
| 1865 | my $range = $max - $min; | ||
| 1866 | my $brange = $bmax - $bmin; | ||
| 1867 | |||
| 1868 | return $brange < 10 * $range | ||
| 1869 | ? ($brange / $range) | ||
| 1870 | : 10; | ||
| 1871 | } | ||
| 1872 | |||
| 1873 | sub _get_bottom | ||
| 1874 | { | ||
| 1875 | my $self = shift; | ||
| 1876 | my ($ds, $np) = @_; | ||
| 1877 | my $bottom = $self->{zeropoint}; | ||
| 1878 | |||
| 1879 | if ($self->{cumulate} && $ds > 1) | ||
| 1880 | { | ||
| 1881 | my $left; | ||
| 1882 | my $pvalue = $self->{_data}->get_y_cumulative($ds - 1, $np); | ||
| 1883 | ($left, $bottom) = $self->val_to_pixel($np + 1, $pvalue, $ds); | ||
| 1884 | $bottom = $left if $self->{rotate_chart}; | ||
| 1885 | } | ||
| 1886 | |||
| 1887 | return $bottom; | ||
| 1888 | } | ||
| 1889 | |||
| 1890 | # | ||
| 1891 | # Convert value coordinates to pixel coordinates on the canvas. | ||
| 1892 | # TODO Clean up all the rotate_chart stuff | ||
| 1893 | # | ||
| 1894 | sub val_to_pixel # ($x, $y, $dataset) or ($x, $y, -$axis) in real coords | ||
| 1895 | { # return [x, y] in pixel coords | ||
| 1896 | my $self = shift; | ||
| 1897 | my ($x, $y, $i) = @_; | ||
| 1898 | |||
| 1899 | # XXX use_axis | ||
| 1900 | my $axis = 1; | ||
| 1901 | if ( $self->{two_axes} ) { | ||
| 1902 | $axis = $i < 0 ? -$i : $self->{use_axis}[$i - 1]; | ||
| 1903 | } | ||
| 1904 | |||
| 1905 | my $y_min = $self->{y_min}[$axis]; | ||
| 1906 | my $y_max = $self->{y_max}[$axis]; | ||
| 1907 | my $y_range = ($y_max - $y_min) || 1; | ||
| 1908 | # XXX the above might be an appropriate place for a conditional warning | ||
| 1909 | |||
| 1910 | my $y_step = $self->{rotate_chart} ? | ||
| 1911 | abs(($self->{right} - $self->{left}) / $y_range) : | ||
| 1912 | abs(($self->{bottom} - $self->{top}) / $y_range); | ||
| 1913 | |||
| 1914 | my $ret_x; | ||
| 1915 | my $origin = $self->{rotate_chart} ? $self->{top} : $self->{left}; | ||
| 1916 | |||
| 1917 | if (defined($self->{x_min_value}) && defined($self->{x_max_value})) | ||
| 1918 | { | ||
| 1919 | $ret_x = $origin + ($x - $self->{x_min}) * $self->{x_step}; | ||
| 1920 | } | ||
| 1921 | else | ||
| 1922 | { | ||
| 1923 | $ret_x = ($self->{x_tick_number} ? $self->{x_offset} : $origin) | ||
| 1924 | + $x * $self->{x_step}; | ||
| 1925 | } | ||
| 1926 | my $ret_y = $self->{rotate_chart} ? | ||
| 1927 | $self->{left} + ($y - $y_min) * $y_step : | ||
| 1928 | $self->{bottom} - ($y - $y_min) * $y_step; | ||
| 1929 | |||
| 1930 | return $self->{rotate_chart} ? | ||
| 1931 | (_round($ret_y), _round($ret_x)) : | ||
| 1932 | (_round($ret_x), _round($ret_y)); | ||
| 1933 | } | ||
| 1934 | |||
| 1935 | # | ||
| 1936 | # Legend | ||
| 1937 | # | ||
| 1938 | sub setup_legend | ||
| 1939 | { | ||
| 1940 | my $self = shift; | ||
| 1941 | |||
| 1942 | return unless defined $self->{legend}; | ||
| 1943 | |||
| 1944 | my $maxlen = 0; | ||
| 1945 | my $num = 0; | ||
| 1946 | |||
| 1947 | # Save some variables | ||
| 1948 | $self->{r_margin_abs} = $self->{r_margin}; | ||
| 1949 | $self->{b_margin_abs} = $self->{b_margin}; | ||
| 1950 | |||
| 1951 | foreach my $legend (@{$self->{legend}}) | ||
| 1952 | { | ||
| 1953 | if (defined($legend) and $legend ne "") | ||
| 1954 | { | ||
| 1955 | $self->{gdta_legend}->set_text($legend); | ||
| 1956 | my $len = $self->{gdta_legend}->get('width'); | ||
| 1957 | $maxlen = ($maxlen > $len) ? $maxlen : $len; | ||
| 1958 | $num++; | ||
| 1959 | } | ||
| 1960 | last if $num >= $self->{_data}->num_sets; | ||
| 1961 | } | ||
| 1962 | |||
| 1963 | $self->{lg_num} = $num or return; | ||
| 1964 | # not actually bug 20792 (unsure that this will ever get hit, but if it does..!) | ||
| 1965 | |||
| 1966 | # calculate the height and width of each element | ||
| 1967 | my $legend_height = _max($self->{lgfh}, $self->{legend_marker_height}); | ||
| 1968 | |||
| 1969 | $self->{lg_el_width} = | ||
| 1970 | $maxlen + $self->{legend_marker_width} + 3 * $self->{legend_spacing}; | ||
| 1971 | $self->{lg_el_height} = $legend_height + 2 * $self->{legend_spacing}; | ||
| 1972 | |||
| 1973 | my ($lg_pos, $lg_align) = split(//, $self->{legend_placement}); | ||
| 1974 | |||
| 1975 | if ($lg_pos eq 'R') | ||
| 1976 | { | ||
| 1977 | # Always work in one column | ||
| 1978 | $self->{lg_cols} = 1; | ||
| 1979 | $self->{lg_rows} = $num; | ||
| 1980 | |||
| 1981 | # Just for completeness, might use this in later versions | ||
| 1982 | $self->{lg_x_size} = $self->{lg_cols} * $self->{lg_el_width}; | ||
| 1983 | $self->{lg_y_size} = $self->{lg_rows} * $self->{lg_el_height}; | ||
| 1984 | |||
| 1985 | # Adjust the right margin for the rest of the graph | ||
| 1986 | $self->{r_margin} += $self->{lg_x_size}; | ||
| 1987 | |||
| 1988 | # Set the x starting point | ||
| 1989 | $self->{lg_xs} = $self->{width} - $self->{r_margin}; | ||
| 1990 | |||
| 1991 | # Set the y starting point, depending on alignment | ||
| 1992 | if ($lg_align eq 'T') | ||
| 1993 | { | ||
| 1994 | $self->{lg_ys} = $self->{t_margin}; | ||
| 1995 | } | ||
| 1996 | elsif ($lg_align eq 'B') | ||
| 1997 | { | ||
| 1998 | $self->{lg_ys} = $self->{height} - $self->{b_margin} - | ||
| 1999 | $self->{lg_y_size}; | ||
| 2000 | } | ||
| 2001 | else # default 'C' | ||
| 2002 | { | ||
| 2003 | my $height = $self->{height} - $self->{t_margin} - | ||
| 2004 | $self->{b_margin}; | ||
| 2005 | |||
| 2006 | $self->{lg_ys} = | ||
| 2007 | int($self->{t_margin} + $height/2 - $self->{lg_y_size}/2) ; | ||
| 2008 | } | ||
| 2009 | } | ||
| 2010 | else # 'B' is the default | ||
| 2011 | { | ||
| 2012 | # What width can we use | ||
| 2013 | my $width = $self->{width} - $self->{l_margin} - $self->{r_margin}; | ||
| 2014 | |||
| 2015 | (!defined($self->{lg_cols})) and | ||
| 2016 | $self->{lg_cols} = int($width/$self->{lg_el_width}) || 1; # bug 20792 | ||
| 2017 | |||
| 2018 | $self->{lg_cols} = _min($self->{lg_cols}, $num); | ||
| 2019 | |||
| 2020 | $self->{lg_rows} = | ||
| 2021 | int($num / $self->{lg_cols}) + (($num % $self->{lg_cols}) ? 1 : 0); | ||
| 2022 | |||
| 2023 | $self->{lg_x_size} = $self->{lg_cols} * $self->{lg_el_width}; | ||
| 2024 | $self->{lg_y_size} = $self->{lg_rows} * $self->{lg_el_height}; | ||
| 2025 | |||
| 2026 | # Adjust the bottom margin for the rest of the graph | ||
| 2027 | $self->{b_margin} += $self->{lg_y_size}; | ||
| 2028 | |||
| 2029 | # Set the y starting point | ||
| 2030 | $self->{lg_ys} = $self->{height} - $self->{b_margin}; | ||
| 2031 | |||
| 2032 | # Set the x starting point, depending on alignment | ||
| 2033 | if ($lg_align eq 'R') | ||
| 2034 | { | ||
| 2035 | $self->{lg_xs} = $self->{width} - $self->{r_margin} - | ||
| 2036 | $self->{lg_x_size}; | ||
| 2037 | } | ||
| 2038 | elsif ($lg_align eq 'L') | ||
| 2039 | { | ||
| 2040 | $self->{lg_xs} = $self->{l_margin}; | ||
| 2041 | } | ||
| 2042 | else # default 'C' | ||
| 2043 | { | ||
| 2044 | $self->{lg_xs} = | ||
| 2045 | int($self->{l_margin} + $width/2 - $self->{lg_x_size}/2); | ||
| 2046 | } | ||
| 2047 | } | ||
| 2048 | } | ||
| 2049 | |||
| 2050 | sub draw_legend | ||
| 2051 | { | ||
| 2052 | my $self = shift; | ||
| 2053 | |||
| 2054 | return unless defined $self->{legend}; | ||
| 2055 | |||
| 2056 | my $xl = $self->{lg_xs} + $self->{legend_spacing}; | ||
| 2057 | my $y = $self->{lg_ys} + $self->{legend_spacing} - 1; | ||
| 2058 | |||
| 2059 | my $i = 0; | ||
| 2060 | my $row = 1; | ||
| 2061 | my $x = $xl; # start position of current element | ||
| 2062 | |||
| 2063 | foreach my $legend (@{$self->{legend}}) | ||
| 2064 | { | ||
| 2065 | $i++; | ||
| 2066 | last if $i > $self->{_data}->num_sets; | ||
| 2067 | |||
| 2068 | my $xe = $x; # position within an element | ||
| 2069 | |||
| 2070 | next unless defined($legend) && $legend ne ""; | ||
| 2071 | |||
| 2072 | $self->draw_legend_marker($i, $xe, $y); | ||
| 2073 | |||
| 2074 | $xe += $self->{legend_marker_width} + $self->{legend_spacing}; | ||
| 2075 | my $ys = int($y + $self->{lg_el_height}/2 - $self->{lgfh}/2); | ||
| 2076 | |||
| 2077 | $self->{gdta_legend}->set_text($legend); | ||
| 2078 | $self->{gdta_legend}->draw($xe, $ys); | ||
| 2079 | |||
| 2080 | $x += $self->{lg_el_width}; | ||
| 2081 | |||
| 2082 | if (++$row > $self->{lg_cols}) | ||
| 2083 | { | ||
| 2084 | $row = 1; | ||
| 2085 | $y += $self->{lg_el_height}; | ||
| 2086 | $x = $xl; | ||
| 2087 | } | ||
| 2088 | } | ||
| 2089 | } | ||
| 2090 | |||
| 2091 | # | ||
| 2092 | # This will be virtual; every sub class should define their own | ||
| 2093 | # if this one doesn't suffice | ||
| 2094 | # | ||
| 2095 | sub draw_legend_marker # data_set_number, x, y | ||
| 2096 | { | ||
| 2097 | my $s = shift; | ||
| 2098 | my $n = shift; | ||
| 2099 | my $x = shift; | ||
| 2100 | my $y = shift; | ||
| 2101 | |||
| 2102 | my $g = $s->{graph}; | ||
| 2103 | |||
| 2104 | my $ci = $s->set_clr($s->pick_data_clr($n),$s->{alpha}); | ||
| 2105 | return unless defined $ci; | ||
| 2106 | |||
| 2107 | $y += int($s->{lg_el_height}/2 - $s->{legend_marker_height}/2); | ||
| 2108 | |||
| 2109 | $g->filledRectangle( | ||
| 2110 | $x, $y, | ||
| 2111 | $x + $s->{legend_marker_width}, $y + $s->{legend_marker_height}, | ||
| 2112 | $ci | ||
| 2113 | ); | ||
| 2114 | |||
| 2115 | $g->rectangle( | ||
| 2116 | $x, $y, | ||
| 2117 | $x + $s->{legend_marker_width}, $y + $s->{legend_marker_height}, | ||
| 2118 | $s->{acci} | ||
| 2119 | ); | ||
| 2120 | } | ||
| 2121 | |||
| 2122 | "Just another true value"; | ||