<?php
session_start();
include("../functions/conexion.php");
include ("src/jpgraph.php");
include ("src/jpgraph_line.php");

$idticker = $_GET["idticker"];
$graph = $_GET["graph"];




$ydata = array();
$datax = array();

//if the user selects per days
if($graph == 0){
	$query = "SELECT DISTINCT year(time) AS year, month(time) AS month, day(time) AS day, hour(time) AS hour, minute(time) AS minute, year(now()) AS yearnow, month(now()) AS monthnow, day(now()) AS daynow, time, price FROM History WHERE stock=".$idticker;
	$answer = mysql_query($query);
	while($row = mysql_fetch_array($answer)){		
		if ($row["day"] == $row["daynow"]){			
			array_push($ydata, floatval($row["price"]));
			$st = $row["hour"].":".$row["minute"];
			array_push($datax, $st);
		}	
	}
}
//if the user selects per months
if($graph == 1){		
	$query= "SELECT AVG(price), month(time) AS month, day(time) AS d, month(now()) AS monthnow, time FROM History WHERE stock=".$idticker." GROUP BY d";
	$answer = mysql_query($query);
	while($row = mysql_fetch_array($answer)){	
		if ($row["month"] == $row["monthnow"]){			
			array_push($ydata, floatval($row["AVG(price)"]));
			array_push($datax, $row["d"]);
		}	
	}
}
//if the user selects per years
if($graph == 2){		
	$query= "SELECT AVG(price), month(time) AS m, year(time) AS year, month(now()) AS monthnow, year(now()) AS yearnow, time FROM History WHERE stock=".$idticker." GROUP BY m";
	$answer = mysql_query($query);
	while($row = mysql_fetch_array($answer)){	
		if ($row["year"] == $row["yearnow"]){
			array_push($ydata, floatval($row["AVG(price)"]));
			if($row["m"] == 1){ 
				array_push($datax, "January");
			}
			if($row["m"] == 2){ 
				array_push($datax, "February");
			}
			if($row["m"] == 3){ 
				array_push($datax, "March");
			}
			if($row["m"] == 4){ 
				array_push($datax, "April");
			}
			if($row["m"] == 5){ 
				array_push($datax, "May");
			}
			if($row["m"] == 6){ 
				array_push($datax, "Juny");
			}
			if($row["m"] == 7){ 
				array_push($datax, "July");
			}
			if($row["m"] == 8){ 
				array_push($datax, "August");
			}
			if($row["m"] == 9){ 
				array_push($datax, "September");
			}
			if($row["m"] == 10){ 
				array_push($datax, "October");
			}
			if($row["m"] == 11){ 
				array_push($datax, "November");
			}
			if($row["m"] == 12){ 
				array_push($datax, "December");
			}
		}	
	}
}




// Create the graph. These two calls are always required
$graph = new Graph(300,200,"auto");	
$graph->SetScale("textlin");

// Specify the tick labels
$graph->xaxis->SetTickLabels($datax);

// Create the linear plot
$lineplot=new LinePlot($ydata);
$lineplot->mark->SetType(MARK_UTRIANGLE);

// Add the plot to the graph
$graph->Add($lineplot);

$graph->img->SetMargin(40,20,20,40);
$graph->title->Set("Graphic");
$graph->xaxis->title->Set("Date");
$graph->yaxis->title->Set("Price");

$graph->title->SetFont(FF_FONT1,FS_BOLD);
$graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD);
$graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);

$lineplot->SetColor("blue");
$lineplot->SetWeight(2);
$graph->yaxis->SetColor("red");
$graph->yaxis->SetWeight(2);
$graph->SetShadow();

// Display the graph
$graph->Stroke();
?>