File: Lines.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 (347 lines) | stat: -rw-r--r-- 10,925 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
## @file
# Implementation of Chart::Lines
#
#  written by david bonner
#  dbonner@cs.bu.edu
#
#  maintained by the Chart Group at Geodetic Fundamental Station Wettzell
#  Chart@fs.wettzell.de
# @author Chart Group (Chart@fs.wettzell.de)
# @date 2015-03-01
# @version 2.4.10

## @class Chart::Lines
# Lines class derived from class Base.
#
# This class provides all functions which are specific to
# lines
#
package Chart::Lines;

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

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

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

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

## @method private _draw_data
# finally get around to plotting the data for lines
sub _draw_data
{
    my $self      = shift;
    my $data      = $self->{'dataref'};
    my $misccolor = $self->_color_role_to_index('misc');
    my ( $x1, $x2, $x3, $y1, $y2, $y3, $mod, $abs_x_max, $abs_y_max, $tan_alpha );
    my ( $width, $height, $delta, $delta_num, $map, $t_x_min, $t_x_max, $t_y_min, $t_y_max );
    my ( $i, $j, $color, $brush, $zero_offset );
    my $repair_top_flag    = 0;
    my $repair_bottom_flag = 0;

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

    # find the delta value between data points, as well
    # as the mapping constant
    $width  = $self->{'curr_x_max'} - $self->{'curr_x_min'};
    $height = $self->{'curr_y_max'} - $self->{'curr_y_min'};
    $delta  = $width / ( $self->{'num_datapoints'} > 0 ? $self->{'num_datapoints'} : 1 );
    $map    = $height / ( $self->{'max_val'} - $self->{'min_val'} );

    #for a xy-plot, use this delta and maybe an offset for the zero-axes
    if ( $self->true( $self->{'xy_plot'} ) )
    {
        $delta_num = $width / ( $self->{'x_max_val'} - $self->{'x_min_val'} );

        if ( $self->{'x_min_val'} <= 0 && $self->{'x_max_val'} >= 0 )
        {
            $zero_offset = abs( $self->{'x_min_val'} ) * abs($delta_num);
        }
        elsif ( $self->{'x_min_val'} > 0 || $self->{'x_max_val'} < 0 )
        {
            $zero_offset = -$self->{'x_min_val'} * $delta_num;
        }
        else
        {
            $zero_offset = 0;
        }
    }

    # get the base x-y values
    if ( $self->true( $self->{'xy_plot'} ) )
    {
        $x1 = $self->{'curr_x_min'};
    }
    else
    {
        $x1 = $self->{'curr_x_min'} + ( $delta / 2 );
    }
    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 );
    }

    # draw the lines

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

        # get the color for this dataset, and set the brush
        $color = $self->_color_role_to_index( 'dataset' . ( $i - 1 ) );
        $brush = $self->_prepare_brush($color);
        $self->{'gd_obj'}->setBrush($brush);

        # draw every line for this dataset
        for $j ( 1 .. $self->{'num_datapoints'} - 1 )
        {

            # don't try to draw anything if there's no data
            if ( defined( $data->[$i][$j] ) and defined( $data->[$i][ $j - 1 ] ) )
            {
                if ( $self->true( $self->{'xy_plot'} ) )
                {
                    $x2 = $x1 + $delta_num * $data->[0][ $j - 1 ] + $zero_offset;
                    $x3 = $x1 + $delta_num * $data->[0][$j] + $zero_offset;
                }
                else
                {
                    $x2 = $x1 + ( $delta * ( $j - 1 ) );
                    $x3 = $x1 + ( $delta * $j );
                }
                $y2 = $y1 - ( ( $data->[$i][ $j - 1 ] - $mod ) * $map );
                $y3 = $y1 - ( ( $data->[$i][$j] - $mod ) * $map );

                # now draw the line
                # ----------------
                # stepline option added by G.ST. 2005/02
                #----------------
                if ( $self->true( $self->{'stepline'} ) )
                {
                    if ( $self->{'stepline_mode'} =~ /^begin$/i )
                    {
                        $self->{'gd_obj'}->line( $x2, $y2, $x3, $y2, gdBrushed );
                        $self->{'gd_obj'}->line( $x3, $y2, $x3, $y3, gdBrushed );
                    }
                    else
                    {
                        $self->{'gd_obj'}->line( $x2, $y2, $x2, $y3, gdBrushed );
                        $self->{'gd_obj'}->line( $x2, $y3, $x3, $y3, gdBrushed );
                    }
                }

                # -----------------------------------
                # end stepline option
                #------------------------------------
                else
                {
                    $self->{'gd_obj'}->line( $x2, $y2, $x3, $y3, gdBrushed );
                }

                # set the flags, if the lines are out of the borders of the chart
                if ( ( $data->[$i][$j] > $self->{'max_val'} ) || ( $data->[$i][ $j - 1 ] > $self->{'max_val'} ) )
                {
                    $repair_top_flag = 1;
                }

                if (   ( $self->{'max_val'} <= 0 )
                    && ( ( $data->[$i][$j] > $self->{'max_val'} ) || ( $data->[$i][ $j - 1 ] > $self->{'max_val'} ) ) )
                {
                    $repair_top_flag = 1;
                }
                if ( ( $data->[$i][$j] < $self->{'min_val'} ) || ( $data->[$i][ $j - 1 ] < $self->{'min_val'} ) )
                {
                    $repair_bottom_flag = 1;
                }

                # store the imagemap data if they asked for it
                if ( $self->true( $self->{'imagemap'} ) )
                {
                    $self->{'imagemap_data'}->[$i][ $j - 1 ] = [ $x2, $y2 ];
                    $self->{'imagemap_data'}->[$i][$j] = [ $x3, $y3 ];
                }
            }
            else
            {
                if ( $self->true( $self->{'imagemap'} ) )
                {
                    $self->{'imagemap_data'}->[$i][ $j - 1 ] = [ undef(), undef() ];
                    $self->{'imagemap_data'}->[$i][$j] = [ undef(), undef() ];
                }
            }
        }
    }

    # 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 );

    #get the width and the heigth of the complete picture
    ( $abs_x_max, $abs_y_max ) = $self->{'gd_obj'}->getBounds();

    #repair the chart, if the lines are out of the borders of the chart
    if ($repair_top_flag)
    {

        #overwrite the ugly mistakes
        $self->{'gd_obj'}->filledRectangle(
            $self->{'curr_x_min'} - ( $self->{'brush_size'} / 2 ),
            0, $self->{'curr_x_max'},
            $self->{'curr_y_min'} - 1,
            $self->_color_role_to_index('background')
        );

        #save the actual x and y values
        $t_x_min = $self->{'curr_x_min'};
        $t_x_max = $self->{'curr_x_max'};
        $t_y_min = $self->{'curr_y_min'};
        $t_y_max = $self->{'curr_y_max'};

        #get back to the point, where everything began
        $self->{'curr_x_min'} = 0;
        $self->{'curr_y_min'} = 0;
        $self->{'curr_x_max'} = $abs_x_max;
        $self->{'curr_y_max'} = $abs_y_max;

        #draw the title again
        if ( $self->{'title'} )
        {
            $self->_draw_title;
        }

        #draw the sub title again
        if ( $self->{'sub_title'} )
        {
            $self->_draw_sub_title;
        }

        #draw the top legend again
        if ( $self->{'legend'} =~ /^top$/i )
        {
            $self->_draw_top_legend;
        }

        #reset the actual values
        $self->{'curr_x_min'} = $t_x_min;
        $self->{'curr_x_max'} = $t_x_max;
        $self->{'curr_y_min'} = $t_y_min;
        $self->{'curr_y_max'} = $t_y_max;
    }

    if ($repair_bottom_flag)
    {

        #overwrite the ugly mistakes
        $self->{'gd_obj'}->filledRectangle(
            $self->{'curr_x_min'} - ( $self->{'brush_size'} / 2 ),
            $self->{'curr_y_max'} + 1,
            $self->{'curr_x_max'}, $abs_y_max, $self->_color_role_to_index('background')
        );

        #save the actual x and y values
        $t_x_min = $self->{'curr_x_min'};
        $t_x_max = $self->{'curr_x_max'};
        $t_y_min = $self->{'curr_y_min'};
        $t_y_max = $self->{'curr_y_max'};

        #get back to the point, where everything began
        $self->{'curr_x_min'} = 0;
        $self->{'curr_y_min'} = 0;
        $self->{'curr_x_max'} = $abs_x_max;
        $self->{'curr_y_max'} = $abs_y_max - 1;

        # mark off the graph_border space
        $self->{'curr_y_max'} -= 2 * $self->{'graph_border'};

        #draw the bottom legend again
        if ( $self->{'legend'} =~ /^bottom$/i )
        {
            $self->_draw_bottom_legend;
        }

        #draw the x label again
        if ( $self->{'x_label'} )
        {
            $self->_draw_x_label;
        }

        #get back to the start point for the ticks
        $self->{'curr_x_min'} = $self->{'temp_x_min'};
        $self->{'curr_y_min'} = $self->{'temp_y_min'};
        $self->{'curr_x_max'} = $self->{'temp_x_max'};
        $self->{'curr_y_max'} = $self->{'temp_y_max'};

        #draw the x ticks again
        if ( $self->true( $self->{'xy_plot'} ) )
        {
            $self->_draw_x_number_ticks;
        }
        else
        {
            $self->_draw_x_ticks;
        }

        #reset the actual values
        $self->{'curr_x_min'} = $t_x_min;
        $self->{'curr_x_max'} = $t_x_max;
        $self->{'curr_y_min'} = $t_y_min;
        $self->{'curr_y_max'} = $t_y_max;
    }

    return;

}

## @fn private int _prepare_brush($color)
# set the gdBrush object to trick GD into drawing fat lines
#
sub _prepare_brush
{
    my $self   = shift;
    my $color  = shift;
    my $radius = $self->{'brush_size'} / 2;
    my ( @rgb, $brush, $white, $newcolor );

    # get the rgb values for the desired color
    @rgb = $self->{'gd_obj'}->rgb($color);

    # 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 );

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

## be a good module and return 1
1;