lib/Chart/Points.pm

filter: Statementstmt 75.0% 6 / 8 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 81.8% 9 / 11 CC 1 SCAR 0.9
CC
1
Cov
75%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« lib/Chart/Pie.pm ↑ Summary lib/Chart/Base.pm »
1  
2 # xy charts with points (circles or other shapes)
3  
4 7 ▶use v5.12;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 package Chart::Points;
7 our @ISA = qw(Chart::Base);
8 our $VERSION = 'v2.403.9';
9  
10 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 0 ▶use GD;
Subroutine BEGIN: called CC=1 SCAR=2.6
12 use Chart::Base;
13  
14  
15 #>>>>>>>>>>>>>>>>>>>>>>>>>>#
16 # public methods go here #
17 #<<<<<<<<<<<<<<<<<<<<<<<<<<#
18  
19 #>>>>>>>>>>>>>>>>>>>>>>>>>>>#
20 # private methods go here #
21 #<<<<<<<<<<<<<<<<<<<<<<<<<<<#
22  
23 ## @fn private _draw_data
24 # finally get around to plotting the data
25 sub _draw_data
26 {
27 my $self = shift;
28 my $data = $self->{'dataref'};
29 my $misccolor = $self->_color_role_to_index('misc');
30 my ( $x1, $x2, $x3, $y1, $y2, $y3, $mod );
31 my ( $width, $height, $delta, $map, $delta_num, $zero_offset );
32 my ( $i, $j, $color, $brush );
33 my $diff;
34  
35 # init the imagemap data field if they want it
36 if ( $self->true( $self->{'imagemap'} ) )
37 {
38 $self->{'imagemap_data'} = [];
39 }
40  
41 # find the delta value between data points, as well
42 # as the mapping constant
43 $width = $self->{'curr_x_max'} - $self->{'curr_x_min'};
44 $height = $self->{'curr_y_max'} - $self->{'curr_y_min'};
45 $delta = $width / ( $self->{'num_datapoints'} > 0 ? $self->{'num_datapoints'} : 1 );
46 $diff = ( $self->{'max_val'} - $self->{'min_val'} );
47 $diff = 1 if $diff == 0;
48 $map = $height / $diff;
49  
50 #for a xy-plot, use this delta and maybe an offset for the zero-axes
51 if ( $self->true( $self->{'xy_plot'} ) )
52 {
53 $diff = ( $self->{'x_max_val'} - $self->{'x_min_val'} );
54 $diff = 1 if $diff == 0;
55 $delta_num = $width / $diff;
56  
57 if ( $self->{'x_min_val'} <= 0 && $self->{'x_max_val'} >= 0 )
58 {
59 $zero_offset = abs( $self->{'x_min_val'} ) * abs($delta_num);
60 }
61 elsif ( $self->{'x_min_val'} > 0 || $self->{'x_max_val'} < 0 )
62 {
63 $zero_offset = -$self->{'x_min_val'} * $delta_num;
64 }
65 else
66 {
67 $zero_offset = 0;
68 }
69 }
70  
71 # get the base x-y values
72 if ( $self->false( $self->{'xy_plot'} ) )
73 {
74 $x1 = $self->{'curr_x_min'} + ( $delta / 2 );
75 }
76 else
77 {
78 $x1 = $self->{'curr_x_min'};
79 }
80 if ( $self->{'min_val'} >= 0 )
81 {
82 $y1 = $self->{'curr_y_max'};
83 $mod = $self->{'min_val'};
84 }
85 elsif ( $self->{'max_val'} <= 0 )
86 {
87 $y1 = $self->{'curr_y_min'};
88 $mod = $self->{'max_val'};
89 }
90 else
91 {
92 $y1 = $self->{'curr_y_min'} + ( $map * $self->{'max_val'} );
93 $mod = 0;
94 $self->{'gd_obj'}->line( $self->{'curr_x_min'}, $y1, $self->{'curr_x_max'}, $y1, $misccolor );
95 }
96  
97 # draw the points
98 for $i ( 1 .. $self->{'num_datasets'} )
99 {
100  
101 # get the color for this dataset, and set the brush
102 $color = $self->_color_role_to_index( 'dataset' . ( $i - 1 ) );
103 my $offset = 0;
104 ( $brush, $offset ) = $self->_prepare_brush( $color, 'point', 'dataset' . ( $i - 1 ) );
105 $self->{'gd_obj'}->setBrush($brush);
106  
107 # draw every point for this dataset
108 for $j ( 0 .. $self->{'num_datapoints'} )
109 {
110  
111 # don't try to draw anything if there's no data
112 if ( defined( $data->[$i][$j] ) )
113 {
114 if ( $self->true( $self->{'xy_plot'} ) )
115 {
116 $x2 = $x1 + $delta_num * $data->[0][$j] + $zero_offset;
117 $x3 = $x2;
118 }
119 else
120 {
121 $x2 = $x1 + ( $delta * $j );
122 $x3 = $x2;
123 }
124 $y2 = $y1 - ( ( $data->[$i][$j] - $mod ) * $map );
125 $y3 = $y2;
126  
127 # draw the point only if it is within the chart borders
128 if ( $data->[$i][$j] <= $self->{'max_val'} && $data->[$i][$j] >= $self->{'min_val'} )
129 {
130 $self->{'gd_obj'}->line( $x2, $y2, $x3, $y3, gdBrushed );
131 }
132  
133 # store the imagemap data if they asked for it
134 if ( $self->true( $self->{'imagemap'} ) )
135 {
136 $self->{'imagemap_data'}->[$i][$j] = [ $x2, $y2 ];
137 }
138 }
139 }
140 }
141  
142 # and finaly box it off
143 $self->{'gd_obj'}
144 ->rectangle( $self->{'curr_x_min'}, $self->{'curr_y_min'}, $self->{'curr_x_max'}, $self->{'curr_y_max'}, $misccolor );
145 return;
146  
147 }
148  
149 1; # be a good module and return 1
« lib/Chart/Pie.pm ↑ Summary lib/Chart/Base.pm »