File "graph2_2.php"

Full path: /www/ansys/ttest/accmes/graph2_2.php
File size: 14.8 KiB (15160 bytes)
MIME-type: text/x-php
Charset: utf-8

Download   Open   Back

<?php
$indexURL = "./index_.php";
$position = isset($_POST["position"]) ? htmlspecialchars($_POST["position"]) : "";

$sepPosition = split(",", $position);
$len = count($sepPosition);

$imgFname = "./img/acc_". md5(uniqid(rand(), true)). ".png";

// 読み込むファイル名、戻る場所を決める
$dataFname = "/";
$returnPosition = "";
for($iCount = 0; $iCount < $len; $iCount++)
{
	if ($iCount > 0)
	{
		$dataFname .= "/";
		if ($iCount < $len - 1)
			$returnPosition .= ",";
	}
	$dataFname .= $sepPosition[$iCount];
	if ($iCount < $len - 1)
		$returnPosition .= $sepPosition[$iCount];
}

try
{
	$img = imagecreate(1350, 610);
	$font = "/www/ansys/config/fonts/ipagp.ttf";
	
	// color一覧
	$black = imagecolorallocate($img, 0x00, 0x00, 0x00);
	$white = imagecolorallocate($img, 0xff, 0xff, 0xff);
	//$gmain = imagecolorallocate($img, 0xff, 0xff, 0xff);
	$gmain = imagecolorallocate($img, 0x00, 0x00, 0x00);
	$gsub = imagecolorallocate($img, 0x69, 0x69, 0x69);
	//$back = imagecolorallocate($img, 0x00, 0x00, 0x00);
	$back = imagecolorallocate($img, 0xff, 0xff, 0xff);
	
	$cpx = array(imagecolorallocate($img, 0xff, 0x00, 0x00), imagecolorallocate($img, 0xff, 0x69, 0xb4), imagecolorallocate($img, 0xff, 0xb6, 0xc1));
	//$clx = imagecolorallocate($img, 0xff, 0x63, 0x47);
	$cpy = array(imagecolorallocate($img, 0x00, 0xff, 0x00), imagecolorallocate($img, 0x22, 0x8b, 0x22), imagecolorallocate($img, 0x3c, 0xb3, 0x71));
	//$cly = imagecolorallocate($img, 0x22, 0x8b, 0x57);
	$cpz = array(imagecolorallocate($img, 0x00, 0x00, 0xff), imagecolorallocate($img, 0x00, 0xbf, 0xff), imagecolorallocate($img, 0x87, 0xce, 0xeb));
	//$clz = imagecolorallocate($img, 0x19, 0x19, 0x70);
	$cpv = array(imagecolorallocate($img, 0xff, 0xd7, 0x00), imagecolorallocate($img, 0xda, 0xa5, 0x20), imagecolorallocate($img, 0xa0, 0x52, 0x2d));
	//$clv = imagecolorallocate($img, 0xff, 0xa5, 0x00);
	
	$blx = imagecolorallocate($img, 0xff, 0xc0, 0xcb);
	$bly = imagecolorallocate($img, 0x98, 0xfb, 0x98);
	$blz = imagecolorallocate($img, 0xad, 0xd8, 0xe6);
	
	imagefilledrectangle($img, 0, 0, 1350, 610, $back);
	imagesetthickness($img, 1);
	
	// 基準軸
	imageline($img, 50, 50, 50, 550, $gmain);
	imageline($img, 50, 50, 45, 55, $gmain);
	imageline($img, 50, 50, 55, 55, $gmain);
	imageline($img, 50, 550, 1310, 550, $gmain);
	imageline($img, 1310, 550, 1305, 545, $gmain);
	imageline($img, 1310, 550, 1305, 555, $gmain);
	
	//$yparse = 42;
	// 補助軸
	//for($iCount = 1; $iCount < $yparse; $iCount++)
	//{
	//	//imagettftext($img, 7, 0, 50 + (60 * $iCount) - 3, 565, $gmain, $font, mb_convert_encoding($iCount, "UTF-8", "auto"));
	//	imageline($img, 50 + (60 * $iCount), 50, 50 + (60 * $iCount), 550, $gsub);
	//	imageline($img, 50 + (60 * $iCount), 545, 50 + (60 * $iCount), 555, $gmain);
	//}
	imagettftext($img, 8, 0, 650, 585, $gmain, $font, mb_convert_encoding("経過秒[sec]", "UTF-8", "auto"));
	
	imageline($img, 45, 300, 1310, 300, $gmain);
	imagettftext($img, 7, 0, 33, 300 + 4, $gmain, $font, mb_convert_encoding("0", "UTF-8", "auto"));
	for($iCount = 1; $iCount < 10; $iCount++)
	{
		imageline($img, 50, 300 + ($iCount * 25), 1310, 300 + ($iCount * 25), $gsub);
		imageline($img, 45, 300 + ($iCount * 25), 55, 300 + ($iCount * 25), $gmain);
		imagettftext($img, 7, 0, 30, 300 + ($iCount * 25) + 4, $gmain, $font, mb_convert_encoding("-". $iCount, "UTF-8", "auto"));
		
		imageline($img, 50, 300 - ($iCount * 25), 1310, 300 - ($iCount * 25), $gsub);
		imageline($img, 45, 300 - ($iCount * 25), 55, 300 - ($iCount * 25), $gmain);
		imagettftext($img, 7, 0, 28, 300 - ($iCount * 25) + 4, $gmain, $font, mb_convert_encoding("+". $iCount, "UTF-8", "auto"));
	}
	imagettftext($img, 8, 90, 15, 320, $gmain, $font, mb_convert_encoding("加速度値", "UTF-8", "auto"));
	
	// 凡例(長期)
	imagefilledellipse($img, 260, 30, 6, 6, $cpx[0]);
	imagettftext($img, 8, 0, 240, 20, $gmain, $font, mb_convert_encoding("X(長期)", "UTF-8", "auto"));
	imagefilledellipse($img, 330, 30, 6, 6, $cpy[0]);
	imagettftext($img, 8, 0, 310, 20, $gmain, $font, mb_convert_encoding("Y(長期)", "UTF-8", "auto"));
	imagefilledellipse($img, 400, 30, 6, 6, $cpz[0]);
	imagettftext($img, 8, 0, 380, 20, $gmain, $font, mb_convert_encoding("Z(長期)", "UTF-8", "auto"));
	imagefilledellipse($img, 470, 30, 6, 6, $cpv[0]);
	imagettftext($img, 8, 0, 435, 20, $gmain, $font, mb_convert_encoding("3軸合成(長期)", "UTF-8", "auto"));
	// 凡例(中期)
	imagefilledellipse($img, 260 + 300, 30, 6, 6, $cpx[1]);
	imagettftext($img, 8, 0, 240 + 300, 20, $gmain, $font, mb_convert_encoding("X(中期)", "UTF-8", "auto"));
	imagefilledellipse($img, 330 + 300, 30, 6, 6, $cpy[1]);
	imagettftext($img, 8, 0, 310 + 300, 20, $gmain, $font, mb_convert_encoding("Y(中期)", "UTF-8", "auto"));
	imagefilledellipse($img, 400 + 300, 30, 6, 6, $cpz[1]);
	imagettftext($img, 8, 0, 380 + 300, 20, $gmain, $font, mb_convert_encoding("Z(中期)", "UTF-8", "auto"));
	imagefilledellipse($img, 470 + 300, 30, 6, 6, $cpv[1]);
	imagettftext($img, 8, 0, 435 + 300, 20, $gmain, $font, mb_convert_encoding("3軸合成(中期)", "UTF-8", "auto"));
	// 凡例(短期)
	imagefilledellipse($img, 260 + 600, 30, 6, 6, $cpx[2]);
	imagettftext($img, 8, 0, 240 + 600, 20, $gmain, $font, mb_convert_encoding("X(短期)", "UTF-8", "auto"));
	imagefilledellipse($img, 330 + 600, 30, 6, 6, $cpy[2]);
	imagettftext($img, 8, 0, 310 + 600, 20, $gmain, $font, mb_convert_encoding("Y(短期)", "UTF-8", "auto"));
	imagefilledellipse($img, 400 + 600, 30, 6, 6, $cpz[2]);
	imagettftext($img, 8, 0, 380 + 600, 20, $gmain, $font, mb_convert_encoding("Z(短期)", "UTF-8", "auto"));
	imagefilledellipse($img, 470 + 600, 30, 6, 6, $cpv[2]);
	imagettftext($img, 8, 0, 435 + 600, 20, $gmain, $font, mb_convert_encoding("3軸合成(短期)", "UTF-8", "auto"));
	
	// 各軸の最大最小
	$xmin = array("pix" => 0, "val" => 0);
	$xmax = array("pix" => 1260, "val" => 0);
	$ymin = array("pix" => 500, "val" => -10.0);
	$ymax = array("pix" => 0, "val" => 10.0);
	
	// 軸の比率
	$dx = 0;
	$dy = number_format(20 / 500, 10);
	
	$ard = array();
	$index = 0;
	$fp = fopen($dataFname, "r");
	if ($fp != NULL)
	{
		while(!feof($fp))
		{
			$line = trim(fgets($fp));
			if (strcmp($line, "") != 0)
			{
				$stac = split(",", $line);
				
				// 重力加速度の描画
				if (strcmp(substr($stac[0], 0, 1), "#") == 0)
				{
					$gacc["x"] = $stac[1];
					$gacc["y"] = $stac[2];
					$gacc["z"] = $stac[3];
					
					imageline($img, 50, 500 - (($stac[1] - $ymin["val"]) / $dy) + 50, 1310, (500 - ($stac[1] - $ymin["val"]) / $dy) + 50, $blx);
					imageline($img, 50, 500 - (($stac[2] - $ymin["val"]) / $dy) + 50, 1310, (500 - ($stac[2] - $ymin["val"]) / $dy) + 50, $bly);
					imageline($img, 50, 500 - (($stac[3] - $ymin["val"]) / $dy) + 50, 1310, (500 - ($stac[3] - $ymin["val"]) / $dy) + 50, $blz);
					
					imagettftext($img, 8, 0, 20, 600, $gmain, $font, mb_convert_encoding("基準値(x):". $gacc["x"], "UTF-8", "auto"));
					imagettftext($img, 8, 0, 120, 600, $gmain, $font, mb_convert_encoding("基準値(y):". $gacc["y"], "UTF-8", "auto"));
					imagettftext($img, 8, 0, 220, 600, $gmain, $font, mb_convert_encoding("基準値(z):". $gacc["z"], "UTF-8", "auto"));
					continue;
				}
				// y軸の最小を決める
				if ($index == 0)
				{
					$xmin["val"] = floor(($stac[0] / 1000.0)) * 1000.0;
					$xmin["val"] = $xmin["val"] - ($xmin["val"] % 5000);
					//$xmax["val"] = $xmin["val"] + ($yparse * 1000);
					//$dx = number_format($yparse * 1000 / 1260, 10);
					//for($iCount = 0; $iCount < 22; $iCount++)
					//	imagettftext($img, 7, 0, 50 - $yparse + ($iCount * 60), 570, $gmain, $font, mb_convert_encoding(date("H:i:s", $xmin["val"] / 1000.0 + ($iCount)), "UTF-8", "auto"));
				}
				$xmax["val"] = (floor(($stac[0] / 1000.0)) * 1000.0);
				$ard[$index]["msec"] = $stac[0];
				$ard[$index]["x"] = $stac[1];
				$ard[$index]["y"] = $stac[2];
				$ard[$index]["z"] = $stac[3];
				$ard[$index]["v"] = pow(($ard[$index]["x"] * $ard[$index]["x"]) + ($ard[$index]["y"] * $ard[$index]["y"]) + ($ard[$index]["z"] * $ard[$index]["z"]), 0.5);
				$ard[$index]["long"] = array("index" => 0, "x" => 0, "y" => 0, "z" => 0, "v" => 0);
				$ard[$index]["middle"] = array("index" => 0, "x" => 0, "y" => 0, "z" => 0, "v" => 0);
				$ard[$index]["short"] = array("index" => 0, "x" => 0, "y" => 0, "z" => 0, "v" => 0);
				
				$iid = $index;
				while(true)
				{
					$diff = $ard[$index]["msec"] - $ard[$iid]["msec"];
					if ($diff <= 1000)
					{
						$ard[$index]["long"]["index"]++;
						$ard[$index]["long"]["x"] += $ard[$iid]["x"];
						$ard[$index]["long"]["y"] += $ard[$iid]["y"];
						$ard[$index]["long"]["z"] += $ard[$iid]["z"];
						$ard[$index]["long"]["v"] += $ard[$iid]["v"];
						if ($diff <= 200)
						{
							$ard[$index]["middle"]["index"]++;
							$ard[$index]["middle"]["x"] += $ard[$iid]["x"];
							$ard[$index]["middle"]["y"] += $ard[$iid]["y"];
							$ard[$index]["middle"]["z"] += $ard[$iid]["z"];
							$ard[$index]["middle"]["v"] += $ard[$iid]["v"];
							if ($diff <= 40)
							{
								$ard[$index]["short"]["index"]++;
								$ard[$index]["short"]["x"] += $ard[$iid]["x"];
								$ard[$index]["short"]["y"] += $ard[$iid]["y"];
								$ard[$index]["short"]["z"] += $ard[$iid]["z"];
								$ard[$index]["short"]["v"] += $ard[$iid]["v"];
							}
						}
					}
					else
					{
						break;
					}
					$iid--;
					if ($iid < 0)
						break;
				}
				$ard[$index]["long"]["x"] = $ard[$index]["long"]["x"] / $ard[$index]["long"]["index"];
				$ard[$index]["long"]["y"] = $ard[$index]["long"]["y"] / $ard[$index]["long"]["index"];
				$ard[$index]["long"]["z"] = $ard[$index]["long"]["z"] / $ard[$index]["long"]["index"];
				$ard[$index]["long"]["v"] = $ard[$index]["long"]["v"] / $ard[$index]["long"]["index"];
				$ard[$index]["middle"]["x"] = $ard[$index]["middle"]["x"] / $ard[$index]["middle"]["index"];
				$ard[$index]["middle"]["y"] = $ard[$index]["middle"]["y"] / $ard[$index]["middle"]["index"];
				$ard[$index]["middle"]["z"] = $ard[$index]["middle"]["z"] / $ard[$index]["middle"]["index"];
				$ard[$index]["middle"]["v"] = $ard[$index]["middle"]["v"] / $ard[$index]["middle"]["index"];
				$ard[$index]["short"]["x"] = $ard[$index]["short"]["x"] / $ard[$index]["short"]["index"];
				$ard[$index]["short"]["y"] = $ard[$index]["short"]["y"] / $ard[$index]["short"]["index"];
				$ard[$index]["short"]["z"] = $ard[$index]["short"]["z"] / $ard[$index]["short"]["index"];
				$ard[$index]["short"]["v"] = $ard[$index]["short"]["v"] / $ard[$index]["short"]["index"];
				
				$index++;
			}
		}
		fclose($fp);
	}
	
	// x軸の最大を決定
	$xdiff = $xmax["val"] - $xmin["val"];
	$xmax["val"] = $xmax["val"] + (5000 - ($xdiff % 5000));
	$dx = number_format(($xmax["val"] - $xmin["val"]) / 1260, 10);
	
	// 縦軸の補助メモリを描画
	$xpnum = ($xmax["val"] - $xmin["val"]) / 5000;
	for($iCount = 0; $iCount < $xpnum + 1; $iCount++)
	{
		if ($iCount > 0 && $iCount < $xpnum)
		{
			imageline($img, 50 + ((1260 / $xpnum) * $iCount), 50, 50 + ((1260 / $xpnum) * $iCount), 550, $gsub);
			imageline($img, 50 + ((1260 / $xpnum) * $iCount), 545, 50 + ((1260 / $xpnum) * $iCount), 555, $gmain);
		}
		//imagettftext($img, 7, 0, 50 - $xpnum + ($iCount * (1260 / $xpnum)), 570, $gmain, $font, mb_convert_encoding(date("H:i:s", $xmin["val"] / 1000.0 + ($iCount)), "UTF-8", "auto"));
		imagettftext($img, 7, 0, 50 + ((1260 / $xpnum) * $iCount) - 20, 570, $gmain, $font, mb_convert_encoding(date("H:i:s", ($xmin["val"] / 1000) + ($iCount * 5)), "UTF-8", "auto"));
	}
	
	// グラフにプロット
	for($iCount = 0; $iCount < $index - 1; $iCount++)
	{
		$p = array();
		$p["x"] = ($ard[$iCount]["msec"] - $xmin["val"]) / $dx;
		$p["y"]["long"]["x"] = 500 - ($ard[$iCount]["long"]["x"] - $ymin["val"]) / $dy;
		$p["y"]["long"]["y"] = 500 - ($ard[$iCount]["long"]["y"] - $ymin["val"]) / $dy;
		$p["y"]["long"]["z"] = 500 - ($ard[$iCount]["long"]["z"] - $ymin["val"]) / $dy;
		$p["y"]["long"]["v"] = 500 - ($ard[$iCount]["long"]["v"] - $ymin["val"]) / $dy;
		
		$p["y"]["middle"]["x"] = 500 - ($ard[$iCount]["middle"]["x"] - $ymin["val"]) / $dy;
		$p["y"]["middle"]["y"] = 500 - ($ard[$iCount]["middle"]["y"] - $ymin["val"]) / $dy;
		$p["y"]["middle"]["z"] = 500 - ($ard[$iCount]["middle"]["z"] - $ymin["val"]) / $dy;
		$p["y"]["middle"]["v"] = 500 - ($ard[$iCount]["middle"]["v"] - $ymin["val"]) / $dy;
		
		$p["y"]["short"]["x"] = 500 - ($ard[$iCount]["short"]["x"] - $ymin["val"]) / $dy;
		$p["y"]["short"]["y"] = 500 - ($ard[$iCount]["short"]["y"] - $ymin["val"]) / $dy;
		$p["y"]["short"]["z"] = 500 - ($ard[$iCount]["short"]["z"] - $ymin["val"]) / $dy;
		$p["y"]["short"]["v"] = 500 - ($ard[$iCount]["short"]["v"] - $ymin["val"]) / $dy;
		
		imagefilledellipse($img, $p["x"] + 50, $p["y"]["short"]["x"] + 50, 3, 3, $cpx[2]);
		imagefilledellipse($img, $p["x"] + 50, $p["y"]["short"]["y"] + 50, 3, 3, $cpy[2]);
		imagefilledellipse($img, $p["x"] + 50, $p["y"]["short"]["z"] + 50, 3, 3, $cpz[2]);
		imagefilledellipse($img, $p["x"] + 50, $p["y"]["short"]["v"] + 50, 3, 3, $cpv[2]);
		
		imagefilledellipse($img, $p["x"] + 50, $p["y"]["middle"]["x"] + 50, 3, 3, $cpx[1]);
		imagefilledellipse($img, $p["x"] + 50, $p["y"]["middle"]["y"] + 50, 3, 3, $cpy[1]);
		imagefilledellipse($img, $p["x"] + 50, $p["y"]["middle"]["z"] + 50, 3, 3, $cpz[1]);
		imagefilledellipse($img, $p["x"] + 50, $p["y"]["middle"]["v"] + 50, 3, 3, $cpv[1]);
		
		imagefilledellipse($img, $p["x"] + 50, $p["y"]["long"]["x"] + 50, 3, 3, $cpx[0]);
		imagefilledellipse($img, $p["x"] + 50, $p["y"]["long"]["y"] + 50, 3, 3, $cpy[0]);
		imagefilledellipse($img, $p["x"] + 50, $p["y"]["long"]["z"] + 50, 3, 3, $cpz[0]);
		imagefilledellipse($img, $p["x"] + 50, $p["y"]["long"]["v"] + 50, 3, 3, $cpv[0]);
	}
	imagepng($img, $imgFname);
	imagedestroy($img);
}
catch(Exception $exc)
{
	print $exc->getMessage(). "<br />";
}

print "<!DOCTYPE html>";
print "<html>";

print "<head>";
print "<meta chaset=\"utf-8\">";
print "<link rel=\"stylesheet\" type=\"text/css\" href=\"./css/style.css\">";
print "<title>";
print "加速度グラフ";
print "</title>";
print "</head>";

print "<body>";

print "<img src=\"". $imgFname. "?t=$now_t\">";
print "<br />";
print "<form name=\"form_return\" method=\"post\" action=\"". $indexURL. "\" style=\"display:inline\">";
print "<a href=\"javascript:form_return.submit()\">戻る</a>";
print "<input type=\"hidden\" name=\"position\" value=\"". $returnPosition. "\">";
print "</form>";
/*
print "&nbsp;&nbsp;&nbsp;";

print "<form name=\"move_graph\" method=\"post\" action=\"./graph.php\" style=\"display:inline\">";
print "<a href=\"javascript:move_graph.submit()\">瞬時値を見る</a>";
print "<input type=\"hidden\" name=\"position\" value=\"". $position. "\">";
print "</form>";

print "&nbsp;&nbsp;&nbsp;";

print "<form name=\"move_graph2\" method=\"post\" action=\"./graph3.php\" style=\"display:inline\">";
print "<a href=\"javascript:move_graph2.submit()\">初期値と短期移動平均の外積値を見る</a>";
print "<input type=\"hidden\" name=\"position\" value=\"". $position. "\">";
print "</form>";
*/
print "</body>";
print "</html>";

PHP File Manager