Graph-ChartSVG

 view release on metacpan or  search on metacpan

lib/Graph/ChartSVG.pm  view on Meta::CPAN

                # {
                    # $Y = $layer->y;
                # }
                # else
                # {
                    # $Y += $layer->y - $self->border->bottom;
                # }
            # }

                if ( exists $layer->{ type } && $layer->{ type } eq 'image' )
                { 
                    $X = $layer->{ x };
                    $Y = $layer->y;
                }
                else
                {
                     $X += $layer->{ x } - $self->border->left;
                    $Y += $layer->y - $self->border->bottom;
                }

            if ( exists $layer->{ type } && $layer->{ type } eq 'text' )
            {
                foreach my $set ( @{ $layer->data_set } )
                {
                    my $text_angle  = exists( $set->{ rotation } ) && $set->{ rotation } || 0;
                    my $font_style  = 'normal';
                    my $font_weight = $set->{ font_weight } || $layer->font_weight || 'normal';

                    my $f_style = $set->{ style } || $layer->style;
                    if ( $f_style =~ /(italic)|(oblique)/ )
                    {
                        $font_style = 'italic';
                    }
                    if ( $f_style =~ /bold/ )
                    {
                        $font_weight = 'bold';
                    }

                    my $letter_spacing = $set->{ letter_spacing } || $layer->letter_spacing || 'normal';
                    my $word_spacing   = $set->{ word_spacing }   || $layer->word_spacing   || 'normal';
                    my $font_stretch   = $set->{ stretch }        || $layer->stretch        || 'normal';

                    my $txt = $layer_goup->text(
                        x => $set->{ x }  || 0,
                        y => -$set->{ y } || 0,
                        style => {
                            'font-family' => $set->{ font } || $layer->font,
                            'font-size'   => $set->{ size } || $layer->size,
                            'font-style'  => $font_style,
                            'font-weight' => $font_weight,
                            'font-stretch'   => $font_stretch,
                            'letter-spacing' => $letter_spacing,
                            'word-spacing'   => $word_spacing,
                            'fill'           => '#' . ( $set->{ color } || $layer->color || 'ffffff' ),
                            'stroke'         => '#' . ( $set->{ stroke } || $set->{ color } || $layer->color || '000000' ),
                            'writing-mode'   => 'lr',
                            'text-anchor' => $set->{ anchor } || $layer->anchor
                        },
                        transform => "matrix(1,0,0,-1," . ( $X ) . "," . ( $Y ) . ") rotate($text_angle)"
                    );
                    $txt->tspan( dy => "0" )->cdata( $set->{ text } );

                }
            }
            elsif ( exists $layer->{ type } && $layer->{ type } eq 'line' )
            {
                my $ind = 0;
                foreach my $set ( @{ $layer->data_set } )
                {
                    my @xv;
                    my @yv;
                    my $dot = -1;

                    foreach my $point ( @{ $set->{ data } } )
                    {
                        next unless ( ref $point eq 'ARRAY' );
                        push @xv, $point->[0] + $X;
                        push @yv, $point->[1] + $Y;
                        $dot++;
                    }
                    my %style;
                    my $color_hex = $set->{ color } || $layer->{ color };
                    if ( exists $layer->{ filled } && $layer->{ filled } == 1 )
                    {
                        push @xv, $xv[0];
                        push @yv, $yv[0];
                        %style = (
                            'opacity' => eval( hex( ( unpack "a6 a2", $color_hex )[1] ) / 255 ) || 1,
                            'stroke' => '#' . ( unpack "a6", $color_hex ) || 0,
                            'stroke-width' => ( $set->{ thickness } || $layer->{ thickness } ),
                            'fill' => '#' . ( unpack "a6", $color_hex ) || 0,
                            'fill-opacity' => eval( hex( ( unpack "a6 a2", $color_hex )[1] ) / 255 ) || 1,
                            'fill-rule' => 'nonzero'
                        );
                    }
                    else
                    {
                        %style = (
                            'opacity' => eval( hex( ( unpack "a6 a2", $color_hex )[1] ) / 255 )
                              || 1,
                            'stroke' => '#' . ( unpack "a6", $color_hex )
                              || 0,
                            'stroke-width' => ( $set->{ thickness } || $layer->{ thickness } ),
#                         'fill'         => '#ff0000',
                            'fill-opacity' => 0,
                            'fill-rule'    => 'nonzero'
                        );
                    }
                    my $points = $layer_goup->get_path(
                        x       => \@xv,
                        y       => \@yv,
                        -type   => 'polyline',
                        -closed => 'true',
                    );
                    my $id_data;
                    if ( exists $layer->{ label } )
                    {
                        $id_data = $layer->{ label };
                    }
                    else
                    {

lib/Graph/ChartSVG.pm  view on Meta::CPAN

                    my $len    = length( $self->grid->x->label->text->[$text_indx] );

                    my $font_style  = 'normal';
                    my $font_weight = 'normal';
                    my $f_style     = $self->grid->x->label->style || '';
                    if ( $f_style =~ /(italic)|(oblique)/ )
                    {
                        $font_style = 'italic';
                    }
                    if ( $f_style =~ /bold/ )
                    {
                        $font_weight = 'bold';
                    }

                    my $x_offset = 0;
                    my $y_offset = 0;

                    my %style;
                    if ( $self->grid->x->label->align =~ /left/i )
                    {
                        %style = (
                            'font-family'  => $self->grid->x->label->font,
                            'font-size'    => $self->grid->x->label->size,
                            'font-style'   => $font_style,
                            'font-weight'  => $font_weight,
                            'fill'         => '#' . ( $text_color || 'ffffff' ),
                            'stroke'       => '#' . ( $text_color || '000000' ),
                            'writing-mode' => 'lr',
                            'text-anchor'  => 'start',
#                             'baseline-shift'=> '-10%',
                        );
                        $x_offset = $self->grid->x->label->size * ( $max_length_x - 1 );
                        if ( $self->grid->x->label->rotation )
                        {
                            $y_offset = ( $sin * $len * $self->grid->x->label->size ) - ( $sin * $self->grid->x->label->size );
                        }
                    }
                    else
                    {
                        %style = (
                            'font-family'  => $self->grid->x->label->font,
                            'font-size'    => $self->grid->x->label->size,
                            'font-style'   => $font_style,
                            'font-weight'  => $font_weight,
                            'fill'         => '#' . ( $text_color || 'ffffff' ),
                            'stroke'       => '#' . ( $text_color || '000000' ),
                            'writing-mode' => 'lr',
                            'text-anchor'  => 'end',
#                             'baseline-shift'=> '-10%',
                        );
                    }

                    my $txt = $x_grid_text->text(
                        x     => $self->border->left - $self->grid->debord->left - $self->grid->x->label->space - $x_offset,
                        y     => $self->border->top - $self->border->bottom + $val + ( $self->grid->x->label->size * 0.3 ) - $y_offset,
                        style => \%style,
# transform => " rotate( " . $self->grid->x->label->rotation . "," . ( $self->border->left - $self->grid->debord->left - $self->grid->x->label->space - $x_offset ) . "," . ( $self->border->bottom + $val - $y_offset ) . " ) ",
                        transform => " rotate( " . $self->grid->x->label->rotation . "," . ( $self->border->left - $self->grid->debord->left - $self->grid->x->label->space - $x_offset ) . "," . ( $self->border->bottom + $val - $y_offset ) . " ) ",

                    );
                    $txt->tspan( dy => "0" )->cdata( $self->grid->x->label->text->[$text_indx] );
                }

##########################################
# second x label ( right side )
##########################################
                if ( defined $self->grid->x->label2 && defined $self->grid->x->label2->text->[$text_indx] )
                {
                    my $text_color = ( $self->grid->x->label2->color || $color_hex );
                    my $radian      = ( $self->grid->x->label2->rotation / 180 ) * PI || 0;
                    my $cos         = cos( $radian );
                    my $sin         = sin( $radian );
                    my $len         = length( $self->grid->x->label2->text->[$text_indx] );
                    my $font_style  = 'normal';
                    my $font_weight = 'normal';
                    my $f_style     = $self->grid->x->label2->style || '';

                    if ( $f_style =~ /(italic)|(oblique)/ )
                    {
                        $font_style = 'italic';
                    }
                    if ( $f_style =~ /bold/ )
                    {
                        $font_weight = 'bold';
                    }
                    my $x_offset = 0;
                    my $y_offset = 0;

                    my %style;
                    if ( $self->grid->x->label2->align =~ /right/i )
                    {
                        %style = (
                            'font-family'  => $self->grid->x->label2->font,
                            'font-size'    => $self->grid->x->label2->size,
                            'font-style'   => $font_style,
                            'font-weight'  => $font_weight,
                            'fill'         => '#' . ( $text_color || 'ffffff' ),
                            'stroke'       => '#' . ( $text_color || '000000' ),
                            'writing-mode' => 'lr',
                            'text-anchor'  => 'end',
#                             'baseline-shift'=> '-10%',
                        );
                        $x_offset = $self->grid->x->label2->size * ( $max_length_x2 - 1 );
                        if ( $self->grid->x->label2->rotation )
                        {
                            $y_offset = ( $sin * ( $max_length_x2 - 1 ) * $self->grid->x->label2->size ) - ( $sin * $self->grid->x->label2->size );
                        }
                    }
                    else
                    {
                        %style = (
                            'font-family'  => $self->grid->x->label2->font,
                            'font-size'    => $self->grid->x->label2->size,
                            'font-style'   => $font_style,
                            'font-weight'  => $font_weight,
                            'fill'         => '#' . ( $text_color || 'ffffff' ),
                            'stroke'       => '#' . ( $text_color || '000000' ),
                            'writing-mode' => 'lr',
                            'text-anchor'  => 'start',
#                             'baseline-shift'=> '-10%',
                        );
                    }
                    my $txt = $x_grid_text->text(
                        x => $self->border->left + $self->grid->debord->right + $self->grid->x->label2->space + $x_offset + $self->active_size->[0],
# y         => $self->border->bottom + $val + ( $self->grid->x->label2->size * 0.3 ) + $y_offset,
                        y => $self->border->top - $self->border->bottom + $val + ( $self->grid->x->label2->size * 0.3 ) - $y_offset,

                        style     => \%style,
                        transform => " rotate( " . $self->grid->x->label2->rotation . "," . ( $self->border->left + $self->grid->debord->right + $self->grid->x->label2->space + $x_offset + $self->active_size->[0] ) . "," . ( $self->border->bottom + $...
                    );
                    $txt->tspan( dy => "0" )->cdata( $self->grid->x->label2->text->[$text_indx] );
                }
            }
        }

####################################
## grid Y ( vertical )
####################################
        if ( defined $self->grid->y )
        {
            my $y_grid_group = $svg->group( id => "y_grid", transform => "matrix(1,0,0,-1, 0," . ( $self->total_size->[1] ) . " )" );
            my $y_grid_text = $y_grid_group->group(
                id        => "y_grid_text",
                transform => "matrix(1,0,0,-1, 0," . ( $self->border->bottom ) . " )"
            );
            my $thickness = $self->grid->y->thickness || 1;
            my $color_hex = $self->grid->y->color;
            my $max_length_y;
            $max_length_y = max( map( length, @{ $self->grid->y->label->text } ) )
              if ( defined( $self->grid->y->label )
                && ( ref( $self->grid->y->label->text ) eq 'ARRAY' ) );
            my $max_length_y2;
            $max_length_y2 = max( map( length, @{ $self->grid->y->label2->text } ) )
              if ( defined( $self->grid->y->label2 )
                && ( ref( $self->grid->y->label2->text ) eq 'ARRAY' ) );

            for my $nbr ( 0 .. ( $self->grid->y->number - 1 ) )
            {
                my $val = ( ( $nbr ) * ( $self->active_size->[0] / ( $self->grid->y->number - 1 ) ) );

                my $tag = $y_grid_group->line(
                    id    => 'y_grid_' . $nbr,
                    x1    => $self->border->left + $val,
                    y1    => $self->border->bottom - $self->grid->debord->bottom,
                    x2    => $self->border->left + $val,
                    y2    => $self->border->bottom + $self->active_size->[1] + $self->grid->debord->top,
                    style => {
                        'fill'            => 'none',
                        'opacity'         => eval( hex( ( unpack "a6 a2", $color_hex )[1] ) / 255 ) || 1,
                        'stroke'          => '#' . ( unpack "a6", $color_hex ) || 0,
                        'stroke-width'    => $thickness,
                        'stroke-linecap'  => 'butt',
                        'stroke-linejoin' => 'miter',
                        'stroke-opacity'  => 1
                    },
                );

                if ( defined $self->grid->y->label && defined $self->grid->y->label->text->[$nbr] )
                {
                    my $text_color  = $self->grid->y->label->color || $color_hex;
                    my $font_style  = 'normal';
                    my $font_weight = 'normal';
                    my $f_style     = $self->grid->y->label->style || '';

                    if ( $f_style =~ /(italic)|(oblique)/ )
                    {
                        $font_style = 'italic';
                    }
                    if ( $f_style =~ /bold/ )
                    {
                        $font_weight = 'bold';
                    }

                    my $radian   = ( $self->grid->y->label->rotation / 180 ) * PI || 0;
                    my $cos      = cos( $radian );
                    my $sin      = sin( $radian );
                    my $len      = length( $self->grid->y->label->text->[$nbr] );
                    my $x_offset = 0;
                    my $y_offset = 0;

                    my $l = ( 0.628 * $self->grid->y->label->size * $max_length_y ) - 5.052;
                    my %style;
                    if ( $self->grid->y->label->align =~ /left/i )
                    {
                        %style = (
                            'font-family'  => $self->grid->y->label->font,
                            'font-size'    => $self->grid->y->label->size,
                            'font-style'   => $font_style,
                            'font-weight'  => $font_weight,
                            'fill'         => '#' . ( $text_color || 'ffffff' ),
                            'stroke'       => '#' . ( $text_color || '000000' ),
                            'writing-mode' => 'lr',
                            'text-anchor'  => 'end',
#                             'baseline-shift'=> '-10%',
                        );
                        if ( $self->grid->y->label->rotation )
                        {
                            $y_offset = ( $self->grid->debord->bottom + $self->grid->y->label->space ) * 2;
                        }
                    }
                    else
                    {
                        %style = (
                            'font-family'  => $self->grid->y->label->font,
                            'font-size'    => $self->grid->y->label->size,
                            'font-style'   => $font_style,
                            'font-weight'  => $font_weight,
                            'fill'         => '#' . ( $text_color || 'ffffff' ),
                            'stroke'       => '#' . ( $text_color || '000000' ),
                            'writing-mode' => 'lr',
                            'text-anchor'  => 'start',
#                             'baseline-shift'=> '-10%',
                        );
                        $x_offset = $self->grid->y->label->size * ( ( 0.7025 * $max_length_y ) - 1.601 ) * $cos;
                        $y_offset = ( $l * $sin ) + ( $self->grid->debord->bottom + $self->grid->y->label->space ) + ( $l / $cos );

                    }

                    my $txt = $y_grid_text->text(
                        x         => $self->border->left + $val - $x_offset,
                        y         => $y_offset,
                        style     => \%style,
                        transform => " rotate( " . $self->grid->y->label->rotation . "," . ( $self->border->left + $val - $x_offset ) . ", " . $y_offset . " ) ",
                    );
                    $txt->tspan( dy => "0" )->cdata( $self->grid->y->label->text->[$nbr] );

                }

                if ( defined $self->grid->y->label2 && defined $self->grid->y->label2->text->[$nbr] )
                {
                    my $text_color  = $self->grid->y->label2->color || $color_hex;
                    my $font_style  = 'normal';
                    my $font_weight = 'normal';
                    my $f_style     = $self->grid->y->label2->style || '';

                    if ( $f_style =~ /(italic)|(oblique)/ )
                    {
                        $font_style = 'italic';
                    }
                    if ( $f_style =~ /bold/ )
                    {
                        $font_weight = 'bold';
                    }

                    my $radian   = ( $self->grid->y->label2->rotation / 180 ) * PI || 0;
                    my $cos      = cos( $radian );
                    my $sin      = sin( $radian );
                    my $len      = length( $self->grid->y->label2->text->[$nbr] );
                    my $x_offset = 0;
                    my $y_offset = 0;
#                     my $l        = ( 0.628 * $self->grid->y->label2->size * $max_length_y2 ) - 5.052;
                    my $l = $self->grid->y->label2->size * $max_length_y2 * $self->grid->y->label2->font_scaling;
                    my %style;

                    if ( $self->grid->y->label2->align =~ /left/i )
                    {
                        %style = (
                            'font-family'  => $self->grid->y->label2->font,
                            'font-size'    => $self->grid->y->label2->size,
                            'font-style'   => $font_style,
                            'font-weight'  => $font_weight,
                            'fill'         => '#' . ( $text_color || 'ffffff' ),
                            'stroke'       => '#' . ( $text_color || '000000' ),
                            'writing-mode' => 'lr',
                            'text-anchor'  => 'start',
#                             'baseline-shift'=> '-10%',
                        );
                        if ( $self->grid->y->label2->rotation )
                        {
                            $y_offset = -( $self->active_size->[1] + $self->grid->debord->top + $self->grid->y->label2->space );
                        }
                    }
                    else
                    {
                        %style = (
                            'font-family'  => $self->grid->y->label2->font,
                            'font-size'    => $self->grid->y->label2->size,
                            'font-style'   => $font_style,
                            'font-weight'  => $font_weight,
                            'fill'         => '#' . ( $text_color || 'ffffff' ),
                            'stroke'       => '#' . ( $text_color || '000000' ),
                            'writing-mode' => 'lr',
                            'text-anchor'  => 'end',
#                             'baseline-shift'=> '-10%',
                        );
                        $x_offset = ( $l * $cos ) + $val + $self->border->left;
                        $y_offset = ( $l * $sin ) - $self->grid->debord->bottom - $self->active_size->[1] - $self->grid->debord->top - $self->grid->y->label2->space;
                    }
                    my $txt = $y_grid_text->text(
                        x         => $x_offset,
                        y         => $y_offset,
                        style     => \%style,
                        transform => " rotate( " . $self->grid->y->label2->rotation . "," . ( $x_offset ) . ", " . $y_offset . " ) ",
                    );
                    $txt->tspan( dy => "0" )->cdata( $self->grid->y->label2->text->[$nbr] );
                }
            }
        }
    }

#######################################
####### Frame #######
#######################################
    if ( defined $self->frame )
    {
        my $k = $svg->rectangle(
            x      => $self->{ border }->left,
            y      => $self->{ border }->top,
            width  => $self->{ active_size }->[0],
            height => $self->{ active_size }->[1],
#               rx    => 10, ry     => 5,
            style => {
                stroke => '#' . ( ( $self->frame )->{ color } ),
                fill => 'none',
                'stroke-width' => ( $self->frame )->{ thickness } || 0,
            },
            id => 'frame'
        );
    }

#######################################
####### TAG #######
#######################################
    if ( exists $self->{ tag } && $self->{ tag } )
    {
        my $tag_group = $svg->group( id => "serial_tag" );
        my $obj = Data::Serializer->new( 'compress' => 1 );
        my $tag = $obj->serialize( $self->{ tag } );
        $tag_group->comment( $tag );
    }

    $self->image(
        $svg->xmlify(
            -namespace  => "svg",
            -pubid      => "-//W3C//DTD SVG 1.0//EN",
            -standalone => "no",
            -inline     => 1
        )
    );
    $self->{ svg_raw } = $svg;
}

sub sum_array
{
    my $col  = shift;
    my $line = shift;
    my $data = shift;
    my $res  = 0;
    for my $c_idx ( 0 .. $col )
    {
        $res += $data->[$c_idx][$line] || 0;
    }
    return $res;
}



( run in 1.428 second using v1.01-cache-2.11-cpan-e1769b4cff6 )