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
chart.series.RemoveAll();
chart.ClearData();
}
void Init(string filePath)
{
ClearSerie();
dtRead = CSVtoTable(filePath);
List
List
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
countTime = xlist[xlist.Count - 1];
float yCount = 0f;
List
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
avtThrushObj.GetComponent
countTimeObj.GetComponent
}
void GetMaxThrush()
{
List
List
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
maxTimeObj.GetComponent
}
public void AddSerie(string Name, List
{
LineChart chart = linechart.GetComponent
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
{
LineChart chart = linechart.GetComponent
chart.xAxis0.data = xlabelList;
}
List
{
List
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
{
List
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 () {
}
}