File: StackedBars.pm

package info (click to toggle)
libchart-perl 2.4.10ds1-2.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 1,900 kB
  • sloc: perl: 11,697; makefile: 6
file content (641 lines) | stat: -rw-r--r-- 17,958 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
## @file
# Implementation of Chart::StackedBars
#
# written by
# @author david bonner (dbonner@cs.bu.edu)
#
# maintained by the
# @author Chart Group at Geodetic Fundamental Station Wettzell (Chart@fs.wettzell.de)
# @date 2015-03-01
# @version 2.4.10
#

## @class Chart::StackedBars
# StackedBars class derived from class Base.
#
# This class provides all functions which are specific to
# stacked bars
package Chart::StackedBars;

use Chart::Base '2.4.10';
use GD;
use Carp;
use strict;

@Chart::StackedBars::ISA     = qw(Chart::Base);
$Chart::StackedBars::VERSION = '2.4.10';

#>>>>>>>>>>>>>>>>>>>>>>>>>>#
#  public methods go here  #
#<<<<<<<<<<<<<<<<<<<<<<<<<<#

#>>>>>>>>>>>>>>>>>>>>>>>>>>>#
#  private methods go here  #
#<<<<<<<<<<<<<<<<<<<<<<<<<<<#

## @fn private _check_data
# override check_data to make sure we don't get datasets with positive
# and negative values mixed
sub _check_data
{
    my $self   = shift;
    my $data   = $self->{'dataref'};
    my $length = 0;
    my ( $i, $j, $posneg );
    my $composite;

    # remember the number of datasets
    if ( defined $self->{'composite_info'} )
    {
        if ( $self->{'composite_info'}[0][0] =~ /^StackedBars$/i )
        {
            $composite = 0;
        }
        if ( $self->{'composite_info'}[1][0] =~ /^StackedBars$/i )
        {
            $composite = 1;
        }

        # $self->{'num_datasets'} = $#{$data};     ###

        $self->{'num_datasets'} = ( $#{ $self->{'composite_info'}[$composite][1] } ) + 1;
    }
    else
    {
        $self->{'num_datasets'} = $#{$data};
    }

    # remember the number of points in the largest dataset
    $self->{'num_datapoints'} = 0;
    for ( 0 .. $self->{'num_datasets'} )
    {
        if ( scalar( @{ $data->[$_] } ) > $self->{'num_datapoints'} )
        {
            $self->{'num_datapoints'} = scalar( @{ $data->[$_] } );
        }
    }

    # make sure the datasets don't mix pos and neg values
    for $i ( 0 .. $self->{'num_datapoints'} - 1 )
    {
        $posneg = '';
        for $j ( 1 .. $self->{'num_datasets'} )
        {
            if ( $data->[$j][$i] > 0 )
            {
                if ( $posneg eq 'neg' )
                {
                    croak "The values for a Chart::StackedBars data point must either be all positive or all negative";
                }
                else
                {
                    $posneg = 'pos';
                }
            }
            elsif ( $data->[$j][$i] < 0 )
            {
                if ( $posneg eq 'pos' )
                {
                    croak "The values for a Chart::StackedBars data point must either be all positive or all negative";
                }
                else
                {
                    $posneg = 'neg';
                }
            }
        }
    }

    # find good min and max y-values for the plot
    $self->_find_y_scale;

    # find the longest x-tick label
    for ( @{ $data->[0] } )
    {
        if ( length($_) > $length )
        {
            $length = length($_);
        }
    }

    # now store it in the object
    $self->{'x_tick_label_length'} = $length;

    return;
}

## @fn private _find_y_range
sub _find_y_range
{
    my $self = shift;

    #   This finds the minimum and maximum point-sum over all x points,
    #   where the point-sum is the sum of the dataset values for that point.
    #   If the y value in any dataset is undef for a given x, it simply
    #   adds nothing to the sum.

    my $data = $self->{'dataref'};
    my $max  = undef;
    my $min  = undef;
    for my $i ( 0 .. $#{ $data->[0] } )
    {    # data point
        my $sum = $data->[1]->[$i] || 0;
        for my $dataset ( @$data[ 2 .. $#$data ] )
        {    # order not important
            my $datum = $dataset->[$i];
            $sum += $datum if defined $datum;
        }
        if ( defined $max )
        {
            if    ( $sum > $max ) { $max = $sum }
            elsif ( $sum < $min ) { $min = $sum }
        }
        else { $min = $max = $sum }
    }

    # make sure all-positive or all-negative charts get anchored at
    # zero so that we don't cut out some parts of the bars
    if ( ( $max > 0 ) && ( $min > 0 ) )
    {
        $min = 0;
    }
    if ( ( $min < 0 ) && ( $max < 0 ) )
    {
        $max = 0;
    }

    ( $min, $max );
}

# ## override _find_y_scale to account for stacked bars
# sub _find_y_scale {
#   my $self = shift;
#   my $raw = $self->{'dataref'};
#   my $data = [@{$raw->[1]}];
#   my ($i, $j, $max, $min);
#   my ($order, $mult, $tmp);
#   my ($range, $delta, @dec, $y_ticks);
#   my $labels = [];
#   my $length = 0;
#
#   # use realy weird max and min values
#   $max = -999999999999;
#   $min = 999999999999;
#
#   # go through and stack them
#   for $i (0..$self->{'num_datapoints'}-1) {
#     for $j (2..$self->{'num_datasets'}) {
#       $data->[$i] += $raw->[$j][$i];
#     }
#   }
#
#   # get max and min values
#   for $i (0..$self->{'num_datapoints'}-1) {
#     if ($data->[$i] > $max) {
#       $max = $data->[$i];
#     }
#     if ($data->[$i] < $min) {
#       $min = $data->[$i];
#     }
#   }
#
#   # make sure all-positive or all-negative charts get anchored at
#   # zero so that we don't cut out some parts of the bars
#   if (($max > 0) && ($min > 0)) {
#     $min = 0;
#   }
#   if (($min < 0) && ($max < 0)) {
#     $max = 0;
#   }
#
#   # calculate good max value
#   if ($max < -10) {
#     $tmp = -$max;
#     $order = int((log $tmp) / (log 10));
#     $mult = int ($tmp / (10 ** $order));
#     $tmp = ($mult - 1) * (10 ** $order);
#     $max = -$tmp;
#   }
#   elsif ($max < 0) {
#     $max = 0;
#   }
#   elsif ($max > 10) {
#     $order = int((log $max) / (log 10));
#     $mult = int ($max / (10 ** $order));
#     $max = ($mult + 1) * (10 ** $order);
#   }
#   elsif ($max >= 0) {
#     $max = 10;
#   }
#
#   # now go for a good min
#   if ($min < -10) {
#     $tmp = -$min;
#     $order = int((log $tmp) / (log 10));
#     $mult = int ($tmp / (10 ** $order));
#     $tmp = ($mult + 1) * (10 ** $order);
#     $min = -$tmp;
#   }
#   elsif ($min < 0) {
#     $min = -10;
#   }
#   elsif ($min > 10) {
#     $order = int ((log $min) / (log 10));
#     $mult = int ($min / (10 ** $order));
#     $min = $mult * (10 ** $order);
#   }
#   elsif ($min >= 0) {
#     $min = 0;
#   }
#
#   # put the appropriate min and max values into the object if necessary
#   unless (defined ($self->{'max_val'})) {
#     $self->{'max_val'} = $max;
#   }
#   unless (defined ($self->{'min_val'})) {
#     $self->{'min_val'} = $min;
#   }
#
#   # generate the y_tick labels, store them in the object
#   # figure out which one is going to be the longest
#   $range = $self->{'max_val'} - $self->{'min_val'};
#   $y_ticks = $self->{'y_ticks'} - 1;
#   ## Don't adjust y_ticks if the user specified custom labels
#   if ($self->{'integer_ticks_only'} =~ /^true$/i && ! $self->{'y_tick_labels'}) {
#     unless (($range % $y_ticks) == 0) {
#       while (($range % $y_ticks) != 0) {
# 	$y_ticks++;
#       }
#       $self->{'y_ticks'} = $y_ticks + 1;
#     }
#   }
#
#   $delta = $range / $y_ticks;
#   for (0..$y_ticks) {
#     $tmp = $self->{'min_val'} + ($delta * $_);
#     @dec = split /\./, $tmp;
#     if ($dec[1] && (length($dec[1]) > 3)) {
#       $tmp = sprintf("%.3f", $tmp);
#     }
#     $labels->[$_] = $tmp;
#     if (length($tmp) > $length) {
#       $length = length($tmp);
#     }
#   }
#
#   # store it in the object
#   $self->{'y_tick_labels'} = $labels;
#   $self->{'y_tick_label_length'} = $length;
#
#   # and return
#   return;
# }

## @fn private _draw_data
# finally get around to plotting the data
sub _draw_data
{
    my $self      = shift;
    my $raw       = $self->{'dataref'};
    my $data      = [];
    my $misccolor = $self->_color_role_to_index('misc');
    my ( $width, $height, $delta, $map, $mod );
    my ( $x1, $y1, $x2, $y2, $x3, $y3, $i, $j, $color, $cut );
    my $pink = $self->{'gd_obj'}->colorAllocate( 255, 0, 255 );

    # init the imagemap data field if they want it
    if ( $self->true( $self->{'imagemap'} ) )
    {
        $self->{'imagemap_data'} = [];
    }

    # width and height of remaining area, delta for width of bars, mapping value
    $width = $self->{'curr_x_max'} - $self->{'curr_x_min'};

    if ( $self->true( $self->{'spaced_bars'} ) )
    {
        $delta = ( $width / ( $self->{'num_datapoints'} * 2 ) );
    }
    else
    {
        $delta = $width / $self->{'num_datapoints'};
    }
    $height = $self->{'curr_y_max'} - $self->{'curr_y_min'};
    $map = $height / ( $self->{'max_val'} - $self->{'min_val'} );

    # get the base x and y values
    $x1 = $self->{'curr_x_min'};
    if ( $self->{'min_val'} >= 0 )
    {
        $y1  = $self->{'curr_y_max'};
        $mod = $self->{'min_val'};
    }
    elsif ( $self->{'max_val'} <= 0 )
    {
        $y1  = $self->{'curr_y_min'};
        $mod = $self->{'max_val'};
    }
    else
    {
        $y1 = $self->{'curr_y_min'} + ( $map * $self->{'max_val'} );
        $mod = 0;
        $self->{'gd_obj'}->line( $self->{'curr_x_min'}, $y1, $self->{'curr_x_max'}, $y1, $misccolor );
    }

    # create another copy of the data, but stacked
    $data->[1] = [ @{ $raw->[1] } ];
    for $i ( 0 .. $self->{'num_datapoints'} - 1 )
    {
        for $j ( 2 .. $self->{'num_datasets'} )
        {
            $data->[$j][$i] = $data->[ $j - 1 ][$i] + $raw->[$j][$i];
        }
    }

    # draw the damn bars
    for $i ( 0 .. $self->{'num_datapoints'} - 1 )
    {

        # init the y values for this datapoint
        $y2 = $y1;

        for $j ( 1 .. $self->{'num_datasets'} )
        {

            # get the color
            $color = $self->_color_role_to_index( 'dataset' . ( $j - 1 ) );

            # set up the geometry for the bar
            if ( $self->true( $self->{'spaced_bars'} ) )
            {
                $x2 = $x1 + ( 2 * $i * $delta ) + ( $delta / 2 );
                $x3 = $x2 + $delta;

            }
            else
            {
                $x2 = $x1 + ( $i * $delta );
                $x3 = $x2 + $delta;
            }
            $y3 = $y1 - ( ( $data->[$j][$i] - $mod ) * $map );

            #cut the bars off, if needed
            if ( $data->[$j][$i] > $self->{'max_val'} )
            {
                $y3 = $y1 - ( ( $self->{'max_val'} - $mod ) * $map );
                $cut = 1;
            }
            elsif ( $data->[$j][$i] < $self->{'min_val'} )
            {
                $y3 = $y1 - ( ( $self->{'min_val'} - $mod ) * $map );
                $cut = 1;
            }
            else
            {
                $cut = 0;
            }

            # draw the bar
            ## y2 and y3 are reversed in some cases because GD's fill
            ## algorithm is lame
            if ( $data->[$j][$i] > 0 )
            {
                $self->{'gd_obj'}->filledRectangle( $x2, $y3, $x3, $y2, $color );
                if ( $self->true( $self->{'imagemap'} ) )
                {
                    $self->{'imagemap_data'}->[$j][$i] = [ $x2, $y3, $x3, $y2 ];
                }
            }
            else
            {
                $self->{'gd_obj'}->filledRectangle( $x2, $y2, $x3, $y3, $color );
                if ( $self->true( $self->{'imagemap'} ) )
                {
                    $self->{'imagemap_data'}->[$j][$i] = [ $x2, $y2, $x3, $y3 ];
                }
            }

            # now outline it. outline red if the bar had been cut off
            unless ($cut)
            {
                $self->{'gd_obj'}->rectangle( $x2, $y2, $x3, $y3, $misccolor );
            }
            else
            {
                $self->{'gd_obj'}->rectangle( $x2, $y2, $x3, $y3, $misccolor );
                $self->{'gd_obj'}->rectangle( $x2, $y1, $x3, $y3, $pink );
            }

            # now bootstrap the y values
            $y2 = $y3;
        }
    }

    # and finaly box it off
    $self->{'gd_obj'}
      ->rectangle( $self->{'curr_x_min'}, $self->{'curr_y_min'}, $self->{'curr_x_max'}, $self->{'curr_y_max'}, $misccolor );
    return;

}

## @fn private _draw_left_legend
sub _draw_left_legend
{
    my $self   = shift;
    my @labels = @{ $self->{'legend_labels'} };
    my ( $x1, $x2, $x3, $y1, $y2, $width, $color, $misccolor, $w, $h, $brush );
    my $font = $self->{'legend_font'};

    # make sure we're using a real font
    unless ( ( ref($font) ) eq 'GD::Font' )
    {
        croak "The subtitle font you specified isn\'t a GD Font object";
    }

    # get the size of the font
    ( $h, $w ) = ( $font->height, $font->width );

    # get the miscellaneous color
    $misccolor = $self->_color_role_to_index('misc');

    # find out how wide the largest label is
    $width =
      ( 2 * $self->{'text_space'} ) +
      ( $self->{'max_legend_label'} * $w ) +
      $self->{'legend_example_size'} +
      ( 2 * $self->{'legend_space'} );

    # get some base x-y coordinates
    $x1 = $self->{'curr_x_min'};
    $x2 = $self->{'curr_x_min'} + $width;
    $y1 = $self->{'curr_y_min'} + $self->{'graph_border'};
    $y2 =
      $self->{'curr_y_min'} +
      $self->{'graph_border'} +
      $self->{'text_space'} +
      ( $self->{'num_datasets'} * ( $h + $self->{'text_space'} ) ) +
      ( 2 * $self->{'legend_space'} );

    # box the legend off
    $self->{'gd_obj'}->rectangle( $x1, $y1, $x2, $y2, $misccolor );

    # leave that nice space inside the legend box
    $x1 += $self->{'legend_space'};
    $y1 += $self->{'legend_space'} + $self->{'text_space'};

    # now draw the actual legend
    for ( 0 .. $#labels )
    {

        # get the color
        my $c = $self->{'num_datasets'} - $_ - 1;

        # color of the datasets in the legend
        if ( $self->{'dataref'}[1][0] < 0 )
        {
            $color = $self->_color_role_to_index( 'dataset' . $_ );
        }
        else
        {
            $color = $self->_color_role_to_index( 'dataset' . $c );
        }

        # find the x-y coords
        $x2 = $x1;
        $x3 = $x2 + $self->{'legend_example_size'};
        $y2 = $y1 + ( $_ * ( $self->{'text_space'} + $h ) ) + $h / 2;

        # do the line first
        $self->{'gd_obj'}->line( $x2, $y2, $x3, $y2, $color );

        # reset the brush for points
        $brush = $self->_prepare_brush( $color, 'point', $self->{ 'pointStyle' . $_ } );
        $self->{'gd_obj'}->setBrush($brush);

        # draw the point
        $self->{'gd_obj'}->line( int( ( $x3 + $x2 ) / 2 ), $y2, int( ( $x3 + $x2 ) / 2 ), $y2, gdBrushed );

        # now the label
        $x2 = $x3 + ( 2 * $self->{'text_space'} );
        $y2 -= $h / 2;

        # order of the datasets in the legend
        if ( $self->{'dataref'}[1][0] < 0 )
        {
            $self->{'gd_obj'}->string( $font, $x2, $y2, $labels[$_], $color );
        }
        else
        {
            $self->{'gd_obj'}->string( $font, $x2, $y2, $labels[$c], $color );
        }
    }

    # mark off the used space
    $self->{'curr_x_min'} += $width;

    # and return
    return 1;
}

## @fn private _draw_right_legend
sub _draw_right_legend
{
    my $self   = shift;
    my @labels = @{ $self->{'legend_labels'} };
    my ( $x1, $x2, $x3, $y1, $y2, $width, $color, $misccolor, $w, $h, $brush );
    my $font = $self->{'legend_font'};

    # make sure we're using a real font
    unless ( ( ref($font) ) eq 'GD::Font' )
    {
        croak "The subtitle font you specified isn\'t a GD Font object";
    }

    # get the size of the font
    ( $h, $w ) = ( $font->height, $font->width );

    # get the miscellaneous color
    $misccolor = $self->_color_role_to_index('misc');

    # find out how wide the largest label is
    $width =
      ( 2 * $self->{'text_space'} ) +
      ( $self->{'max_legend_label'} * $w ) +
      $self->{'legend_example_size'} +
      ( 2 * $self->{'legend_space'} );

    # get some starting x-y values
    $x1 = $self->{'curr_x_max'} - $width;
    $x2 = $self->{'curr_x_max'};
    $y1 = $self->{'curr_y_min'} + $self->{'graph_border'};
    $y2 =
      $self->{'curr_y_min'} +
      $self->{'graph_border'} +
      $self->{'text_space'} +
      ( $self->{'num_datasets'} * ( $h + $self->{'text_space'} ) ) +
      ( 2 * $self->{'legend_space'} );

    # box the legend off
    $self->{'gd_obj'}->rectangle( $x1, $y1, $x2, $y2, $misccolor );

    # leave that nice space inside the legend box
    $x1 += $self->{'legend_space'};
    $y1 += $self->{'legend_space'} + $self->{'text_space'};

    # now draw the actual legend
    for ( 0 .. $#labels )
    {

        # get the color
        my $c = $self->{'num_datasets'} - $_ - 1;

        # color of the datasets in the legend

        if ( $self->{'dataref'}[1][0] < 0 )
        {
            $color = $self->_color_role_to_index( 'dataset' . $_ );
        }
        else
        {
            $color = $self->_color_role_to_index( 'dataset' . $c );
        }

        # find the x-y coords
        $x2 = $x1;
        $x3 = $x2 + $self->{'legend_example_size'};
        $y2 = $y1 + ( $_ * ( $self->{'text_space'} + $h ) ) + $h / 2;

        # do the line first
        $self->{'gd_obj'}->line( $x2, $y2, $x3, $y2, $color );

        # reset the brush for points
        $brush = $self->_prepare_brush( $color, 'point', $self->{ 'pointStyle' . $_ } );
        $self->{'gd_obj'}->setBrush($brush);

        # draw the point
        $self->{'gd_obj'}->line( int( ( $x3 + $x2 ) / 2 ), $y2, int( ( $x3 + $x2 ) / 2 ), $y2, gdBrushed );

        # now the label
        $x2 = $x3 + ( 2 * $self->{'text_space'} );
        $y2 -= $h / 2;

        # order of the datasets in the legend
        if ( $self->{'dataref'}[1][0] < 0 )
        {
            $self->{'gd_obj'}->string( $font, $x2, $y2, $labels[$_], $color );
        }
        else
        {
            $self->{'gd_obj'}->string( $font, $x2, $y2, $labels[$c], $color );
        }
    }

    # mark off the used space
    $self->{'curr_x_max'} -= $width;

    # and return
    return 1;
}

## be a good module and return 1
1;