unity xcharts
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XCharts;
using System.Data;
using UnityEngine.UI;
using System.Windows.Forms;
public class Thrush : MonoBehaviour {
// Use this for initialization
DataTable dtRead;
public GameObject linechart;
float maxThrush = 0.0f;
string maxTime = "";
string countTime = "";
float avtThrush = 0f;
float jiFenThrush = 0f;
public GameObject maxThrushObj;
public GameObject maxTimeObj;
public GameObject countTimeObj;
public GameObject avtThrushObj;
public GameObject jiFenThrushObj;
string filePath = "";
void Start () {
}
public void ClearSerie()
{
LineChart chart = linechart.GetComponent<LineChart>();
chart.series.RemoveAll();
chart.ClearData();
}
void Init(string filePath)
{
ClearSerie();
dtRead = CSVtoTable(filePath);
List<string> xlist = GetColumnData(0);
List<float> ylist = GetColumnFloatData(1);
SetXAxis(xlist);
AddSerie("Thrush", ylist);
GetMaxThrush();
GetAvgThrush();
}
public void SelectFile()
{
OpenFileDialog openfile = new OpenFileDialog();
openfile.Title = "請選擇要傳送的檔案";
openfile.Filter = "CSV|*.csv|所有檔案型別|*.*";
if (DialogResult.OK == openfile.ShowDialog())
{
//將選擇的檔案的全路徑賦值給文字框
filePath = openfile.FileName;
}
Init(filePath);
//return filePath;
}
void GetAvgThrush()
{
List<string> xlist = GetColumnData(0);
countTime = xlist[xlist.Count - 1];
float yCount = 0f;
List<float> ylist = GetColumnFloatData(1);
int count = ylist.Count;
int index = 0;
for (int i = 0; i < count; i++)
{
float y = ylist[i];
yCount += y;
}
avtThrush = yCount / count;
jiFenThrush = float.Parse(countTime) * avtThrush;
jiFenThrushObj.GetComponent<Text>().text = jiFenThrush.ToString();
avtThrushObj.GetComponent<Text>().text = avtThrush.ToString();
countTimeObj.GetComponent<Text>().text = countTime.ToString();
}
void GetMaxThrush()
{
List<string> xlist = GetColumnData(0);
List<float> ylist = GetColumnFloatData(1);
int count = ylist.Count;
int index = 0;
for(int i = 0; i< count; i++)
{
float y = ylist[i];
if (y > maxThrush)
{
maxThrush = y;
index = i;
}
}
maxTime = xlist[index];
maxThrushObj.GetComponent<Text>().text = "最大推力:" + maxThrush.ToString();
maxTimeObj.GetComponent<Text>().text = "時間:" + maxTime.ToString();
}
public void AddSerie(string Name, List<float> dataList)
{
LineChart chart = linechart.GetComponent<LineChart>();
chart.AddSerie(SerieType.Line, Name);
int count = chart.series.Count;
//chart.AddData(count, dataList);
foreach (float y in dataList)
{
chart.AddData(count - 1, y);
}
}
public void SetXAxis(List<string> xlabelList)
{
LineChart chart = linechart.GetComponent<LineChart>();
chart.xAxis0.data = xlabelList;
}
List<string> GetColumnData(int index)
{
List<string> indexList = new List<string>();
int count = dtRead.Rows.Count;
for (int i = 0; i < count; i++)
{
DataRow datarow = dtRead.Rows[i];
indexList.Add(datarow[index].ToString());
}
return indexList;
}
List<float> GetColumnFloatData(int index)
{
List<float> indexList = new List<float>();
int count = dtRead.Rows.Count;
for (int i = 0; i < count; i++)
{
DataRow datarow = dtRead.Rows[i];
indexList.Add(float.Parse(datarow[index].ToString()));
}
return indexList;
}
DataTable CSVtoTable(string filePath)
{
//string filePath = "./Assets/CSV/thrush.csv";
//System.Text.Encoding encoding = GetType(filePath); //Encoding.ASCII;//
DataTable dt = new DataTable();
System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Open,
System.IO.FileAccess.Read);
System.IO.StreamReader sr = new System.IO.StreamReader(fs);
//記錄每次讀取的一行記錄
string strLine = "";
//記錄每行記錄中的各欄位內容
string[] aryLine = null;
string[] tableHead = null;
//標示列數
int columnCount = 0;
//標示是否是讀取的第一行
bool IsFirst = true;
//逐行讀取CSV中的資料
while ((strLine = sr.ReadLine()) != null)
{
if (IsFirst == true)
{
tableHead = strLine.Split(',');
IsFirst = false;
columnCount = tableHead.Length;
//建立列
for (int i = 0; i < columnCount; i++)
{
DataColumn dc = new DataColumn(tableHead[i]);
dt.Columns.Add(dc);
}
}
else
{
aryLine = strLine.Split(',');
DataRow dr = dt.NewRow();
for (int j = 0; j < columnCount; j++)
{
dr[j] = aryLine[j];
}
dt.Rows.Add(dr);
}
}
if (aryLine != null && aryLine.Length > 0)
{
dt.DefaultView.Sort = tableHead[0] + " " + "asc";
}
sr.Close();
fs.Close();
return dt;
//dtRead = dt;
}
// Update is called once per frame
void Update () {
}
}
相關文章
- XCharts 開源庫介紹
- Unity——Js和Unity互相呼叫UnityJS
- [Unity]StringBuilderUnityUI
- environmentmap in unityUnity
- Unity元件Unity元件
- unity backbufferUnity
- Unity Shader 00 - 梳理 Unity Shader 的基本結構Unity
- Unity常用宏Unity
- unity學習Unity
- 【Unity】相關Unity
- unity 常用APIUnityAPI
- unity render pipelineUnity
- unity destory的坑Unity
- Unity FPSSample Demo研究Unity
- Unity 體積光Unity
- unity+pico+logUnity
- Unity基礎——InputUnity
- Unity 讀取xmlUnityXML
- Unity——物件池管理Unity物件
- Unity的Deferred ShadingUnity
- Unity-2DUnity
- Unity幀動畫Unity動畫
- Unity XLua熱更新Unity
- Unity GC垃圾回收UnityGC
- Unity AssetBundle 載入Unity
- Unity 2018.2之粒子Unity
- [Unity]常用技巧收集Unity
- dynamic bone unity githubUnityGithub
- Unity3D學習筆記3——Unity Shader的初步使用Unity3D筆記
- [Unity3D] 2D畫素遊戲(一) Hello Unity!Unity3D遊戲
- 【Unity】DrawCall 研究隨記Unity
- Unity 6 今日釋出!Unity
- Unity匯入xLua流程Unity
- unity3d碰撞Unity3D
- unity3d sqliteUnity3DSQLite
- Unity——技能系統(三)Unity
- Unity——技能系統(二)Unity
- Unity——技能系統(一)Unity