fpdf
PHP powered
home was ist fpdf? downloads & add-ons dokumentation faq forum impressum links kontakt
 

Downloads > Add-On

 
         
 

Sector

Informations

Author: Maxime Delorme
License: Freeware

Description

This script allows to draw the sector of a circle. It can be used for instance to render pie charts.
It requires FPDF 1.51.

function Sector(float xc, float yc, float r, float a, float b [, string style [, boolean cw [, float o]]])

xc: abscissa of the center.
yc: ordinate of the center.
r: radius.
a: start angle (in degrees).
b: end angle (in degrees).
style: D, F, FD or DF (draw, fill, fill and draw). Default: FD.
cw: indicates whether to go clockwise (default: true).
o: origin of angles (0 for 3 o'clock, 90 for noon, 180 for 9 o'clock, 270 for 6 o'clock). Default: 90.

Source

<?php
require('fpdf.php');

class PDF_Sector extends FPDF
{
    function Sector($xc,  $yc,  $r,  $a,  $b,  $style='FD',  $cw=true,  $o=90)
    {
        if($cw){
            $d = $b;
            $b = $o - $a;
            $a = $o - $d;
        }else{
            $b += $o;
            $a += $o;
        }
        $a = ($a%360)+360;
        $b = ($b%360)+360;
        if ($a > $b)
            $b +=360;
        $b = $b/360*2*M_PI;
        $a = $a/360*2*M_PI;
        $d = $b-$a;
        if ($d == 0 )
            $d =2*M_PI;
        $k = $this->k;
        $hp = $this->h;
        if($style=='F')
            $op='f';
        elseif($style=='FD' or $style=='DF')
            $op='b';
        else
            $op
='s';
        if (sin($d/2))
            $MyArc = 4/3*(1-cos($d/2))/sin($d/2)*$r;
        //first put the center
        $this->_out(sprintf('%.2f %.2f m', ($xc)*$k, ($hp-$yc)*$k));
        //put the first point
        $this->_out(sprintf('%.2f %.2f l', ($xc+$r*cos($a))*$k, (($hp-($yc-$r*sin($a)))*$k)));
        //draw the arc
        if ($d < M_PI/2){
            $this->_Arc($xc+$r*cos($a)+$MyArc*cos(M_PI/2+$a),
                        $yc-$r*sin($a)-$MyArc*sin(M_PI/2+$a),
                        $xc+$r*cos($b)+$MyArc*cos($b-M_PI/2),
                        $yc-$r*sin($b)-$MyArc*sin($b-M_PI/2),
                        $xc+$r*cos($b),
                        $yc-$r*sin($b)
                        );
        }else{
            $b = $a + $d/4;
            $MyArc = 4/3*(1-cos($d/8))/sin($d/8)*$r;
            $this->_Arc($xc+$r*cos($a)+$MyArc*cos(M_PI/2+$a),
                        $yc-$r*sin($a)-$MyArc*sin(M_PI/2+$a),
                        $xc+$r*cos($b)+$MyArc*cos($b-M_PI/2),
                        $yc-$r*sin($b)-$MyArc*sin($b-M_PI/2),
                        $xc+$r*cos($b),
                        $yc-$r*sin($b)
                        );
            $a = $b;
            $b = $a + $d/4;
            $this->_Arc($xc+$r*cos($a)+$MyArc*cos(M_PI/2+$a),
                        $yc-$r*sin($a)-$MyArc*sin(M_PI/2+$a),
                        $xc+$r*cos($b)+$MyArc*cos($b-M_PI/2),
                        $yc-$r*sin($b)-$MyArc*sin($b-M_PI/2),
                        $xc+$r*cos($b),
                        $yc-$r*sin($b)
                        );
            $a = $b;
            $b = $a + $d/4;
            $this->_Arc($xc+$r*cos($a)+$MyArc*cos(M_PI/2+$a),
                        $yc-$r*sin($a)-$MyArc*sin(M_PI/2+$a),
                        $xc+$r*cos($b)+$MyArc*cos($b-M_PI/2),
                        $yc-$r*sin($b)-$MyArc*sin($b-M_PI/2),
                        $xc+$r*cos($b),
                        $yc-$r*sin($b)
                        );
            $a = $b;
            $b = $a + $d/4;
            $this->_Arc($xc+$r*cos($a)+$MyArc*cos(M_PI/2+$a),
                        $yc-$r*sin($a)-$MyArc*sin(M_PI/2+$a),
                        $xc+$r*cos($b)+$MyArc*cos($b-M_PI/2),
                        $yc-$r*sin($b)-$MyArc*sin($b-M_PI/2),
                        $xc+$r*cos($b),
                        $yc-$r*sin($b)
                        );
        }
        //terminate drawing
        $this->_out($op);
    }

    function _Arc($x1,  $y1,  $x2,  $y2,  $x3,  $y3 )
    {
        $h = $this->h;
        $this->_out(sprintf('%.2f %.2f %.2f %.2f %.2f %.2f c',
            $x1*$this->k,
            ($h-$y1)*$this->k,
            $x2*$this->k,
            ($h-$y2)*$this->k,
            $x3*$this->k,
            ($h-$y3)*$this->k));
    }
}
?>

Example

<?php
require('sector.php');

$pdf=new PDF_Sector();
$pdf->Open();
$pdf->AddPage();
$xc=105;
$yc=55;
$r=40;
$pdf->SetFillColor(120, 120, 255);
$pdf->Sector($xc, $yc, $r, 20, 120);
$pdf->SetFillColor(120, 255, 120);
$pdf->Sector($xc, $yc, $r, 120, 250);
$pdf->SetFillColor(255, 120, 120);
$pdf->Sector($xc, $yc, $r, 250, 20);
$pdf->Output();
?>

View the result here.

Download

ZIP | TGZ
 






 
         
         
 
 
fpdf.de © Copyright 2004-2006 carrib internet solutions - Beachten Sie bitte die Nutzungsbedingungen
Eine Verwendung der auf fpdf.de befindlichen Inhalte (Texte, Grafiken) ist nur mit Zustimmung des Betreibers zulässig.