TCPDF Pie Chart : Missing Slice

Hi Experts,

I have the following code that is to produce a PDF with a Pie Chart.

<?php
ob_start();
$PAGE_SECURE = FALSE;
$PATH_TO_ROOT = '../';
session_start();

$report_name = 'STATISTICS FOR ';

$os_id = $_SESSION['os_id'];
if ($os_id != 0){
    $spec = $_SESSION['specialist'];
    $report_name .= $spec[0]['first_name'] . ' ' . $spec[0]['last_name'];
} else {
    $report_name .= 'Agency-Wide';
}

$user = $_SESSION['user']['first_name'] . ' ' . $_SESSION['user']['last_name'];
$report_footer = 'printed by: ' . $user;

require_once $PATH_TO_ROOT . 'utilities/general_includes.php'; //<-- General
require_once $PATH_TO_ROOT . 'reporting/configure_pdf.php';

$pdf = new ReportPDF('P', 'mm', array(215.9, 279.4));
$pdf->SetupPDF();

$pdf->AddPage();             


//Client Stats
    
    $client_stats = $_SESSION['client_stats'];
    
    $stats_x = 150;
    $stats_lable = 30;
    $stats_value = 20;
    
    $pdf->SetY(80);
    $pdf->SetX($stats_x);    
    $pdf->draw_pdf_cell($stats_lable, 'Active Clients');
    
    $pdf->SetX($stats_x + $stats_lable);
    $pdf->draw_pdf_cell($stats_value, $client_stats['active'], 'R');
    
    $pdf->Ln();
    
    $pdf->SetX($stats_x);    
    $pdf->draw_pdf_cell($stats_lable, 'Closed Clients');
    
    $pdf->SetX($stats_x + $stats_lable);
    $pdf->draw_pdf_cell($stats_value, $client_stats['closed'], 'R', FALSE, 'B');
    
    $pdf->Ln();
    $pdf->SetFont($default_font_family, 'B');
    
    
    $pdf->SetX($stats_x);    
    $pdf->draw_pdf_cell($stats_lable, 'Total Clients');
    
    $pdf->SetX($stats_x + $stats_lable);
    $pdf->draw_pdf_cell($stats_value, $client_stats['total'], 'R');

//Draw Pie START

    $services = $_SESSION['service_stats'];
    
    //Get Total Service Count for 100%
    $service_count = 0;
    foreach ($services as $srv){
        $service_count += $srv['client_count'];
    }

    //print_variable($service_count, 'count');
    //exit();
    
    $pie_x = 70;
    $pie_y = 100;
    $pie_radius = 60;

    //Draw pie slice for each service
    $deg_start = 0;
    $deg_slice = 0;
    $red = 0;
    $green = 0;
    $blue = 0;
    
    $breakdown_y = 180;
    $breakdown_x  = 65;
    $breakdown_line = 7;
    
    $width_label = 70;
    $width_value = 50;    
    
    $count = 0;
    $stop_after = 300;
    
    foreach ($services as $srv):

        $deg_slice = ($srv['client_count'] / $service_count) * 360;
        $red = $srv['pie_red'];
        $green = $srv['pie_green'];
        $blue = $srv['pie_blue'];

        $pdf->setFillColor($red, $green, $blue);
        $pdf->PieSector($pie_x, $pie_y, $pie_radius, $deg_start, $deg_slice); 

        echo $srv['name'] . "   |   " . $srv['client_count'] . " |  /30   |   " . $srv['client_count'] / 30 *100 . "%  |   " ;
        echo "$deg_start   |   $deg_slice   |   " . ($deg_start + $deg_slice) . "   |   $red   |   $green   |   $blue<br>";
        
        $deg_start += $deg_slice;
        
        $count++;
        if ($count == $stop_after) break;
  
    endforeach;
    
    //exit();
    //Display Breakdown
    $pdf->SetY($breakdown_y);

    $count = 0;
    
    foreach ($services as $srv):
        
        $red = $srv['pie_red'];
        $green = $srv['pie_green'];
        $blue = $srv['pie_blue'];

        $pdf->setFillColor($red, $green, $blue);
        $pdf->SetX($breakdown_x);        
        $pdf->draw_checkbox(FALSE, 4, TRUE);
        
        $pdf->SetX($breakdown_x + 4);
        $pdf->draw_pdf_cell($width_label, $srv['name']);
        
        $pdf->SetX($breakdown_x + 4 + $width_label);
        $pdf->draw_pdf_cell($width_value, $srv['client_count']);
        
        $pdf->Ln(7);
                
        $count++;
        if ($count == $stop_after) break;
        
    endforeach;        
    
//Draw Pie END
    
//Final Output
$file = ucwords(strtolower($report_name));
$file = str_replace(' ', '_', $file);
//ob_end_clean();
$pdf->Output($file);

?>

Open in new window


The code produces the attached PDF File, with no errors

If I release Line 116, I get
New Immigrant Service | 3 | /30 | 10% | 0 | 36 | 36 | 213 | 124 | 250
Translation | 3 | /30 | 10% | 36 | 36 | 72 | 0 | 0 | 0
Family Matters | 2 | /30 | 6.6666666666667% | 72 | 24 | 96 | 209 | 120 | 10
Housing / Settlement | 2 | /30 | 6.6666666666667% | 96 | 24 | 120 | 117 | 225 | 34
Debt Management | 2 | /30 | 6.6666666666667% | 120 | 24 | 144 | 255 | 255 | 0
Senior Support | 2 | /30 | 6.6666666666667% | 144 | 24 | 168 | 18 | 164 | 155
Employment / Self Employment | 2 | /30 | 6.6666666666667% | 168 | 24 | 192 | 5 | 111 | 117
Transcription | 1 | /30 | 3.3333333333333% | 192 | 12 | 204 | 251 | 234 | 14
Training | 1 | /30 | 3.3333333333333% | 204 | 12 | 216 | 201 | 107 | 159
Teen Pregnancy & Pre Natal | 1 | /30 | 3.3333333333333% | 216 | 12 | 228 | 147 | 135 | 28
Social Assistance | 1 | /30 | 3.3333333333333% | 228 | 12 | 240 | 132 | 7 | 64
Anger Management | 1 | /30 | 3.3333333333333% | 240 | 12 | 252 | 208 | 153 | 77
Legal Aid or Bail Program | 1 | /30 | 3.3333333333333% | 252 | 12 | 264 | 24 | 37 | 203
Child Care | 1 | /30 | 3.3333333333333% | 264 | 12 | 276 | 10 | 92 | 23
Drug and Alcohol Rehab | 1 | /30 | 3.3333333333333% | 276 | 12 | 288 | 250 | 88 | 140
Disability ODSP | 1 | /30 | 3.3333333333333% | 288 | 12 | 300 | 71 | 212 | 132
Counselling Services | 1 | /30 | 3.3333333333333% | 300 | 12 | 312 | 230 | 28 | 175
Continued Education | 1 | /30 | 3.3333333333333% | 312 | 12 | 324 | 255 | 0 | 0
Computers (Refurbished) | 1 | /30 | 3.3333333333333% | 324 | 12 | 336 | 159 | 107 | 22
Children Aid / Custody | 1 | /30 | 3.3333333333333% | 336 | 12 | 348 | 186 | 193 | 93
Victim Abuse / Sexual | 1 | /30 | 3.3333333333333% | 348 | 12 | 360 | 99 | 67 | 136

Open in new window


If I clean up the above output, by replacing the | with actual tabs, I end up with the attached Excel sheet.

I believe that the missing slice is Translation, but I cannot figure out why? I changed its color to 0,0,0 (black), but it does not help.

Any help will be appreciated.
Statistics_For_Agency-wide.pdf
STATS.xlsx
APD TorontoSoftware DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NerdsOfTechTechnology ScientistCommented:
change this line:
$stop_after = 300;

Open in new window

to:
$stop_after = 360;

Open in new window


It looks like the pie is being cut off at 300 degrees due to this 'flag';

With only 300 degrees showing, New Immigrant Service shows partially (12 degrees showing, emulating a 3.33% slice) and eliminating Translation categories altogether (0 degrees showing).
APD TorontoSoftware DeveloperAuthor Commented:
I added the $stop_after flag just to try and determine, which slice is the missing one. Also, this does not represent degrees, but rather count as you can see on lines 111-112.  There are 21 services, but there can be more, so I just randomly chose 300 to exceed any possibilities, but it is not related to degrees.

I now see that New Immigrant is showing partially, but I am not sure why because the same results will occur if I comment line 112. As well, on line 98 you will see that I am using the constant 360, then adding that to $deg_start on line 109. I am declaring $deg_start on line 80. So all this should go from 0 to 360, as it does in my output in Excel.

I don't understand what I am doing wrong.
APD TorontoSoftware DeveloperAuthor Commented:
If you look at my Excel sheet, sheet 2, E3 you will see that it starts at 0 and G23 ends at 360.

However, since you pointed out that New Immigrant is the issue which I agree, shouldn't 0 degree start more to the left, like 12 o'clock, but it start like 1:15?
NerdsOfTechTechnology ScientistCommented:
I just noticed your slices are going from deg_start to deg_slice instead of deg_start to (deg_start + deg_slice);

The b parameter in PieSector is the ending angle so this should be: ($deg_start + $deg_slice), thus:

$pdf->PieSector($pie_x, $pie_y, $pie_radius, $deg_start, ($deg_start + $deg_slice));

Open in new window


Also, consider rounding:

$deg_slice = intval(($srv['client_count'] / $service_count) * 360));

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
APD TorontoSoftware DeveloperAuthor Commented:
Thank You!!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.