1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

unity > 時系列グラフの描画 > timeGraph

Last updated at Posted at 2015-09-08
実装
Unity 5.1.2-f1 on MacOS X 10.8.5

横軸が時刻、縦軸が何かの値といった時系列グラフを描画したい。

以下のように実装した。

  1. timeGraphScript : 時系列グラフを描画する
  2. dataSetterScript : timeGraphScriptへデータをセットする

code

v0.1 @ github

timeGraphScriptのsnippet.
SetXYVal()で受けた値をもとに描画をする。

timeGraphScript.cs
...
	static private float xval = -1.0f; // dummy value at first
	static private float yval =  0.5f; // dummy value at first
	static private bool isSet = false;
	static private float preX = -2.0f; // should have less than -1.0f at first 
	
	static public void SetXYVal(System.DateTime time, float yval_)
	{
		float xwork = getTimePosition_float (time);
		if (xwork < preX) {
			return; // revert time
		}
		xval = xwork;
		yval = yval_;
		if (xval >= -1.0f) { 
			preX = xval;
			isSet = true;
		}
	}
	
	void Update() {
		accTime += Time.deltaTime;
		if (accTime < 0.3f) { // for every 300 msec
			return;
		}
		accTime = 0.0f;
		
		clearGraph (timeGraphPanel);
		//		Test_drawBox (timeData, timeGraphPanel);
		
		if (isSet) {
			isSet = false;
			timeGraph_xy (timeData, timeGraphPanel, xval, yval);
		}
		
		drawGraph (timeData, timeGraphPanel);
	}

データをセットする側
2015年9月8日00時00分00秒を開始日時として、10分足していきながらyvalを変更している.

dataSetterScript.cs
using UnityEngine;
using System.Collections;

public class dataSetterScript : MonoBehaviour {

	private float accTime = 0.0f;
	private System.DateTime curDt = System.DateTime.Parse("2015/09/08 00:00:00");

	void Start () {
	
	}

	private float yval = 0.5f;
	
	void Update () {
		accTime += Time.deltaTime;
		if (accTime < 0.3f) { // every 300msec
			return;
		}
		accTime = 0.0f;

		curDt = curDt.AddMinutes (10f);

		timeGraphScript.SetXYVal (curDt, yval);
		yval -= 0.001f;
	}
}

実行例

徐々に値が減っていくグラフを描画している。

Main_unity_-150908-udpTimeGraph-PC__Mac___Linux_Standalone__Personal.jpg

Y軸の範囲は[-1.0, 1.0]という制限が今の所はある。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?