File: Direction.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 (1064 lines) | stat: -rw-r--r-- 33,340 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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
## @file
# Implementation of Chart::Direction
#
# written by
# @author Chart Group at Geodetic Fundamental Station Wettzell (Chart@fs.wettzell.de)
# @date 2015-03-01
# @version 2.4.10
#

# @section Chart::Direction
# Implements a circular oriented chart like rotating vectors
#

## @class Chart::Direction
# @brief Direction class derived class for Chart to implement direction
# charts

package Chart::Direction;

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

use strict;
use POSIX;

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

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

## @method int set(%opts)
# @param[in] %opts Hash of options to the Chart
# @return ok or croak
#
# @brief
# Set all options
#
# @details
#  main method for customizing the chart, lets users
#  specify values for different parameters\n
# dont check the number of points in the added datasets in a polarplot\n
# overwrite Base method
#
sub set
{
    my $self = shift;
    my %opts = @_;

    # basic error checking on the options, just warn 'em
    unless ( $#_ % 2 )
    {
        carp "Whoops, some option to be set didn't have a value.\n", "You might want to look at that.\n";
    }

    # set the options
    for ( keys %opts )
    {
        $self->{$_} = $opts{$_};

        # if someone wants to change the grid_lines color, we should set all
        # the colors of the grid_lines
        if ( $_ =~ /^colors$/ )
        {
            my %hash = %{ $opts{$_} };
            foreach my $key ( sort keys %hash )
            {
                if ( $key =~ /^grid_lines$/ )
                {
                    $self->{'colors'}{'y_grid_lines'}  = $hash{'grid_lines'};
                    $self->{'colors'}{'x_grid_lines'}  = $hash{'grid_lines'};
                    $self->{'colors'}{'y2_grid_lines'} = $hash{'grid_lines'};
                }
            }
        }
    }

    if ( $self->false( $self->{'polar'} ) && ( defined $self->{'croak'} ) )
    {
        carp "New data set to be added has an incorrect number of points";
    }

    # now return
    return 1;
}

## @method int add_dataset(@data)
# Add many datasets to the dataref
#
#  Graph API\n
# Overwrite Base method
#
# @param @data Dataset to add
#
sub add_dataset
{
    my $self = shift;
    my @data = @_;

    # error check the data (carp, don't croak)
    if ( $self->{'dataref'} && ( $#{ $self->{'dataref'}->[0] } != $#data ) )
    {

        #  carp "New data set to be added has an incorrect number of points";
        $self->{'croak'} = 'true';
    }

    # copy it into the dataref
    push @{ $self->{'dataref'} }, [@data];

    # now return
    return 1;
}

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

## @fn private int _find_y_scale()
# we use the find_y_scale methode to determine the labels of the circles
# and the amount of them
# @return status
#
# This function is an overwrite to the same function found in the base class
# Chart::Base
#
sub _find_y_scale
{
    my $self = shift;

    # Predeclare vars.
    my ( $d_min,        $d_max );              # Dataset min & max.
    my ( $p_min,        $p_max );              # Plot min & max.
    my ( $tickInterval, $tickCount, $skip );
    my @tickLabels;                            # List of labels for each tick.
    my $maxtickLabelLen = 0;                   # The length of the longest tick label.

    # Find the datatset minimum and maximum.
    ( $d_min, $d_max ) = $self->_find_y_range();

    # Force the inclusion of zero if the user has requested it.
    if ( $self->true( $self->{'include_zero'} ) )
    {
        if ( ( $d_min * $d_max ) > 0 )         # If both are non zero and of the same sign.
        {
            if ( $d_min > 0 )                  # If the whole scale is positive.
            {
                $d_min = 0;
            }
            else                               # The scale is entirely negative.
            {
                $d_max = 0;
            }
        }
    }

    # Allow the dataset range to be overidden by the user.
    # f_min/max are booleans which indicate that the min & max should not be modified.
    my $f_min = defined $self->{'min_val'};
    $d_min = $self->{'min_val'} if $f_min;

    my $f_max = defined $self->{'max_val'};
    $d_max = $self->{'max_val'} if $f_max;

    # Assert against the min is larger than the max.
    if ( $d_min > $d_max )
    {
        croak "The the specified 'min_val' & 'max_val' values are reversed (min > max: $d_min>$d_max)";
    }

    # Calculate the width of the dataset. (posibly modified by the user)
    my $d_width = $d_max - $d_min;

    # If the width of the range is zero, forcibly widen it
    # (to avoid division by zero errors elsewhere in the code).
    if ( 0 == $d_width )
    {
        $d_min--;
        $d_max++;
        $d_width = 2;
    }

    # Descale the range by converting the dataset width into
    # a floating point exponent & mantisa pair.
    my ( $rangeExponent, $rangeMantisa ) = $self->_sepFP($d_width);
    my $rangeMuliplier = 10**$rangeExponent;

    # Find what tick
    # to use & how many ticks to plot,
    # round the plot min & max to suatable round numbers.
    ( $tickInterval, $tickCount, $p_min, $p_max ) = $self->_calcTickInterval(
        $d_min / $rangeMuliplier,
        $d_max / $rangeMuliplier,
        $f_min, $f_max,
        $self->{'min_circles'} + 1,
        $self->{'max_circles'} + 1
    );

    # Restore the tickInterval etc to the correct scale
    $_ *= $rangeMuliplier foreach ( $tickInterval, $p_min, $p_max );

    #get the precision for the labels
    my $precision = $self->{'precision'};

    # Now sort out an array of tick labels.

    if ( $self->false( $self->{'polar'} ) )
    {
        for ( my $labelNum = $p_min ; $labelNum <= $p_max ; $labelNum += $tickInterval )
        {
            my $labelText;

            if ( defined $self->{f_y_tick} )
            {

                # Is _default_f_tick function used?
                if ( $self->{f_y_tick} == \&Chart::Base::_default_f_tick )
                {
                    $labelText = sprintf( "%." . $precision . "f", $labelNum );
                }
                else
                {

                    # print \&_default_f_tick;
                    $labelText = $self->{f_y_tick}->($labelNum);
                }
            }
            else
            {
                $labelText = sprintf( "%." . $precision . "f", $labelNum );
            }
            push @tickLabels, $labelText;
            $maxtickLabelLen = length $labelText if $maxtickLabelLen < length $labelText;
        }
    }
    else
    {

        # polar == true
        for ( my $labelNum = $p_max ; $labelNum >= $p_min ; $labelNum -= $tickInterval )
        {
            my $labelText;

            if ( defined $self->{f_y_tick} )
            {

                # Is _default_f_tick function used?
                if ( $self->{f_y_tick} == \&Chart::Base::_default_f_tick )
                {
                    $labelText = sprintf( "%." . $precision . "f", $labelNum );
                }
                else
                {

                    # print \&_default_f_tick;
                    $labelText = $self->{f_y_tick}->($labelNum);
                }
            }
            else
            {
                $labelText = sprintf( "%." . $precision . "f", $labelNum );
            }
            push @tickLabels, $labelText;
            $maxtickLabelLen = length $labelText if $maxtickLabelLen < length $labelText;
        }
    }

    # Store the calculated data.
    $self->{'min_val'}               = $p_min,
      $self->{'max_val'}             = $p_max,
      $self->{'y_ticks'}             = $tickCount,
      $self->{'y_tick_labels'}       = \@tickLabels,
      $self->{'y_tick_label_length'} = $maxtickLabelLen;

    # and return.
    return 1;
}

## @fn private _calcTickInterval($dataset_min, $dataset_max, $flag_fixed_min, $flag_fixed_max, $minTicks, $maxTicks)
# @brief
# Calculates the ticks for direction in normalised units.
#
# @details
# Calculate the Interval between ticks in y direction
# and compare the number of ticks to
# the user given values min_y_ticks, max_y_ticks
#
# @param[in] dataset_min Minimal value in y direction
# @param[in] dataset_max Maximal value in y direction
# @param[in] flag_fixed_min Indicator whether the dataset_min value is fixed
# @param[in] flag_fixed_max Indicator whether the dataset_max value is fixed
# @param[in] minTicks Minimal number of ticks wanted
# @param[in] maxTicks Maximal number of ticks wanted
# @return $tickInterval, $tickCount, $pMin, $pMax
#

sub _calcTickInterval
{
    my $self = shift;
    my (
        $min,      $max,         # The dataset min & max.
        $minF,     $maxF,        # Indicates if those min/max are fixed.
        $minTicks, $maxTicks,    # The minimum & maximum number of ticks.
    ) = @_;

    # Verify the supplied 'min_y_ticks' & 'max_y_ticks' are sensible.
    if ( $minTicks < 2 )
    {
        carp "Chart::Direction : Incorrect value for 'min_circles', too small.\n";
        $minTicks = 2;
    }

    if ( $maxTicks < 5 * $minTicks )
    {
        carp "Chart::Direction : Incorrect value for 'max_circles', too small.\n";
        $maxTicks = 5 * $minTicks;
    }

    my $width = $max - $min;
    my @divisorList;

    for ( my $baseMul = 1 ; ; $baseMul *= 10 )
    {
      TRY: foreach my $tryMul ( 1, 2, 5 )
        {

            # Calc a fresh, smaller tick interval.
            my $divisor = $baseMul * $tryMul;

            # Count the number of ticks.
            my ( $tickCount, $pMin, $pMax ) = $self->_countTicks( $min, $max, 1 / $divisor );

            # Look a the number of ticks.
            if ( $maxTicks < $tickCount )
            {

                # If it is too high, Backtrack.
                $divisor = pop @divisorList;

                # just for security:
                if ( !defined($divisor) || $divisor == 0 ) { $divisor = 1; }
                ( $tickCount, $pMin, $pMax ) = $self->_countTicks( $min, $max, 1 / $divisor );
                carp "Chart::Direction : Caution: Tick limit of $maxTicks exceeded. Backing of to an interval of "
                  . 1 / $divisor
                  . " which plots $tickCount ticks\n";

                return ( 1 / $divisor, $tickCount, $pMin, $pMax );
            }
            elsif ( $minTicks > $tickCount )
            {

                # If it is too low, try again.
                next TRY;
            }
            else
            {

                # Store the divisor for possible later backtracking.
                push @divisorList, $divisor;

                # if the min or max is fixed, check they will fit in the interval.
                next TRY if ( $minF && ( int( $min * $divisor ) != ( $min * $divisor ) ) );
                next TRY if ( $maxF && ( int( $max * $divisor ) != ( $max * $divisor ) ) );

                # If everything passes the tests, return.
                return ( 1 / $divisor, $tickCount, $pMin, $pMax );
            }
        }
    }
    die "can't happen!";
}

## @fn private int _draw_y_ticks()
# draw the circles and the axes
#
# Overwrites _draw_y_ticks() of Base class
#
# @return status
sub _draw_y_ticks
{
    my $self       = shift;
    my $data       = $self->{'dataref'};
    my $misccolor  = $self->_color_role_to_index('misc');
    my $textcolor  = $self->_color_role_to_index('text');
    my $background = $self->_color_role_to_index('background');
    my @labels     = @{ $self->{'y_tick_labels'} };
    my ( $width,     $height, $centerX, $centerY, $diameter );
    my ( $pi,        $font,   $fontW,   $fontH,   $labelX, $labelY, $label_offset );
    my ( $dia_delta, $dia,    $x,       $y,       @label_degrees, $arc, $angle_interval );

    # set up initial constant values
    $pi               = 3.14159265358979323846,
      $font           = $self->{'legend_font'},
      $fontW          = $self->{'legend_font'}->width,
      $fontH          = $self->{'legend_font'}->height,
      $angle_interval = $self->{'angle_interval'};

    if ( $self->true( $self->{'grey_background'} ) )
    {
        $background = $self->_color_role_to_index('grey_background');
    }

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

    # find width and height
    $width  = $self->{'curr_x_max'} - $self->{'curr_x_min'};
    $height = $self->{'curr_y_max'} - $self->{'curr_y_min'};

    # find center point, from which the pie will be drawn around
    $centerX = int( $width / 2 + $self->{'curr_x_min'} );
    $centerY = int( $height / 2 + $self->{'curr_y_min'} );

    # always draw a circle, which means the diameter will be the smaller
    # of the width and height. let enough space for the labels.

## @todo calculate the width of the labels

    if ( $width < $height )
    {
        $diameter = $width - 110;
    }
    else
    {
        $diameter = $height - 80;
    }

    #the difference between the diameter of two following circles;
    $dia_delta = ceil( $diameter / ( $self->{'y_ticks'} - 1 ) );

    #store the calculated data
    $self->{'centerX'}  = $centerX;
    $self->{'centerY'}  = $centerY;
    $self->{'diameter'} = $diameter;

    #draw the axes and its labels
    # set up an array of labels for the axes
    if ( $angle_interval == 0 )
    {
        @label_degrees = ();
    }
    elsif ( $angle_interval <= 5 && $angle_interval > 0 )
    {
        @label_degrees = qw(180 175 170 165 160 155 150 145 140 135 130 125 120 115
          110 105 100 95 90 85 80 75 70 65 60 55 50 45 40 35 30 25 20 15 10 5 0 355 350
          345 340 335 330 325 320 315 310 305 300 295 290 285 280 275 270 265 260 255
          250 245 240 235 230 225 220 215 210 205 200 195 190 185);
        $angle_interval = 5;
    }
    elsif ( $angle_interval <= 10 && $angle_interval > 5 )
    {
        @label_degrees = qw(180 170 160 150 140 130 120 110 100 90 80 70 60 50 40
          30 20 10 0 350 340 330 320 310 300 290 280 270 260 250 240 230 220 210 200 190);
        $angle_interval = 10;
    }
    elsif ( $angle_interval <= 15 && $angle_interval > 10 )
    {
        @label_degrees = qw(180 165 150 135 120 105 90 75 60 45 30 15 0 345 330 315 300
          285 270 255 240 225 210 195);
        $angle_interval = 15;
    }
    elsif ( $angle_interval <= 20 && $angle_interval > 15 )
    {
        @label_degrees = qw(180 160 140 120 100 80 60 40 20 0 340 320 300 280 260 240
          220 200);
        $angle_interval = 20;
    }
    elsif ( $angle_interval <= 30 && $angle_interval > 20 )
    {
        @label_degrees  = qw(180 150 120 90 60 30 0 330 300 270 240 210);
        $angle_interval = 30;
    }
    elsif ( $angle_interval <= 45 && $angle_interval > 30 )
    {
        @label_degrees  = qw(180 135 90 45 0 315 270 225);
        $angle_interval = 45;
    }
    elsif ( $angle_interval <= 90 && $angle_interval > 45 )
    {
        @label_degrees  = qw(180 90 0 270);
        $angle_interval = 90;
    }
    else
    {
        carp "The angle_interval must be between 0 and 90!\nCorrected value: 30";
        @label_degrees  = qw(180 150 120 90 60 30 0 330 300 270 240 210);
        $angle_interval = 30;
    }
    $arc = 0;

    foreach (@label_degrees)
    {

        #calculated the coordinates of the end point of the line
        $x = sin($arc) * ( $diameter / 2 + 10 ) + $centerX;
        $y = cos($arc) * ( $diameter / 2 + 10 ) + $centerY;

        #some ugly correcture
        if ( $_ == '270' ) { $y++; }

        #draw the line
        $self->{'gd_obj'}->line( $centerX, $centerY, $x, $y, $misccolor );

        #calculate the string point
        $x = sin($arc) * ( $diameter / 2 + 30 ) + $centerX - 8;
        $y = cos($arc) * ( $diameter / 2 + 28 ) + $centerY - 6;

        #draw the labels
        $self->{'gd_obj'}->string( $font, $x, $y, $_ . '', $textcolor );
        $arc += ( ($angle_interval) / 360 ) * 2 * $pi;
    }

    #draw the circles
    $dia = 0;
    foreach (@labels)
    {
        $self->{'gd_obj'}->arc( $centerX, $centerY, $dia, $dia, 0, 360, $misccolor );
        $dia += $dia_delta;
    }

    $self->{'gd_obj'}->filledRectangle(
        $centerX - length( $labels[0] ) / 2 * $fontW - 2,
        $centerY + 2,
        $centerX + 2 + $diameter / 2,
        $centerY + $fontH + 2, $background
    );

    #draw the labels of the circles
    $dia = 0;
    foreach (@labels)
    {
        $self->{'gd_obj'}->string( $font, $centerX + $dia / 2 - length($_) / 2 * $fontW, $centerY + 2, $_, $textcolor );
        $dia += $dia_delta;
    }

    return 1;
}

## @fn private int _draw_x_ticks()
# We don't need x ticks, it's all done in _draw_y_ticks
# @return status
#
# Overwrites the corresponding function in Base
#
sub _draw_x_ticks
{
    my $self = shift;

    return 1;
}

## @fn private _draw_data
# finally get around to plotting the data for direction charts
sub _draw_data
{
    my $self       = shift;
    my $data       = $self->{'dataref'};
    my $misccolor  = $self->_color_role_to_index('misc');
    my $textcolor  = $self->_color_role_to_index('text');
    my $background = $self->_color_role_to_index('background');
    my ( $width, $height, $centerX, $centerY, $diameter );
    my ( $mod, $map, $i, $j, $brush, $color, $x, $y, $winkel, $first_x, $first_y );
    my ( $arrow_x, $arrow_y, $m );
    $color = 1;

    my $pi     = 3.14159265358979323846;
    my $len    = 10;
    my $alpha  = 1;
    my $last_x = undef;
    my $last_y = undef;
    my $diff;
    my $n = 0;

    if ( $self->true( $self->{'pairs'} ) )
    {
        my $a = $self->{'num_datasets'} / 2;
        my $b = ceil($a);
        my $c = $b - $a;

        if ( $c == 0 )
        {
            croak "Wrong number of datasets for 'pairs'";
        }
    }

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

    # find width and height
    $width  = $self->{'curr_x_max'} - $self->{'curr_x_min'};
    $height = $self->{'curr_y_max'} - $self->{'curr_y_min'};

    # get the base values

    if ( $self->false( $self->{'polar'} ) )
    {
        $mod = $self->{'min_val'};
    }
    else
    {
        $mod = $self->{'max_val'};
    }
    $centerX  = $self->{'centerX'};
    $centerY  = $self->{'centerY'};
    $diameter = $self->{'diameter'};
    $diff     = $self->{'max_val'} - $self->{'min_val'};
    $diff     = 1 if $diff < 1;
    $map      = $diameter / 2 / $diff;

    $brush = $self->_prepare_brush( $color, 'point' );
    $self->{'gd_obj'}->setBrush($brush);

    # draw every line for this dataset

    if ( $self->false( $self->{'pairs'} ) )
    {
        for $j ( 1 .. $self->{'num_datasets'} )
        {
            $color = $self->_color_role_to_index( 'dataset' . ( $j - 1 ) );

            for $i ( 0 .. $self->{'num_datapoints'} - 1 )
            {

                # don't try to draw anything if there's no data
                if (   defined( $data->[$j][$i] )
                    && $data->[$j][$i] <= $self->{'max_val'}
                    && $data->[$j][$i] >= $self->{'min_val'} )
                {    #calculate the point
                    $winkel = ( 180 - ( $data->[0][$i] % 360 ) ) / 360 * 2 * $pi;

                    if ( $self->false( $self->{'polar'} ) )
                    {
                        $x = ceil( $centerX + sin($winkel) * ( $data->[$j][$i] - $mod ) * $map );
                        $y = ceil( $centerY + cos($winkel) * ( $data->[$j][$i] - $mod ) * $map );
                    }
                    else
                    {
                        $x = ceil( $centerX + sin($winkel) * ( $mod - $data->[$j][$i] ) * $map );
                        $y = ceil( $centerY + cos($winkel) * ( $mod - $data->[$j][$i] ) * $map );
                    }

                    # set the x and y values back
                    if ( $i == 0 )
                    {
                        $first_x = $x;
                        $first_y = $y;
                        $last_x  = $x;
                        $last_y  = $y;
                    }

                    if ( $self->true( $self->{'point'} ) )
                    {
                        $brush = $self->_prepare_brush( $color, 'point' );
                        $self->{'gd_obj'}->setBrush($brush);

                        #draw the point
                        $self->{'gd_obj'}->line( $x + 1, $y, $x, $y, gdBrushed );
                    }
                    if ( $self->true( $self->{'line'} ) )
                    {
                        $brush = $self->_prepare_brush( $color, 'line' );
                        $self->{'gd_obj'}->setBrush($brush);

                        #draw the line
                        if ( defined $last_x )
                        {
                            $self->{'gd_obj'}->line( $x, $y, $last_x, $last_y, gdBrushed );
                        }
                    }

                    if ( $self->true( $self->{'arrow'} ) )
                    {
                        $brush = $self->_prepare_brush( $color, 'line' );
                        $self->{'gd_obj'}->setBrush($brush);

                        #draw the arrow
                        if ( $data->[$j][$i] > $self->{'min_val'} )
                        {
                            $self->{'gd_obj'}->line( $x, $y, $centerX, $centerY, gdBrushed );

                            $arrow_x = $x - cos( $winkel - $alpha ) * $len;
                            $arrow_y = $y + sin( $winkel - $alpha ) * $len;
                            $self->{'gd_obj'}->line( $x, $y, $arrow_x, $arrow_y, gdBrushed );

                            $arrow_x = $x + sin( $pi / 2 - $winkel - $alpha ) * $len;
                            $arrow_y = $y - cos( $pi / 2 - $winkel - $alpha ) * $len;
                            $self->{'gd_obj'}->line( $x, $y, $arrow_x, $arrow_y, gdBrushed );

                        }
                    }

                    $last_x = $x;
                    $last_y = $y;

                    # store the imagemap data if they asked for it
                    if ( $self->true( $self->{'imagemap'} ) )
                    {
                        $self->{'imagemap_data'}->[$j][$i] = [ $x, $y ];
                    }
                }
                else
                {
                    if ( $self->true( $self->{'imagemap'} ) )
                    {
                        $self->{'imagemap_data'}->[$j][$i] = [ undef(), undef() ];
                    }
                }
            }    # end for

            # draw the last line to the first point
            if ( $self->true( $self->{'line'} ) )
            {
                $self->{'gd_obj'}->line( $x, $y, $first_x, $first_y, gdBrushed );
            }

        }    # end for $j
    }

    if ( $self->true( $self->{'pairs'} ) )
    {
        for ( $j = 1 ; $j <= $self->{'num_datasets'} ; $j += 2 )
        {
            if ( $j == 1 )
            {
                $color = $self->_color_role_to_index( 'dataset' . ( $j - 1 ) );
            }
            else
            {
                $color = $self->_color_role_to_index( 'dataset' . ( $j / 2 - 0.5 ) );
            }

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

            for $i ( 0 .. $self->{'num_datapoints'} - 1 )
            {

                # don't try to draw anything if there's no data
                if (   defined( $data->[$j][$i] )
                    && $data->[$j][$i] <= $self->{'max_val'}
                    && $data->[$j][$i] >= $self->{'min_val'} )
                {

                    # calculate the point
                    $winkel = ( 180 - ( $data->[$n][$i] % 360 ) ) / 360 * 2 * $pi;

                    if ( $self->false( $self->{'polar'} ) )
                    {
                        $x = ceil( $centerX + sin($winkel) * ( $data->[$j][$i] - $mod ) * $map );
                        $y = ceil( $centerY + cos($winkel) * ( $data->[$j][$i] - $mod ) * $map );
                    }
                    else
                    {
                        $x = ceil( $centerX + sin($winkel) * ( $mod - $data->[$j][$i] ) * $map );
                        $y = ceil( $centerY + cos($winkel) * ( $mod - $data->[$j][$i] ) * $map );
                    }

                    # set the x and y values back
                    if ( $i == 0 )
                    {
                        $first_x = $x;
                        $first_y = $y;
                        $last_x  = $x;
                        $last_y  = $y;
                    }

                    if ( $self->true( $self->{'point'} ) )
                    {
                        $brush = $self->_prepare_brush( $color, 'point' );
                        $self->{'gd_obj'}->setBrush($brush);

                        #draw the point
                        $self->{'gd_obj'}->line( $x + 1, $y, $x, $y, gdBrushed );
                    }
                    if ( $self->true( $self->{'line'} ) )
                    {
                        $brush = $self->_prepare_brush( $color, 'line' );
                        $self->{'gd_obj'}->setBrush($brush);

                        #draw the line
                        if ( defined $last_x )
                        {
                            $self->{'gd_obj'}->line( $x, $y, $last_x, $last_y, gdBrushed );
                        }
                        else { }
                    }

                    if ( $self->true( $self->{'arrow'} ) )
                    {
                        $brush = $self->_prepare_brush( $color, 'line' );
                        $self->{'gd_obj'}->setBrush($brush);

                        #draw the arrow
                        if ( $data->[$j][$i] > $self->{'min_val'} )
                        {
                            $self->{'gd_obj'}->line( $x, $y, $centerX, $centerY, gdBrushed );

                            $arrow_x = $x - cos( $winkel - $alpha ) * $len;
                            $arrow_y = $y + sin( $winkel - $alpha ) * $len;
                            $self->{'gd_obj'}->line( $x, $y, $arrow_x, $arrow_y, gdBrushed );

                            $arrow_x = $x + sin( $pi / 2 - $winkel - $alpha ) * $len;
                            $arrow_y = $y - cos( $pi / 2 - $winkel - $alpha ) * $len;
                            $self->{'gd_obj'}->line( $x, $y, $arrow_x, $arrow_y, gdBrushed );
                        }
                    }

                    $last_x = $x;
                    $last_y = $y;

                    # store the imagemap data if they asked for it
                    if ( $self->true( $self->{'imagemap'} ) )
                    {
                        $self->{'imagemap_data'}->[$j][$i] = [ $x, $y ];
                    }
                }    # end if ( defined ...
                else
                {
                    if ( $self->true( $self->{'imagemap'} ) )
                    {
                        $self->{'imagemap_data'}->[$j][$i] = [ undef(), undef() ];
                    }
                }
            }    #end for $i

            # draw the last line to the first point
            if ( $self->true( $self->{'line'} ) )
            {
                $self->{'gd_obj'}->line( $x, $y, $first_x, $first_y, gdBrushed );
            }
            $n += 2;
        }    # end for $j
    }    # end if pairs

    # now outline it
    $self->{'gd_obj'}
      ->rectangle( $self->{'curr_x_min'}, $self->{'curr_y_min'}, $self->{'curr_x_max'}, $self->{'curr_y_max'}, $misccolor );

    return;
}

## @fn private int _prepare_brush($color,$type)
#  set the gdBrush object to trick GD into drawing fat lines
#
#
# @param[in] color Color to be used
# @param[in] type  Type of line
# @return status
sub _prepare_brush
{
    my $self  = shift;
    my $color = shift;
    my $type  = shift;
    my ( $radius, @rgb, $brush, $white, $newcolor );

    @rgb = $self->{'gd_obj'}->rgb($color);

    # get the appropriate brush size
    if ( $type eq 'line' )
    {
        $radius = $self->{'brush_size'} / 2;
    }
    elsif ( $type eq 'point' )
    {
        $radius = $self->{'pt_size'} / 2;
    }

    # create the new image
    $brush = GD::Image->new( $radius * 2, $radius * 2 );

    # get the colors, make the background transparent
    $white = $brush->colorAllocate( 255, 255, 255 );
    $newcolor = $brush->colorAllocate(@rgb);
    $brush->transparent($white);

    # draw the circle
    $brush->arc( $radius - 1, $radius - 1, $radius, $radius, 0, 360, $newcolor );

    # fill it if we're using lines
    $brush->fill( $radius - 1, $radius - 1, $newcolor );

    # set the new image as the main object's brush
    return $brush;
}

## @fn private int _draw_legend()
# let them know what all the pretty colors mean
# @return status
#
# Overwrite corresponding function of Base
#
sub _draw_legend
{
    my $self = shift;
    my $length;

    # check to see if legend type is none..
    if ( $self->{'legend'} =~ /^none$/ )
    {
        return 1;
    }

    # check to see if they have as many labels as datasets,
    # warn them if not
    if (   ( $#{ $self->{'legend_labels'} } >= 0 )
        && ( ( scalar( @{ $self->{'legend_labels'} } ) ) != $self->{'num_datasets'} ) )
    {
        carp "The number of legend labels and datasets doesn\'t match";
    }

    # init a field to store the length of the longest legend label
    unless ( $self->{'max_legend_label'} )
    {
        $self->{'max_legend_label'} = 0;
    }

    # fill in the legend labels, find the longest one

    if ( $self->false( $self->{'pairs'} ) )
    {
        for ( 1 .. $self->{'num_datasets'} )
        {
            unless ( $self->{'legend_labels'}[ $_ - 1 ] )
            {
                $self->{'legend_labels'}[ $_ - 1 ] = "Dataset $_";
            }
            $length = length( $self->{'legend_labels'}[ $_ - 1 ] );
            if ( $length > $self->{'max_legend_label'} )
            {
                $self->{'max_legend_label'} = $length;
            }
        }    #end for
    }

    if ( $self->true( $self->{'pairs'} ) )
    {

        for ( 1 .. ceil( $self->{'num_datasets'} / 2 ) )
        {
            unless ( $self->{'legend_labels'}[ $_ - 1 ] )
            {
                $self->{'legend_labels'}[ $_ - 1 ] = "Dataset $_";
            }
            $length = length( $self->{'legend_labels'}[ $_ - 1 ] );
            if ( $length > $self->{'max_legend_label'} )
            {
                $self->{'max_legend_label'} = $length;
            }
        }
    }

    # different legend types
    if ( $self->{'legend'} eq 'bottom' )
    {
        $self->_draw_bottom_legend;
    }
    elsif ( $self->{'legend'} eq 'right' )
    {
        $self->_draw_right_legend;
    }
    elsif ( $self->{'legend'} eq 'left' )
    {
        $self->_draw_left_legend;
    }
    elsif ( $self->{'legend'} eq 'top' )
    {
        $self->_draw_top_legend;
    }
    else
    {
        carp "I can't put a legend there (at " . $self->{'legend'} . ")\n";
    }

    # and return
    return 1;
}

## @fn private array _find_y_range()
# Find minimum and maximum value of y data sets.
#
# @return ( min, max, flag_all_integers )
#
# Overwrites corresponding Base function
#
sub _find_y_range
{
    my $self = shift;
    my $data = $self->{'dataref'};

    my $max     = undef;
    my $min     = undef;
    my $k       = 1;
    my $dataset = 1;
    my $datum;

    if ( $self->false( $self->{'pairs'} ) )
    {
        for $dataset ( @$data[ 1 .. $#$data ] )
        {

            # print "dataset @$dataset\n";
            for $datum (@$dataset)
            {
                if ( defined $datum )
                {

                    #  Prettier, but probably slower:
                    #         $max = $datum unless defined $max && $max >= $datum;
                    #         $min = $datum unless defined $min && $min <= $datum;
                    if ( defined $max )
                    {
                        if    ( $datum > $max ) { $max = $datum; }
                        elsif ( $datum < $min ) { $min = $datum; }
                    }
                    else
                    {
                        $min = $max = $datum;
                    }
                }    #endif defined
            }    # end for
        }
    }

    if ( $self->true( $self->{'pairs'} ) )
    {

        # only every second dataset must be checked
        for $dataset ( @$data[$k] )
        {
            for $datum (@$dataset)
            {
                if ( defined $datum )
                {

                    #  Prettier, but probably slower:
                    #         $max = $datum unless defined $max && $max >= $datum;
                    #         $min = $datum unless defined $min && $min <= $datum;
                    if ( defined $max )
                    {
                        if    ( $datum > $max ) { $max = $datum; }
                        elsif ( $datum < $min ) { $min = $datum; }
                    }
                    else
                    {
                        $min = $max = $datum;
                    }
                }
            }
            $k += 2;
        }
    }

    ( $min, $max );
}

## be a good module and return 1
1;