| 1 | |||
| 2 | |||
| 3 | # Some Mountain chart details: | ||
| 4 | # | ||
| 5 | # The effective y data value for a given x point and dataset | ||
| 6 | # is the sum of the actual y data values of that dataset and | ||
| 7 | # all datasets "below" it (i.e., with higher dataset indexes). | ||
| 8 | # | ||
| 9 | # If the y data value in any dataset is undef or negative for | ||
| 10 | # a given x, then all datasets are treated as missing for that x. | ||
| 11 | # | ||
| 12 | # The y minimum is always forced to zero. | ||
| 13 | # | ||
| 14 | # To avoid a dataset area "cutting into" the area of the dataset below | ||
| 15 | # it, the y pixel for each dataset point will never be below the y pixel for | ||
| 16 | # the same point in the dataset below the dataset. | ||
| 17 | |||
| 18 | # This probably should have a custom legend method, because each | ||
| 19 | # dataset is identified by the fill color (and optional pattern) | ||
| 20 | # of its area, not just a line color. So the legend shou a square | ||
| 21 | # of the color and pattern for each dataset. | ||
| 22 | |||
| 23 | 4 | ▶ | use v5.12; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 24 | |||
| 25 | package Chart::Mountain; | ||
| 26 | our @ISA = qw(Chart::Base); | ||
| 27 | our $VERSION = 'v2.403.9'; | ||
| 28 | |||
| 29 | 0 | ▶ | use Chart::Base; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 30 | use GD; | ||
| 31 | use Carp; | ||
| 32 | |||
| 33 | #===================# | ||
| 34 | # private methods # | ||
| 35 | #===================# | ||
| 36 | |||
| 37 | ## @fn private array _find_y_range() | ||
| 38 | # Find minimum and maximum value of y data sets. | ||
| 39 | # | ||
| 40 | # @return ( min, max, flag_all_integers ) | ||
| 41 | sub _find_y_range | ||
| 42 | { | ||
| 43 | my $self = shift; | ||
| 44 | |||
| 45 | # This finds the maximum point-sum over all x points, | ||
| 46 | # where the point-sum is the sum of the dataset values at that point. | ||
| 47 | # If the y value in any dataset is undef for a given x, then all datasets | ||
| 48 | # are treated as missing for that x. | ||
| 49 | |||
| 50 | my $data = $self->{'dataref'}; | ||
| 51 | my $max = undef; | ||
| 52 | for my $i ( 0 .. $#{ $data->[0] } ) | ||
| 53 | { | ||
| 54 | my $y_sum = $data->[1]->[$i]; | ||
| 55 | if ( defined $y_sum && $y_sum >= 0 ) | ||
| 56 | { | ||
| 57 | for my $dataset ( @$data[ 2 .. $#$data ] ) | ||
| 58 | { # order not important | ||
| 59 | my $datum = $dataset->[$i]; | ||
| 60 | if ( defined $datum && $datum >= 0 ) | ||
| 61 | { | ||
| 62 | $y_sum += $datum; | ||
| 63 | } | ||
| 64 | else | ||
| 65 | { # undef or negative, treat all at same x as missing. | ||
| 66 | $y_sum = undef; | ||
| 67 | last; | ||
| 68 | } | ||
| 69 | } | ||
| 70 | } | ||
| 71 | if ( defined $y_sum ) | ||
| 72 | { | ||
| 73 | $max = $y_sum unless defined $max && $y_sum <= $max; | ||
| 74 | } | ||
| 75 | } | ||
| 76 | |||
| 77 | ( 0, $max ); | ||
| 78 | } | ||
| 79 | |||
| 80 | ## @fn private _draw_data | ||
| 81 | # draw the data | ||
| 82 | sub _draw_data | ||
| 83 | { | ||
| 84 | my $self = shift; | ||
| 85 | my $data = $self->{'dataref'}; | ||
| 86 | |||
| 87 | my @patterns = @{ $self->{'patterns'} || [] }; | ||
| 88 | |||
| 89 | # Calculate array of x pixel positions (@x). | ||
| 90 | my $x_step = | ||
| 91 | ( $self->{'curr_x_max'} - $self->{'curr_x_min'} ) / ( $self->{'num_datapoints'} > 0 ? $self->{'num_datapoints'} : 1 ); | ||
| 92 | my $x_min = $self->{'curr_x_min'} + $x_step / 2; | ||
| 93 | my $x_max = $self->{'curr_x_max'} - $x_step / 2; | ||
| 94 | my @x = map { $_ * $x_step + $x_min } 0 .. $self->{'num_datapoints'} - 1; | ||
| 95 | my ( $t_x_min, $t_x_max, $t_y_min, $t_y_max, $abs_x_max, $abs_y_max ); | ||
| 96 | my $repair_top_flag = 0; | ||
| 97 | |||
| 98 | # Calculate array of y pixel positions for upper boundary each dataset (@y). | ||
| 99 | |||
| 100 | my $map = | ||
| 101 | ( $self->{'max_val'} ) | ||
| 102 | ? ( $self->{'curr_y_max'} - $self->{'curr_y_min'} ) / $self->{'max_val'} | ||
| 103 | : ( $self->{'curr_y_max'} - $self->{'curr_y_min'} ) / 10; | ||
| 104 | |||
| 105 | my $y_max = $self->{'curr_y_max'}; # max pixel point (lower y values) | ||
| 106 | |||
| 107 | my @y; | ||
| 108 | for my $j ( 0 .. $#{ $data->[0] } ) | ||
| 109 | { | ||
| 110 | my $sum = 0; | ||
| 111 | for my $i ( reverse 1 .. $#{$data} ) | ||
| 112 | { # bottom to top of chart | ||
| 113 | my $datum = $data->[$i][$j]; | ||
| 114 | |||
| 115 | #set the repair flag, if the datum is out of the borders of the chart | ||
| 116 | if ( defined $datum && $datum > $self->{'max_val'} ) { $repair_top_flag = 1; } | ||
| 117 | |||
| 118 | if ( defined $datum && $datum >= 0 ) | ||
| 119 | { | ||
| 120 | $sum += $datum; | ||
| 121 | $y[ $i - 1 ][$j] = $y_max - $map * $sum; | ||
| 122 | } | ||
| 123 | else | ||
| 124 | { # missing value, force all to undefined | ||
| 125 | foreach my $k ( 1 .. $#{$data} ) { $y[ $k - 1 ][$j] = undef } | ||
| 126 | last; | ||
| 127 | } | ||
| 128 | } | ||
| 129 | } | ||
| 130 | |||
| 131 | # Find first and last x where y is defined in the bottom dataset. | ||
| 132 | my $x_begin = 0; | ||
| 133 | my $x_end = $self->{'num_datapoints'} - 1; | ||
| 134 | while ( $x_begin <= $x_end && !defined $y[-1]->[$x_begin] ) { $x_begin++ } | ||
| 135 | while ( $x_begin <= $x_end && !defined $y[-1]->[$x_end] ) { $x_end-- } | ||
| 136 | |||
| 137 | if ( $x_begin > $x_end ) { croak "Internal error: x_begin > x_end ($x_begin > $x_end)"; } | ||
| 138 | |||
| 139 | # For each dataset, generate a polygon for the dataset's area of the chart, | ||
| 140 | # and fill the polygon with the dataset's color/pattern. | ||
| 141 | |||
| 142 | my $poly = GD::Polygon->new; | ||
| 143 | $poly->addPt( $x[$x_end], $y_max ); # right end of x axis | ||
| 144 | $poly->addPt( $x[$x_begin], $y_max ); # left end of x axis (right-to-left) | ||
| 145 | |||
| 146 | for my $dataset ( reverse 0 .. @y - 1 ) | ||
| 147 | { | ||
| 148 | my $y_ref = $y[$dataset]; | ||
| 149 | |||
| 150 | # Append points for this dataset to polygon, direction depends on $dataset % 2. | ||
| 151 | my $last_vertex_count = $poly->length; | ||
| 152 | if ( ( @y - 1 - $dataset ) % 2 ) | ||
| 153 | { # right-to-left | ||
| 154 | for ( reverse $x_begin .. $x_end ) | ||
| 155 | { | ||
| 156 | $poly->addPt( $x[$_], $y_ref->[$_] ) if defined $y_ref->[$_]; | ||
| 157 | } | ||
| 158 | } | ||
| 159 | else | ||
| 160 | { # left-to-right | ||
| 161 | for ( $x_begin .. $x_end ) | ||
| 162 | { | ||
| 163 | $poly->addPt( $x[$_], $y_ref->[$_] ) if defined $y_ref->[$_]; | ||
| 164 | } | ||
| 165 | } | ||
| 166 | |||
| 167 | # draw the polygon | ||
| 168 | my $color = $self->_color_role_to_index( 'dataset' . $dataset ); | ||
| 169 | if ( $patterns[$dataset] ) | ||
| 170 | { | ||
| 171 | $self->{'gd_obj'}->filledPolygon( $poly, $color ) if $patterns[$dataset]->transparent >= 0; | ||
| 172 | $self->{'gd_obj'}->setTile( $patterns[$dataset] ); | ||
| 173 | $self->{'gd_obj'}->filledPolygon( $poly, gdTiled ); | ||
| 174 | } | ||
| 175 | else | ||
| 176 | { | ||
| 177 | $self->{'gd_obj'}->filledPolygon( $poly, $color ); | ||
| 178 | } | ||
| 179 | |||
| 180 | # delete previous dataset's points from the polygon, update $last_vertex_count. | ||
| 181 | unless ( $dataset == 0 ) | ||
| 182 | { # don't bother do delete points after last area | ||
| 183 | while ($last_vertex_count) { $poly->deletePt(0); $last_vertex_count-- } | ||
| 184 | } | ||
| 185 | } | ||
| 186 | |||
| 187 | # Enclose the plots | ||
| 188 | $self->{'gd_obj'}->rectangle( $self->{'curr_x_min'}, $self->{'curr_y_min'}, $self->{'curr_x_max'}, $self->{'curr_y_max'}, | ||
| 189 | $self->_color_role_to_index('misc') ); | ||
| 190 | |||
| 191 | #get the width and the heigth of the complete picture | ||
| 192 | ( $abs_x_max, $abs_y_max ) = $self->{'gd_obj'}->getBounds(); | ||
| 193 | |||
| 194 | #repair the chart, if the lines are out of the borders of the chart | ||
| 195 | if ($repair_top_flag) | ||
| 196 | { | ||
| 197 | |||
| 198 | #overwrite the ugly mistakes | ||
| 199 | $self->{'gd_obj'}->filledRectangle( | ||
| 200 | $self->{'curr_x_min'}, 0, $self->{'curr_x_max'}, | ||
| 201 | $self->{'curr_y_min'} - 1, | ||
| 202 | $self->_color_role_to_index('background') | ||
| 203 | ); | ||
| 204 | |||
| 205 | #save the actual x and y values | ||
| 206 | $t_x_min = $self->{'curr_x_min'}; | ||
| 207 | $t_x_max = $self->{'curr_x_max'}; | ||
| 208 | $t_y_min = $self->{'curr_y_min'}; | ||
| 209 | $t_y_max = $self->{'curr_y_max'}; | ||
| 210 | |||
| 211 | #get back to the point, where everything began | ||
| 212 | $self->{'curr_x_min'} = 0; | ||
| 213 | $self->{'curr_y_min'} = 0; | ||
| 214 | $self->{'curr_x_max'} = $abs_x_max; | ||
| 215 | $self->{'curr_y_max'} = $abs_y_max; | ||
| 216 | |||
| 217 | #draw the title again | ||
| 218 | if ( $self->{'title'} ) | ||
| 219 | { | ||
| 220 | $self->_draw_title(); | ||
| 221 | } | ||
| 222 | |||
| 223 | #draw the sub title again | ||
| 224 | if ( $self->{'sub_title'} ) | ||
| 225 | { | ||
| 226 | $self->_draw_sub_title(); | ||
| 227 | } | ||
| 228 | |||
| 229 | #draw the top legend again | ||
| 230 | if ( $self->{'legend'} =~ /^top$/i ) | ||
| 231 | { | ||
| 232 | $self->_draw_top_legend(); | ||
| 233 | } | ||
| 234 | |||
| 235 | #reset the actual values | ||
| 236 | $self->{'curr_x_min'} = $t_x_min; | ||
| 237 | $self->{'curr_x_max'} = $t_x_max; | ||
| 238 | $self->{'curr_y_min'} = $t_y_min; | ||
| 239 | $self->{'curr_y_max'} = $t_y_max; | ||
| 240 | } | ||
| 241 | } | ||
| 242 | |||
| 243 | ############################################################### | ||
| 244 | |||
| 245 | ### Fix a bug in GD::Polygon. | ||
| 246 | ### A patch has been submitted to Lincoln Stein. | ||
| 247 | |||
| 248 | require GD; | ||
| 249 | unless ( defined &GD::Polygon::deletePt ) | ||
| 250 | { | ||
| 251 | *GD::Polygon::deletePt = sub { | ||
| 252 | my ( $self, $index ) = @_; | ||
| 253 | unless ( ( $index >= 0 ) && ( $index < @{ $self->{'points'} } ) ) | ||
| 254 | { | ||
| 255 | warn "Attempt to set an undefined polygon vertex"; | ||
| 256 | return undef; | ||
| 257 | } | ||
| 258 | my ($vertex) = splice( @{ $self->{'points'} }, $index, 1 ); | ||
| 259 | $self->{'length'}--; | ||
| 260 | return @$vertex; | ||
| 261 | } | ||
| 262 | } | ||
| 263 | |||
| 264 | ############################################################### | ||
| 265 | |||
| 266 | 1; | ||