Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • PHP: Create Bar graph and Pie Chart in pdf

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 5.22k
    Comment on it

    Create Bar graph and Pie chart in PDF format using FPDF

    Hello friends, I am writing this blog which will help you to create bar graph and pie chart using fpdf. Firstly you need to download fpdf.php file from the following fpdf library. Click here to download. For more details you can go to http://www.fpdf.org/

    Once you have downloaded fpdf zip, extract it. Create a new folder inside the directory where you run the project with name pdf and copy fpdf.php from fpdf library and paste it inside pdf folder.

     

    Step 1: Now create file sector.php and paste the following code:

     

    1. <?php
    2. require('fpdf.php');
    3.  
    4. class PDF_Sector extends FPDF
    5. {
    6.     function Sector($xc, $yc, $r, $a, $b, $style='FD', $cw=true, $o=90)
    7.     {
    8.         $d0 = $a - $b;
    9.         if($cw){
    10.             $d = $b;
    11.             $b = $o - $a;
    12.             $a = $o - $d;
    13.         }else{
    14.             $b += $o;
    15.             $a += $o;
    16.         }
    17.         while($a<0)
    18.             $a += 360;
    19.         while($a>360)
    20.             $a -= 360;
    21.         while($b<0)
    22.             $b += 360;
    23.         while($b>360)
    24.             $b -= 360;
    25.         if ($a > $b)
    26.             $b += 360;
    27.         $b = $b/360*2*M_PI;
    28.         $a = $a/360*2*M_PI;
    29.         $d = $b - $a;
    30.         if ($d == 0 && $d0 != 0)
    31.             $d = 2*M_PI;
    32.         $k = $this->k;
    33.         $hp = $this->h;
    34.         if (sin($d/2))
    35.             $MyArc = 4/3*(1-cos($d/2))/sin($d/2)*$r;
    36.         else
    37.             $MyArc = 0;
    38.         //first put the center
    39.         $this->_out(sprintf('%.2F %.2F m',($xc)*$k,($hp-$yc)*$k));
    40.         //put the first point
    41.         $this->_out(sprintf('%.2F %.2F l',($xc+$r*cos($a))*$k,(($hp-($yc-$r*sin($a)))*$k)));
    42.         //draw the arc
    43.         if ($d < M_PI/2){
    44.             $this->_Arc($xc+$r*cos($a)+$MyArc*cos(M_PI/2+$a),
    45.                         $yc-$r*sin($a)-$MyArc*sin(M_PI/2+$a),
    46.                         $xc+$r*cos($b)+$MyArc*cos($b-M_PI/2),
    47.                         $yc-$r*sin($b)-$MyArc*sin($b-M_PI/2),
    48.                         $xc+$r*cos($b),
    49.                         $yc-$r*sin($b)
    50.                         );
    51.         }else{
    52.             $b = $a + $d/4;
    53.             $MyArc = 4/3*(1-cos($d/8))/sin($d/8)*$r;
    54.             $this->_Arc($xc+$r*cos($a)+$MyArc*cos(M_PI/2+$a),
    55.                         $yc-$r*sin($a)-$MyArc*sin(M_PI/2+$a),
    56.                         $xc+$r*cos($b)+$MyArc*cos($b-M_PI/2),
    57.                         $yc-$r*sin($b)-$MyArc*sin($b-M_PI/2),
    58.                         $xc+$r*cos($b),
    59.                         $yc-$r*sin($b)
    60.                         );
    61.             $a = $b;
    62.             $b = $a + $d/4;
    63.             $this->_Arc($xc+$r*cos($a)+$MyArc*cos(M_PI/2+$a),
    64.                         $yc-$r*sin($a)-$MyArc*sin(M_PI/2+$a),
    65.                         $xc+$r*cos($b)+$MyArc*cos($b-M_PI/2),
    66.                         $yc-$r*sin($b)-$MyArc*sin($b-M_PI/2),
    67.                         $xc+$r*cos($b),
    68.                         $yc-$r*sin($b)
    69.                         );
    70.             $a = $b;
    71.             $b = $a + $d/4;
    72.             $this->_Arc($xc+$r*cos($a)+$MyArc*cos(M_PI/2+$a),
    73.                         $yc-$r*sin($a)-$MyArc*sin(M_PI/2+$a),
    74.                         $xc+$r*cos($b)+$MyArc*cos($b-M_PI/2),
    75.                         $yc-$r*sin($b)-$MyArc*sin($b-M_PI/2),
    76.                         $xc+$r*cos($b),
    77.                         $yc-$r*sin($b)
    78.                         );
    79.             $a = $b;
    80.             $b = $a + $d/4;
    81.             $this->_Arc($xc+$r*cos($a)+$MyArc*cos(M_PI/2+$a),
    82.                         $yc-$r*sin($a)-$MyArc*sin(M_PI/2+$a),
    83.                         $xc+$r*cos($b)+$MyArc*cos($b-M_PI/2),
    84.                         $yc-$r*sin($b)-$MyArc*sin($b-M_PI/2),
    85.                         $xc+$r*cos($b),
    86.                         $yc-$r*sin($b)
    87.                         );
    88.         }
    89.         //terminate drawing
    90.         if($style=='F')
    91.             $op='f';
    92.         elseif($style=='FD' || $style=='DF')
    93.             $op='b';
    94.         else
    95.             $op='s';
    96.         $this->_out($op);
    97.     }
    98.  
    99.     function _Arc($x1, $y1, $x2, $y2, $x3, $y3 )
    100.     {
    101.         $h = $this->h;
    102.         $this->_out(sprintf('%.2F %.2F %.2F %.2F %.2F %.2F c',
    103.             $x1*$this->k,
    104.             ($h-$y1)*$this->k,
    105.             $x2*$this->k,
    106.             ($h-$y2)*$this->k,
    107.             $x3*$this->k,
    108.             ($h-$y3)*$this->k));
    109.     }
    110. }
    111. ?>

     

    Step 2: Now create diag.php inside pdf folder and write the following code:

     

    1. <?php
    2. require('sector.php');
    3.  
    4. class PDF_Diag extends PDF_Sector {
    5.     var $legends;
    6.     var $wLegend;
    7.     var $sum;
    8.     var $NbVal;
    9.  
    10.     function PieChart($w, $h, $data, $format, $colors=null)
    11.     {
    12.         $this->SetFont('Courier', '', 10);
    13.         $this->SetLegends($data,$format);
    14.  
    15.         $XPage = $this->GetX();
    16.         $YPage = $this->GetY();
    17.         $margin = 2;
    18.         $hLegend = 5;
    19.         $radius = min($w - $margin * 4 - $hLegend - $this->wLegend, $h - $margin * 2);
    20.         $radius = floor($radius / 2);
    21.         $XDiag = $XPage + $margin + $radius;
    22.         $YDiag = $YPage + $margin + $radius;
    23.         if($colors == null) {
    24.             for($i = 0; $i < $this->NbVal; $i++) {
    25.                 $gray = $i * intval(255 / $this->NbVal);
    26.                 $colors[$i] = array($gray,$gray,$gray);
    27.             }
    28.         }
    29.  
    30.         //Sectors
    31.         $this->SetLineWidth(0.2);
    32.         $angleStart = 0;
    33.         $angleEnd = 0;
    34.         $i = 0;
    35.         foreach($data as $val) {
    36.             $angle = ($val * 360) / doubleval($this->sum);
    37.             if ($angle != 0) {
    38.                 $angleEnd = $angleStart + $angle;
    39.                 $this->SetFillColor($colors[$i][0],$colors[$i][1],$colors[$i][2]);
    40.                 $this->Sector($XDiag, $YDiag, $radius, $angleStart, $angleEnd);
    41.                 $angleStart += $angle;
    42.             }
    43.             $i++;
    44.         }
    45.  
    46.         //Legends
    47.         $this->SetFont('Courier', '', 10);
    48.         $x1 = $XPage + 2 * $radius + 4 * $margin;
    49.         $x2 = $x1 + $hLegend + $margin;
    50.         $y1 = $YDiag - $radius + (2 * $radius - $this->NbVal*($hLegend + $margin)) / 2;
    51.         for($i=0; $i<$this->NbVal; $i++) {
    52.             $this->SetFillColor($colors[$i][0],$colors[$i][1],$colors[$i][2]);
    53.             $this->Rect($x1, $y1, $hLegend, $hLegend, 'DF');
    54.             $this->SetXY($x2,$y1);
    55.             $this->Cell(0,$hLegend,$this->legends[$i]);
    56.             $y1+=$hLegend + $margin;
    57.         }
    58.     }
    59.  
    60.     function BarDiagram($w, $h, $data, $format, $color=null, $maxVal=0, $nbDiv=4)
    61.     {
    62.         $this->SetFont('Courier', '', 10);
    63.         $this->SetLegends($data,$format);
    64.  
    65.         $XPage = $this->GetX();
    66.         $YPage = $this->GetY();
    67.         $margin = 2;
    68.         $YDiag = $YPage + $margin;
    69.         $hDiag = floor($h - $margin * 2);
    70.         $XDiag = $XPage + $margin * 2 + $this->wLegend;
    71.         $lDiag = floor($w - $margin * 3 - $this->wLegend);
    72.         if($color == null)
    73.             $color=array(155,155,155);
    74.         if ($maxVal == 0) {
    75.             $maxVal = max($data);
    76.         }
    77.         $valIndRepere = ceil($maxVal / $nbDiv);
    78.         $maxVal = $valIndRepere * $nbDiv;
    79.         $lRepere = floor($lDiag / $nbDiv);
    80.         $lDiag = $lRepere * $nbDiv;
    81.         $unit = $lDiag / $maxVal;
    82.         $hBar = floor($hDiag / ($this->NbVal + 1));
    83.         $hDiag = $hBar * ($this->NbVal + 1);
    84.         $eBaton = floor($hBar * 80 / 100);
    85.  
    86.         $this->SetLineWidth(0.2);
    87.         $this->Rect($XDiag, $YDiag, $lDiag, $hDiag);
    88.  
    89.         $this->SetFont('Courier', '', 10);
    90.         $this->SetFillColor($color[0],$color[1],$color[2]);
    91.         $i=0;
    92.         foreach($data as $val) {
    93.             //Bar
    94.             $xval = $XDiag;
    95.             $lval = (int)($val * $unit);
    96.             $yval = $YDiag + ($i + 1) * $hBar - $eBaton / 2;
    97.             $hval = $eBaton;
    98.             $this->Rect($xval, $yval, $lval, $hval, 'DF');
    99.             //Legend
    100.             $this->SetXY(0, $yval);
    101.             $this->Cell($xval - $margin, $hval, $this->legends[$i],0,0,'R');
    102.             $i++;
    103.         }
    104.  
    105.         //Scales
    106.         for ($i = 0; $i <= $nbDiv; $i++) {
    107.             $xpos = $XDiag + $lRepere * $i;
    108.             $this->Line($xpos, $YDiag, $xpos, $YDiag + $hDiag);
    109.             $val = $i * $valIndRepere;
    110.             $xpos = $XDiag + $lRepere * $i - $this->GetStringWidth($val) / 2;
    111.             $ypos = $YDiag + $hDiag - $margin;
    112.             $this->Text($xpos, $ypos, $val);
    113.         }
    114.     }
    115.  
    116.     function SetLegends($data, $format)
    117.     {
    118.         $this->legends=array();
    119.         $this->wLegend=0;
    120.         $this->sum=array_sum($data);
    121.         $this->NbVal=count($data);
    122.         foreach($data as $l=>$val)
    123.         {
    124.             $p=sprintf('%.2f',$val/$this->sum*100).'%';
    125.             $legend=str_replace(array('%l','%v','%p'),array($l,$val,$p),$format);
    126.             $this->legends[]=$legend;
    127.             $this->wLegend=max($this->GetStringWidth($legend),$this->wLegend);
    128.         }
    129.     }
    130. }
    131. ?>

     

    Step 3: Finally create index.php file inside pdf folder and write the following code:

     

    1. <?php
    2. require('diag.php');
    3.  
    4. $pdf = new PDF_Diag();
    5. $pdf->AddPage();
    6.  
    7. $data = array('Men' => 1510, 'Women' => 1610, 'Children' => 1400);
    8.  
    9. //Pie chart
    10. $pdf->SetFont('Arial', 'BIU', 12);
    11. $pdf->Cell(0, 5, '1 - Pie chart', 0, 1);
    12. $pdf->Ln(8);
    13.  
    14. $pdf->SetFont('Arial', '', 10);
    15. $valX = $pdf->GetX();
    16. $valY = $pdf->GetY();
    17. $pdf->Cell(30, 5, 'Number of men:');
    18. $pdf->Cell(15, 5, $data['Men'], 0, 0, 'R');
    19. $pdf->Ln();
    20. $pdf->Cell(30, 5, 'Number of women:');
    21. $pdf->Cell(15, 5, $data['Women'], 0, 0, 'R');
    22. $pdf->Ln();
    23. $pdf->Cell(30, 5, 'Number of children:');
    24. $pdf->Cell(15, 5, $data['Children'], 0, 0, 'R');
    25. $pdf->Ln();
    26. $pdf->Ln(8);
    27.  
    28. $pdf->SetXY(90, $valY);
    29. $col1=array(100,100,255);
    30. $col2=array(255,100,100);
    31. $col3=array(255,255,100);
    32. $pdf->PieChart(100, 35, $data, '%l (%p)', array($col1,$col2,$col3));
    33. $pdf->SetXY($valX, $valY + 40);
    34.  
    35. //Bar diagram
    36. $pdf->SetFont('Arial', 'BIU', 12);
    37. $pdf->Cell(0, 5, '2 - Bar diagram', 0, 1);
    38. $pdf->Ln(8);
    39. $valX = $pdf->GetX();
    40. $valY = $pdf->GetY();
    41. $pdf->BarDiagram(190, 70, $data, '%l : %v (%p)', array(255,175,100));
    42. $pdf->SetXY($valX, $valY + 80);
    43.  
    44. $pdf->Output();
    45. ?>

     

    Now, all done, run following url: http://localhost/pdf/  to see the output:

     

     

     

    Thanks for reading.

    PHP: Create Bar graph and Pie Chart in pdf

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: