実装
Unity 5.1.2-f1 on MacOS X 10.8.5
横軸が時刻、縦軸が何かの値といった時系列グラフを描画したい。
以下のように実装した。
- timeGraphScript : 時系列グラフを描画する
- dataSetterScript : timeGraphScriptへデータをセットする
code
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;
}
}
実行例
徐々に値が減っていくグラフを描画している。
Y軸の範囲は[-1.0, 1.0]という制限が今の所はある。