| 1 | # | ||
| 2 | # line charts (plots) | ||
| 3 | # LinesPoints class connect the given x-/y-values by straight lines | ||
| 4 | # and the x-/y-values are plotted by points. | ||
| 5 | # | ||
| 6 | |||
| 7 | 8 | ▶ | use v5.12; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | package Chart::LinesPoints; | ||
| 10 | our @ISA = qw(Chart::Base); | ||
| 11 | our $VERSION = 'v2.403.9'; | ||
| 12 | |||
| 13 | 0 | ▶ | use Chart::Base; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 14 | use GD; | ||
| 15 | use Carp; | ||
| 16 | |||
| 17 | |||
| 18 | |||
| 19 | #>>>>>>>>>>>>>>>>>>>>>>>>>># | ||
| 20 | # public methods go here # | ||
| 21 | #<<<<<<<<<<<<<<<<<<<<<<<<<<# | ||
| 22 | |||
| 23 | #>>>>>>>>>>>>>>>>>>>>>>>>>>># | ||
| 24 | # private methods go here # | ||
| 25 | #<<<<<<<<<<<<<<<<<<<<<<<<<<<# | ||
| 26 | |||
| 27 | ## @fn private _draw_data | ||
| 28 | # draw the data | ||
| 29 | sub _draw_data | ||
| 30 | { | ||
| 31 | my $self = shift; | ||
| 32 | my $data = $self->{'dataref'}; | ||
| 33 | my $misccolor = $self->_color_role_to_index('misc'); | ||
| 34 | my ( $x1, $x2, $x3, $y1, $y2, $y3, $mod, $abs_x_max, $abs_y_max ); | ||
| 35 | my ( $width, $height, $delta, $map, $t_x_min, $t_x_max, $t_y_min, $t_y_max ); | ||
| 36 | my ( $i, $j, $color, $brush, $zero_offset, $delta_num ); | ||
| 37 | my $repair_top_flag = 0; | ||
| 38 | my $repair_bottom_flag = 0; | ||
| 39 | my $diff; | ||
| 40 | |||
| 41 | # init the imagemap data field if they want it | ||
| 42 | if ( $self->true( $self->{'imagemap'} ) ) | ||
| 43 | { | ||
| 44 | $self->{'imagemap_data'} = []; | ||
| 45 | } | ||
| 46 | |||
| 47 | # find the delta value between data points, as well | ||
| 48 | # as the mapping constant | ||
| 49 | $width = $self->{'curr_x_max'} - $self->{'curr_x_min'}; | ||
| 50 | $height = $self->{'curr_y_max'} - $self->{'curr_y_min'}; | ||
| 51 | $delta = $width / ( $self->{'num_datapoints'} > 0 ? $self->{'num_datapoints'} : 1 ); | ||
| 52 | $diff = ( $self->{'max_val'} - $self->{'min_val'} ); | ||
| 53 | $diff = 1 if $diff == 0; | ||
| 54 | $map = $height / $diff; | ||
| 55 | |||
| 56 | # get the base x-y values | ||
| 57 | if ( $self->true( $self->{'xy_plot'} ) ) | ||
| 58 | { | ||
| 59 | $x1 = $self->{'curr_x_min'}; | ||
| 60 | } | ||
| 61 | else | ||
| 62 | { | ||
| 63 | $x1 = $self->{'curr_x_min'} + ( $delta / 2 ); | ||
| 64 | } | ||
| 65 | if ( $self->{'min_val'} >= 0 ) | ||
| 66 | { | ||
| 67 | $y1 = $self->{'curr_y_max'}; | ||
| 68 | $mod = $self->{'min_val'}; | ||
| 69 | } | ||
| 70 | elsif ( $self->{'max_val'} <= 0 ) | ||
| 71 | { | ||
| 72 | $y1 = $self->{'curr_y_min'}; | ||
| 73 | $mod = $self->{'max_val'}; | ||
| 74 | } | ||
| 75 | else | ||
| 76 | { | ||
| 77 | $y1 = $self->{'curr_y_min'} + ( $map * $self->{'max_val'} ); | ||
| 78 | $mod = 0; | ||
| 79 | $self->{'gd_obj'}->line( $self->{'curr_x_min'}, $y1, $self->{'curr_x_max'}, $y1, $misccolor ); | ||
| 80 | } | ||
| 81 | |||
| 82 | #for a xy-plot, use this delta and maybe an offset for the zero-axes | ||
| 83 | if ( $self->true( $self->{'xy_plot'} ) ) | ||
| 84 | { | ||
| 85 | my ( $xmin, $xmax ) = ( $self->{'x_min_val'}, $self->{'x_max_val'} ); | ||
| 86 | if ( $self->{'xlabels'} ) | ||
| 87 | { | ||
| 88 | ( $xmin, $xmax ) = @{ $self->{'xrange'} }; | ||
| 89 | } | ||
| 90 | $diff = $xmax - $xmin; | ||
| 91 | $diff = 1 if $diff == 0; | ||
| 92 | $delta_num = $width / $diff; | ||
| 93 | |||
| 94 | if ( $xmin <= 0 && $xmax >= 0 ) | ||
| 95 | { | ||
| 96 | $zero_offset = abs($xmin) * abs($delta_num); | ||
| 97 | } | ||
| 98 | elsif ( $xmin > 0 || $xmax < 0 ) | ||
| 99 | { | ||
| 100 | $zero_offset = -$xmin * $delta_num; | ||
| 101 | } | ||
| 102 | else | ||
| 103 | { | ||
| 104 | $zero_offset = 0; | ||
| 105 | } | ||
| 106 | } | ||
| 107 | |||
| 108 | # draw the lines | ||
| 109 | for $i ( 1 .. $self->{'num_datasets'} ) | ||
| 110 | { | ||
| 111 | |||
| 112 | # get the color for this dataset, and set the brush | ||
| 113 | $color = $self->_color_role_to_index( 'dataset' . ( $i - 1 ) ); | ||
| 114 | $brush = $self->_prepare_brush( $color, 'line', 'dataset' . ( $i - 1 ) ); | ||
| 115 | $self->{'gd_obj'}->setBrush($brush); | ||
| 116 | |||
| 117 | # draw every line for this dataset | ||
| 118 | for $j ( 1 .. $self->{'num_datapoints'} ) | ||
| 119 | { | ||
| 120 | |||
| 121 | # don't try to draw anything if there's no data | ||
| 122 | if ( defined( $data->[$i][$j] ) and defined( $data->[$i][ $j - 1 ] ) ) | ||
| 123 | { | ||
| 124 | if ( $self->true( $self->{'xy_plot'} ) ) | ||
| 125 | { | ||
| 126 | $x2 = $x1 + $delta_num * $data->[0][ $j - 1 ] + $zero_offset; | ||
| 127 | $x3 = $x1 + $delta_num * $data->[0][$j] + $zero_offset; | ||
| 128 | } | ||
| 129 | else | ||
| 130 | { | ||
| 131 | $x2 = $x1 + ( $delta * ( $j - 1 ) ); | ||
| 132 | $x3 = $x1 + ( $delta * $j ); | ||
| 133 | } | ||
| 134 | $y2 = $y1 - ( ( $data->[$i][ $j - 1 ] - $mod ) * $map ); | ||
| 135 | $y3 = $y1 - ( ( $data->[$i][$j] - $mod ) * $map ); | ||
| 136 | |||
| 137 | #set the flags, if the lines are out of the borders of the chart | ||
| 138 | if ( ( $data->[$i][$j] > $self->{'max_val'} ) || ( $data->[$i][ $j - 1 ] > $self->{'max_val'} ) ) | ||
| 139 | { | ||
| 140 | $repair_top_flag = 1; | ||
| 141 | } | ||
| 142 | |||
| 143 | if ( ( $self->{'max_val'} <= 0 ) | ||
| 144 | && ( ( $data->[$i][$j] > $self->{'max_val'} ) || ( $data->[$i][ $j - 1 ] > $self->{'max_val'} ) ) ) | ||
| 145 | { | ||
| 146 | $repair_top_flag = 1; | ||
| 147 | } | ||
| 148 | if ( ( $data->[$i][$j] < $self->{'min_val'} ) || ( $data->[$i][ $j - 1 ] < $self->{'min_val'} ) ) | ||
| 149 | { | ||
| 150 | $repair_bottom_flag = 1; | ||
| 151 | } | ||
| 152 | |||
| 153 | # draw the line | ||
| 154 | # ---------------- | ||
| 155 | # stepline option added by G.ST. 2005/02 | ||
| 156 | #---------------- | ||
| 157 | if ( $self->true( $self->{'stepline'} ) ) | ||
| 158 | { | ||
| 159 | if ( $self->{'stepline_mode'} =~ /^begin$/i ) | ||
| 160 | { | ||
| 161 | $self->{'gd_obj'}->line( $x2, $y2, $x3, $y2, gdBrushed ); | ||
| 162 | $self->{'gd_obj'}->line( $x3, $y2, $x3, $y3, gdBrushed ); | ||
| 163 | } | ||
| 164 | else | ||
| 165 | { | ||
| 166 | $self->{'gd_obj'}->line( $x2, $y2, $x2, $y3, gdBrushed ); | ||
| 167 | $self->{'gd_obj'}->line( $x2, $y3, $x3, $y3, gdBrushed ); | ||
| 168 | } | ||
| 169 | } | ||
| 170 | |||
| 171 | # ----------------------------------- | ||
| 172 | # end stepline option | ||
| 173 | #------------------------------------ | ||
| 174 | else | ||
| 175 | { | ||
| 176 | $self->{'gd_obj'}->line( $x2, $y2, $x3, $y3, gdBrushed ); | ||
| 177 | } | ||
| 178 | } | ||
| 179 | } | ||
| 180 | |||
| 181 | # reset the brush for points | ||
| 182 | $brush = $self->_prepare_brush( $color, 'point', 'dataset' . ( $i - 1 ) ); | ||
| 183 | $self->{'gd_obj'}->setBrush($brush); | ||
| 184 | |||
| 185 | # draw every point for this dataset | ||
| 186 | for $j ( 0 .. $self->{'num_datapoints'} ) | ||
| 187 | { | ||
| 188 | |||
| 189 | # don't try to draw anything if there's no data | ||
| 190 | if ( defined( $data->[$i][$j] ) ) | ||
| 191 | { | ||
| 192 | if ( $self->true( $self->{'xy_plot'} ) ) | ||
| 193 | { | ||
| 194 | $x2 = $x1 + $delta_num * $data->[0][$j] + $zero_offset; | ||
| 195 | $x3 = $x2; | ||
| 196 | } | ||
| 197 | else | ||
| 198 | { | ||
| 199 | $x2 = $x1 + ( $delta * $j ); | ||
| 200 | $x3 = $x2; | ||
| 201 | } | ||
| 202 | $y2 = $y1 - ( ( $data->[$i][$j] - $mod ) * $map ); | ||
| 203 | $y3 = $y2; | ||
| 204 | |||
| 205 | # draw the point | ||
| 206 | $self->{'gd_obj'}->line( $x2, $y2, $x3, $y3, gdBrushed ); | ||
| 207 | |||
| 208 | # remember the imagemap data if they wanted it | ||
| 209 | if ( $self->true( $self->{'imagemap'} ) ) | ||
| 210 | { | ||
| 211 | $self->{'imagemap_data'}->[$i][$j] = [ $x2, $y2 ]; | ||
| 212 | } | ||
| 213 | } | ||
| 214 | else | ||
| 215 | { | ||
| 216 | if ( $self->true( $self->{'imagemap'} ) ) | ||
| 217 | { | ||
| 218 | $self->{'imagemap_data'}->[$i][$j] = [ undef(), undef() ]; | ||
| 219 | } | ||
| 220 | } | ||
| 221 | } | ||
| 222 | } | ||
| 223 | |||
| 224 | # and finaly box it off | ||
| 225 | $self->{'gd_obj'} | ||
| 226 | ->rectangle( $self->{'curr_x_min'}, $self->{'curr_y_min'}, $self->{'curr_x_max'}, $self->{'curr_y_max'}, $misccolor ); | ||
| 227 | |||
| 228 | #get the width and the heigth of the complete picture | ||
| 229 | ( $abs_x_max, $abs_y_max ) = $self->{'gd_obj'}->getBounds(); | ||
| 230 | |||
| 231 | #repair the chart, if the lines are out of the borders of the chart | ||
| 232 | if ($repair_top_flag) | ||
| 233 | { | ||
| 234 | |||
| 235 | #overwrite the ugly mistakes | ||
| 236 | # $self->{'gd_obj'}->filledRectangle ($self->{'curr_x_min'}, 0, | ||
| 237 | $self->{'gd_obj'}->filledRectangle( | ||
| 238 | $self->{'curr_x_min'} - ( $self->{'brush_size'} / 2 ), | ||
| 239 | 0, $self->{'curr_x_max'}, | ||
| 240 | $self->{'curr_y_min'} - 2, | ||
| 241 | $self->_color_role_to_index('background') | ||
| 242 | ); | ||
| 243 | |||
| 244 | #save the actual x and y values | ||
| 245 | $t_x_min = $self->{'curr_x_min'}; | ||
| 246 | $t_x_max = $self->{'curr_x_max'}; | ||
| 247 | $t_y_min = $self->{'curr_y_min'}; | ||
| 248 | $t_y_max = $self->{'curr_y_max'}; | ||
| 249 | |||
| 250 | #get back to the point, where everything began | ||
| 251 | $self->{'curr_x_min'} = 0; | ||
| 252 | $self->{'curr_y_min'} = 0; | ||
| 253 | $self->{'curr_x_max'} = $abs_x_max; | ||
| 254 | $self->{'curr_y_max'} = $abs_y_max; | ||
| 255 | |||
| 256 | #draw the title again | ||
| 257 | if ( $self->{'title'} ) | ||
| 258 | { | ||
| 259 | $self->_draw_title; | ||
| 260 | } | ||
| 261 | |||
| 262 | #draw the sub title again | ||
| 263 | if ( $self->{'sub_title'} ) | ||
| 264 | { | ||
| 265 | $self->_draw_sub_title; | ||
| 266 | } | ||
| 267 | |||
| 268 | #draw the top legend again | ||
| 269 | if ( $self->{'legend'} =~ /^top$/i ) | ||
| 270 | { | ||
| 271 | $self->_draw_top_legend; | ||
| 272 | } | ||
| 273 | |||
| 274 | #reset the actual values | ||
| 275 | $self->{'curr_x_min'} = $t_x_min; | ||
| 276 | $self->{'curr_x_max'} = $t_x_max; | ||
| 277 | $self->{'curr_y_min'} = $t_y_min; | ||
| 278 | $self->{'curr_y_max'} = $t_y_max; | ||
| 279 | } | ||
| 280 | |||
| 281 | if ($repair_bottom_flag) | ||
| 282 | { | ||
| 283 | |||
| 284 | #overwrite the ugly mistakes | ||
| 285 | # $self->{'gd_obj'}->filledRectangle ($self->{'curr_x_min'}, $self->{'curr_y_max'}+1, | ||
| 286 | $self->{'gd_obj'}->filledRectangle( | ||
| 287 | $self->{'curr_x_min'} - ( $self->{'brush_size'} / 2 ), | ||
| 288 | $self->{'curr_y_max'} + 1, | ||
| 289 | $self->{'curr_x_max'}, $abs_y_max, $self->_color_role_to_index('background') | ||
| 290 | ); | ||
| 291 | |||
| 292 | #save the actual x and y values | ||
| 293 | $t_x_min = $self->{'curr_x_min'}; | ||
| 294 | $t_x_max = $self->{'curr_x_max'}; | ||
| 295 | $t_y_min = $self->{'curr_y_min'}; | ||
| 296 | $t_y_max = $self->{'curr_y_max'}; | ||
| 297 | |||
| 298 | #get back to the point, where everything began | ||
| 299 | $self->{'curr_x_min'} = 0; | ||
| 300 | $self->{'curr_y_min'} = 0; | ||
| 301 | $self->{'curr_x_max'} = $abs_x_max; | ||
| 302 | $self->{'curr_y_max'} = $abs_y_max - 1; | ||
| 303 | |||
| 304 | # mark off the graph_border space | ||
| 305 | $self->{'curr_y_max'} -= 2 * $self->{'graph_border'}; | ||
| 306 | |||
| 307 | #draw the bottom legend again | ||
| 308 | if ( $self->{'legend'} =~ /^bottom$/i ) | ||
| 309 | { | ||
| 310 | $self->_draw_bottom_legend; | ||
| 311 | } | ||
| 312 | |||
| 313 | #draw the x label again | ||
| 314 | if ( $self->{'x_label'} ) | ||
| 315 | { | ||
| 316 | $self->_draw_x_label; | ||
| 317 | } | ||
| 318 | |||
| 319 | #get back to the start point for the ticks | ||
| 320 | $self->{'curr_x_min'} = $self->{'temp_x_min'}; | ||
| 321 | $self->{'curr_y_min'} = $self->{'temp_y_min'}; | ||
| 322 | $self->{'curr_x_max'} = $self->{'temp_x_max'}; | ||
| 323 | $self->{'curr_y_max'} = $self->{'temp_y_max'}; | ||
| 324 | |||
| 325 | #draw the x ticks again | ||
| 326 | $self->_draw_x_ticks; | ||
| 327 | |||
| 328 | #reset the actual values | ||
| 329 | $self->{'curr_x_min'} = $t_x_min; | ||
| 330 | $self->{'curr_x_max'} = $t_x_max; | ||
| 331 | $self->{'curr_y_min'} = $t_y_min; | ||
| 332 | $self->{'curr_y_max'} = $t_y_max; | ||
| 333 | } | ||
| 334 | |||
| 335 | return; | ||
| 336 | |||
| 337 | } | ||
| 338 | |||
| 339 | ## be a good module and return 1 | ||
| 340 | 1; | ||