Chart

 view release on metacpan or  search on metacpan

lib/Chart/Mountain.pm  view on Meta::CPAN

        for my $i ( reverse 1 .. $#{$data} )
        {                                 # bottom to top of chart
            my $datum = $data->[$i][$j];

            #set the repair flag, if the datum is out of the borders of the chart
            if ( defined $datum && $datum > $self->{'max_val'} ) { $repair_top_flag = 1; }

            if ( defined $datum && $datum >= 0 )
            {
                $sum += $datum;
                $y[ $i - 1 ][$j] = $y_max - $map * $sum;
            }
            else
            {                             # missing value, force all to undefined
                foreach my $k ( 1 .. $#{$data} ) { $y[ $k - 1 ][$j] = undef }
                last;
            }
        }
    }

    # Find first and last x where y is defined in the bottom dataset.
    my $x_begin = 0;
    my $x_end   = $self->{'num_datapoints'} - 1;
    while ( $x_begin <= $x_end && !defined $y[-1]->[$x_begin] ) { $x_begin++ }
    while ( $x_begin <= $x_end && !defined $y[-1]->[$x_end] )   { $x_end-- }

    if ( $x_begin > $x_end ) { croak "Internal error: x_begin > x_end ($x_begin > $x_end)"; }

    # For each dataset, generate a polygon for the dataset's area of the chart,
    # and fill the polygon with the dataset's color/pattern.

    my $poly = GD::Polygon->new;
    $poly->addPt( $x[$x_end],   $y_max );    # right end of x axis
    $poly->addPt( $x[$x_begin], $y_max );    # left end of x axis (right-to-left)

    for my $dataset ( reverse 0 .. @y - 1 )
    {
        my $y_ref = $y[$dataset];

        # Append points for this dataset to polygon, direction depends on $dataset % 2.
        my $last_vertex_count = $poly->length;
        if ( ( @y - 1 - $dataset ) % 2 )
        {                                    # right-to-left
            for ( reverse $x_begin .. $x_end )
            {
                $poly->addPt( $x[$_], $y_ref->[$_] ) if defined $y_ref->[$_];
            }
        }
        else
        {                                    # left-to-right
            for ( $x_begin .. $x_end )
            {
                $poly->addPt( $x[$_], $y_ref->[$_] ) if defined $y_ref->[$_];
            }
        }

        # draw the polygon
        my $color = $self->_color_role_to_index( 'dataset' . $dataset );
        if ( $patterns[$dataset] )
        {
            $self->{'gd_obj'}->filledPolygon( $poly, $color ) if $patterns[$dataset]->transparent >= 0;
            $self->{'gd_obj'}->setTile( $patterns[$dataset] );
            $self->{'gd_obj'}->filledPolygon( $poly, gdTiled );
        }
        else
        {
            $self->{'gd_obj'}->filledPolygon( $poly, $color );
        }

        # delete previous dataset's points from the polygon, update $last_vertex_count.
        unless ( $dataset == 0 )
        {    # don't bother do delete points after last area
            while ($last_vertex_count) { $poly->deletePt(0); $last_vertex_count-- }
        }
    }

    # Enclose the plots
    $self->{'gd_obj'}->rectangle( $self->{'curr_x_min'}, $self->{'curr_y_min'}, $self->{'curr_x_max'}, $self->{'curr_y_max'},
        $self->_color_role_to_index('misc') );

    #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'}, 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 )
        {



( run in 0.618 second using v1.01-cache-2.11-cpan-39bf76dae61 )