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 開源庫介紹
- xCharts:基於D3的JavaScript圖表庫JavaScript
- Unity——Js和Unity互相呼叫UnityJS
- environmentmap in unityUnity
- Unity元件Unity元件
- unity backbufferUnity
- cinemachine unityMacUnity
- mipmap of unityUnity
- [Unity3d]unity中http通訊Unity3DHTTP
- Mozilla和Unity合作將Unity引擎帶到WebUnityWeb
- Unity幀動畫Unity動畫
- unity 常用APIUnityAPI
- unity shadowUnity
- Unity 加密解密Unity加密解密
- unity學習Unity
- 【Unity】相關Unity
- Unity常用宏Unity
- Unity Shader 00 - 梳理 Unity Shader 的基本結構Unity
- Unity GC垃圾回收UnityGC
- [Unity]常用技巧收集Unity
- dynamic bone unity githubUnityGithub
- Unity-2DUnity
- Unity——物件池管理Unity物件
- Unity——觀察者模式Unity模式
- Unity XLua熱更新Unity
- Unity Dotween build errorUnityUIError
- Unity 拖動UIUnityUI
- 【Unity】4.6 燈光Unity
- 服務端 unity服務端Unity
- Unity 讀取xmlUnityXML
- unity+pico+logUnity
- Unity 體積光Unity
- [Unity]StringBuilderUnityUI
- Unity基礎——InputUnity
- unity destory的坑Unity
- [Unity3D] 2D畫素遊戲(一) Hello Unity!Unity3D遊戲
- Unity3d 入門(一) 瞭解Unity3dUnity3D
- [Unity-24] Unity的四種載入場景的方法Unity