<?php
ini_set("memory_limit", "64M");
class PieChart{
    public $VALUES;
    public $LEGEND;
    public $width;
    public $height;
    public $depth=35;
    
    private $COLORS;
    private $SHADOWS;
    private $white;
    private $black;
    private $font_file = '/home/camdpams_www/font/TimesNewRomanBold.ttf';
    private $coord_file;
    private $total;
    private $FONT=5;
    private $im;
    private $imd;
    private $legend_height;
    private $legend_width;
    private $l_x1 = NULL; //legend x1
    private $l_y1;//legend y1
    private $poly_coord = "";
    //FFFFFF,"CCCCCC","999999","666666","333333","000000"
    private $color_str = array("FFFFCC","FFFF99","FFFF66","FFFF33","FFFF00","CCCC00","FFCC66","FFCC00","FFCC33","CC9900","CC9933","996600",
			"FF9900","FF9933","CC9966","CC6600","996633","663300","FFCC99","FF9966","FF6600","CC6633","993300","660000",
			"FF6633","CC3300","FF3300","FF0000","CC0000","990000","FFCCCC","FF9999","FF6666","FF3333","FF0033","CC0033",
			"CC9999","CC6666","CC3333","993333","990033","330000","FF6699","FF3366","FF0066","CC3366","996666","663333",
			"FF99CC","FF3399","FF0099","CC0066","993366","660033","FF66CC","FF00CC","FF33CC","CC6699","CC0099","990066",
			"FFCCFF","FF99FF","FF66FF","FF33FF","FF00FF","CC3399","CC99CC","CC66CC","CC00CC","CC33CC","990099","993399",
			"CC66FF","CC33FF","CC00FF","9900CC","996699","660066","CC99FF","9933CC","9933FF","9900FF","660099","663366",
			"9966CC","9966FF","6600CC","6633CC","663399","330033","CCCCFF","9999FF","6633FF","6600FF","330099","330066",
			"9999CC","6666FF","6666CC","666699","333399","333366","3333FF","3300FF","3300CC","3333CC","000099","000066",
			"6699FF","3366FF","0000FF","0000CC","0033CC","000033","0066FF","0066CC","3366CC","0033FF","003399","003366",
			"99CCFF","3399FF","0099FF","6699CC","336699","006699","66CCFF","33CCFF","00CCFF","3399CC","0099CC","003333",
			"99CCCC","66CCCC","339999","669999","006666","336666","CCFFFF","99FFFF","66FFFF","33FFFF","00FFFF","00CCCC",
			"99FFCC","66FFCC","33FFCC","00FFCC","33CCCC","009999","66CC99","33CC99","00CC99","339966","009966","006633",
			"66FF99","33FF99","00FF99","33CC66","00CC66","009933","99FF99","66FF66","33FF66","00FF66","339933","006600",
			"CCFFCC","99CC99","66CC66","669966","336633","003300","33FF33","00FF33","00FF00","00CC00","33CC33","00CC33",
			"66FF00","66FF33","33FF00","33CC00","339900","009900","CCFF99","99FF66","66CC00","66CC33","669933","336600",
			"99FF00","99FF33","99CC66","99CC00","99CC33","669900","CCFF66","CCFF00","CCFF33","CCCC99","666633","333300",
			"CCCC66","CCCC33","999966","999933","999900","666600","CCCCCC","999999","666666","333333","000000");

    public function  __construct($w,$h,$val,$leg,$f_coord) { 
	$this->width=$w;
	$this->height=$h;
	$this->VALUES = $val;
	$this->LEGEND = $leg;
	$this->coord_file = $f_coord;
        $this->im=ImageCreate($this->width*2,$this->height*2);
        $this->imd=imagecreatetruecolor($this->width,$this->height);
        ImageColorAllocate($this->im,255,255,255);
        ImageColorAllocate($this->imd,255,255,255);
        $this->white=ImageColorAllocate($this->im,255,255,255);
	$this->black=ImageColorAllocate($this->im,0,0,0);
	$this->setColors();
    }
    
    public function setHeader($title){
	imagettftext($this->im,25,0,200,80,$this->black,$this->font_file,$title); 
    }
    
    private function hex2rgb( $colour ) {
	if ( $colour[0] == '#' ) {
	    $colour = substr( $colour, 1 );
	}
	if ( strlen( $colour ) == 6 ) {
	    list( $r, $g, $b ) = array( $colour[0] . $colour[1], $colour[2] . $colour[3], $colour[4] . $colour[5] );
	}
	elseif ( strlen( $colour ) == 3 ) {
	    list( $r, $g, $b ) = array( $colour[0] . $colour[0], $colour[1] . $colour[1], $colour[2] . $colour[2] );
	}
	else {return false;}
	$r = hexdec( $r );
	$g = hexdec( $g );
	$b = hexdec( $b );
	return array( 'red' => $r, 'green' => $g, 'blue' => $b );
    }

    public function plotLegend(){
        $legend_count=count($this->LEGEND);
        $max_length=0;
        foreach($this->LEGEND as $v) if ($max_length<strlen($v)) $max_length=strlen($v);
        // Fonts parametr
        $font_w=ImageFontWidth($this->FONT);
        $font_h=ImageFontHeight($this->FONT);
        // Coordinats legends
        $this->legend_width=($font_w*$max_length)+$font_h+25;
        $this->legend_height=$font_h*$legend_count+20;
        $this->l_x1=$this->width-$this->legend_width -$this->width/5.5;
//        $this->l_x1=$this->width-$this->legend_width -300;
        $this->l_y1=($this->height-$this->legend_height)/10;
        ImageRectangle($this->im, $this->l_x1, $this->l_y1, $this->l_x1+$this->legend_width, $this->l_y1+$this->legend_height, $this->white);
        $text_x=$this->l_x1+$this->width/3+55;
        $square_x=$this->l_x1+$this->width/3+20;
        $y=$this->l_y1+10;
        $i=0;

        foreach($this->LEGEND as $v) {
    	    $dy=$y+($i*45);
	    $percent =$v." - ". round(($this->VALUES[$i]/array_sum($this->VALUES))*100,2)." % "."- Infected Computers: ".$this->VALUES[$i];
//	    $percent = $v." - ". round(($this->VALUES[$i]/array_sum($this->VALUES))*100,2)." % ";
//    	    imagettftext($this->im,20,0,$text_x,$dy, $this->COLORS[$i],$this->font_file,$v);
    	    imagettftext($this->im,22,0,$text_x,$dy, $this->black,$this->font_file,$percent);
    	    ImageFilledRectangle($this->im,$square_x+1,$dy+1,$square_x+$font_h-1,$dy-15, $this->COLORS[$i]);
	    $i++; 
    	}
    }

    public function PIEplot(){
	$this->total = array_sum($this->VALUES);
	$anglesum=$angle=Array(0);
	$i=1;
// angles	
	while ($i<count($this->VALUES)) {
	    $part=($this->VALUES[$i-1]/$this->total)*1;
//	    $angle[$i]=floor($part*360);
	    $angle[$i]=$part*360;
//	    if($angle[$i]>360) $angle[$i]=$angle[$i]-360;
	    $anglesum[$i]=array_sum($angle);
	    $i++;
	}
	
	$anglesum[]=$anglesum[0];
// calc diametr
		$diametr=$this->height-600-$this->width/4;	
//		$diametr=$this->height-500;	
		
// X and Y 
	$circle_x=($diametr/2)+$this->width/6; 
	$circle_y=$this->height/3-10;
//Correct diametr
        if ($diametr>($this->height*2)-20) $diametr=($this->height*2)-80;

//Shadows
//$this->setHeader($anglesum[count($anglesum)-2]);
        for ($this->depth;$this->depth > 0;$this->depth--){
            for ($i=1;$i<count($anglesum);$i++){
                if ($anglesum[$i] == 0) $anglesum[$i] = 360;
                $arcs = (($anglesum[$i]-$anglesum[$i-1])/2) + $anglesum[$i-1];
                $xt = -round(cos($arcs*3.1418/180)*(($diametr)/150+25));
                $yt = -round(sin($arcs*3.1418/180)*($diametr/150));
		if($i==1) ImageFilledArc($this->im,$circle_x-$xt,$circle_y+$this->depth-$yt,$diametr,$diametr/2,$anglesum[count($anglesum)-2],$anglesum[$i],$this->SHADOWS[$i-1],IMG_ARC_EDGED);
                else ImageFilledArc($this->im,$circle_x-$xt,$circle_y+$this->depth-$yt,$diametr,$diametr/2,$anglesum[$i-1],$anglesum[$i],$this->SHADOWS[$i-1],IMG_ARC_EDGED);
            }
        }
//polygon's diagramm
       for ($i=1;$i<count($anglesum);$i++){
       //      ImageFilledArc($im,$circle_x,$circle_y,$diametr,$diametr/2,$anglesum[$i],$anglesum[$i+1],$COLORS[$i],IMG_ARC_PIE);                                                       
                if ($anglesum[$i] == 0) $anglesum[$i] = 360;
		$xpp = 70;
		$angle_pp = 2;
        	$arcs1 = (($anglesum[$i]-$anglesum[$i-1])/2) + $anglesum[$i-1];
                $xt1 = -round(cos($arcs1*3.1418/180)*(($diametr)/150+25));
                $yt1 = -round(sin($arcs1*3.1418/180)*($diametr/150));
		$arcs2 =(($anglesum[$i]-$anglesum[$i-1])/3) + $anglesum[$i-1];
		$xt2 = -round(cos($arcs2*3.1418/180)*(($diametr+$xpp)/2));
		$yt2 = -round(sin($arcs2*3.1418/180)*(($diametr+$xpp)/4));
		$arcs3 =(($anglesum[$i]-$anglesum[$i-1])/2) + $anglesum[$i-1] ;
		$xt3 = -round(cos($arcs3*3.1418/180)*(($diametr+$xpp)/2));
		$yt3 = -round(sin($arcs3*3.1418/180)*(($diametr+$xpp)/4));
		$arcs4 =$anglesum[$i] -(($anglesum[$i]-$anglesum[$i-1])/3) ;
		$xt4 = -round(cos($arcs4*3.1418/180)*(($diametr+$xpp)/2));
		$yt4 = -round(sin($arcs4*3.1418/180)*(($diametr+$xpp)/4));


		if($i==1) ImageFilledArc($this->im,$circle_x-$xt1,$circle_y-$yt1,$diametr,$diametr/2,$anglesum[count($anglesum)-1],$anglesum[$i],$this->COLORS[$i-1],IMG_ARC_EDGED);
		else ImageFilledArc($this->im,$circle_x-$xt1,$circle_y-$yt1,$diametr,$diametr/2,$anglesum[$i-1],$anglesum[$i],$this->COLORS[$i-1],IMG_ARC_EDGED);
		
		$this->poly_coord .= "\t<Node Type=\"Section Name\" Name=\"".$this->LEGEND[$i-1]."\">\n";
                $this->poly_coord .= "\t\t<Node Type= \"Coordinate\" Name = \"Poly\"  Val=\"".round(($circle_x-$xt1)/2)."\" />\n";
                $this->poly_coord .= "\t\t<Node Type= \"Coordinate\" Name = \"Poly\"  Val=\"".round(($circle_y-$yt1)/2)."\" />\n";
		$this->poly_coord .= "\t\t<Node Type= \"Coordinate\" Name = \"Poly\"  Val=\"".round(($circle_x-(-cos(($anglesum[$i-1]+$angle_pp)*3.1418/180)*(($diametr+$xpp)/2)))/2)."\" />\n";
		$this->poly_coord .= "\t\t<Node Type= \"Coordinate\" Name = \"Poly\"  Val=\"".round(($circle_y-(-sin(($anglesum[$i-1]+$angle_pp)*3.1418/180)*(($diametr+$xpp)/4)))/2)."\" />\n";
                $this->poly_coord .= "\t\t<Node Type= \"Coordinate\" Name = \"Poly\"  Val=\"".round(($circle_x-$xt2)/2)."\" />\n";
                $this->poly_coord .= "\t\t<Node Type= \"Coordinate\" Name = \"Poly\"  Val=\"".round(($circle_y-$yt2)/2)."\" />\n";
		$this->poly_coord .= "\t\t<Node Type= \"Coordinate\" Name = \"Poly\"  Val=\"".round(($circle_x-$xt3)/2)."\" />\n";
		$this->poly_coord .= "\t\t<Node Type= \"Coordinate\" Name = \"Poly\"  Val=\"".round(($circle_y-$yt3)/2)."\" />\n";
		$this->poly_coord .= "\t\t<Node Type= \"Coordinate\" Name = \"Poly\"  Val=\"".round(($circle_x-$xt4)/2)."\" />\n";
		$this->poly_coord .= "\t\t<Node Type= \"Coordinate\" Name = \"Poly\"  Val=\"".round(($circle_y-$yt4)/2)."\" />\n";
                $this->poly_coord .= "\t\t<Node Type= \"Coordinate\" Name = \"Poly\"  Val=\"".round(($circle_x-(-cos(($anglesum[$i]-$angle_pp)*3.1418/180)*(($diametr+$xpp)/2)))/2)."\" />\n";
		$this->poly_coord .= "\t\t<Node Type= \"Coordinate\" Name = \"Poly\"  Val=\"".round(($circle_y-(-sin(($anglesum[$i]-$angle_pp)*3.1418/180)*(($diametr+$xpp)/4)))/2)."\" />\n";
		$this->poly_coord .= "\t</Node>\n";

//		$percent = $this->LEGEND[$i-1]." - ".round(($this->VALUES[$i-1]/array_sum($this->VALUES))*100)." %";
		$percent = round(($this->VALUES[$i-1]/array_sum($this->VALUES))*100,2)." %";
		if ($anglesum[$i] == 0) $anglesum[$i] = 360;
		$arcs = (($anglesum[$i]-$anglesum[$i-1])/2) + $anglesum[$i-1];
		$xt = -round(cos($arcs*3.1418/180)*(($diametr+190)/2));
		$yt = -round(sin($arcs*3.1418/180)*(($diametr+390)/4));
		$xt1 = -round(cos($arcs*3.1418/180)*(($diametr)/2));
		$yt1 = -round(sin($arcs*3.1418/180)*(($diametr)/4));
//		ImageString($this->im, $this->FONT, 0,20*($i+1),"x = ".($xt)." y = ".($yt). " angle = ".$arcs . " anglesum =".$anglesum[$i+1], $this->black);
		if($xt>0){$xt += 100;$xtt=100;}
		else{$xt -= 100;$xtt=0;}
//		imagettftext($this->im,25,0,$circle_x-$xt-$xtt,$circle_y-$yt,$this->black,$this->font_file,$percent);
//		imageline ($this->im, $circle_x-$xt1,$circle_y-$yt1, $circle_x-$xt,$circle_y-$yt, $this->black);

	}         
      $handle= fopen($this->coord_file,'w');
	fwrite($handle,"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
	fwrite($handle,"<Node Name=\"Pie chart\">\n");
	fwrite($handle,$this->poly_coord);
	fwrite($handle,'</Node>');
      fclose($handle);
    }

    public function setColors(){

    $cnt =count($this->color_str)-1;
    for($i = 0 ; $i< count($this->VALUES); $i++){
    $t= mt_rand(0,$cnt-1);
	  $r = $this->hex2rgb($this->color_str[$t]);
	  $r1 = $this->hex2rgb($this->color_str[$t+1]);
	  $this->COLORS[$i] = imagecolorallocate($this->im,$r['red'],$r['green'],$r['blue']);
	  $this->SHADOWS[$i] = imagecolorallocate($this->im,$r1['red'],$r1['green'],$r1['blue']);
    }
    }
    public function get_PIE($fname){
        imagecopyresampled($this->imd,$this->im,0,0,0,0,$this->width,$this->height,$this->width*2,$this->height*2);
//	imagedestroy($this->im);
	imagepng($this->imd,$fname);
//	return 	imagepng($this->imd);
//	imagedestroy($this->imd);
    }
    
   function __destruct(){ 
	imagedestroy($this->im);
	imagedestroy($this->imd);
   }

}

?>