lib/Chart/Composite.pm

filter: Statementstmt 60.0% 3 / 5 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 71.4% 5 / 7 CC 1 SCAR 1.4
CC
1
Cov
60%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
« lib/Chart/Bars.pm ↑ Summary lib/Chart/Direction.pm »
1 # charts composed of several types
2  
3 12 ▶use v5.12;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Chart::Composite;
6 our @ISA = qw(Chart::Base);
7 our $VERSION = 'v2.403.9';
8  
9 0 ▶use Chart::Base;
Subroutine BEGIN: called CC=1 SCAR=2.6
10 use GD;
11 use Carp;
12  
13 #>>>>>>>>>>>>>>>>>>>>>>>>>>#
14 # public methods go here #
15 #<<<<<<<<<<<<<<<<<<<<<<<<<<#
16  
17 ## @method int set(%opts)
18 # @param[in] %opts Hash of options to the Chart
19 # @return ok or croak
20 #
21 # @brief
22 # Set all options
23 #
24 # @details
25 # Overwrite the set function of class Base to pass
26 # options to the sub-objects later
27 sub set
28 {
29 my $self = shift;
30 my %opts = @_;
31  
32 # basic error checking on the options, just warn 'em
33 unless ( $#_ % 2 )
34 {
35 carp "Whoops, some option to be set didn't have a value.\n", "You might want to look at that.\n";
36 }
37  
38 # store the options they gave us
39 unless ( $self->{'opts'} )
40 {
41 $self->{'opts'} = {};
42 }
43  
44 # now set 'em
45 for ( keys %opts )
46 {
47 $self->{$_} = $opts{$_};
48 $self->{'opts'}{$_} = $opts{$_};
49 }
50  
51 # now return
52 return;
53 }
54  
55 ## @fn imagemap_dump()
56 # @brief
57 # Overwrite function imagemap_dump of base class
58 #
59 # @details
60 # Get the information to turn the chart into an imagemap
61 # had to override it to reassemble the \@data array correctly
62 #
63 # @return Reference to an array of the image
64 sub imagemap_dump {
65 my $self = shift;
66 my ( $i, $j );
67 my @map;
68 my $dataset_count = 0;
69  
70 # croak if they didn't ask me to remember the data, or if they're asking
71 # for the data before I generate it
72 unless ( ( $self->true( $self->{'imagemap'} ) ) && $self->{'imagemap_data'} )
73 {
74 croak "You need to set the imagemap option to true, and then call the png method, before you can get the imagemap data";
75 }
76  
77 #make a copy of the imagemap data
78 #this is the data of the first component
79 for $i ( 1 .. $#{ $self->{'sub_0'}->{'imagemap_data'} } )
80 {
81 for $j ( 0 .. $#{ $self->{'sub_0'}->{'imagemap_data'}->[$i] } )
82 {
83 $map[$i][$j] = \@{ $self->{'sub_0'}->{'imagemap_data'}->[$i][$j] };
84 }
85 $dataset_count++;
86 }
87  
88 #and add the data of the second component
89 for $i ( 1 .. $#{ $self->{'sub_1'}->{'imagemap_data'} } )
90 {
91 for $j ( 0 .. $#{ $self->{'sub_1'}->{'imagemap_data'}->[$i] } )
92 {
93 $map[ $i + $dataset_count ][$j] = \@{ $self->{'sub_1'}->{'imagemap_data'}->[$i][$j] };
94 }
95 }
96  
97 # return their copy
98 return \@map;
99  
100 }
101  
102 # private routine
103 sub __print_array
104 {
105 my @a = @_;
106 my $i;
107  
108 my $li = $#a;
109  
110 $li++;
111 print STDERR "Anzahl der Elemente = $li\n";
112 $li--;
113  
114 for ( $i = 0 ; $i <= $li ; $i++ )
115 {
116 print STDERR "\t$i\t$a[$i]\n";
117 }
118 }
119  
120 #>>>>>>>>>>>>>>>>>>>>>>>>>>>#
121 # private methods go here #
122 #<<<<<<<<<<<<<<<<<<<<<<<<<<<#
123  
124 ## @fn private int _check_data
125 # Overwrite _check_data of Chart::Base and check the internal data to be displayed.
126 #
127 # Make sure the data isn't really weird
128 # and collect some basic info about it\n
129 # @return status of check
130 sub _check_data
131 {
132 my $self = shift;
133 my $length = 0;
134  
135 # first things first, make sure we got the composite_info
136 unless ( ( $self->{'composite_info'} ) && ( $#{ $self->{'composite_info'} } == 1 ) )
137 {
138 croak "Chart::Composite needs to be told what kind of components to use";
139 }
140  
141 # make sure we don't end up dividing by zero if they ask for
142 # just one y_tick
143 if ( $self->{'y_ticks'} == 1 )
144 {
145 $self->{'y_ticks'} = 2;
146 carp "The number of y_ticks displayed must be at least 2";
147 }
148  
149 # remember the number of datasets
150 $self->{'num_datasets'} = $#{ $self->{'dataref'} };
151  
152 # remember the number of points in the largest dataset
153 $self->{'num_datapoints'} = 0;
154 for ( 0 .. $self->{'num_datasets'} )
155 {
156 if ( scalar( @{ $self->{'dataref'}[$_] } ) > $self->{'num_datapoints'} )
157 {
158 $self->{'num_datapoints'} = scalar( @{ $self->{'dataref'}[$_] } );
159 }
160 }
161  
162 # find the longest x-tick label, and remember how long it is
163 for ( @{ $self->{'dataref'}[0] } )
164 {
165 if ( length($_) > $length )
166 {
167 $length = length($_);
168 }
169 }
170 $self->{'x_tick_label_length'} = $length;
171  
172 # now split the data into sub-objects
173 $self->_split_data;
174  
175 return;
176 }
177  
178 ## @fn private _split_data
179 # split data to the composited classes
180 #
181 # create sub-objects for each type, store the appropriate
182 # data sets in each one, and stick the correct values into
183 # them (ie. 'gd_obj');
184 sub _split_data
185 {
186 my $self = shift;
187 my @types = ( $self->{'composite_info'}[0][0], $self->{'composite_info'}[1][0] );
188 my ( $ref, $i, $j );
189  
190 # Already checked for number of components in _check_data, above.
191 # we can only do two at a time
192 # if ($self->{'composite_info'}[2]) {
193 # croak "Sorry, Chart::Composite can only do two chart types at a time";
194 # }
195  
196 # load the individual modules
197 require "Chart/" . $types[0] . ".pm";
198 require "Chart/" . $types[1] . ".pm";
199  
200 # create the sub-objects
201 $self->{'sub_0'} = ( "Chart::" . $types[0] )->new();
202 $self->{'sub_1'} = ( "Chart::" . $types[1] )->new();
203  
204 # forward options to sub-objects (set the min_val, max_val, brush_size, y_ticks,
205
206 $self->{'sub_0'}->set( %{ $self->{'opts'} } );
207 $self->{'sub_1'}->set( %{ $self->{'opts'} } );
208
209 $self->{'sub_0'}->set( 'min_val' => $self->{'opts'}{'min_val1'} ) if defined $self->{'opts'}{'min_val1'};
210 $self->{'sub_0'}->set( 'max_val' => $self->{'opts'}{'max_val1'} ) if defined $self->{'opts'}{'max_val1'};
211 $self->{'sub_1'}->set( 'min_val' => $self->{'opts'}{'min_val2'} ) if defined $self->{'opts'}{'min_val2'};
212 $self->{'sub_1'}->set( 'max_val' => $self->{'opts'}{'max_val2'} ) if defined $self->{'opts'}{'max_val2'};
213 $self->{'sub_0'}->set( 'y_ticks' => $self->{'opts'}{'y_ticks1'} ) if $self->{'opts'}{'y_ticks1'};
214 $self->{'sub_1'}->set( 'y_ticks' => $self->{'opts'}{'y_ticks2'} ) if $self->{'opts'}{'y_ticks2'};
215 $self->{'sub_0'}->set( 'brush_size' => $self->{'opts'}{'brush_size1'} ) if $self->{'opts'}{'brush_size1'};
216 $self->{'sub_1'}->set( 'brush_size' => $self->{'opts'}{'brush_size2'} ) if $self->{'opts'}{'brush_size2'};
217 $self->{'sub_0'}->set( 'brushStyle' => $self->{'opts'}{'brushStyle1'} ) if $self->{'opts'}{'brushStyle1'};
218 $self->{'sub_0'}->set( 'brushStyle' => $self->{'opts'}{'brushStyle1'} ) if $self->{'opts'}{'brush_style1'};
219 $self->{'sub_1'}->set( 'brushStyle' => $self->{'opts'}{'brushStyle2'} ) if $self->{'opts'}{'brushStyle2'};
220 $self->{'sub_1'}->set( 'brushStyle' => $self->{'opts'}{'brushStyle2'} ) if $self->{'opts'}{'brush_style2'};
221 # f_y_tick for left and right axis
222 $self->{'sub_0'}->set( 'f_y_tick' => $self->{'opts'}{'f_y_tick1'} ) if defined $self->{'opts'}{'f_y_tick1'};
223 $self->{'sub_1'}->set( 'f_y_tick' => $self->{'opts'}{'f_y_tick2'} ) if defined $self->{'opts'}{'f_y_tick2'};
224  
225 # replace the gd_obj fields
226 $self->{'sub_0'}->{'gd_obj'} = $self->{'gd_obj'};
227 $self->{'sub_1'}->{'gd_obj'} = $self->{'gd_obj'};
228  
229 # let the sub-objects know they're sub-objects
230 $self->{'sub_0'}->{'component'} = 'true';
231 $self->{'sub_1'}->{'component'} = 'true';
232  
233 # give each sub-object its data
234 $self->{'component_datasets'} = [];
235 for $i ( 0 .. 1 )
236 {
237 $ref = [];
238 $self->{'component_datasets'}[$i] = $self->{'composite_info'}[$i][1];
239 push @{$ref}, $self->{'dataref'}[0];
240 for $j ( @{ $self->{'composite_info'}[$i][1] } )
241 {
242 $self->_color_role_to_index( 'dataset' . ( $j - 1 ) ); # allocate color index
243 push @{$ref}, $self->{'dataref'}[$j];
244 }
245 $self->{ 'sub_' . $i }->_copy_data($ref);
246 }
247  
248 # and let them check it
249 $self->{'sub_0'}->_check_data;
250 $self->{'sub_1'}->_check_data;
251  
252 # realign the y-axes if they want
253 if ( $self->true( $self->{'same_y_axes'} ) )
254 {
255 if ( $self->{'sub_0'}{'min_val'} < $self->{'sub_1'}{'min_val'} )
256 {
257 $self->{'sub_1'}{'min_val'} = $self->{'sub_0'}{'min_val'};
258 }
259 else
260 {
261 $self->{'sub_0'}{'min_val'} = $self->{'sub_1'}{'min_val'};
262 }
263  
264 if ( $self->{'sub_0'}{'max_val'} > $self->{'sub_1'}{'max_val'} )
265 {
266 $self->{'sub_1'}{'max_val'} = $self->{'sub_0'}{'max_val'};
267 }
268 else
269 {
270 $self->{'sub_0'}{'max_val'} = $self->{'sub_1'}{'max_val'};
271 }
272  
273 $self->{'sub_0'}->_check_data;
274 $self->{'sub_1'}->_check_data;
275 }
276  
277 # find out how big the y-tick labels will be from sub_0 and sub_1
278 $self->{'y_tick_label_length1'} = $self->{'sub_0'}->{'y_tick_label_length'};
279 $self->{'y_tick_label_length2'} = $self->{'sub_1'}->{'y_tick_label_length'};
280  
281 # now return
282 return;
283 }
284  
285 ## @fn private int _draw_legend()
286 # let the user know what all the pretty colors mean
287 # @return status
288 #
289 sub _draw_legend
290 {
291 my $self = shift;
292 my ($length);
293  
294 # check to see if they have as many labels as datasets,
295 # warn them if not
296 if ( ( $#{ $self->{'legend_labels'} } >= 0 )
297 && ( ( scalar( @{ $self->{'legend_labels'} } ) ) != $self->{'num_datasets'} ) )
298 {
299 carp "The number of legend labels and datasets doesn\'t match";
300 }
301  
302 # init a field to store the length of the longest legend label
303 unless ( $self->{'max_legend_label'} )
304 {
305 $self->{'max_legend_label'} = 0;
306 }
307  
308 # fill in the legend labels, find the longest one
309 for ( 1 .. $self->{'num_datasets'} )
310 {
311 unless ( $self->{'legend_labels'}[ $_ - 1 ] )
312 {
313 $self->{'legend_labels'}[ $_ - 1 ] = "Dataset $_";
314 }
315 $length = length( $self->{'legend_labels'}[ $_ - 1 ] );
316 if ( $length > $self->{'max_legend_label'} )
317 {
318 $self->{'max_legend_label'} = $length;
319 }
320 }
321  
322 # different legend types
323 if ( $self->{'legend'} eq 'bottom' )
324 {
325 $self->_draw_bottom_legend;
326 }
327 elsif ( $self->{'legend'} eq 'right' )
328 {
329 $self->_draw_right_legend;
330 }
331 elsif ( $self->{'legend'} eq 'left' )
332 {
333 $self->_draw_left_legend;
334 }
335 elsif ( $self->{'legend'} eq 'top' )
336 {
337 $self->_draw_top_legend;
338 }
339 elsif ( $self->{'legend'} eq 'none' )
340 {
341 $self->_draw_none_legend;
342 }
343 else
344 {
345 carp "I can't put a legend there\n";
346 }
347  
348 # and return
349 return 1;
350 }
351  
352 ## @fn private int _draw_top_legend()
353 # put the legend on the top of the data plot
354 #
355 # Overwrite the base class _draw_top_legend
356 #
357 # @return status
358 sub _draw_top_legend
359 {
360 my $self = shift;
361 my @labels = @{ $self->{'legend_labels'} };
362 my ( $x1, $y1, $x2, $y2, $empty_width, $max_label_width );
363 my ( $cols, $rows, $color );
364 my ( $col_width, $row_height, $i, $j, $r, $c, $index, $x, $y, $sub, $w, $h );
365 my ( $yh, $yi ); # for boxing legends
366 my $font = $self->{'legend_font'};
367 my ( %colors, @datasets );
368 my $max_legend_example = 0;
369 $yh = 0;
370  
371 # copy the current boundaries into the sub-objects
372 $self->_sub_update;
373  
374 # init the legend_example_height
375 $self->_legend_example_height_init;
376  
377 ## Make datasetI numbers match indexes of @{ $self->{'dataref'} }[1.....].
378 # # modify the dataset color table entries to avoid duplicating
379 # # dataset colors (this limits the number of possible data sets
380 # # for each component to 8)
381 # for (0..7) {
382 # $self->{'sub_1'}{'color_table'}{'dataset'.$_}
383 # = $self->{'color_table'}{'dataset'.($_+8)};
384 # }
385  
386 # modify the dataset color table entries to avoid duplicating
387 # dataset colors.
388 my ( $n0, $n1 ) = map { scalar @{ $self->{'composite_info'}[$_][1] } } 0 .. 1;
389 for ( 0 .. $n1 - 1 )
390 {
391 $self->{'sub_1'}{'color_table'}{ 'dataset' . $_ } = $self->{'color_table'}{ 'dataset' . ( $_ + $n0 ) };
392 }
393  
394 # make sure we use the right colors for the legend
395 @datasets = @{ $self->{'composite_info'}[0][1] };
396 $i = 0;
397 for ( 0 .. $#datasets )
398 {
399 $colors{ $datasets[$_] - 1 } = $self->{'color_table'}{ 'dataset' . ($i) };
400 $i++;
401 }
402 @datasets = @{ $self->{'composite_info'}[1][1] };
403 $i = 0;
404 for ( 0 .. $#datasets )
405 {
406 $colors{ $datasets[$_] - 1 } = $self->{'color_table'}{ 'dataset' . ( $i + $n0 ) };
407 $i++;
408 }
409  
410 # make sure we're using a real font
411 unless ( ( ref($font) ) eq 'GD::Font' )
412 {
413 croak "The subtitle font you specified isn\'t a GD Font object";
414 }
415  
416 # get the size of the font
417 ( $h, $w ) = ( $font->height, $font->width );
418  
419 # get some base x coordinates
420 $x1 =
421 $self->{'curr_x_min'} +
422 $self->{'graph_border'} +
423 $self->{'y_tick_label_length1'} * $self->{'tick_label_font'}->width +
424 $self->{'tick_len'} +
425 ( 3 * $self->{'text_space'} );
426 $x2 =
427 $self->{'curr_x_max'} -
428 $self->{'graph_border'} -
429 $self->{'y_tick_label_length2'} * $self->{'tick_label_font'}->width -
430 $self->{'tick_len'} -
431 ( 3 * $self->{'text_space'} );
432 if ( $self->{'y_label'} )
433 {
434 $x1 += $self->{'label_font'}->height + 2 * $self->{'text_space'};
435 }
436 if ( $self->{'y_label2'} )
437 {
438 $x2 -= $self->{'label_font'}->height + 2 * $self->{'text_space'};
439 }
440  
441 # figure out how wide the widest label is, then figure out how many
442 # columns we can fit into the allotted space
443 $empty_width = $x2 - $x1 - ( 2 * $self->{'legend_space'} );
444 $max_label_width =
445 $self->{'max_legend_label'} * $self->{'legend_font'}->width + 4 * $self->{'text_space'} + $self->{'legend_example_size'};
446 $cols = int( $empty_width / $max_label_width );
447 unless ($cols)
448 {
449 $cols = 1;
450 }
451 $col_width = $empty_width / $cols;
452  
453 # figure out how many rows we need and how tall they are
454 $rows = int( $self->{'num_datasets'} / $cols );
455 unless ( ( $self->{'num_datasets'} % $cols ) == 0 )
456 {
457 $rows++;
458 }
459 unless ($rows)
460 {
461 $rows = 1;
462 }
463 $row_height = $h + $self->{'text_space'};
464  
465 # box the legend off
466 $y1 = $self->{'curr_y_min'};
467 $y2 = $self->{'curr_y_min'} + $self->{'text_space'} + ( $rows * $row_height ) + ( 2 * $self->{'legend_space'} );
468 $self->{'gd_obj'}->rectangle( $x1, $y1, $x2, $y2, $self->_color_role_to_index('misc') );
469  
470 $max_legend_example = $y2 - $y1;
471  
472 # leave some space inside the legend
473 $x1 += $self->{'legend_space'} + $self->{'text_space'};
474 $x2 -= $self->{'legend_space'};
475 $y1 += $self->{'legend_space'} + $self->{'text_space'};
476 $y2 -= $self->{'legend_space'} + $self->{'text_space'};
477  
478 # draw in the actual legend
479 $r = 0; # current row
480 $c = 0; # current column
481 $yi = 0; # current dataset
482  
483 for $i ( 0 .. 1 )
484 {
485 for $j ( 0 .. $#{ $self->{'component_datasets'}[$i] } )
486 {
487  
488 # get the color
489 $color = $self->{ 'sub_' . $i }->{'color_table'}{ 'dataset' . $j };
490 $index = $self->{'component_datasets'}[$i][$j] - 1; # index in label list
491  
492 # find the x-y coordinates for the beginning of the example line
493 $x = $x1 + ( $col_width * $c );
494 $y = $y1 + ( $row_height * $r ) + $h / 2;
495  
496 # draw the example line if legend_example_height==1 or ==0
497 if ( $rows == 1 )
498 {
499 if ( $self->{ 'legend_example_height' . $yi } < $max_legend_example )
500 {
501 $yh = $self->{ 'legend_example_height' . $yi };
502 }
503 else
504 {
505 $yh = $max_legend_example;
506 }
507 }
508 else
509 {
510 if ( $self->{ 'legend_example_height' . $yi } < $row_height )
511 {
512 $yh = $self->{ 'legend_example_height' . $yi };
513 }
514 else
515 {
516 $yh = $row_height;
517 }
518 }
519 $yi++;
520 if ( $yh <= 1 )
521 {
522 $self->{'gd_obj'}->line( $x, $y, $x + $self->{'legend_example_size'}, $y, $color );
523 }
524 else
525 {
526  
527 # draw the example bar if legend_example_height > 1
528 $yh = int( $yh / 2 );
529 $self->{'gd_obj'}->filledRectangle( $x, $y - $yh, $x + $self->{'legend_example_size'}, $y + $yh, $color );
530 }
531  
532 # find the x-y coordinates for the beginning of the label
533 $x += $self->{'legend_example_size'} + 2 * $self->{'text_space'};
534 $y -= $h / 2;
535  
536 # now draw the label
537 $self->{'gd_obj'}->string( $font, $x, $y, $labels[$index], $color );
538  
539 # keep track of which row/column we're using
540 $r = ( $r + 1 ) % $rows;
541 if ( $r == 0 )
542 {
543 $c++;
544 }
545 }
546 }
547  
548 # mark of the space used
549 $self->{'curr_y_min'} += $rows * $row_height + $self->{'text_space'} + 2 * $self->{'legend_space'};
550  
551 return;
552 }
553  
554 ## @fn private int _draw_right_legend()
555 # put the legend on the right of the chart
556 #
557 # Overwrite the base class _draw_right_legend
558 #
559 # @return status
560 sub _draw_right_legend
561 {
562 my $self = shift;
563 my @labels = @{ $self->{'legend_labels'} };
564 my ( $x1, $x2, $x3, $y1, $y2, $width, $color, $misccolor, $w, $h );
565 my ($yh) = 0; # for boxing legend
566 my $font = $self->{'legend_font'};
567 my ( %colors, @datasets, $i );
568 my $max_legend_example = 0;
569  
570 # copy the current boundaries and colors into the sub-objects
571 $self->_sub_update;
572  
573 # init the legend exapmle height
574 $self->_legend_example_height_init;
575  
576 # # modify the dataset color table entries to avoid duplicating
577 # # dataset colors (this limits the number of possible data sets
578 # # for each component to 8)
579 # for (0..7) {
580 # $self->{'sub_1'}{'color_table'}{'dataset'.$_}
581 # = $self->{'color_table'}{'dataset'.($_+8)};
582 # }
583 # modify the dataset color table entries to avoid duplicating
584 # dataset colors.
585 my ( $n0, $n1 ) = map { scalar @{ $self->{'composite_info'}[$_][1] } } 0 .. 1;
586  
587 for ( 0 .. $n1 - 1 )
588 {
589 $self->{'sub_1'}{'color_table'}{ 'dataset' . $_ } = $self->{'color_table'}{ 'dataset' . ( $_ + $n0 ) };
590 }
591  
592 # make sure we use the right colors for the legend
593 @datasets = @{ $self->{'composite_info'}[0][1] };
594 $i = 0;
595 for ( 0 .. $#datasets )
596 {
597 $colors{ $datasets[$_] - 1 } = $self->{'color_table'}{ 'dataset' . ($_) };
598 $i++;
599 }
600  
601 @datasets = @{ $self->{'composite_info'}[1][1] };
602 $i = 0;
603 for ( 0 .. $#datasets )
604 {
605 $colors{ $datasets[$_] - 1 } = $self->{'color_table'}{ 'dataset' . ( $i + $n0 ) };
606 $i++;
607 }
608  
609 # make sure we're using a real font
610 unless ( ( ref($font) ) eq 'GD::Font' )
611 {
612 croak "The subtitle font you specified isn\'t a GD Font object";
613 }
614  
615 # get the size of the font
616 ( $h, $w ) = ( $font->height, $font->width );
617  
618 # get the miscellaneous color
619 $misccolor = $self->_color_role_to_index('misc');
620  
621 # find out how wide the largest label is
622 $width =
623 ( 2 * $self->{'text_space'} ) +
624 ( $self->{'max_legend_label'} * $w ) +
625 $self->{'legend_example_size'} +
626 ( 2 * $self->{'legend_space'} );
627  
628 # box the thing off
629 $x1 = $self->{'curr_x_max'} - $width;
630 $x2 = $self->{'curr_x_max'};
631 $y1 = $self->{'curr_y_min'} + $self->{'graph_border'};
632 $y2 =
633 $self->{'curr_y_min'} +
634 $self->{'graph_border'} +
635 $self->{'text_space'} +
636 ( $self->{'num_datasets'} * ( $h + $self->{'text_space'} ) ) +
637 ( 2 * $self->{'legend_space'} );
638 $self->{'gd_obj'}->rectangle( $x1, $y1, $x2, $y2, $misccolor );
639  
640 # leave that nice space inside the legend box
641 $x1 += $self->{'legend_space'};
642 $y1 += $self->{'legend_space'} + $self->{'text_space'};
643  
644 # now draw the actual legend
645 for ( 0 .. $#labels )
646 {
647  
648 # get the color
649 $color = $colors{$_};
650  
651 # find the max_legend_example
652 $max_legend_example = $self->{'legend_space'} + $h;
653  
654 # find the x-y coords
655 $x2 = $x1;
656 $x3 = $x2 + $self->{'legend_example_size'};
657 $y2 = $y1 + ( $_ * ( $self->{'text_space'} + $h ) ) + $h / 2;
658  
659 # draw the example line if legend_example_height==1 or ==0
660 if ( $self->{ 'legend_example_height' . $_ } < $max_legend_example )
661 {
662 $yh = $self->{ 'legend_example_height' . $_ };
663 }
664 else
665 {
666 $yh = $max_legend_example;
667 }
668 if ( $yh <= 1 )
669 {
670 $self->{'gd_obj'}->line( $x2, $y2, $x3, $y2, $color );
671 }
672 else
673 {
674 $yh = int( $yh / 2 );
675 $self->{'gd_obj'}->filledRectangle( $x2, $y2 - $yh, $x3, $y2 + $yh, $color );
676 }
677  
678 # now the label
679 $x2 = $x3 + ( 2 * $self->{'text_space'} );
680 $y2 -= $h / 2;
681 $self->{'gd_obj'}->string( $font, $x2, $y2, $labels[$_], $color );
682 }
683  
684 # mark off the used space
685 $self->{'curr_x_max'} -= $width;
686  
687 # and return
688 return;
689 }
690  
691 ## @fn private int _draw_left_legend()
692 # draw the legend at the left of the data plot
693 #
694 # Overwrite the base class _draw_left_legend
695 #
696 # @return status
697 sub _draw_left_legend
698 {
699 my $self = shift;
700 my @labels = @{ $self->{'legend_labels'} };
701 my ( $x1, $x2, $x3, $y1, $y2, $width, $color, $misccolor, $w, $h );
702 my $yh; # for boxing legend
703 my $font = $self->{'legend_font'};
704 my ( %colors, @datasets, $i );
705 my $max_legend_example = 0;
706  
707 # copy the current boundaries and colors into the sub-objects
708 $self->_sub_update;
709  
710 # init the legend_example height
711 $self->_legend_example_height_init;
712  
713 # # modify the dataset color table entries to avoid duplicating
714 # # dataset colors (this limits the number of possible data sets
715 # # for each component to 8)
716 # for (0..7) {
717 # $self->{'sub_1'}{'color_table'}{'dataset'.$_}
718 # = $self->{'color_table'}{'dataset'.($_+8)};
719 # }
720 # modify the dataset color table entries to avoid duplicating
721 # dataset colors.
722 my ( $n0, $n1 ) = map { scalar @{ $self->{'composite_info'}[$_][1] } } 0 .. 1;
723 for ( 0 .. $n1 - 1 )
724 {
725 $self->{'sub_1'}{'color_table'}{ 'dataset' . $_ } = $self->{'color_table'}{ 'dataset' . ( $_ + $n0 ) };
726 }
727  
728 # make sure we use the right colors for the legend
729 @datasets = @{ $self->{'composite_info'}[0][1] };
730 $i = 0;
731 for ( 0 .. $#datasets )
732 {
733 $colors{ $datasets[$_] - 1 } = $self->{'color_table'}{ 'dataset' . ($i) };
734 $i++;
735 }
736 @datasets = @{ $self->{'composite_info'}[1][1] };
737 $i = 0;
738 for ( 0 .. $#datasets )
739 {
740 $colors{ $datasets[$_] - 1 } = $self->{'color_table'}{ 'dataset' . ( $i + $n0 ) };
741 $i++;
742 }
743  
744 # make sure we're using a real font
745 unless ( ( ref($font) ) eq 'GD::Font' )
746 {
747 croak "The subtitle font you specified isn\'t a GD Font object";
748 }
749  
750 # get the size of the font
751 ( $h, $w ) = ( $font->height, $font->width );
752  
753 # get the miscellaneous color
754 $misccolor = $self->_color_role_to_index('misc');
755  
756 # find out how wide the largest label is
757 $width =
758 ( 2 * $self->{'text_space'} ) +
759 ( $self->{'max_legend_label'} * $w ) +
760 $self->{'legend_example_size'} +
761 ( 2 * $self->{'legend_space'} );
762  
763 # get some base x-y coordinates
764 $x1 = $self->{'curr_x_min'};
765 $x2 = $self->{'curr_x_min'} + $width;
766 $y1 = $self->{'curr_y_min'} + $self->{'graph_border'};
767 $y2 =
768 $self->{'curr_y_min'} +
769 $self->{'graph_border'} +
770 $self->{'text_space'} +
771 ( $self->{'num_datasets'} * ( $h + $self->{'text_space'} ) ) +
772 ( 2 * $self->{'legend_space'} );
773  
774 # box the legend off
775 $self->{'gd_obj'}->rectangle( $x1, $y1, $x2, $y2, $misccolor );
776  
777 # leave that nice space inside the legend box
778 $x1 += $self->{'legend_space'};
779 $y1 += $self->{'legend_space'} + $self->{'text_space'};
780  
781 # now draw the actual legend
782 for ( 0 .. $#labels )
783 {
784  
785 # get the color
786 $color = $colors{$_};
787  
788 # find the max_legend_example
789 $max_legend_example = $self->{'legend_space'} + $h;
790  
791 # find the x-y coords
792 $x2 = $x1;
793 $x3 = $x2 + $self->{'legend_example_size'};
794 $y2 = $y1 + ( $_ * ( $self->{'text_space'} + $h ) ) + $h / 2;
795  
796 # draw the example line if legend_example_height==1 or ==0
797 if ( $self->{ 'legend_example_height' . $_ } < $max_legend_example )
798 {
799 $yh = $self->{ 'legend_example_height' . $_ };
800 }
801 else
802 {
803 $yh = $max_legend_example;
804 }
805 if ( $yh <= 1 )
806 {
807 $self->{'gd_obj'}->line( $x2, $y2, $x3, $y2, $color );
808 }
809 else
810 {
811  
812 # draw the example bar if legend_example_height > 1
813 $yh = int( $yh / 2 );
814 $self->{'gd_obj'}->filledRectangle( $x2, $y2 - $yh, $x3, $y2 + $yh, $color );
815 }
816  
817 # now the label
818 $x2 = $x3 + ( 2 * $self->{'text_space'} );
819 $y2 -= $h / 2;
820 $self->{'gd_obj'}->string( $font, $x2, $y2, $labels[$_], $color );
821 }
822  
823 # mark off the used space
824 $self->{'curr_x_min'} += $width;
825  
826 # and return
827 return 1;
828 }
829  
830 ## @fn private int _draw_bottom_legend()
831 # put the legend on the bottom of the chart
832 #
833 # Overwrite the base class _draw_bottom_legend
834 #
835 # @return status
836 sub _draw_bottom_legend
837 {
838 my $self = shift;
839 my @labels = @{ $self->{'legend_labels'} };
840 my ( $x1, $y1, $x2, $y2, $empty_width, $max_label_width, $cols, $rows, $color );
841 my ( $col_width, $row_height, $i, $j, $r, $c, $index, $x, $y, $sub, $w, $h );
842 my ( $yh, $yi ); # for boxing legend
843 my $font = $self->{'legend_font'};
844 my ( %colors, @datasets );
845 my $max_legend_example = 0;
846 $yh = 0;
847  
848 # copy the current boundaries and colors into the sub-objects
849 $self->_sub_update;
850  
851 # init the legend example height
852 $self->_legend_example_height_init;
853  
854 # # modify the dataset color table entries to avoid duplicating
855 # # dataset colors (this limits the number of possible data sets
856 # # for each component to 8)
857 # for (0..7) {
858 # $self->{'sub_1'}{'color_table'}{'dataset'.$_}
859 # = $self->{'color_table'}{'dataset'.($_+8)};
860 # }
861 # modify the dataset color table entries to avoid duplicating
862 # dataset colors.
863 my ( $n0, $n1 ) = map { scalar @{ $self->{'composite_info'}[$_][1] } } 0 .. 1;
864 for ( 0 .. $n1 - 1 )
865 {
866 $self->{'sub_1'}{'color_table'}{ 'dataset' . $_ } = $self->{'color_table'}{ 'dataset' . ( $_ + $n0 ) };
867 }
868  
869 @datasets = @{ $self->{'composite_info'}[0][1] };
870 $i = 0;
871 for ( 0 .. $#datasets )
872 {
873 $colors{ $datasets[$_] - 1 } = $self->{'color_table'}{ 'dataset' . ($i) };
874 $i++;
875 }
876 @datasets = @{ $self->{'composite_info'}[1][1] };
877 $i = 0;
878 for ( 0 .. $#datasets )
879 {
880 $colors{ $datasets[$_] - 1 } = $self->{'color_table'}{ 'dataset' . ( $i + $n0 ) };
881 $i++;
882 }
883  
884 # make sure we're using a real font
885 unless ( ( ref($font) ) eq 'GD::Font' )
886 {
887 croak "The subtitle font you specified isn\'t a GD Font object";
888 }
889  
890 # get the size of the font
891 ( $h, $w ) = ( $font->height, $font->width );
892  
893 # figure out how many columns we can fit
894 $x1 =
895 $self->{'curr_x_min'} +
896 $self->{'graph_border'} +
897 $self->{'y_tick_label_length1'} * $self->{'tick_label_font'}->width +
898 $self->{'tick_len'} +
899 ( 3 * $self->{'text_space'} );
900 $x2 =
901 $self->{'curr_x_max'} -
902 $self->{'graph_border'} -
903 $self->{'y_tick_label_length2'} * $self->{'tick_label_font'}->width -
904 $self->{'tick_len'} -
905 ( 3 * $self->{'text_space'} );
906 if ( $self->{'y_label'} )
907 {
908 $x1 += $self->{'label_font'}->height + 2 * $self->{'text_space'};
909 }
910 if ( $self->{'y_label2'} )
911 {
912 $x2 -= $self->{'label_font'}->height + 2 * $self->{'text_space'};
913 }
914 $empty_width = $x2 - $x1 - ( 2 * $self->{'legend_space'} );
915 $max_label_width =
916 $self->{'max_legend_label'} * $self->{'legend_font'}->width + 4 * $self->{'text_space'} + $self->{'legend_example_size'};
917 $cols = int( $empty_width / $max_label_width );
918 unless ($cols)
919 {
920 $cols = 1;
921 }
922 $col_width = $empty_width / $cols;
923  
924 # figure out how many rows we need
925 $rows = int( $self->{'num_datasets'} / $cols );
926 unless ( ( $self->{'num_datasets'} % $cols ) == 0 )
927 {
928 $rows++;
929 }
930 unless ($rows)
931 {
932 $rows = 1;
933 }
934 $row_height = $h + $self->{'text_space'};
935  
936 # box it off
937 $y1 = $self->{'curr_y_max'} - $self->{'text_space'} - ( $rows * $row_height ) - ( 2 * $self->{'legend_space'} );
938 $y2 = $self->{'curr_y_max'};
939 $self->{'gd_obj'}->rectangle( $x1, $y1, $x2, $y2, $self->_color_role_to_index('misc') );
940  
941 # get the max_legend_example_height
942 $max_legend_example = $y2 - $y1;
943  
944 $x1 += $self->{'legend_space'} + $self->{'text_space'};
945 $x2 -= $self->{'legend_space'};
946 $y1 += $self->{'legend_space'} + $self->{'text_space'};
947 $y2 -= $self->{'legend_space'} + $self->{'text_space'};
948  
949 # draw in the actual legend
950 $r = 0;
951 $c = 0;
952 $yi = 0; # current dataset
953 for $i ( 0 .. 1 )
954 {
955 for $j ( 0 .. $#{ $self->{'component_datasets'}[$i] } )
956 {
957 $color = $self->{ 'sub_' . $i }->{'color_table'}{ 'dataset' . $j };
958 $index = $self->{'component_datasets'}[$i][$j] - 1;
959  
960 $x = $x1 + ( $col_width * $c );
961 $y = $y1 + ( $row_height * $r ) + $h / 2;
962  
963 # draw the example line if legend_example_height==1 or ==0
964 if ( $rows == 1 )
965 {
966 if ( $self->{ 'legend_example_height' . $yi } < $max_legend_example )
967 {
968 $yh = $self->{ 'legend_example_height' . $yi };
969 }
970 else
971 {
972 $yh = $max_legend_example;
973 }
974 }
975 else
976 {
977 if ( $self->{ 'legend_example_height' . $yi } < $row_height )
978 {
979 $yh = $self->{ 'legend_example_height' . $yi };
980 }
981 else
982 {
983 $yh = $row_height;
984 }
985 }
986 $yi++;
987 if ( $yh <= 1 )
988 {
989 $self->{'gd_obj'}->line( $x, $y, $x + $self->{'legend_example_size'}, $y, $color );
990 }
991 else
992 {
993  
994 # draw the example bar if legend_example_height > 1
995 $yh = int( $yh / 2 );
996 $self->{'gd_obj'}->filledRectangle( $x, $y - $yh, $x + $self->{'legend_example_size'}, $y + $yh, $color );
997 }
998  
999 $x += $self->{'legend_example_size'} + 2 * $self->{'text_space'};
1000 $y -= $h / 2;
1001 $self->{'gd_obj'}->string( $font, $x, $y, $labels[$index], $color );
1002  
1003 # keep track of which row/column we're using
1004 $r = ( $r + 1 ) % $rows;
1005 if ( $r == 0 )
1006 {
1007 $c++;
1008 }
1009 }
1010 }
1011  
1012 # mark of the space used
1013 $self->{'curr_y_max'} -= ( $rows * $row_height ) + 2 * $self->{'text_space'} + 2 * $self->{'legend_space'};
1014  
1015 return;
1016 }
1017  
1018 ## @fn private int _draw_none_legend()
1019 # no legend to draw.. just update the color tables for subs
1020 #
1021 # This routine overwrites this function of the Base class
1022 #
1023 # @return status
1024 sub _draw_none_legend
1025 {
1026 my $self = shift;
1027 my $status = 1;
1028  
1029 $self->_sub_update();
1030  
1031 # for (0..7) {
1032 # $self->{'sub_1'}{'color_table'}{'dataset'.$_}
1033 # = $self->{'color_table'}{'dataset'.($_+8)};
1034 # }
1035 # modify the dataset color table entries to avoid duplicating
1036 # dataset colors.
1037 my ( $n0, $n1 ) = map { scalar @{ $self->{'composite_info'}[$_][1] } } 0 .. 1;
1038 for ( 0 .. $n1 - 1 )
1039 {
1040 $self->{'sub_1'}{'color_table'}{ 'dataset' . $_ } = $self->{'color_table'}{ 'dataset' . ( $_ + $n0 ) };
1041 }
1042  
1043 return $status;
1044 }
1045  
1046 ## @fn private int _draw_ticks()
1047 # draw the ticks and tick labels
1048 #
1049 # Overwrites function _draw_ticks() of base class
1050 #
1051 # @return status
1052 sub _draw_ticks
1053 {
1054 my $self = shift;
1055  
1056 #draw the x ticks again
1057 if ( $self->true( $self->{'xy_plot'} ) )
1058 {
1059 $self->_find_x_scale;
1060  
1061 # The following statement is necessary as the
1062 # _draw_x_number_ticks() located in Base.pm does nothing know
1063 # about different y_tick_label_length variables!
1064 # This is a hack here
1065 $self->{'y_tick_label_length'} = $self->{'y_tick_label_length1'};
1066 $self->_draw_x_number_ticks;
1067 }
1068 else
1069 {
1070 $self->_draw_x_ticks;
1071 }
1072  
1073 # update the boundaries in the sub-objects
1074 $self->_boundary_update( $self, $self->{'sub_0'} );
1075 $self->_boundary_update( $self, $self->{'sub_1'} );
1076  
1077 # now the y ticks
1078 $self->_draw_y_ticks;
1079  
1080 # then return
1081 return;
1082 }
1083  
1084 ## @fn private int _draw_x_ticks()
1085 # draw the x-ticks and their labels
1086 #
1087 # Overwrites function _draw_x_ticks() of base class
1088 #
1089 # @return status
1090 sub _draw_x_ticks
1091 {
1092 my $self = shift;
1093 my $data = $self->{'dataref'};
1094 my $font = $self->{'tick_label_font'};
1095 my $textcolor = $self->_color_role_to_index('text');
1096 my $misccolor = $self->_color_role_to_index('misc');
1097 my ( $h, $w );
1098 my ( $x1, $x2, $y1, $y2 );
1099 my ( $width, $delta );
1100 my ($stag);
1101  
1102 $self->{'grid_data'}->{'x'} = [];
1103  
1104 # make sure we got a real font
1105 unless ( ( ref $font ) eq 'GD::Font' )
1106 {
1107 croak "The tick label font you specified isn\'t a GD Font object";
1108 }
1109  
1110 # get the height and width of the font
1111 ( $h, $w ) = ( $font->height, $font->width );
1112  
1113 # allow for the amount of space the y-ticks will push the
1114 # axes over to the right and to the left
1115 ## _draw_y_ticks allows 3 * text_space, not 2 * ; this caused mismatch between
1116 ## the ticks (and grid lines) and the data.
1117 # $x1 = $self->{'curr_x_min'} + ($w * $self->{'y_tick_label_length1'})
1118 # + (2 * $self->{'text_space'}) + $self->{'tick_len'};
1119 # $x2 = $self->{'curr_x_max'} - ($w * $self->{'y_tick_label_length2'})
1120 # - (2 * $self->{'text_space'}) - $self->{'tick_len'};
1121  
1122 $x1 =
1123 $self->{'curr_x_min'} + ( $w * $self->{'y_tick_label_length1'} ) + ( 3 * $self->{'text_space'} ) + $self->{'tick_len'};
1124 $x2 =
1125 $self->{'curr_x_max'} - ( $w * $self->{'y_tick_label_length2'} ) - ( 3 * $self->{'text_space'} ) - $self->{'tick_len'};
1126 $y1 = $self->{'curr_y_max'} - $h - $self->{'text_space'};
1127  
1128 # get the delta value, figure out how to draw the labels
1129 $width = $x2 - $x1;
1130 $delta = $width / ( $self->{'num_datapoints'} > 0 ? $self->{'num_datapoints'} : 1 );
1131 if ( $delta <= ( $self->{'x_tick_label_length'} * $w ) )
1132 {
1133 unless ( $self->{'x_ticks'} =~ /^vertical$/i )
1134 {
1135 $self->{'x_ticks'} = 'staggered';
1136 }
1137 }
1138  
1139 # now draw the labels
1140 if ( $self->{'x_ticks'} =~ /^normal$/i )
1141 { # normal ticks
1142 if ( $self->{'skip_x_ticks'} )
1143 {
1144 for ( 0 .. int( ( $self->{'num_datapoints'} - 1 ) / $self->{'skip_x_ticks'} ) )
1145 {
1146 $x2 =
1147 $x1 +
1148 ( $delta / 2 ) +
1149 ( $delta * ( $_ * $self->{'skip_x_ticks'} ) ) -
1150 ( $w * length( $self->{'f_x_tick'}->( $data->[0][ $_ * $self->{'skip_x_ticks'} ] ) ) ) / 2;
1151 $self->{'gd_obj'}
1152 ->string( $font, $x2, $y1, $self->{'f_x_tick'}->( $data->[0][ $_ * $self->{'skip_x_ticks'} ] ), $textcolor );
1153 }
1154 }
1155 elsif ( $self->{'custom_x_ticks'} )
1156 {
1157 for ( @{ $self->{'custom_x_ticks'} } )
1158 {
1159 $x2 = $x1 + ( $delta / 2 ) + ( $delta * $_ ) - ( $w * length( $self->{'f_x_tick'}->( $data->[0][$_] ) ) ) / 2;
1160 $self->{'gd_obj'}->string( $font, $x2, $y1, $self->{'f_x_tick'}->( $data->[0][$_] ), $textcolor );
1161 }
1162 }
1163 else
1164 {
1165 for ( 0 .. $self->{'num_datapoints'} - 1 )
1166 {
1167 $x2 = $x1 + ( $delta / 2 ) + ( $delta * $_ ) - ( $w * length( $self->{'f_x_tick'}->( $data->[0][$_] ) ) ) / 2;
1168 $self->{'gd_obj'}->string( $font, $x2, $y1, $self->{'f_x_tick'}->( $data->[0][$_] ), $textcolor );
1169 }
1170 }
1171 }
1172 elsif ( $self->{'x_ticks'} =~ /^staggered$/i )
1173 { # staggered ticks
1174 if ( $self->{'skip_x_ticks'} )
1175 {
1176 $stag = 0;
1177 for ( 0 .. int( ( $self->{'num_datapoints'} - 1 ) / $self->{'skip_x_ticks'} ) )
1178 {
1179 $x2 =
1180 $x1 +
1181 ( $delta / 2 ) +
1182 ( $delta * ( $_ * $self->{'skip_x_ticks'} ) ) -
1183 ( $w * length( $self->{'f_x_tick'}->( $data->[0][ $_ * $self->{'skip_x_ticks'} ] ) ) ) / 2;
1184 if ( ( $stag % 2 ) == 1 )
1185 {
1186 $y1 -= $self->{'text_space'} + $h;
1187 }
1188 $self->{'gd_obj'}
1189 ->string( $font, $x2, $y1, $self->{'f_x_tick'}->( $data->[0][ $_ * $self->{'skip_x_ticks'} ] ), $textcolor );
1190 if ( ( $stag % 2 ) == 1 )
1191 {
1192 $y1 += $self->{'text_space'} + $h;
1193 }
1194 $stag++;
1195 }
1196 }
1197 elsif ( $self->{'custom_x_ticks'} )
1198 {
1199 $stag = 0;
1200 for ( sort ( @{ $self->{'custom_x_ticks'} } ) )
1201 {
1202 $x2 = $x1 + ( $delta / 2 ) + ( $delta * $_ ) - ( $w * length( $self->{'f_x_tick'}->( $data->[0][$_] ) ) ) / 2;
1203 if ( ( $stag % 2 ) == 1 )
1204 {
1205 $y1 -= $self->{'text_space'} + $h;
1206 }
1207 $self->{'gd_obj'}->string( $font, $x2, $y1, $self->{'f_x_tick'}->( $data->[0][$_] ), $textcolor );
1208 if ( ( $stag % 2 ) == 1 )
1209 {
1210 $y1 += $self->{'text_space'} + $h;
1211 }
1212 $stag++;
1213 }
1214 }
1215 else
1216 {
1217 for ( 0 .. $self->{'num_datapoints'} - 1 )
1218 {
1219 $x2 = $x1 + ( $delta / 2 ) + ( $delta * $_ ) - ( $w * length( $self->{'f_x_tick'}->( $data->[0][$_] ) ) ) / 2;
1220 if ( ( $_ % 2 ) == 1 )
1221 {
1222 $y1 -= $self->{'text_space'} + $h;
1223 }
1224 $self->{'gd_obj'}->string( $font, $x2, $y1, $self->{'f_x_tick'}->( $data->[0][$_] ), $textcolor );
1225 if ( ( $_ % 2 ) == 1 )
1226 {
1227 $y1 += $self->{'text_space'} + $h;
1228 }
1229 }
1230 }
1231 }
1232 elsif ( $self->{'x_ticks'} =~ /^vertical$/i )
1233 { # vertical ticks
1234 $y1 = $self->{'curr_y_max'} - $self->{'text_space'};
1235 if ( defined( $self->{'skip_x_ticks'} ) && $self->{'skip_x_ticks'} > 1 )
1236 {
1237 for ( 0 .. int( ( $self->{'num_datapoints'} - 1 ) / $self->{'skip_x_ticks'} ) )
1238 {
1239 $x2 = $x1 + ( $delta / 2 ) + ( $delta * ( $_ * $self->{'skip_x_ticks'} ) ) - $h / 2;
1240 $y2 = $y1 - (
1241 (
1242 $self->{'x_tick_label_length'} -
1243 length( $self->{'f_x_tick'}->( $data->[0][ $_ * $self->{'skip_x_ticks'} ] ) )
1244 ) * $w
1245 );
1246 $self->{'gd_obj'}
1247 ->stringUp( $font, $x2, $y2, $self->{'f_x_tick'}->( $data->[0][ $_ * $self->{'skip_x_ticks'} ] ),
1248 $textcolor );
1249 }
1250 }
1251 elsif ( $self->{'custom_x_ticks'} )
1252 {
1253 for ( @{ $self->{'custom_x_ticks'} } )
1254 {
1255 $x2 = $x1 + ( $delta / 2 ) + ( $delta * $_ ) - $h / 2;
1256 $y2 = $y1 - ( ( $self->{'x_tick_label_length'} - length( $self->{'f_x_tick'}->( $data->[0][$_] ) ) ) * $w );
1257 $self->{'gd_obj'}->stringUp( $font, $x2, $y2, $self->{'f_x_tick'}->( $data->[0][$_] ), $textcolor );
1258 }
1259 }
1260 else
1261 {
1262 for ( 0 .. $self->{'num_datapoints'} - 1 )
1263 {
1264 $x2 = $x1 + ( $delta / 2 ) + ( $delta * $_ ) - $h / 2;
1265 $y2 = $y1 - ( ( $self->{'x_tick_label_length'} - length( $self->{'f_x_tick'}->( $data->[0][$_] ) ) ) * $w );
1266 $self->{'gd_obj'}->stringUp( $font, $x2, $y2, $self->{'f_x_tick'}->( $data->[0][$_] ), $textcolor );
1267 }
1268 }
1269 }
1270 else
1271 { # error time
1272 carp "I don't understand the type of x-ticks you specified";
1273 }
1274  
1275 # update the current y-max value
1276 if ( $self->{'x_ticks'} =~ /^normal$/i )
1277 {
1278 $self->{'curr_y_max'} -= $h + ( 2 * $self->{'text_space'} );
1279 }
1280 elsif ( $self->{'x_ticks'} =~ /^staggered$/i )
1281 {
1282 $self->{'curr_y_max'} -= ( 2 * $h ) + ( 3 * $self->{'text_space'} );
1283 }
1284 elsif ( $self->{'x_ticks'} =~ /^vertical$/i )
1285 {
1286 $self->{'curr_y_max'} -= ( $w * $self->{'x_tick_label_length'} ) + ( 2 * $self->{'text_space'} );
1287 }
1288  
1289 # now plot the ticks
1290 $y1 = $self->{'curr_y_max'};
1291 $y2 = $self->{'curr_y_max'} - $self->{'tick_len'};
1292 if ( $self->{'skip_x_ticks'} )
1293 {
1294 for ( 0 .. int( ( $self->{'num_datapoints'} - 1 ) / $self->{'skip_x_ticks'} ) )
1295 {
1296 $x2 = $x1 + ( $delta / 2 ) + ( $delta * ( $_ * $self->{'skip_x_ticks'} ) );
1297 $self->{'gd_obj'}->line( $x2, $y1, $x2, $y2, $misccolor );
1298 if ( $self->true( $self->{'grid_lines'} )
1299 or $self->true( $self->{'x_grid_lines'} ) )
1300 {
1301 $self->{'grid_data'}->{'x'}->[$_] = $x2;
1302 }
1303 }
1304 }
1305 elsif ( $self->{'custom_x_ticks'} )
1306 {
1307 for ( @{ $self->{'custom_x_ticks'} } )
1308 {
1309 $x2 = $x1 + ( $delta / 2 ) + ( $delta * $_ );
1310 $self->{'gd_obj'}->line( $x2, $y1, $x2, $y2, $misccolor );
1311 if ( $self->true( $self->{'grid_lines'} )
1312 or $self->true( $self->{'x_grid_lines'} ) )
1313 {
1314 $self->{'grid_data'}->{'x'}->[$_] = $x2;
1315 }
1316 }
1317 }
1318 else
1319 {
1320 for ( 0 .. $self->{'num_datapoints'} - 1 )
1321 {
1322 $x2 = $x1 + ( $delta / 2 ) + ( $delta * $_ );
1323 $self->{'gd_obj'}->line( $x2, $y1, $x2, $y2, $misccolor );
1324 if ( $self->true( $self->{'grid_lines'} )
1325 or $self->true( $self->{'x_grid_lines'} ) )
1326 {
1327 $self->{'grid_data'}->{'x'}->[$_] = $x2;
1328 }
1329 }
1330 }
1331  
1332 # update the current y-max value
1333 $self->{'curr_y_max'} -= $self->{'tick_len'};
1334  
1335 # and return
1336 return;
1337 }
1338  
1339 ## @fn private int _draw_y_ticks()
1340 # draw the y-ticks and their labels
1341 #
1342 # Overwrites function _draw_y_ticks() of base class
1343 #
1344 # @return status
1345 sub _draw_y_ticks
1346 {
1347 my $self = shift;
1348  
1349 # let the first guy do his
1350 $self->{'sub_0'}->_draw_y_ticks('left');
1351  
1352 # and update the other two objects
1353 $self->_boundary_update( $self->{'sub_0'}, $self );
1354 $self->_boundary_update( $self->{'sub_0'}, $self->{'sub_1'} );
1355  
1356 # now draw the other ones
1357 $self->{'sub_1'}->_draw_y_ticks('right');
1358  
1359 # and update the other two objects
1360 $self->_boundary_update( $self->{'sub_1'}, $self );
1361 $self->_boundary_update( $self->{'sub_1'}, $self->{'sub_0'} );
1362  
1363 # then return
1364 return;
1365 }
1366  
1367 ## @fn private _draw_data
1368 # finally get around to plotting the data for composite chart
1369 sub _draw_data
1370 {
1371 my $self = shift;
1372  
1373 # do a grey background if they want it
1374 if ( $self->true( $self->{'grey_background'} ) )
1375 {
1376 $self->_grey_background;
1377 $self->{'sub_0'}->{'grey_background'} = 'false';
1378 $self->{'sub_1'}->{'grey_background'} = 'false';
1379 }
1380  
1381 # draw grid again if necessary (if grey background ruined it..)
1382 unless ( !$self->true( $self->{grey_background} ) )
1383 {
1384 $self->_draw_grid_lines if ( $self->true( $self->{grid_lines} ) );
1385 $self->_draw_x_grid_lines if ( $self->true( $self->{x_grid_lines} ) );
1386 $self->_draw_y_grid_lines if ( $self->true( $self->{y_grid_lines} ) );
1387 $self->_draw_y2_grid_lines if ( $self->true( $self->{y2_grid_lines} ) );
1388 }
1389  
1390 # do a final bounds update
1391 $self->_boundary_update( $self, $self->{'sub_0'} );
1392 $self->_boundary_update( $self, $self->{'sub_1'} );
1393  
1394 # init the imagemap data field if they wanted it
1395 if ( $self->true( $self->{'imagemap'} ) )
1396 {
1397 $self->{'imagemap_data'} = [];
1398 }
1399  
1400 # now let the component modules go to work
1401  
1402 $self->{'sub_0'}->_draw_data;
1403 $self->{'sub_1'}->_draw_data;
1404  
1405 return;
1406 }
1407  
1408 ## @fn private _sub_update()
1409 # update all the necessary information in the sub-objects
1410 #
1411 # Only for Chart::Composite
1412 sub _sub_update
1413 {
1414 my $self = shift;
1415 my $sub0 = $self->{'sub_0'};
1416 my $sub1 = $self->{'sub_1'};
1417  
1418 # update the boundaries
1419 $self->_boundary_update( $self, $sub0 );
1420 $self->_boundary_update( $self, $sub1 );
1421  
1422 # copy the color tables
1423 $sub0->{'color_table'} = { %{ $self->{'color_table'} } };
1424 $sub1->{'color_table'} = { %{ $self->{'color_table'} } };
1425  
1426 # now return
1427 return;
1428 }
1429  
1430 ## @fn private _boundary_update()
1431 # copy the current gd_obj boundaries from one object to another
1432 #
1433 # Only for Chart::Composite
1434 sub _boundary_update
1435 {
1436 my $self = shift;
1437 my $from = shift;
1438 my $to = shift;
1439  
1440 $to->{'curr_x_min'} = $from->{'curr_x_min'};
1441 $to->{'curr_x_max'} = $from->{'curr_x_max'};
1442 $to->{'curr_y_min'} = $from->{'curr_y_min'};
1443 $to->{'curr_y_max'} = $from->{'curr_y_max'};
1444  
1445 return;
1446 }
1447  
1448 ## @fn private int _draw_y_grid_lines()
1449 # draw grid_lines for y
1450 #
1451 # Overwrites this function of Base
1452 sub _draw_y_grid_lines
1453 {
1454 my ($self) = shift;
1455 $self->{'sub_0'}->_draw_y_grid_lines();
1456 return;
1457 }
1458  
1459 ## @fn private int _draw_y2_grid_lines()
1460 # draw grid_lines for y
1461 #
1462 # Overwrites this function of Base
1463 sub _draw_y2_grid_lines
1464 {
1465 my ($self) = shift;
1466 $self->{'sub_1'}->_draw_y2_grid_lines();
1467 return;
1468 }
1469  
1470 ## @fn private _legend_example_height_values
1471 # init the legend_example_height_values
1472 #
1473 sub _legend_example_height_init
1474 {
1475 my $self = shift;
1476 my $a = $self->{'num_datasets'};
1477 my ( $b, $e ) = ( 0, 0 );
1478 my $bis = '..';
1479  
1480 if ( $self->false( $self->{'legend_example_height'} ) )
1481 {
1482 for my $i ( 0 .. $a )
1483 {
1484 $self->{ 'legend_example_height' . $i } = 1;
1485 }
1486 }
1487  
1488 if ( $self->true( $self->{'legend_example_height'} ) )
1489 {
1490 for my $i ( 0 .. $a )
1491 {
1492 if ( defined( $self->{ 'legend_example_height' . $i } ) ) { }
1493 else
1494 {
1495 ( $self->{ 'legend_example_height' . $i } ) = 1;
1496 }
1497 }
1498  
1499 for $b ( 0 .. $a )
1500 {
1501 for $e ( 0 .. $a )
1502 {
1503 my $anh = sprintf( $b . $bis . $e );
1504 if ( defined( $self->{ 'legend_example_height' . $anh } ) )
1505 {
1506 if ( $b > $e )
1507 {
1508 croak "Please reverse the datasetnumber in legend_example_height\n";
1509 }
1510 for ( my $n = $b ; $n <= $e ; $n++ )
1511 {
1512 $self->{ 'legend_example_height' . $n } = $self->{ 'legend_example_height' . $anh };
1513 }
1514 }
1515 }
1516 }
1517 }
1518 }
1519  
1520 ## be a good module and return 1
1521 1;
« lib/Chart/Bars.pm ↑ Summary lib/Chart/Direction.pm »