<?php

        
/*
        ** SVG Pie Chart
        */

        /*
        ** get x,y pair on cirle, 
        ** assuming center is 0,0
        */

        
function circle_point($degrees$diameter){
                
//avoid problems with doubles

                
$degrees += 0.0001;
                
$x cos(deg2rad($degrees)) * ($diameter/2);
                
$y sin(deg2rad($degrees)) * ($diameter/2);

            return (array(
$x$y));
        }

        
//fill in chart parameters

        
$ChartRadius 150;
        
$ChartDiameter $ChartRadius*2;
        
$ChartFontHeight 12;
        
$ChartData = array('Beef'=>99'Chicken'=>75'Lamb'=>66'Fish'=>22'Pork'=>15);

        
//determine graphic size

        
$ChartWidth $ChartDiameter 20;
        
$ChartHeight $ChartDiameter 20 + (($ChartFontHeight 2) * count($ChartData));

        
//determine total of all values
        
$ChartTotal=0;

        foreach(
$ChartData as $Value){
                
$ChartTotal += $Value;
        }

        
$ChartCenterX $ChartDiameter/10;
        
$ChartCenterY $ChartDiameter/10;

        
//allocate colors
        
$colorBody 'white';
        
$colorBorder 'black';
        
$colorText 'black';
        
$colorSlice = array('#FF0000','#00FF00','#0000FF','#FFFF00','#FF00FF',
                
'#00FFFF','#990000','#009900','#000099','#999900','#990099','#009999');
        
//set the content type
        
header("Content-type: image/svg-xml");
        
//mark this as XML
        
print('<?xml version="1.0" encoding="iso-8859-1"?>' "\n");
        
//Point to SVG DTD
        
print('<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20000303 Stylable//EN" ');
        print(
'"http://www.w3.org/TR/2000/03/WD-SVG-20000303/DTD/ svg-20000303-stylable.dtd">' "\n");
        
//start SVG document, set size in "user units"
        
print("<svg xml:space=\"preserve\"  width=\"5in\" height=\"5in\"  viewBox=\"0 0 $ChartWidth $ChartHeight\">\n");
        
/*
        ** make background rectangle
        */
        
print("<rect x=\"0\" y=\"0\" width=\"$ChartWidth\" height=\"$ChartHeight\" style=\"fill:$colorBody\" />\n");
        
/*
        ** draw each slice
        */
        
$Degrees 0;
        
$index 0;
        foreach(
$ChartData as $Label=>$Value)
        {
                
$StartDegrees round($Degrees);
                
$Degrees += ($Value/$ChartTotal)*360;
                
$EndDegrees round($Degrees);
                
$CurrentColor $colorSlice[($index++)%(count($colorSlice))];
                print(
"<!--$Label$Value (of $ChartTotal) " .
                        
"$StartDegrees to $EndDegrees degrees " .
                        
"in $CurrentColor-->\n");
                print(
"<path d=\"");
        
                
//start at center of circle
                
print("M $ChartCenterX,$ChartCenterY ");
                
                
//draw out to circle edge
                
list($ArcX$ArcY) = circle_point($StartDegrees$ChartDiameter);
                print(
"L " floor($ChartCenterX $ArcX) . "," .
                        
floor($ChartCenterY $ArcY) . " ");
                
//draw arc
                
list($ArcX$ArcY) = circle_point($EndDegrees$ChartDiameter);
                print(
"A $ChartRadius,$ChartRadius 0 0 1 " .
                        
floor($ChartCenterX $ArcX) . "," .
                        
floor($ChartCenterY $ArcY) ." ");

                
//close polygon
                
print("Z\" ");
                print(
"style=\"fill:$CurrentColor;\"/>\n");
        }
        
/*
        ** draw border
        */
        
print("<circle cx=\"$ChartCenterX\" cy=\"$ChartCenterY\" " .
                
"r=\"$ChartRadius\" " .
                
"style=\"fill:none; stroke:$colorBorder; stroke-width:5\" />\n");
        
/*
        ** draw legend
        */
        
$index=0;
        foreach(
$ChartData as $Label=>$Value)
        {
                
$CurrentColor $colorSlice[$index%(count($colorSlice))];
                
$BoxY $ChartDiameter 20 + (($index++)*($ChartFontHeight+2));
                
$LabelY $BoxY $ChartFontHeight;
                
                
//draw color box
                
print("<rect x=\"10\" y=\"$BoxY\" " 
                        
"width=\"20\" height=\"$ChartFontHeight\" " .
                        
"rx=\"5\" " .
                        
"style=\"fill:$CurrentColor; stroke:$colorBorder; " .
                        
"stroke-width:1\" />\n");
                
//draw label
                
print("<text x=\"40\" y=\"$LabelY\" " .
                        
"style=\"file:$colorText; font-size:$ChartFontHeight\">" .
                        
"$Label$Value.
                        
"</text>\n");
        }
        
//end SVG document
        
print('</svg>' "\n");

?>