262 lines
8.6 KiB
C#
262 lines
8.6 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Xml.Linq;
|
||
using TMPro;
|
||
using Unity.VisualScripting;
|
||
using UnityEngine;
|
||
using UnityEngine.EventSystems;
|
||
using UnityEngine.Networking;
|
||
using UnityEngine.UI;
|
||
using XCharts.Runtime;
|
||
|
||
public class ChartControl : MonoBehaviour
|
||
{
|
||
public BaseChart chart;
|
||
public Serie serie;
|
||
List<string> data = new List<string>();
|
||
|
||
bool isclick = true;//是否可以开始添加新数据
|
||
|
||
|
||
public TextMeshProUGUI x1;
|
||
public TextMeshProUGUI x2;
|
||
public TextMeshProUGUI y1;
|
||
public TextMeshProUGUI y2;
|
||
public TextMeshProUGUI Sx1;
|
||
public TextMeshProUGUI Sy1;
|
||
|
||
List<string> togglename = new List<string>();
|
||
|
||
private Coroutine coroutine;
|
||
/// <summary>
|
||
/// 通道名称
|
||
/// </summary>
|
||
public string fondname;
|
||
public List<Toggle> toggles = new List<Toggle>();
|
||
private string streaming;
|
||
public Button Start_;
|
||
public Button Stop_;
|
||
/// <summary>
|
||
/// 文本的数据
|
||
/// </summary>
|
||
public string txtdata;
|
||
/// <summary>
|
||
/// 通道面板
|
||
/// </summary>
|
||
public GameObject tdbg;
|
||
/// <summary>
|
||
/// 确认选择的通道
|
||
/// </summary>
|
||
public Button EnterTd;
|
||
/// <summary>
|
||
/// 选择通道
|
||
/// </summary>
|
||
public TMP_Dropdown TdDrop;
|
||
void Start()
|
||
{
|
||
Start_.onClick.AddListener(() =>
|
||
{
|
||
Start_.interactable = false;
|
||
if (togglename.Count == 1)
|
||
{
|
||
if (chart.series.Count > 1)
|
||
{
|
||
//chart.RemoveSerie<SimplifiedLine>();
|
||
chart.series.Remove(chart.series[1]);
|
||
}
|
||
coroutine = StartCoroutine(InitializeFile("cmd1.ini"));
|
||
tdbg.SetActive(false);
|
||
}
|
||
if (togglename.Count == 2)
|
||
{
|
||
|
||
if (chart.series.Count < 2)
|
||
{
|
||
chart.AddSerie<SimplifiedLine>();
|
||
|
||
}
|
||
coroutine = StartCoroutine(InitializeFile("cmd2.ini"));
|
||
tdbg.SetActive(true);
|
||
}
|
||
|
||
});
|
||
Stop_.onClick.AddListener(() =>
|
||
{
|
||
Start_.interactable = true;
|
||
});
|
||
EnterTd.onClick.AddListener(() =>
|
||
{
|
||
fondname = TdDrop.captionText.text;
|
||
});
|
||
|
||
streaming = Application.streamingAssetsPath;
|
||
|
||
}
|
||
public string tempstr = "";
|
||
// 初始化:从 StreamingAssets 复制到 persistentDataPath
|
||
IEnumerator InitializeFile(string fileName)
|
||
{
|
||
|
||
// 从 StreamingAssets 读取
|
||
string streamingPath = Path.Combine(Application.streamingAssetsPath, fileName);
|
||
using (UnityWebRequest request = UnityWebRequest.Get(streamingPath))
|
||
{
|
||
yield return request.SendWebRequest();
|
||
|
||
if (request.result == UnityWebRequest.Result.Success)
|
||
{
|
||
chart.ClearData();
|
||
// 写入到可读写目录
|
||
var str = request.downloadHandler.text.Split('\n');
|
||
if (fileName == "cmd1.ini")
|
||
{
|
||
str[1] = "td=" + togglename[0];
|
||
fondname = "td=" + togglename[0];
|
||
chart.series[0].serieName = "通道" + togglename[0];
|
||
string ini = string.Concat(str[0], "\n", str[1], "\n", str[2]);
|
||
File.WriteAllText(streaming + "/" + fileName, ini);
|
||
// yield return new WaitForSeconds(1.5f);
|
||
StartCoroutine(GetConfig(b =>
|
||
{
|
||
var data = txtdata.Split('@');
|
||
for (int i = 0; i < data.Length - 1; i++)
|
||
{
|
||
var datas = data[i].Split(',');
|
||
chart.AddXAxisData(datas[0]);
|
||
serie.AddYData(double.Parse(datas[1]));
|
||
}
|
||
}, "data1.txt"));
|
||
}
|
||
else
|
||
{
|
||
TdDrop.ClearOptions();
|
||
chart.series[0].serieName = "通道" + togglename[0];
|
||
chart.series[1].serieName = "通道" + togglename[1];
|
||
List<string> list = new List<string>();
|
||
list.Add(chart.series[0].serieName);
|
||
list.Add(chart.series[1].serieName);
|
||
TdDrop.AddOptions(list);
|
||
fondname = TdDrop.captionText.text;
|
||
str[1] = "td=" + togglename[0];
|
||
str[2] = "td=" + togglename[1];
|
||
string ini = string.Concat(str[0], "\n", str[1], "\n", str[2], "\n", str[3]);
|
||
//yield return new WaitForSeconds(1.5f);
|
||
File.WriteAllText(streaming + "/" + fileName, ini);
|
||
Debug.Log(ini);
|
||
StartCoroutine(GetConfig(b =>
|
||
{
|
||
var data = txtdata.Split('@');
|
||
for (int i = 0; i < data.Length - 1; i++)
|
||
{
|
||
var datas = data[i].Split(',');
|
||
chart.AddXAxisData(datas[0]);
|
||
chart.series[0].AddYData(double.Parse(datas[1]));
|
||
chart.series[1].AddYData(double.Parse(datas[2]));
|
||
}
|
||
Debug.Log(data.Count());
|
||
}, "data2.txt"));
|
||
}
|
||
}
|
||
else
|
||
{
|
||
Debug.LogError($"读取失败:{request.error}");
|
||
}
|
||
}
|
||
}
|
||
private void Awake()
|
||
{
|
||
serie = chart.GetSerie<Serie>();
|
||
|
||
}
|
||
|
||
void Update()
|
||
{
|
||
for (int i = 0; i < toggles.Count; i++)
|
||
{
|
||
if (toggles[i].isOn)
|
||
{
|
||
if (!togglename.Contains(toggles[i].name))
|
||
togglename.Add(toggles[i].name);
|
||
}
|
||
else
|
||
{
|
||
if (!toggles[i].isOn)
|
||
{
|
||
if (togglename.Contains(toggles[i].name))
|
||
togglename.Remove(toggles[i].name);
|
||
}
|
||
}
|
||
}
|
||
if (togglename.Count > 2)
|
||
{
|
||
Start_.interactable = false;
|
||
Stop_.interactable = false;
|
||
Debug.Log("所选的通道超过最大限制");
|
||
}
|
||
else if (togglename.Count > 0 && togglename.Count < 3)
|
||
{
|
||
Start_.interactable = true;
|
||
Stop_.interactable = true;
|
||
}
|
||
Debug.Log("数据:" + serie.context.pointerItemDataIndex);
|
||
if (serie.context.pointerItemDataIndex >= 0 && isclick)
|
||
{
|
||
|
||
SerieData series = chart.GetSerie(fondname).GetSerieData(serie.context.pointerItemDataIndex);
|
||
Debug.Log("鼠标点击的数据:" + series.data[1]);
|
||
XAxis Xaxis = chart.GetChartComponent<XAxis>();
|
||
Debug.Log("X轴名称" + Xaxis.data[0]);
|
||
string value = Xaxis.data[serie.context.pointerItemDataIndex] + "|" + series.data[1].ToString();
|
||
if (!data.Contains(value))
|
||
{
|
||
data.Add(value);
|
||
}
|
||
}
|
||
if (data.Count > 1)
|
||
{
|
||
string[] str1 = data[0].Split('|');
|
||
string[] str2 = data[1].Split('|');
|
||
x1.text = "X1 = " + str1[0];
|
||
x2.text = "X2 = " + str2[0];
|
||
y1.text = "Y1 = " + str1[1];
|
||
y2.text = "Y2 = " + str2[1];
|
||
Sy1.text = "▲Y = " + MathF.Abs(float.Parse(str1[1]) - float.Parse(str2[1])).ToString();
|
||
//Text.text = str1[0] + ":" + str1[1] + "和" + str2[0] + ":" + str2[1] + "的差值为:" + MathF.Abs(float.Parse(str1[1]) - float.Parse(str2[1]));
|
||
Debug.Log(str1[0] + ":" + str1[1] + "和" + str2[0] + ":" + str2[1] + "的差值为:" + MathF.Abs(float.Parse(str1[1]) - float.Parse(str2[1])));
|
||
data = new List<string>();
|
||
isclick = false;
|
||
}
|
||
if (Input.GetMouseButtonDown(0))
|
||
{
|
||
if (!isclick)
|
||
{
|
||
isclick = true;
|
||
}
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 获取配置数据
|
||
/// </summary>
|
||
/// <param name="action"></param>
|
||
/// <returns></returns>
|
||
private IEnumerator GetConfig(System.Action<bool> action, string name)
|
||
{
|
||
UnityWebRequest www = UnityWebRequest.Get(Application.streamingAssetsPath + "/" + name);
|
||
yield return www.SendWebRequest();
|
||
if ((www.result == UnityWebRequest.Result.ProtocolError) || (www.result == UnityWebRequest.Result.ConnectionError))
|
||
{
|
||
Debug.Log(www.error);
|
||
action(false);
|
||
}
|
||
else
|
||
{
|
||
Debug.Log(www.downloadHandler.text);
|
||
txtdata = www.downloadHandler.text;
|
||
action(true);
|
||
}
|
||
}
|
||
}
|