/**
  * Draw a circle sector.
  *
  * @param integer $xc center abscis
  * @param integer $yc center ordonnate
  * @param integer $a begin angle (degree)
  * @param integer $b end angle (degree)
  * @param string $color fill color
  * @param string $style fill style [D(draw)|F(fill)|FD|DF]
  * @param boolean $cw rotate wise (true: clock wise, false: counter clock wise)
  * @param integer $o origine angle (0 to right, 90 to top, 180 to left, 270 to bottom)
  * @return void 
  */
 public static function sector($xc, $yc, $r, $a, $b, $color = '#ffffff', $style = 'FD', $cw = true, $o = 90)
 {
     $tab = Xml2Pdf::ConvertColor($color);
     $pdf = Pdf::singleton();
     $pdf->SetFillColor($tab['r'], $tab['g'], $tab['b']);
     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 = $pdf->k;
     $hp = $pdf->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
     $pdf->_out(sprintf('%.2f %.2f m', $xc * $k, ($hp - $yc) * $k));
     //put the first point
     $pdf->_out(sprintf('%.2f %.2f l', ($xc + $r * cos($a)) * $k, ($hp - ($yc - $r * sin($a))) * $k));
     //draw the arc
     if ($d < M_PI / 2) {
         xml2pdf_graph_circle::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;
         xml2pdf_graph_circle::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;
         xml2pdf_graph_circle::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;
         xml2pdf_graph_circle::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;
         xml2pdf_graph_circle::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
     $pdf->_out($op);
 }
 /**
  * Draw a curve type graph
  *
  * @param Object $graph Object Xml2Pdf_Tag_graph
  * @return void
  */
 public static function render($graph)
 {
     $graph->pdf->SetFont('Courier', '', $graph->fontSize);
     $graph->setLegends();
     $XPage = $graph->pdf->GetX();
     $YPage = $graph->pdf->GetY();
     $marge = 2;
     $hDiag = $graph->height;
     $lDiag = $graph->width - $graph->legendWidth - 5 * $marge - 5;
     $XDiag = $XPage + $marge;
     $YDiag = $YPage + $marge;
     $xMax = 0;
     $yMax = 0;
     foreach ($graph->data as $line) {
         if ($xMax < max($line['x'])) {
             $xMax = max($line['x']);
         }
         if ($yMax < max($line['y'])) {
             $yMax = max($line['y']);
         }
     }
     $uniteX = $lDiag / $xMax;
     $uniteY = $hDiag / ($yMax + 5);
     $graph->pdf->SetLineWidth(0.2);
     $graph->pdf->Rect($XDiag, $YDiag, $lDiag, $hDiag);
     //reperes
     $tab = Xml2Pdf::ConvertColor('#dcdcdc');
     $graph->pdf->SetDrawColor($tab['r'], $tab['g'], $tab['b']);
     $deltaX = $lDiag / $graph->nbIndRepere;
     $deltaY = $hDiag / $graph->nbIndRepere;
     $graph->pdf->SetLineWidth(0.2);
     for ($i = 0; $i <= $graph->nbIndRepere; $i++) {
         if ($i > 0 && $i < $graph->nbIndRepere) {
             $graph->pdf->Line($XDiag, $YDiag + $i * $deltaY, $XDiag + $lDiag, $YDiag + $i * $deltaY);
             $graph->pdf->Line($XDiag + $i * $deltaX, $YDiag, $XDiag + $i * $deltaX, $YDiag + $hDiag);
         }
         $valY = floor(($hDiag - $i * $deltaY) / $uniteY);
         $valX = floor($i * $deltaX / $uniteX);
         $lenY = $graph->pdf->GetStringWidth($valY);
         $lenX = $graph->pdf->GetStringWidth($valX);
         $graph->pdf->Text($XDiag - $lenY - 2, $YDiag + $i * $deltaY, $valY);
         $graph->pdf->Text($XDiag + $i * $deltaX - $lenX / 2, $YDiag + $hDiag + 5, $valX);
     }
     //lignes
     $nbPoint = 0;
     $ligne = 0;
     $graph->pdf->SetLineWidth(0.5);
     foreach ($graph->data as $key => $line) {
         if ($graph->colors[$key] == null) {
             $graph->colors[$key] = Xml2Pdf::getColor();
         }
         $tab = Xml2Pdf::ConvertColor($graph->colors[$key]);
         $graph->pdf->SetDrawColor($tab['r'], $tab['g'], $tab['b']);
         $nbPoint = min(count($line['x']), count($line['y']));
         for ($i = 0; $i < $nbPoint - 1; $i++) {
             $xd = $XDiag + $line['x'][$i] * $uniteX;
             $yd = $YDiag + $hDiag - $line['y'][$i] * $uniteY;
             $xf = $XDiag + $line['x'][$i + 1] * $uniteX;
             $yf = $YDiag + $hDiag - $line['y'][$i + 1] * $uniteY;
             $graph->pdf->Line($xd, $yd, $xf, $yf);
         }
         $ligne++;
     }
     //Légende
     $graph->pdf->SetLineWidth(0.2);
     $tab = Xml2Pdf::ConvertColor('#000000');
     $graph->pdf->SetDrawColor($tab['r'], $tab['g'], $tab['b']);
     $graph->pdf->SetFont('Courier', '', $graph->fontSize);
     $x1 = $XPage + $lDiag + 4 * $marge;
     $x2 = $x1 + 5 + $marge;
     $y1 = $YDiag + 3 * $marge;
     for ($i = 0; $i < $graph->nbVal; $i++) {
         $tab = Xml2Pdf::ConvertColor($graph->colors[$graph->legends[$i]]);
         $graph->pdf->SetFillColor($tab['r'], $tab['g'], $tab['b']);
         $graph->pdf->Rect($x1, $y1 + $i * $marge * 2, 5, 5, 'DF');
         $graph->pdf->SetXY($x2, $y1 + $i * $marge * 2);
         $graph->pdf->Cell(0, 5, $graph->legends[$i]);
         $y1 += $marge;
     }
 }
 /**
  * Draw an horizontal bar graph.
  *
  * @param Object $graph object xml2pdf_tag_graph
  * @return void 
  */
 public static function render($graph)
 {
     $graph->pdf->SetFont('Courier', '', $graph->fontSize);
     $graph->setLegends();
     $XPage = $graph->pdf->GetX();
     $YPage = $graph->pdf->GetY();
     $marge = 2;
     $YDiag = $YPage + $marge;
     $hDiag = floor($graph->height - $marge * 2);
     $XDiag = $XPage + $marge * 2 + $graph->legendWidth;
     $lDiag = floor($graph->width - $marge * 3 - $graph->legendWidth);
     if ($graph->color == null) {
         $graph->color = Xml2Pdf::getColor();
     }
     if ($graph->maxValRepere == 0) {
         $graph->maxValRepere = max($graph->data);
     }
     $valIndRepere = ceil($graph->maxValRepere / $graph->nbIndRepere);
     $graph->maxValRepere = $valIndRepere * $graph->nbIndRepere;
     $lRepere = floor($lDiag / $graph->nbIndRepere);
     $lDiag = $lRepere * $graph->nbIndRepere;
     $unite = $lDiag / $graph->maxValRepere;
     $hBaton = floor($hDiag / ($graph->nbVal + 1));
     $hDiag = $hBaton * ($graph->nbVal + 1);
     $eBaton = floor($hBaton * 80 / 100);
     $graph->pdf->SetLineWidth(0.2);
     $graph->pdf->Rect($XDiag, $YDiag, $lDiag, $hDiag);
     //Echelles
     $tab = Xml2Pdf::ConvertColor('#dcdcdc');
     $graph->pdf->SetDrawColor($tab['r'], $tab['g'], $tab['b']);
     for ($i = 0; $i <= $graph->nbIndRepere; $i++) {
         $xpos = $XDiag + $lRepere * $i;
         if ($i > 0 && $i < $graph->nbIndRepere) {
             $graph->pdf->Line($xpos, $YDiag, $xpos, $YDiag + $hDiag);
         }
         $val = $i * $valIndRepere;
         $xpos = $XDiag + $lRepere * $i - $graph->pdf->GetStringWidth($val) / 2;
         $ypos = $YDiag + $hDiag - $marge;
         $graph->pdf->Text($xpos, $ypos, $val);
     }
     $tab = Xml2Pdf::ConvertColor('#000000');
     $graph->pdf->SetDrawColor($tab['r'], $tab['g'], $tab['b']);
     $graph->pdf->SetFont('Courier', '', $graph->fontSize);
     $tab = Xml2Pdf::ConvertColor($graph->color);
     $graph->pdf->SetFillColor($tab['r'], $tab['g'], $tab['b']);
     $i = 0;
     foreach ($graph->data as $val) {
         //Barre
         $xval = $XDiag;
         $lval = (int) ($val * $unite);
         $yval = $YDiag + ($i + 1) * $hBaton - $eBaton / 2;
         $hval = $eBaton;
         $graph->pdf->Rect($xval, $yval, $lval, $hval, 'DF');
         //Légende
         $graph->pdf->SetXY(0, $yval);
         $graph->pdf->Cell($xval - $marge, $hval, $graph->legends[$i], 0, 0, 'R');
         $i++;
     }
 }