<?php
EXTRACT($_GET);
$id = $_GET['id'];
$graph_id = $_GET['graph_id'];

//id毎に設定値
if($graph_id == 1)
{
	if($id == 4324)
	{
		$legend[0] = "最高波高";
		$legend[1] = "";
		$wave_data_no = 2;
		$period_data_no = 3;
	}
	else
	{
		$legend[0] = "最高波高";
		$legend[1] = "最高波周期";
		$wave_data_no = 2;
		$period_data_no = 3;
	}
	
}
else if($graph_id == 2)
{
	$legend[0] = "有義波高";
	$legend[1] = "平均周期";
	$legend[2] = "ピーク周期";
	$wave_data_no = 4;
	$period_data_no = 5;
	$peek_data_no = 3;
}


// ===============================================
// GD処理準備
// ===============================================
$x_size = 840;
$y_size = 520;

//パレットの作成
$img = imagecreatetruecolor($x_size,$y_size);

$src_im = imagecreatefrompng('arrow.png');

$font1 = "/www/ansys/config/fonts/ipam.ttf";

//使用色の準備
$Black =			imagecolorallocate($img,  0,  0,  0);
$White =			imagecolorallocate($img,255,255,255);
$Red =				imagecolorallocate($img,255,  0,  0);
$Green =			imagecolorallocate($img,  0,128,  0);
$Blue =				imagecolorallocate($img,  0,  0,255);
$LightBlue =		imagecolorallocate($img,  0,191,255);
$LightGray =		imagecolorallocate($img,211,211,211);
$midnightblue =		imagecolorallocate($img, 25, 25,112);	//グラフ下側背景(紺色)
$cornflowerblue =	imagecolorallocate($img,100,149,237);	//棒グラフ(少し薄い青)
$deepskyblue =		imagecolorallocate($img,  0,191,255);	//有意義グラフ(明るい水色)
$hotpink =			imagecolorallocate($img,255,105,180);	//折れ線グラフ(ピンク)

//破線のスタイル作成
$style = array($midnightblue,$midnightblue, IMG_COLOR_TRANSPARENT,IMG_COLOR_TRANSPARENT);
imagesetstyle($img, $style);

imagefilledrectangle($img,0,0,$x_size,$y_size,$LightGray);
imagefilledrectangle($img,60,0,$x_size-60,465,$White);
imagefilledrectangle($img,60,421,$x_size-60,460,$midnightblue);

imagefilledrectangle($img,60,480,$x_size-60,$y_size-5,$White);
imagerectangle($img,60,480,$x_size-60,$y_size-5,$Black);
	
//凡例
//	imagefilledrectangle($img,180,$y_size-30,210,$y_size-15,$deepskyblue);
//	ImageTTFText ($img, 9, 0, 220, $y_size-17, $Black, $font1, "有義波高(水圧変動推定値)");

//	imageline($img,490,$y_size-23,520,$y_size-23,$deepskyblue);
//	imagefilledellipse($img, 505, $y_size-23, 4, 4, $Blue);
//	ImageTTFText ($img, 9, 0, 530, $y_size-17, $Black, $font1, "有義周期(水圧変動推定値)");
	


imagefilledrectangle($img,70,$y_size-30,100,$y_size-15,$cornflowerblue);
ImageTTFText ($img, 9, 0, 110, $y_size-17, $Black, $font1, $legend[0]);

if($graph_id == 1)
{
	ImageTTFText ($img, 11, 0, 15, 65, $cornflowerblue, $font1, "最");
	ImageTTFText ($img, 11, 0, 15, 80, $cornflowerblue, $font1, "高");
	ImageTTFText ($img, 11, 0, 15, 95, $cornflowerblue, $font1, "波");
	ImageTTFText ($img, 11, 0, 15, 110, $cornflowerblue, $font1, "高");
	ImageTTFText ($img, 11, 0, 13, 130, $cornflowerblue, $font1, "(m)");
	
	if($id == 4325)
	{
		imageline($img,380,$y_size-23,410,$y_size-23,$hotpink);
		ImageTTFText ($img, 9, 0, 420, $y_size-17, $Black, $font1, $legend[1]);
		
		ImageTTFText ($img, 11, 0, $x_size-25, 65, $hotpink, $font1, "最");
		ImageTTFText ($img, 11, 0, $x_size-25, 80, $hotpink, $font1, "高");
		ImageTTFText ($img, 11, 0, $x_size-25, 95, $hotpink, $font1, "波");
		ImageTTFText ($img, 11, 0, $x_size-25, 110, $hotpink, $font1, "周");
		ImageTTFText ($img, 11, 0, $x_size-25, 125, $hotpink, $font1, "期");
		ImageTTFText ($img, 11, 0, $x_size-27, 145, $hotpink, $font1, "(s)");
	}
}
else if($graph_id == 2)
{
	ImageTTFText ($img, 11, 0, 15, 65, $cornflowerblue, $font1, "有");
	ImageTTFText ($img, 11, 0, 15, 80, $cornflowerblue, $font1, "義");
	ImageTTFText ($img, 11, 0, 15, 95, $cornflowerblue, $font1, "波");
	ImageTTFText ($img, 11, 0, 15, 110, $cornflowerblue, $font1, "高");
	ImageTTFText ($img, 11, 0, 13, 130, $cornflowerblue, $font1, "(m)");


	imageline($img,380,$y_size-23,410,$y_size-23,$hotpink);
	ImageTTFText ($img, 9, 0, 420, $y_size-17, $Black, $font1, $legend[1]);
	
	imageline($img,530,$y_size-23,560,$y_size-23,$LightBlue);
	ImageTTFText ($img, 9, 0, 570, $y_size-17, $Black, $font1, $legend[2]);

//	ImageTTFText ($img, 11, 0, $x_size-25, 65, $hotpink, $font1, "有");
//	ImageTTFText ($img, 11, 0, $x_size-25, 80, $hotpink, $font1, "義");
	ImageTTFText ($img, 11, 0, $x_size-25, 95, $hotpink, $font1, "周");
	ImageTTFText ($img, 11, 0, $x_size-25, 110, $hotpink, $font1, "期");
	ImageTTFText ($img, 11, 0, $x_size-27, 130, $hotpink, $font1, "(s)");
}


imageline($img,690,$y_size-23,695,$y_size-28,$Blue);
imageline($img,690,$y_size-23,695,$y_size-18,$Blue);
imageline($img,695,$y_size-28,695,$y_size-26,$Blue);
imageline($img,695,$y_size-18,695,$y_size-20,$Blue);
imageline($img,695,$y_size-26,705,$y_size-26,$Blue);
imageline($img,695,$y_size-20,705,$y_size-20,$Blue);
imageline($img,705,$y_size-26,705,$y_size-20,$Blue);
ImageTTFText ($img, 9, 0, 715, $y_size-17, $Black, $font1, "波向");



$img2 = ImageCreateFromPNG('arrow.png');

$transColor = imagecolorallocatealpha($img2, 255, 255, 255, 127);

//グラフの軸の作成

//表示データの読み込み
$DirPath = "/www/waveheight/data/".$id ."/data_.csv";
$data = array();

//昇順表示
exec('cat ' .$DirPath , $data, $retval);

//降順表示
//	exec('tac' .' ' .$DirPath , $data, $retval);

$data_ct = count($data);

//$data = array();
//$data = explode("\n",file_get_contents($DirPath));
//$data_ct = count($data);

//データを配列の代入
for($i=0;$i < $data_ct;$i++)
{
	$wave_data[$i] = explode(",",$data[$i]);
}


//●Y軸(横軸)
//軸の本数
$axis_x = 24;
$axis_y = 4;

//軸の設置位置
$x_st = 60;
$x_end = $x_size -60;
$y_st = 420;
$y_end = 0;

$ax_x_range = ($x_end - $x_st) / $axis_x;
$ax_y_range = (($y_st - $y_end) - 20) / $axis_y;

//(X軸)時間
$x_min = 0;
$x_max = 72;
$scale_x = ($x_max - $x_min) / $axis_x;


//(Y軸)有義波高の値の範囲
$y_min1 = 0;
$y_max1 = 4;
$scale_y1 = ($y_max1 - $y_min1) / $axis_y;

//(Y軸)有義周期の値の範囲
$y_min2 = 0;
$y_max2 = 12;
$scale_y2 = ($y_max2 - $y_min2) / $axis_y;


//水圧変動推定値
$judg_value = 1.5;


//データの描画
$st_x_g = $wave_data[0][1];
$end_x_g = $wave_data[$data_ct-1][1]; 
for($i = 0;$i < $data_ct;$i++)
{
	//
	$date1 = $wave_data[$i][1];
	$date2 = $wave_data[$i+1][1];

	$date_ = $date2 - $date1;
	
	//波高表示
	$x_data =(($date1 -$st_x_g) / 1800) * 5;
	$y_data1 = $wave_data[$i][$wave_data_no] * 100;
	
	//グラフのカラー設定
	$y1_color = $cornflowerblue;
	$y2_color = $hotpink;
	$y3_color = $LightBlue;
	
	
	//周期表示
	if($graph_id == 1)
	{}
	else
	{
		if($date_ == 1800)
		{
			$y_data2_1 = $wave_data[$i][$period_data_no] * 100 / 3;
			$y_data2_2 = $wave_data[$i+1][$period_data_no] * 100 / 3;
			$x_data2 = (($date2 -$st_x_g) / 1800) * 5;
			
			imageline($img,$x_st + $x_data ,$y_st - $y_data2_1,$x_st + $x_data2,$y_st - $y_data2_2,$y2_color);
			
			$y_data3_1 = $wave_data[$i][$peek_data_no] * 100 / 3;
			$y_data3_2 = $wave_data[$i+1][$peek_data_no] * 100 / 3;
				
			imageline($img,$x_st + $x_data ,$y_st - $y_data3_1,$x_st + $x_data2,$y_st - $y_data3_2,$y3_color);
			//ImageTTFText ($img, 9, 0, 50, 50 + $i*10, $Black, $font1, $wave_data[$i+1][5]);
		}
	}
	
	//判定用
	$aa = ($date1 -$st_x_g) / 1800;
	
	$y_set = ($aa%2) ?0:20;

	//波の向きの表示
//	$rotatedImage = imagerotate($img2, $wave_data[$i][6] + 180, $transColor);
	$rotatedImage = imagerotate($img2, $wave_data[$i][6] * (-1) + (-180), $transColor);
	imagesavealpha($rotatedImage, true);
	imagecopy($img, $rotatedImage, $x_st + $x_data-10, 0 + $y_set, 0, 0, 15, 15);
	imagefilledrectangle($img,$x_st + $x_data ,$y_st,$x_st + $x_data+5,$y_st -$y_data1 ,$y1_color);

}



//軸を配置
//X軸(縦軸)
//スタート位置のオフセット計算
if($wave_data[0][1])
{
	$date_y = date("Y" ,$wave_data[0][1]);
	$date_m = date("m" ,$wave_data[0][1]);
    $date_d = date("d" ,$wave_data[0][1]);
    $date_h = date("H" ,$wave_data[0][1]);
    $date_i = date("i" ,$wave_data[0][1]);
    
    $st_date_h_calc = ceil($date_h / 3) * 3;
    $st_date_h_calc_conv = mktime($st_date_h_calc,0,0,$date_m,$date_d,$date_y);
    $st_x_calc = ($st_date_h_calc_conv - $wave_data[0][1]) / 1800;
    
    $st_x_offset = $st_x_calc * 5;
}


for($ax = 0;$ax <= $axis_x;$ax++)
{
	$x_label = date("H" ,$st_date_h_calc_conv + $ax * 10800);
	$ax_x_set = $x_st + $ax * $ax_x_range + $st_x_offset;

	if(($x_st <= $ax_x_set) && ($ax_x_set < $x_end))
	{
		$txt_offset = ($ax_x_set - 4 > $x_st)? 4:0;
		imageline($img, $ax_x_set, $y_st, $ax_x_set, $y_st+4, $White);
		imageline($img, $ax_x_set, $y_end, $ax_x_set, $y_st, IMG_COLOR_STYLED);
		ImageTTFText ($img, 9, 0, $ax_x_set - $txt_offset, $y_st + 19, $White, $font1, $x_label);
	}
	if($ax == 0)
	{
		$st_date_ = date("m/d" ,$st_date_h_calc_conv + $ax * 10800);
		ImageTTFText ($img, 9, 0, $x_st, $y_st + 39, $White, $font1, $st_date_);
	}
	else if($ax == $axis_x)
	{
		$st_date_ = date("m/d" ,$st_date_h_calc_conv + $ax * 10800);
		ImageTTFText ($img, 9, 0, $x_end -30, $y_st + 39, $White, $font1, $st_date_);
	}
}

//Y軸(横軸)
for($ax = 0;$ax <= $axis_y;$ax++)
{
	$ax_y_set = $y_st - ($ax+1) * $ax_y_range;
	$ax_y_set2 = $y_st - ($ax) * $ax_y_range +5;
	
	$scale_y1_value = $y_min1 + $ax * $scale_y1;
	$scale_y2_value = $y_min2 + $ax * $scale_y2;
	
	imageline($img, $x_st, $ax_y_set, $x_end, $ax_y_set, $midnightblue);
	ImageTTFText ($img, 11, 0, $x_st-10, $ax_y_set2, $cornflowerblue, $font1, $scale_y1_value);

	if(($graph_id == 1) && ($id == 4324))
	{}
	else
	{
		ImageTTFText ($img, 11, 0, $x_end + 2, $ax_y_set2, $hotpink, $font1, $scale_y2_value);
	}
}


// ===============================================
// 描画した画像を保存 & パレットを削除
// ===============================================
header('Content-Type: image/png');

//imagepng($img, $imgFileName);
imagepng($img);
imagedestroy($img);
?>
