SVGGraph-Pie

 view release on metacpan or  search on metacpan

lib/SVGGraph/Pie.pm  view on Meta::CPAN

	my $radians = $slice * PI / 180;
	$do_arc++  if $slice > 180;

	my $ry = ($radius * sin($radians));
	my $rx = $radius * cos($radians);
	push(@separator_lines, $rx, $ry, $start);

	my $g = $pie->tag('g', id => "wedge_$i", transform => "rotate($start)");
	$g->path(
	    style => {'fill' => "$color"},
	    d => "M $radius,0 A $radius,$radius 0 $do_arc,1 $rx,$ry L 0,0 z"
	);

	$start += $slice;
    }

    ## Draw circle
    my $circlestyle = {'fill-opacity' => 0};
    if ($borderwidth) {
        $circlestyle->{stroke} = 'black';
        $circlestyle->{'stroke-width'} = $borderwidth;
    }

    $svg->circle(
	cx => $cx,
	cy => $cy,
	r  => $radius,
	style => $circlestyle,
    );

    ## Draw separater
    my $i = 0;
    while (my $start = pop(@separator_lines)) {
	$i++;
	my $separator_y = pop(@separator_lines);
	my $separator_x = pop(@separator_lines);
	my $g = $pie->tag('g', id => "line_$i", transform => "rotate($start)");
        my $linestyle = {};
        if ($borderwidth) {
            $linestyle->{stroke} = 'black';
            $linestyle->{'stroke-width'} = $borderwidth;
        }

	$g->line(
	    x1 => 0,
	    y1 => 0,
	    x2 => $separator_x,
	    y2 => $separator_y,
	    style => $linestyle,
	);
    }

    ## Title
    if ($$options{title}) {
	my $titlestyle = 'font-size:24;';
	$titlestyle = $$options{titlestyle} if $$options{titlestyle};
	$svg->text(
	    'x' => 20,
	    'y' => 40,
	    'style' => $titlestyle,
	)->cdata($$options{title});
    }

    ## Label
    if ($$options{label}) {
	my $labelleft = $cx + $radius + 10;
	$labelleft = $$options{labelleft} if $$options{labelleft};
	$start = $cy - $radius;
	$start = $$options{labeltop} if $$options{labeltop};

	for (my $i = 0; $i < @$values; $i++) {
	    $svg->rectangle(
		'x' => $labelleft,
		'y' => $start,
		'width' => 20,
		'height' => 20,
		'rx' => 5,
		'ry' => 5,
		'style' => {
		    fill => $labelcolors[$i],
		    stroke => 'black',
		},
	    );
	    $svg->text(
		'x' => $labelleft + 25,
		'y' => $start + GAP,
	    )->cdata($values->[$i]->{label});

	    $start += 25;
	}
    }

    return $svg->xmlify;
}

## private method
sub _round {
    my $decimal = shift;

    $decimal *= 10;
    $decimal = int($decimal + 0.5);
    $decimal /= 10;

    return $decimal;
}

1;
__END__

=head1 NAME

SVGGraph::Pie - Perl extension for Pie as SVG

=head1 SYNOPSIS

  use SVGGraph::Pie;

  my $svggraph = SVGGraph::Pie->new;
  $svggraph->CreateGraph(
      {
          imageheight => 500,
          imagewidth  => 500,
          centertop  => 250,
          centerleft => 250,
          radius => 200,
          title => 'Financial Results Q1 2002',
          titlestyle => 'font-size:24;fill:#FF0000;',
          borderwidth => 4, # border line's width
          label => 'true',  # Woud you like display label?
          labeltop  => '100',
          labelleft => '400',
      },
      [
          {value => 10, color => 'red'},
          {value => 20, color => 'blue'),
          ...
          ..
          .
      ],
  );

=head1 DESCRIPTION

SVGGraph::Pie allow you to create Piegraphs as SVG very easily.

=head1 EXAMPLES



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