| 1 | |||
| 2 | 4 | ▶ | use v5.12; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | |||
| 4 | package Chart::HorizontalBars; | ||
| 5 | our @ISA = qw(Chart::Base); | ||
| 6 | our $VERSION = 'v2.403.9'; | ||
| 7 | |||
| 8 | 0 | ▶ | use Chart::Base; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 9 | use GD; | ||
| 10 | use Carp; | ||
| 11 | |||
| 12 | #>>>>>>>>>>>>>>>>>>>>>>>>>># | ||
| 13 | # public methods go here # | ||
| 14 | #<<<<<<<<<<<<<<<<<<<<<<<<<<# | ||
| 15 | |||
| 16 | #>>>>>>>>>>>>>>>>>>>>>>>>>>># | ||
| 17 | # private methods go here # | ||
| 18 | #<<<<<<<<<<<<<<<<<<<<<<<<<<<# | ||
| 19 | |||
| 20 | ## @method private int _draw_x_ticks() | ||
| 21 | # draw the x-ticks and their labels | ||
| 22 | # Overwrites this function of Chart::Base | ||
| 23 | # @return status | ||
| 24 | # | ||
| 25 | sub _draw_x_ticks { | ||
| 26 | my $self = shift; | ||
| 27 | my $data = $self->{'dataref'}; | ||
| 28 | my $font = $self->{'tick_label_font'}; | ||
| 29 | my $textcolor = $self->_color_role_to_index('text'); | ||
| 30 | my $misccolor = $self->_color_role_to_index('misc'); | ||
| 31 | my ( $h, $w, $x1, $y1, $y2, $x2, $delta, $width, $label ); | ||
| 32 | my @labels = @{ $self->{'y_tick_labels'} }; | ||
| 33 | |||
| 34 | $self->{'grid_data'}->{'x'} = []; | ||
| 35 | |||
| 36 | #make sure we have a real font | ||
| 37 | unless ( ( ref $font ) eq 'GD::Font' ) | ||
| 38 | { | ||
| 39 | croak "The tick label font you specified isn't a GD font object"; | ||
| 40 | } | ||
| 41 | |||
| 42 | #get height and width of the font | ||
| 43 | ( $h, $w ) = ( $font->height, $font->width ); | ||
| 44 | |||
| 45 | #get the right x-value and width | ||
| 46 | if ( $self->{'y_axes'} =~ /^right$/i ) { | ||
| 47 | $x1 = $self->{'curr_x_min'}; | ||
| 48 | $width = | ||
| 49 | $self->{'curr_x_max'} - $x1 - $self->{'tick_len'} - $self->{'text_space'} - $w * $self->{'x_tick_label_length'}; | ||
| 50 | } elsif ( $self->{'y_axes'} =~ /^both$/i ) { | ||
| 51 | $x1 = $self->{'curr_x_min'} + $self->{'text_space'} + $w * $self->{'x_tick_label_length'} + $self->{'tick_len'}; | ||
| 52 | $width = | ||
| 53 | $self->{'curr_x_max'} - $x1 - $self->{'tick_len'} - $self->{'text_space'} - $w * $self->{'x_tick_label_length'}; | ||
| 54 | } else { | ||
| 55 | $x1 = $self->{'curr_x_min'} + $self->{'text_space'} + $w * $self->{'x_tick_label_length'} + $self->{'tick_len'}; | ||
| 56 | $width = $self->{'curr_x_max'} - $x1; | ||
| 57 | } | ||
| 58 | |||
| 59 | #get the delta value | ||
| 60 | $delta = $width / ( $self->{'y_ticks'} - 1 ); | ||
| 61 | |||
| 62 | #draw the labels | ||
| 63 | $y2 = $y1; | ||
| 64 | |||
| 65 | if ( $self->{'x_ticks'} =~ /^normal/i ) { # just normal ticks | ||
| 66 | # get the point for updating later | ||
| 67 | $y1 = $self->{'curr_y_max'} - 2 * $self->{'text_space'} - $h - $self->{'tick_len'}; | ||
| 68 | |||
| 69 | #get the start point | ||
| 70 | $y2 = $y1 + $self->{'tick_len'} + $self->{'text_space'}; | ||
| 71 | for ( 0 .. $#labels ) { | ||
| 72 | $label = $self->{'y_tick_labels'}[$_]; | ||
| 73 | $x2 = $x1 + ( $delta * $_ ) - ( $w * length($label) / 2 ); | ||
| 74 | $self->{'gd_obj'}->string( $font, $x2, $y2, $label, $textcolor ); | ||
| 75 | } | ||
| 76 | } elsif ( $self->{'x_ticks'} =~ /^staggered/i ) { # staggered ticks | ||
| 77 | # get the point for updating later | ||
| 78 | $y1 = $self->{'curr_y_max'} - 3 * $self->{'text_space'} - 2 * $h - $self->{'tick_len'}; | ||
| 79 | |||
| 80 | for ( 0 .. $#labels ) { | ||
| 81 | $label = $self->{'y_tick_labels'}[$_]; | ||
| 82 | $x2 = $x1 + ( $delta * $_ ) - ( $w * length($label) / 2 ); | ||
| 83 | unless ( $_ % 2 ) { | ||
| 84 | $y2 = $y1 + $self->{'text_space'} + $self->{'tick_len'}; | ||
| 85 | $self->{'gd_obj'}->string( $font, $x2, $y2, $label, $textcolor ); | ||
| 86 | } else { | ||
| 87 | $y2 = $y1 + $h + 2 * $self->{'text_space'} + $self->{'tick_len'}; | ||
| 88 | $self->{'gd_obj'}->string( $font, $x2, $y2, $label, $textcolor ); | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | } | ||
| 93 | |||
| 94 | elsif ( $self->{'x_ticks'} =~ /^vertical/i ) { # vertical ticks | ||
| 95 | # get the point for updating later | ||
| 96 | $y1 = $self->{'curr_y_max'} - 2 * $self->{'text_space'} - $w * $self->{'y_tick_label_length'} - $self->{'tick_len'}; | ||
| 97 | |||
| 98 | for ( 0 .. $#labels ) { | ||
| 99 | $label = $self->{'y_tick_labels'}[$_]; | ||
| 100 | |||
| 101 | # get the start point | ||
| 102 | $y2 = $y1 + $self->{'tick_len'} + $w * length($label) + $self->{'text_space'}; | ||
| 103 | |||
| 104 | $x2 = $x1 + ( $delta * $_ ) - ( $h / 2 ); | ||
| 105 | $self->{'gd_obj'}->stringUp( $font, $x2, $y2, $label, $textcolor ); | ||
| 106 | } | ||
| 107 | |||
| 108 | } else { carp "I don't understand the type of x-ticks you specified" } | ||
| 109 | |||
| 110 | # update the curr x and y max value | ||
| 111 | $self->{'curr_y_max'} = $y1; | ||
| 112 | $self->{'curr_x_max'} = $x1 + $width; | ||
| 113 | |||
| 114 | # draw the ticks | ||
| 115 | $y1 = $self->{'curr_y_max'}; | ||
| 116 | $y2 = $self->{'curr_y_max'} + $self->{'tick_len'}; | ||
| 117 | for ( 0 .. $#labels ) { | ||
| 118 | $x2 = $x1 + ( $delta * $_ ); | ||
| 119 | $self->{'gd_obj'}->line( $x2, $y1, $x2, $y2, $misccolor ); | ||
| 120 | if ( $self->true( $self->{'grid_lines'} ) | ||
| 121 | or $self->true( $self->{'x_grid_lines'} ) ) { | ||
| 122 | |||
| 123 | $self->{'grid_data'}->{'x'}->[$_] = $x2; | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | return 1; | ||
| 128 | } | ||
| 129 | |||
| 130 | ## @fn private int _draw_y_ticks() | ||
| 131 | # draw the y-ticks and their labels | ||
| 132 | # Overwrites this function of Chart::Base | ||
| 133 | # @return status | ||
| 134 | sub _draw_y_ticks { | ||
| 135 | my $self = shift; | ||
| 136 | my $side = shift || 'left'; | ||
| 137 | my $data = $self->{'dataref'}; | ||
| 138 | my $font = $self->{'tick_label_font'}; | ||
| 139 | my $textcolor = $self->_color_role_to_index('text'); | ||
| 140 | my $misccolor = $self->_color_role_to_index('misc'); | ||
| 141 | my ( $h, $w, $x1, $x2, $y1, $y2 ); | ||
| 142 | my ( $width, $height, $delta ); | ||
| 143 | |||
| 144 | $self->{'grid_data'}->{'y'} = []; | ||
| 145 | |||
| 146 | #make sure that is a real font | ||
| 147 | unless ( ( ref $font ) eq 'GD::Font' ) | ||
| 148 | { | ||
| 149 | croak "The tick label font isn't a GD Font object!"; | ||
| 150 | } | ||
| 151 | |||
| 152 | #get the size of the font | ||
| 153 | ( $h, $w ) = ( $font->height, $font->width ); | ||
| 154 | |||
| 155 | #figure out, where to draw | ||
| 156 | if ( $side =~ /^right$/i ) { | ||
| 157 | |||
| 158 | #get the right startposition | ||
| 159 | $x1 = $self->{'curr_x_max'}; | ||
| 160 | $y1 = $self->{'curr_y_max'} - $h / 2; | ||
| 161 | |||
| 162 | #get the delta values | ||
| 163 | $height = $self->{'curr_y_max'} - $self->{'curr_y_min'}; | ||
| 164 | $delta = ($height) / ( $self->{'num_datapoints'} > 0 ? $self->{'num_datapoints'} : 1 ); | ||
| 165 | $y1 -= ( $delta / 2 ); | ||
| 166 | |||
| 167 | #look if skipping is desired | ||
| 168 | if ( !defined( $self->{'skip_y_ticks'} ) ) | ||
| 169 | { | ||
| 170 | $self->{'skip_y_ticks'} = 1; | ||
| 171 | } | ||
| 172 | |||
| 173 | #draw the labels | ||
| 174 | for ( 0 .. int( ( $self->{'num_datapoints'} - 1 ) / $self->{'skip_y_ticks'} ) ) | ||
| 175 | { | ||
| 176 | $y2 = $y1 - ($delta) * ( $_ * $self->{'skip_y_ticks'} ); | ||
| 177 | $x2 = $x1 + $self->{'tick_len'} + $self->{'text_space'}; | ||
| 178 | $self->{'gd_obj'} | ||
| 179 | ->string( $font, $x2, $y2, $self->{f_y_tick}->( $data->[0][ $_ * $self->{'skip_y_ticks'} ] ), $textcolor ); | ||
| 180 | } | ||
| 181 | |||
| 182 | #draw the ticks | ||
| 183 | $x1 = $self->{'curr_x_max'}; | ||
| 184 | $x2 = $self->{'curr_x_max'} + $self->{'tick_len'}; | ||
| 185 | $y1 += $h / 2; | ||
| 186 | for ( 0 .. ( $self->{'num_datapoints'} - 1 / $self->{'skip_y_ticks'} ) ) | ||
| 187 | { | ||
| 188 | $y2 = $y1 - ( $delta * $_ ); | ||
| 189 | $self->{'gd_obj'}->line( $x1, $y2, $x2, $y2, $misccolor ); | ||
| 190 | if ( $self->true( $self->{'grid_lines'} ) | ||
| 191 | or $self->true( $self->{'x_grid_lines'} ) ) | ||
| 192 | { | ||
| 193 | $self->{'grid_data'}->{'y'}->[$_] = $y2; | ||
| 194 | } | ||
| 195 | } | ||
| 196 | |||
| 197 | } elsif ( $side =~ /^both$/i ) { | ||
| 198 | |||
| 199 | #get the right startposition | ||
| 200 | $x1 = $self->{'curr_x_max'}; | ||
| 201 | $y1 = $self->{'curr_y_max'} - $h / 2; | ||
| 202 | |||
| 203 | #get the delta values | ||
| 204 | $height = $self->{'curr_y_max'} - $self->{'curr_y_min'}; | ||
| 205 | $delta = ($height) / ( $self->{'num_datapoints'} > 0 ? $self->{'num_datapoints'} : 1 ); | ||
| 206 | $y1 -= ( $delta / 2 ); | ||
| 207 | |||
| 208 | #look if skipping is desired | ||
| 209 | if ( !defined( $self->{'skip_y_ticks'} ) ) | ||
| 210 | { | ||
| 211 | $self->{'skip_y_ticks'} = 1; | ||
| 212 | } | ||
| 213 | |||
| 214 | #first draw the right labels | ||
| 215 | for ( 0 .. int( ( $self->{'num_datapoints'} - 1 ) / $self->{'skip_y_ticks'} ) ) | ||
| 216 | { | ||
| 217 | $y2 = $y1 - ($delta) * ( $_ * $self->{'skip_y_ticks'} ); | ||
| 218 | $x2 = $x1 + $self->{'tick_len'} + $self->{'text_space'}; | ||
| 219 | $self->{'gd_obj'} | ||
| 220 | ->string( $font, $x2, $y2, $self->{f_y_tick}->( $data->[0][ $_ * $self->{'skip_y_ticks'} ] ), $textcolor ); | ||
| 221 | } | ||
| 222 | |||
| 223 | #then draw the right ticks | ||
| 224 | $x1 = $self->{'curr_x_max'}; | ||
| 225 | $x2 = $self->{'curr_x_max'} + $self->{'tick_len'}; | ||
| 226 | $y1 += $h / 2; | ||
| 227 | for ( 0 .. ( $self->{'num_datapoints'} - 1 / $self->{'skip_y_ticks'} ) ) | ||
| 228 | { | ||
| 229 | $y2 = $y1 - ( $delta * $_ ); | ||
| 230 | $self->{'gd_obj'}->line( $x1, $y2, $x2, $y2, $misccolor ); | ||
| 231 | if ( $self->true( $self->{'grid_lines'} ) | ||
| 232 | or $self->true( $self->{'x_grid_lines'} ) ) | ||
| 233 | { | ||
| 234 | $self->{'grid_data'}->{'y'}->[$_] = $y2; | ||
| 235 | } | ||
| 236 | } | ||
| 237 | |||
| 238 | #get the right startposition | ||
| 239 | $x1 = $self->{'curr_x_min'}; | ||
| 240 | $y1 = $self->{'curr_y_max'} - $h / 2; | ||
| 241 | |||
| 242 | #get the delta values for positioning | ||
| 243 | $height = $self->{'curr_y_max'} - $self->{'curr_y_min'}; | ||
| 244 | $delta = ($height) / ( $self->{'num_datapoints'} > 0 ? $self->{'num_datapoints'} : 1 ); | ||
| 245 | $y1 -= ( $delta / 2 ); | ||
| 246 | |||
| 247 | #then draw the left labels | ||
| 248 | for ( 0 .. int( ( $self->{'num_datapoints'} - 1 ) / $self->{'skip_y_ticks'} ) ) | ||
| 249 | { | ||
| 250 | $y2 = $y1 - ($delta) * ( $_ * $self->{'skip_y_ticks'} ); | ||
| 251 | $x2 = | ||
| 252 | $x1 - | ||
| 253 | $w * length( $self->{f_y_tick}->( $data->[0][ $_ * $self->{'skip_y_ticks'} ] ) ) #print the Labels right-sided | ||
| 254 | + $w * $self->{'x_tick_label_length'}; | ||
| 255 | $self->{'gd_obj'} | ||
| 256 | ->string( $font, $x2, $y2, $self->{f_y_tick}->( $data->[0][ $_ * $self->{'skip_y_ticks'} ] ), $textcolor ); | ||
| 257 | } | ||
| 258 | |||
| 259 | #update the curr_x_min val | ||
| 260 | $self->{'curr_x_min'} = $x1 + $self->{'text_space'} + $w * $self->{'x_tick_label_length'} + $self->{'tick_len'}; | ||
| 261 | |||
| 262 | #finally draw the left ticks | ||
| 263 | $x1 = $self->{'curr_x_min'}; | ||
| 264 | $x2 = $self->{'curr_x_min'} - $self->{'tick_len'}; | ||
| 265 | $y1 += $h / 2; | ||
| 266 | for ( 0 .. ( $self->{'num_datapoints'} - 1 / $self->{'skip_y_ticks'} ) ) | ||
| 267 | { | ||
| 268 | $y2 = $y1 - ( $delta * $_ ); | ||
| 269 | $self->{'gd_obj'}->line( $x1, $y2, $x2, $y2, $misccolor ); | ||
| 270 | if ( $self->true( $self->{'grid_lines'} ) | ||
| 271 | or $self->true( $self->{'x_grid_lines'} ) ) | ||
| 272 | { | ||
| 273 | $self->{'grid_data'}->{'y'}->[$_] = $y2; | ||
| 274 | } | ||
| 275 | } | ||
| 276 | } else { | ||
| 277 | |||
| 278 | #get the right startposition | ||
| 279 | $x1 = $self->{'curr_x_min'}; | ||
| 280 | $y1 = $self->{'curr_y_max'} - $h / 2; | ||
| 281 | |||
| 282 | #get the delta values for positioning | ||
| 283 | $height = $self->{'curr_y_max'} - $self->{'curr_y_min'}; | ||
| 284 | $delta = ($height) / ( $self->{'num_datapoints'} > 0 ? $self->{'num_datapoints'} : 1 ); | ||
| 285 | $y1 -= ( $delta / 2 ); | ||
| 286 | |||
| 287 | if ( !defined( $self->{'skip_y_ticks'} ) ) | ||
| 288 | { | ||
| 289 | $self->{'skip_y_ticks'} = 1; | ||
| 290 | } | ||
| 291 | |||
| 292 | #draw the labels | ||
| 293 | for ( 0 .. int( ( $self->{'num_datapoints'} - 1 ) / $self->{'skip_y_ticks'} ) ) { | ||
| 294 | $y2 = $y1 - ($delta) * ( $_ * $self->{'skip_y_ticks'} ); | ||
| 295 | $x2 = | ||
| 296 | $x1 - | ||
| 297 | $w * length( $self->{f_y_tick}->( $data->[0][ $_ * $self->{'skip_y_ticks'} ] ) ) #print the Labels right-sided | ||
| 298 | + $w * $self->{'x_tick_label_length'}; | ||
| 299 | $self->{'gd_obj'} | ||
| 300 | ->string( $font, $x2, $y2, $self->{f_y_tick}->( $data->[0][ $_ * $self->{'skip_y_ticks'} ] ), $textcolor ); | ||
| 301 | } | ||
| 302 | |||
| 303 | #update the curr_x_min val | ||
| 304 | $self->{'curr_x_min'} = $x1 + $self->{'text_space'} + $w * $self->{'x_tick_label_length'} + $self->{'tick_len'}; | ||
| 305 | |||
| 306 | #draw the ticks | ||
| 307 | $x1 = $self->{'curr_x_min'}; | ||
| 308 | $x2 = $self->{'curr_x_min'} - $self->{'tick_len'}; | ||
| 309 | $y1 += $h / 2; | ||
| 310 | for ( 0 .. ( $self->{'num_datapoints'} - 1 / $self->{'skip_y_ticks'} ) ) { | ||
| 311 | $y2 = $y1 - ( $delta * $_ ); | ||
| 312 | $self->{'gd_obj'}->line( $x1, $y2, $x2, $y2, $misccolor ); | ||
| 313 | if ( $self->true( $self->{'grid_lines'} ) | ||
| 314 | or $self->true( $self->{'x_grid_lines'} ) ) { | ||
| 315 | $self->{'grid_data'}->{'y'}->[$_] = $y2; | ||
| 316 | } | ||
| 317 | } | ||
| 318 | } | ||
| 319 | |||
| 320 | #now return | ||
| 321 | return 1; | ||
| 322 | } | ||
| 323 | |||
| 324 | ## @fn private int _find_y_scale() | ||
| 325 | # find good values for the minimum and maximum y-value on the chart | ||
| 326 | # overwrite the find_y_scale function, only to get the right f_x_ticks !!!!! | ||
| 327 | # @return status | ||
| 328 | sub _find_y_scale | ||
| 329 | { | ||
| 330 | my $self = shift; | ||
| 331 | |||
| 332 | # Predeclare vars. | ||
| 333 | my ( $d_min, $d_max ); # Dataset min & max. | ||
| 334 | my ( $p_min, $p_max ); # Plot min & max. | ||
| 335 | my ( $tickInterval, $tickCount ); | ||
| 336 | my @tickLabels; # List of labels for each tick. | ||
| 337 | my $maxtickLabelLen = 0; # The length of the longest tick label. | ||
| 338 | |||
| 339 | # Find the datatset minimum and maximum. | ||
| 340 | ( $d_min, $d_max ) = $self->_find_y_range(); | ||
| 341 | |||
| 342 | # Force the inclusion of zero if the user has requested it. | ||
| 343 | if ( $self->true( $self->{'include_zero'} ) ) | ||
| 344 | { | ||
| 345 | if ( ( $d_min * $d_max ) > 0 ) # If both are non zero and of the same sign. | ||
| 346 | { | ||
| 347 | if ( $d_min > 0 ) # If the whole scale is positive. | ||
| 348 | { | ||
| 349 | $d_min = 0; | ||
| 350 | } | ||
| 351 | else # The scale is entirely negative. | ||
| 352 | { | ||
| 353 | $d_max = 0; | ||
| 354 | } | ||
| 355 | } | ||
| 356 | } | ||
| 357 | |||
| 358 | if ( $self->{'integer_ticks_only'} =~ /^\d$/ ) | ||
| 359 | { | ||
| 360 | if ( $self->{'integer_ticks_only'} == 1 ) | ||
| 361 | { | ||
| 362 | $self->{'integer_ticks_only'} = 'true'; | ||
| 363 | } | ||
| 364 | else | ||
| 365 | { | ||
| 366 | $self->{'integer_ticks_only'} = 'false'; | ||
| 367 | } | ||
| 368 | } | ||
| 369 | if ( $self->true( $self->{'integer_ticks_only'} ) ) | ||
| 370 | { | ||
| 371 | |||
| 372 | # Allow the dataset range to be overidden by the user. | ||
| 373 | # f_min/max are booleans which indicate that the min & max should not be modified. | ||
| 374 | my $f_min = defined $self->{'min_val'}; | ||
| 375 | $d_min = $self->{'min_val'} if $f_min; | ||
| 376 | |||
| 377 | my $f_max = defined $self->{'max_val'}; | ||
| 378 | $d_max = $self->{'max_val'} if $f_max; | ||
| 379 | |||
| 380 | # Assert against the min is larger than the max. | ||
| 381 | if ( $d_min > $d_max ) | ||
| 382 | { | ||
| 383 | croak "The the specified 'min_val' & 'max_val' values are reversed (min > max: $d_min>$d_max)"; | ||
| 384 | } | ||
| 385 | |||
| 386 | # The user asked for integer ticks, force the limits to integers. | ||
| 387 | # & work out the range directly. | ||
| 388 | $p_min = $self->_round2Tick( $d_min, 1, -1 ); | ||
| 389 | $p_max = $self->_round2Tick( $d_max, 1, 1 ); | ||
| 390 | |||
| 391 | my $skip = $self->{skip_int_ticks}; | ||
| 392 | |||
| 393 | $tickInterval = $skip; | ||
| 394 | $tickCount = ( $p_max - $p_min ) / $skip + 1; | ||
| 395 | |||
| 396 | # Now sort out an array of tick labels. | ||
| 397 | |||
| 398 | for ( my $labelNum = $p_min ; $labelNum <= $p_max ; $labelNum += $tickInterval ) | ||
| 399 | { | ||
| 400 | my $labelText; | ||
| 401 | |||
| 402 | if ( defined $self->{f_x_tick} ) | ||
| 403 | { | ||
| 404 | |||
| 405 | # Is _default_f_tick function used? | ||
| 406 | if ( $self->{f_x_tick} == \&_default_f_tick ) | ||
| 407 | { | ||
| 408 | $labelText = sprintf( "%d", $labelNum ); | ||
| 409 | } | ||
| 410 | else | ||
| 411 | { | ||
| 412 | $labelText = $self->{f_x_tick}->($labelNum); | ||
| 413 | } | ||
| 414 | } | ||
| 415 | |||
| 416 | else | ||
| 417 | { | ||
| 418 | $labelText = sprintf( "%d", $labelNum ); | ||
| 419 | } | ||
| 420 | |||
| 421 | #print "labelText = $labelText\n"; | ||
| 422 | push @tickLabels, $labelText; | ||
| 423 | $maxtickLabelLen = length $labelText if $maxtickLabelLen < length $labelText; | ||
| 424 | } | ||
| 425 | |||
| 426 | } | ||
| 427 | else | ||
| 428 | { | ||
| 429 | |||
| 430 | # Allow the dataset range to be overidden by the user. | ||
| 431 | # f_min/max are booleans which indicate that the min & max should not be modified. | ||
| 432 | my $f_min = defined $self->{'min_val'}; | ||
| 433 | $d_min = $self->{'min_val'} if $f_min; | ||
| 434 | |||
| 435 | my $f_max = defined $self->{'max_val'}; | ||
| 436 | $d_max = $self->{'max_val'} if $f_max; | ||
| 437 | |||
| 438 | # Assert against the min is larger than the max. | ||
| 439 | if ( $d_min > $d_max ) | ||
| 440 | { | ||
| 441 | croak "The the specified 'min_val' & 'max_val' values are reversed (min > max: $d_min>$d_max)"; | ||
| 442 | } | ||
| 443 | |||
| 444 | # Calculate the width of the dataset. (posibly modified by the user) | ||
| 445 | my $d_width = $d_max - $d_min; | ||
| 446 | |||
| 447 | # If the width of the range is zero, forcibly widen it | ||
| 448 | # (to avoid division by zero errors elsewhere in the code). | ||
| 449 | if ( 0 == $d_width ) | ||
| 450 | { | ||
| 451 | $d_min--; | ||
| 452 | $d_max++; | ||
| 453 | $d_width = 2; | ||
| 454 | } | ||
| 455 | |||
| 456 | # Descale the range by converting the dataset width into | ||
| 457 | # a floating point exponent & mantisa pair. | ||
| 458 | my ( $rangeExponent, $rangeMantisa ) = $self->_sepFP($d_width); | ||
| 459 | my $rangeMuliplier = 10**$rangeExponent; | ||
| 460 | |||
| 461 | # Find what tick | ||
| 462 | # to use & how many ticks to plot, | ||
| 463 | # round the plot min & max to suatable round numbers. | ||
| 464 | ( $tickInterval, $tickCount, $p_min, $p_max ) = $self->_calcTickInterval( | ||
| 465 | $d_min / $rangeMuliplier, | ||
| 466 | $d_max / $rangeMuliplier, | ||
| 467 | $f_min, $f_max, | ||
| 468 | $self->{'min_y_ticks'}, | ||
| 469 | $self->{'max_y_ticks'} | ||
| 470 | ); | ||
| 471 | |||
| 472 | # Restore the tickInterval etc to the correct scale | ||
| 473 | $_ *= $rangeMuliplier foreach ( $tickInterval, $p_min, $p_max ); | ||
| 474 | |||
| 475 | #get teh precision for the labels | ||
| 476 | my $precision = $self->{'precision'}; | ||
| 477 | |||
| 478 | # Now sort out an array of tick labels. | ||
| 479 | for ( my $labelNum = $p_min ; $labelNum <= $p_max ; $labelNum += $tickInterval ) | ||
| 480 | { | ||
| 481 | my $labelText; | ||
| 482 | |||
| 483 | if ( defined $self->{f_x_tick} ) { | ||
| 484 | |||
| 485 | # Is _default_f_tick function used? | ||
| 486 | if ( $self->{f_x_tick} == \&_default_f_tick ) | ||
| 487 | { | ||
| 488 | $labelText = sprintf( "%." . $precision . "f", $labelNum ); | ||
| 489 | } | ||
| 490 | else | ||
| 491 | { | ||
| 492 | $labelText = $self->{f_x_tick}->($labelNum); | ||
| 493 | } | ||
| 494 | } | ||
| 495 | else | ||
| 496 | { | ||
| 497 | $labelText = sprintf( "%." . $precision . "f", $labelNum ); | ||
| 498 | } | ||
| 499 | |||
| 500 | $labelText = 0 if abs $labelText < (0.1 ** $precision); | ||
| 501 | |||
| 502 | #print "labelText = $labelText\n"; | ||
| 503 | push @tickLabels, $labelText; | ||
| 504 | $maxtickLabelLen = length $labelText if $maxtickLabelLen < length $labelText; | ||
| 505 | } | ||
| 506 | } | ||
| 507 | |||
| 508 | # Store the calculated data. | ||
| 509 | $self->{'min_val'} = $p_min; | ||
| 510 | $self->{'max_val'} = $p_max; | ||
| 511 | $self->{'y_ticks'} = $tickCount; | ||
| 512 | $self->{'y_tick_labels'} = \@tickLabels; | ||
| 513 | $self->{'y_tick_label_length'} = $maxtickLabelLen; | ||
| 514 | |||
| 515 | # and return. | ||
| 516 | return 1; | ||
| 517 | } | ||
| 518 | |||
| 519 | ## @fn private _draw_data | ||
| 520 | # finally get around to plotting the data for (horizontal) bars | ||
| 521 | sub _draw_data { | ||
| 522 | my $self = shift; | ||
| 523 | my $data = $self->{'dataref'}; | ||
| 524 | my $misccolor = $self->_color_role_to_index('misc'); | ||
| 525 | my ( $x1, $x2, $x3, $y1, $y2, $y3 ); | ||
| 526 | my $cut = 0; | ||
| 527 | my ( $width, $height, $delta1, $delta2, $map, $mod, $pink ); | ||
| 528 | my ( $i, $j, $color ); | ||
| 529 | |||
| 530 | # init the imagemap data field if they wanted it | ||
| 531 | if ( $self->true( $self->{'imagemap'} ) ) { | ||
| 532 | $self->{'imagemap_data'} = []; | ||
| 533 | } | ||
| 534 | |||
| 535 | # find both delta values ($delta1 for stepping between different | ||
| 536 | # datapoint names, $delta2 for setpping between datasets for that | ||
| 537 | # point) and the mapping constant | ||
| 538 | $width = $self->{'curr_x_max'} - $self->{'curr_x_min'}; | ||
| 539 | $height = $self->{'curr_y_max'} - $self->{'curr_y_min'}; | ||
| 540 | $delta1 = $height / ( $self->{'num_datapoints'} > 0 ? $self->{'num_datapoints'} : 1 ); | ||
| 541 | $map = $width / ( $self->{'max_val'} - $self->{'min_val'} ); | ||
| 542 | if ( $self->true( $self->{'spaced_bars'} ) ) { | ||
| 543 | $delta2 = $delta1 / ( $self->{'num_datasets'} + 2 ); | ||
| 544 | } else { | ||
| 545 | $delta2 = $delta1 / $self->{'num_datasets'}; | ||
| 546 | } | ||
| 547 | |||
| 548 | # get the base x-y values | ||
| 549 | $y1 = $self->{'curr_y_max'} - $delta2; | ||
| 550 | if ( $self->{'min_val'} >= 0 ) { | ||
| 551 | $x1 = $self->{'curr_x_min'}; | ||
| 552 | $mod = $self->{'min_val'}; | ||
| 553 | } elsif ( $self->{'max_val'} <= 0 ){ | ||
| 554 | $x1 = $self->{'curr_x_max'}; | ||
| 555 | $mod = $self->{'max_val'}; | ||
| 556 | } else { | ||
| 557 | $x1 = $self->{'curr_x_min'} + abs( $map * $self->{'min_val'} ); | ||
| 558 | $mod = 0; | ||
| 559 | $self->{'gd_obj'}->line( $x1, $self->{'curr_y_min'}, $x1, $self->{'curr_y_max'}, $misccolor ); | ||
| 560 | } | ||
| 561 | |||
| 562 | # draw the bars | ||
| 563 | for $i ( 1 .. $self->{'num_datasets'} ) { | ||
| 564 | |||
| 565 | # get the color for this dataset | ||
| 566 | $color = $self->_color_role_to_index( 'dataset' . ( $i - 1 ) ); | ||
| 567 | |||
| 568 | # draw every bar for this dataset | ||
| 569 | for $j ( 0 .. $self->{'num_datapoints'} ) { | ||
| 570 | |||
| 571 | # don't try to draw anything if there's no data | ||
| 572 | if ( defined( $data->[$i][$j] ) ) { | ||
| 573 | |||
| 574 | # find the bounds of the rectangle | ||
| 575 | if ( $self->true( $self->{'spaced_bars'} ) ) { | ||
| 576 | $y2 = $y1 - ( $j * $delta1 ) - ( $self->{'num_datasets'} * $delta2 ) + ( ( $i - 1 ) * $delta2 ); | ||
| 577 | } else { | ||
| 578 | $y2 = $y1 - ( $j * $delta1 ) - ( $self->{'num_datasets'} * $delta2 ) + ( ($i) * $delta2 ); | ||
| 579 | } | ||
| 580 | $x2 = $x1; | ||
| 581 | $y3 = $y2 + $delta2; | ||
| 582 | |||
| 583 | #cut the bars off, if needed | ||
| 584 | if ( $data->[$i][$j] > $self->{'max_val'} ) { | ||
| 585 | $x3 = $x1 + ( ( $self->{'max_val'} - $mod ) * $map ) - 1; | ||
| 586 | $cut = 1; | ||
| 587 | } | ||
| 588 | elsif ( $data->[$i][$j] < $self->{'min_val'} ) { | ||
| 589 | $x3 = $x1 + ( ( $self->{'min_val'} - $mod ) * $map ) + 1; | ||
| 590 | $cut = 1; | ||
| 591 | } else { | ||
| 592 | $x3 = $x1 + ( ( $data->[$i][$j] - $mod ) * $map ); | ||
| 593 | $cut = 0; | ||
| 594 | } | ||
| 595 | |||
| 596 | # draw the bar | ||
| 597 | ## y2 and y3 are reversed in some cases because GD's fill | ||
| 598 | ## algorithm is lame | ||
| 599 | if ( $data->[$i][$j] < 0 ) { | ||
| 600 | $self->{'gd_obj'}->filledRectangle( $x3, $y2, $x2, $y3, $color ); | ||
| 601 | if ( $self->true( $self->{'imagemap'} ) ) { | ||
| 602 | $self->{'imagemap_data'}->[$i][$j] = [ $x3, $y2, $x2, $y3 ]; | ||
| 603 | } | ||
| 604 | |||
| 605 | $self->{'gd_obj'}->filledRectangle( $x3, $y2, $x2, $y3, $color ); | ||
| 606 | if ( $self->true( $self->{'imagemap'} ) ) { | ||
| 607 | $self->{'imagemap_data'}->[$i][$j] = [ $x3, $y2, $x2, $y3 ]; | ||
| 608 | } | ||
| 609 | } else { | ||
| 610 | $self->{'gd_obj'}->filledRectangle( $x2, $y2, $x3, $y3, $color ); | ||
| 611 | if ( $self->true( $self->{'imagemap'} ) ) { | ||
| 612 | $self->{'imagemap_data'}->[$i][$j] = [ $x2, $y2, $x3, $y3 ]; | ||
| 613 | } | ||
| 614 | } | ||
| 615 | |||
| 616 | # now outline it. outline red if the bar had been cut off | ||
| 617 | unless ($cut) { | ||
| 618 | $self->{'gd_obj'}->rectangle( $x2, $y3, $x3, $y2, $misccolor ); | ||
| 619 | } else { | ||
| 620 | $pink = $self->{'gd_obj'}->colorAllocate( 255, 0, 255 ); | ||
| 621 | $self->{'gd_obj'}->rectangle( $x2, $y3, $x3, $y2, $pink ); | ||
| 622 | } | ||
| 623 | |||
| 624 | } else { | ||
| 625 | if ( $self->true( $self->{'imagemap'} ) ) { | ||
| 626 | $self->{'imagemap_data'}->[$i][$j] = [ undef(), undef(), undef(), undef() ]; | ||
| 627 | } | ||
| 628 | } | ||
| 629 | } | ||
| 630 | } | ||
| 631 | |||
| 632 | # and finaly box it off | ||
| 633 | $self->{'gd_obj'} | ||
| 634 | ->rectangle( $self->{'curr_x_min'}, $self->{'curr_y_min'}, $self->{'curr_x_max'}, $self->{'curr_y_max'}, $misccolor ); | ||
| 635 | return; | ||
| 636 | |||
| 637 | } | ||
| 638 | |||
| 639 | 1; # be a good module and return 1 | ||