<?

/*************************************
**	
**	vezérlés
**
**	méret: X-Y
**	grafikon típus: circle,vline,hline,bar
**	tengely felirat: X-Y
**	tengely érték: szám-dátum
**	
**	Meghívásai minta
**	
**	$g=new chart(500,300,$stat[type]);
**	$g->setData($stat,0,1);
**	$graph=$g->draw();
**	
**	
*************************************/


$colors = array(
	"black"		=> array(  0,   0,   0),
	"green"		=> array(  0, 128,   0),
	"lightgreen"    => array(148, 232, 148),
	"olive"		=> array(128, 128,   0),
	"navy"          => array(  0,   0, 128),
	"white"		=> array(255, 255, 255),
	"red"		=> array(245,  12,  12),
	"lightgrey"	=> array(200, 200, 200)
);



class chart{


function chart($w,$h,$type=''){

	global $colors;
	
	// kép létrehozása
	$this->image=ImageCreate($w,$h);

	// szinek inicializálása
	$this->SetAvailableColors();

	// fehér háttér kerettel
	Imagefilledrectangle($this->image,1,1,$w-2,$h-2,$this->colors['white']);

	// további használatra méretek és típus továbbítva
	$this->width=$w;
	$this->height=$h;
	$this->type=$type;

	if($w<150 || $h < 150){
		$this->ShowLabels=0;
	}else{
		$this->ShowLabels=1;
	}

	$this->LoadFont();

}

function LoadFont(){

	$this->gdfont=imageloadfont('include/fonts/andale12.gdf');
	$this->font='include/fonts/arialhunormal.ttf';
}

function SetAvailableColors(){

	global $colors;

	while(list($name,$c)=each($colors)){
		$this->colors[$name]=ImageColorAllocate($this->image,$c[0],$c[1],$c[2]);		
	}
}

function draw(){

	global $colors;

//	$this->SetAvailableColors();
	$image=$this->image;
	$this->TitleOfChart();
	$this->FooterOfChart();
	$this->TypeSelect();
   	Imagepng($image);
  	Imagedestroy($image);

}

function DrawXLine(){

	imageline($this->image,$this->XLineStart,$this->YLineEnd,$this->XLineEnd,$this->YLineEnd,$this->colors['black']);
	// tengely vég
	$x1=$this->XLineEnd-$this->arrowWidth;
	$y1=$this->YLineEnd-$this->arrowHeight;
	$x2=$x1;
	$y2=$this->YLineEnd+$this->arrowHeight;
	$x3=$this->XLineEnd;
	$y3=$this->YLineEnd;
	imagefilledpolygon($this->image,array($x1,$y1,$x2,$y2,$x3,$y3),3,$this->colors['black']);

}

function DrawYLine(){

	imageline($this->image,$this->XLineStart,$this->YLineStartDraw,$this->XLineStart,$this->YLineEnd,$this->colors['black']);
	// tengely vég
	$x1=$this->XLineStart-$this->arrowHeight;
	$y1=$this->YLineStartDraw;
	$x2=$this->XLineStart+$this->arrowHeight;
	$y2=$y1;
	$x3=$this->XLineStart;
	$y3=$this->YLineStartDraw-$this->arrowWidth;
	imagefilledpolygon($this->image,array($x1,$y1,$x2,$y2,$x3,$y3),3,$this->colors['black']);

}

function DrawXsubLine($y){

	imageline($this->image,$this->XLineStart,$y,$this->XLineEnd,$y,$this->colors['lightgrey']);

}

function DrawYsubLine($x){

	imageline($this->image,$x,$this->YLineStartDraw*1.15,$x,$this->YLineEnd,$this->colors['lightgrey']);

}

function DrawXmarks($text_pos=''){

	switch($text_pos){
		case 'left':
			$pos=-4;
		break;
		case 'center':
			$pos=$this->Xmarks/2-5;
		break;
		case 'right':
			$pos=$this->Xmarks;
		break;
	}
	
	$y1=$this->YLineEnd;
	for($i=0;$i<sizeof($this->Xdata);$i++){
		$x1=$this->XLineStart+$this->Xmarks*$i;
		imageline($this->image,$x1,$y1,$x1,$y1+4,$this->colors['black']);
		if($this->ShowLabels) imagettftext($this->image,$this->XdataSize,300,$x1+$pos,$y1+10,$this->colors['black'],$this->font,$this->Xdata[$i]);
	}

}

function DrawYmarks(){

	$x1=$this->XLineStart;
	$YstepValue=0;
	for($i=0;$i<=$this->Ystep;$i++){
		$y1=$this->YLineEnd-$this->Ymarks*$i;
		if($i>0) {
			$YstepValue+=$this->YstepValue;
			$this->DrawXsubLine($y1);
		}
		imageline($this->image,$x1,$y1,$x1-4,$y1,$this->colors['black']);
		if($this->ShowLabels) imagettftext($this->image,$this->YdataSize,0,$x1-$this->width*0.08,$y1+3,$this->colors['black'],$this->font,$YstepValue);
	}

}


function TypeSelect(){

	switch($this->type){
		case 'vline':
		default:
			$this->LineVerticalChart();
			break;
		case 'hline':

			break;
		case 'circle':

			break;
		case 'bar':
			$this->barChart();
			break;
	}

}

function TitleOfChart(){

	if($this->ShowLabels) imagettftext($this->image,$this->titleSize,0,$this->width*0.25,22,$this->colors['black'],$this->font,$this->title);
}


function FooterOfChart(){

	imagettftext($this->image,6,90,$this->width-4,$this->height-4,$this->colors['red'],$this->font,'vam.ibusz.hu');

}


function LineVerticalChart(){

	$this->DrawXline();
	$this->DrawXmarks();
	$this->DrawYline();
	$this->DrawYmarks();

	// Y lépés értéke
	$rate=$this->Ymarks/$this->YstepValue;

	for($i=0;$i<sizeof($this->Xdata)-1;$i++){
		$x1=$this->XLineStart+$this->Xmarks*$i;
		$x2=$this->XLineStart+$this->Xmarks*($i+1);
		$y1=$this->YLineEnd-$this->Ydata[$i]*$rate;
		$y2=$this->YLineEnd-$this->Ydata[$i+1]*$rate;

		$this->DrawYsubLine($x2);
		imageline($this->image,$x1,$y1,$x2,$y2,$this->colors['red']);
	}
}


function LineHorizontalLineChart(){

}


function CircleChart(){


}

function barChart(){

	// X tengely hosszabbítása
	$this->XLineEnd+=$this->Xmarks;

	$this->DrawXline();
	$this->DrawXmarks('center');
	$this->DrawYline();
	$this->DrawYmarks();

	// Y lépés értéke
	$rate=$this->Ymarks/$this->YstepValue;

	for($i=0;$i<=sizeof($this->Xdata)-1;$i++){
		$x1=$this->XLineStart+$this->Xmarks*$i;
		$x2=$this->XLineStart+$this->Xmarks*($i+1);
		$y1=$this->YLineEnd-$this->Ydata[$i]*$rate;
		$y2=$this->YLineEnd;

		$this->DrawYsubLine($x2);
		$c=round($this->Xmarks*0.2);
		imagefilledrectangle($this->image,$x1+$c,$y1,$x2-$c,$y2,$this->colors['green']);

	}

}

function getPixelInfo($size){

	$pixel=2;

	if($size>4) $pixel++;
	if($size>7) $pixel++;
	if($size>9) $pixel++;
	if($size>11) $pixel++;

	$this->pixelRate=$pixel;

	return $this->pixelRate;
}

function getLongestData($data){

	if($this->ShowLabels){

		$longest=0;

		for($i=0;$i<sizeof($data);$i++){
			$l=strlen($data[$i]);
			if ($longest<$l) $longest=$l;
		}	
	
		$longestInPixel=$longest*$this->getPixelInfo($this->XdataSize);
	
		return $longestInPixel;
	}else{
		return 0;
	}
}

function setData($data,$x_data_pos,$y_data_pos){

	// beállítja a kapott adatsorból az X és Y tengely értékeit, léptéket,
	// a feliratokat, rajzolható területet

	// cím beállítása	
	$this->title=$data[title];

	// hol vannak a tengely adatok
	$this->Xlabel=$data[header][$x_data_pos];
	$this->Ylabel=$data[header][$y_data_pos];

	for($i=0;$i<sizeof($data[value]);$i++){

		$this->Xdata[]=$data[value][$i][$x_data_pos];
		$dataYnow=$data[value][$i][$y_data_pos];
		$this->Ydata[]=$dataYnow;

		if($i==0){
			$this->YDatamin=$dataYnow;
			$this->YDatamax=$dataYnow;
		}else{
			if($dataYnow<$this->YDatamin) $this->YDatamin=$dataYnow;
			if($dataYnow>$this->YDatamax) $this->YDatamax=$dataYnow;
		}

		// biztonsági korlát programozás idejére !!!!!
		//if($i==60) $i=sizeof($data[value])-2;
	}

	// a leghosszab adat karakterszáma
//	echo $this->getLongestData($this->Xdata);exit;


	// betü méretek

	$this->titleSize=round($this->width*0.6/strlen($this->title));

	$this->XlabelSize=$this->titleSize*0.9;
	$this->YlabelSize=$this->titleSize*0.9;

	$this->XdataSize=$this->titleSize*0.8;
	$this->YdataSize=$this->titleSize*0.8;

	// min max értékek, lépésköz

	if ($this->YDatamin>=0) $this->Ymin=0;
	$d=strlen($this->YDatamax);
	if($d>5 || $d>50 || $d>500) $d=-1*$d;
	$this->Ymax=round($this->YDatamax,$d);
	$this->Ystep=5;
	$this->YstepValue=round($this->Ymax/$this->Ystep);

	// tényleges rajzolási adatok

	$this->XLineStart=round($this->width*0.1+$this->getLongestData($this->Ydata));
	$this->XLineEnd=round($this->width-20);
	$this->Xmarks=round((round($this->XLineEnd-$this->XLineStart))/sizeof($data[value]));
	
	$this->YLineStartDraw=round($this->height*0.15);
	$this->YLineStart=round($this->height*0.2);
	$this->YLineEnd=round($this->height*0.95-$this->getLongestData($this->Xdata));
	$this->Ymarks=round((round($this->YLineEnd-$this->YLineStart))/$this->Ystep);
	
	// tengely vég nyilak méretei
	$this->arrowWidth=7;
	$this->arrowHeight=3;

//	echo "<pre>";print_r($this);echo "</pre>";

}



} //class

?>