<?php
session_start();

//require_once('pChart.class');       commented by rakesh
//require_once('pData.class');         commented by rakesh

require_once('pChart.class.php');      // file added and customized by rakesh	  
require_once('pData.class.php');       // file added and customized by rakesh
//$date = array();
$date = $_SESSION['MDD'];
$width = 1000;
$height = 500;
$radius = 180;
$DataSet = new pData();
//die('hellooo');
function dump($log = 'NULL',$logf='dump'){
        $FP = fopen($logf.'.html','w');
        ob_start();
        //var_dump($_REQUEST);
       // echo "---------------------------------------------------------------\n";
        var_dump($log);
        $header = @ob_get_contents();
        ob_end_clean();
        fwrite($FP,$header);
        fclose($FP);
}

if($_GET['chart_type']=='column')
{	 
	
    if(count($date['MDD_DATA'])>2)
    {		
            foreach ($date['MDD_DATA'][0] as $key=>$value)
                      $series[0][$key] = $value;
            
            foreach ($date['MDD_DATA'] as $key=>$value)
                $value<>''?$series[$key] = $value:$series[$key] = 0;

            foreach ($series as $key=>$value)
                $second[$key] = $series[$key][0];
            
            unset($series[0][0]);
            foreach($series as $key => $value)
            {
                unset($value[0]);
                $DataSet->AddPoint($value, "Serie$key");
            
           
            }
            $DataSet->AddAllSeries();
            foreach($series as $key => $value)
            {
                if($series[$key][0] == '')
                {
                    $DataSet->removeSerieName("Serie$key");
                    $DataSet->RemoveSerie("Serie$key");
                    $DataSet->SetAbsciseLabelSerie("Serie$key");
                }
                else
                    $DataSet->SetSerieName($series[$key][0],"Serie$key");
            }
//            $DataSet->SetYAxisName($_SESSION['second_category_array'][1]['TITLE']);
            $DataSet->SetYAxisName('Колличество');
            $DataSet->SetXAxisName($_SESSION['category_array'][1]['TITLE']);
            $title2 = ' и '.$_SESSION['second_category_array'][1]['TITLE'];
    }
    else
    {
            
            foreach ($date['MDD_DATA'][0] as $key=>$value)
                $series[0][$key] = $value;
            
                        
            foreach ($date['MDD_DATA'][1] as $key=>$value)
                $value<>''?$series[1][$key] = $value:$series[1][$key] = 0;
            foreach ($series as $key=>$value)
                $second[$key] = $series[$key][0];
            
            
            $count = count($series[1]);
            for($i=2;$i<$count;$i++)
            {
                $series[$i] = $series[1];
                foreach ($series[$i] as $key=>$value)
                {
                    if($key<>$i)
                        $series[$i][$key] = NULL;
                }
            }
            foreach ($series[1] as $key=>$value)
                if($key>1)
                    $series[1][$key]=NULL;
            foreach($series as $key => $value)
            {
                unset($value[0]);
                $DataSet->AddPoint($value, "Serie$key");
                $DataSet->AddSerie("Serie$key");
            }
            $DataSet->AddPoint($second, "line");
            $DataSet->AddSerie("line");
            $DataSet->removeSerieName("line");
            $DataSet->RemoveSerie("line");
            $DataSet->SetAbsciseLabelSerie("line");
            foreach ($series[0] as $key=>$title)
            {
                $DataSet->SetSerieName($title.'['.$value[$key].']',"Serie$key");
            }
            foreach($series as $key => $value)
            {
                if($series[$key][0] == '')
                {
                    $DataSet->removeSerieName("Serie$key");
                    if(!$abciss){
                    $DataSet->RemoveSerie("Serie$key");
                    
                    $DataSet->SetAbsciseLabelSerie("Serie$key");
                    $abciss = true;
                    }
                }
                else
                    $DataSet->SetSerieName($series[$key][0],"Serie$key");
            }
//            $DataSet->SetYAxisName($_SESSION['second_category_array'][1]['TITLE']);
            $DataSet->SetYAxisName('Колличество');
            $DataSet->SetXAxisName($_SESSION['category_array'][1]['TITLE']);
        
    }
            
    //$DataSet->debug($series);
            /*Check data fo null values*/
            $exdata = $series;
            $ex_data = false;
            unset($exdata[0]);
//            $DataSet->debug($exdata);
            foreach($exdata as $key=>$value)
            {
                unset($value[0]);
                if(is_array($value))
                {
                    foreach ($value as $key2=>$value2)
                    {
                        if(intval($value2)!==0)
                            $ex_data = true;
                    }
                }
             }
//            $DataSet->debug($ex_data);
            /*End Check data fo null values*/
//        $ex_data = false;
        if($ex_data)
        {

            $chart = new pChart($width,$height);
            //$chart->debug($width);
        //die('vv');
            $chart->setFontProperties("arial.ttf", 8);
            
                $chart->setGraphArea(100, 100, $width-300, $height-150);
                $chart->drawFilledRoundedRectangle(10,10,$width-10,$height-10,10,180,180,180);
                $chart->drawScale($DataSet->GetData(),$DataSet->GetDataDescription(),SCALE_NORMAL |  SCALE_ADDALL ,0,0,0,TRUE,45,0,TRUE);
                $chart->drawBarGraph($DataSet->GetData(),$DataSet->GetDataDescription(),FALSE, 100 , TRUE /* black border */);
        
                $chart->setFontProperties("arial.ttf",8);
                $chart->AntialiasQuality = 0;
                if(count($date['MDD_DATA'])>2)
                $chart->drawLegend($width-250,40,$DataSet->GetDataDescription(),180,180,180,$Rs=-21,$Gs=-1,$Bs=-1,$Rt=255,$Gt=255,$Bt=255,$Border=NO);
                else
                $chart->drawPieLegend($width-250,40,$DataSet->GetData(),$DataSet->GetDataDescription(),180,180,180);
                $chart->setFontProperties("arial.ttf",10);
        		$title_ = '';   $title2 = '';     //   added by rakesh 
                foreach ($date['MDD_TITLE'] as $key=>$title)
                    $title_ .= ' '.$title;
                if($title2 <> '')
                    $title_ .= $title2;
                    $chart->drawTitle(30,30/*(+(12*intval($key)))*/, $title_,255,255,255);
                $chart->Stroke("diagramm.png");
       
        }
        else
        {
                $chart = new pChart($width,$height);
                $chart->setFontProperties("arial.ttf",25);
                $chart->drawTitle($width/2-$width/4,$height/2, 'Недостаточно данных',255,0,0);
                $chart->Stroke("diagramm.png");
        }
 }
 else
 {
    $DataSet = new pData;
    $lable = $val = array();
    foreach ($date['MDD_DATA'][0] as $key=>$value)
        $lable[$key] = $value;
    foreach ($date['MDD_DATA'][1] as $key=>$value)
        $value<>''?$val[$key] = $value:$val[$key] = 0;
    unset($lable[0]);
    unset($val[0]);
    $DataSet->AddPoint($lable,"Serie2");
    $DataSet->AddPoint($val,"Serie1");
    $DataSet->AddAllSeries();
    $DataSet->SetAbsciseLabelSerie("Serie2");

    /*Check data fo null values*/
    $exdata = $val;
    $ex_data = false;
//    unset($exdata[0]);
//    $DataSet->debug($exdata);
    foreach($exdata as $key=>$value)
    {
        if(intval($value)!==0)
            $ex_data = true;
    }
//    $DataSet->debug($ex_data);
            /*End Check data fo null values*/
             
if($ex_data)
{
    $Test = new pChart($width,$height);
    $Test->drawFilledRoundedRectangle(10,10,$width-10,$height-10,10,180,180,180);
    //$Test->drawFilledRoundedRectangle(7,7,730,400,5,240,240,240);
    $Test->createColorGradientPalette(195,250,80,223,110,41,1);
    $Test->setFontProperties("arial.ttf",8);
    $Test->AntialiasQuality = 0;
    $Test->drawPieGraph($DataSet->GetData(),$DataSet->GetDataDescription(),(($width/2)-($radius/2))+20,(($height/2)-($radius/3))+20,$radius,PIE_VALUES_LABEL,FALSE,45,20,5);
    //$Test->drawPieLegend(600,15,$DataSet->GetData(),$DataSet->GetDataDescription(),250,250,250);
//    $Test->drawLegend($width-150,40,$DataSet->GetDataDescription(),100,100,100,$Rs=-21,$Gs=-1,$Bs=-1,$Rt=255,$Gt=255,$Bt=255,$Border=YES);
    $Test->drawPieLegend($width-250,40,$DataSet->GetData(),$DataSet->GetDataDescription(),180,180,180);
    $Test->setFontProperties("arial.ttf",10);
	$title_ = '';
    foreach ($date['MDD_TITLE'] as $key=>$title)
        $title_ .= ' '.$title;
        $Test->drawTitle(30,30/*(+(12*intval($key)))*/, $title_,255,255,255);

        $Test->Stroke("diagramm.png");
    }
    else
    {
                $Test = new pChart($width,$height);
                $Test->setFontProperties("arial.ttf",25);
                $Test->drawTitle($width/2-$width/4,$height/2, 'Недостаточно данных',255,0,0);
                $Test->Stroke("diagramm.png");
    }
 }
unset($_SESSION['MDD']);


?>
