123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Threading.Tasks;
- using Bitsplash.DatePicker;
- using Newtonsoft.Json;
- using UnityEngine;
- using UnityEngine.UI;
- using XCharts.Runtime;
- public class DeviceTrendPanel : MonoBehaviour
- {
- public LineChart _LineChart;
- public Text nameText;
- public GameObject listItemOri;
- public List<GameObject> listObj = new List<GameObject>();
- public Transform listRoot;
- public Button closeButton;
- public Button resetButton;
- public Button checkButton;
- public DatePickerDropDownTextMeshPro DatePicker_Start;
- public DatePickerDropDownTextMeshPro DatePicker_End;
- public string startDateStr = "";
- public string endDateStr = "";
- public int currentType = 0; //0补元 1套口
- public string _sid;
- public string _gid;
- private void Awake()
- {
- _LineChart = this.transform.Find("LineChart").GetComponent<LineChart>();
- listRoot = this.transform.Find("ScrollView/Viewport/Content").transform;
- closeButton = this.transform.Find("CloseButton").GetComponent<Button>();
- nameText = this.transform.Find("nameText").GetComponent<Text>();
- checkButton = this.transform.Find("CheckButton").GetComponent<Button>();
- checkButton.onClick.AddListener(() => { GetData(); });
- closeButton.onClick.AddListener(() => { Hide(); });
- }
- private void Start()
- {
- DatePicker_Start = this.transform.Find("DatePicker_Start").GetComponent<DatePickerDropDownTextMeshPro>();
- DatePicker_End = this.transform.Find("DatePicker_End").GetComponent<DatePickerDropDownTextMeshPro>();
- DatePicker_Start.GetDateString += (string dateStr) =>
- {
- startDateStr = dateStr;
- CheckDate();
- };
- DatePicker_End.GetDateString += (string dateStr) =>
- {
- endDateStr = dateStr;
- CheckDate();
- };
-
- Hide();
- }
- public void CheckDate()
- {
- if (!startDateStr.Equals("") && !endDateStr.Equals(""))
- {
- var startD = DateTime.ParseExact(startDateStr, "yyyy-MM-dd", null);
- var endD = DateTime.ParseExact(endDateStr, "yyyy-MM-dd", null);
- var disD = endD - startD;
- Debug.Log(disD.TotalDays);
- if (disD.TotalDays > 183)
- {
- Debug.Log("时间间隔大于6个月");
- checkButton.gameObject.SetActive(false);
- }
- else
- {
- checkButton.gameObject.SetActive(true);
- }
- }
- }
- public async Task GetData()
- {
- string getJsonStr =
- await HttpHelper._Instance.GetDeviceTrend(currentType, _gid, _sid, startDateStr, endDateStr);
- GetTrend(getJsonStr);
- }
- public async Task Show(string name, GongChengType type, string gid, string sid)
- {
- _gid = gid;
- _sid = sid;
- name = name.Replace(" ", "");
- string typeText = "";
- switch (type)
- {
- case GongChengType.shuiWei:
- typeText = "水位";
- break;
- case GongChengType.shuiYa:
- typeText = "水压";
- break;
- case GongChengType.weiYi:
- typeText = "位移";
- break;
- }
- XAxis tempXaxis = _LineChart.GetChartComponent<XAxis>();
- tempXaxis.axisName.name = typeText;
- nameText.text = $"{name} {typeText}趋势";
- this.gameObject.SetActive(true);
- string endTime = DateTime.Now.ToString("yyyy-MM-dd");
- string startTime = DateTime.Now.AddMonths(-3).ToString("yyyy-MM-dd");
- startDateStr = startTime;
- endDateStr = endTime;
- DatePicker_Start.SetLabelText(startTime);
- DatePicker_End.SetLabelText(endTime);
- string getJsonStr = await HttpHelper._Instance.GetDeviceTrend(currentType, gid, sid, startTime, endTime);
- GetTrend(getJsonStr);
- }
- public void Hide()
- {
- this.gameObject.SetActive(false);
- }
- /// <summary>
- /// 获取时间范围内的趋势图
- /// </summary>
- public void GetTrend(string jsonStr)
- {
- try
- {
- Debug.Log("GetTrend requestData:" + jsonStr);
- DeviceTrendRequest requestData = JsonConvert.DeserializeObject<DeviceTrendRequest>(jsonStr);
- XAxis tempXaxis = _LineChart.GetChartComponent<XAxis>();
- tempXaxis.data.Clear();
- for (int i = 0; i < requestData.data.Length; i++)
- {
- tempXaxis.AddData(requestData.data[i].timeGroup);
- }
- var tempSeries = _LineChart.series;
- SerieData[] tempDatas = new SerieData[requestData.data.Length];
- for (int i = 0; i < tempDatas.Length; i++)
- {
- tempDatas[i] = new SerieData();
- tempDatas[i].data = new List<double>();
- tempDatas[i].data.Add(i);
- tempDatas[i].data.Add(float.Parse(requestData.data[i].r1));
- }
- tempSeries[0].data.Clear();
- tempSeries[0].data.AddRange(tempDatas);
- StartCoroutine(CreatList(requestData));
- }
- catch (Exception e)
- {
- Debug.LogError(e.ToString());
- throw;
- }
- }
- IEnumerator CreatList(DeviceTrendRequest deviceData)
- {
- var wait = new WaitForEndOfFrame();
- if (listObj.Count > 0)
- {
- GameObject[] deleteObjs = listObj.ToArray();
- for (int i = 0; i < deleteObjs.Length; i++)
- {
- Destroy(deleteObjs[i]);
- }
- }
- listObj.Clear();
- int creatCount = 0;
- float lastWaterValue = 0;
- for (int i = 0; i < deviceData.data.Length; i++)
- {
- GameObject tempObj = Instantiate(listItemOri, listRoot);
- tempObj.transform.Find("id").GetComponent<Text>().text = $"{i + 1}";
- tempObj.transform.Find("time").GetComponent<Text>().text = $"{deviceData.data[i].timeGroup}";
- tempObj.transform.Find("value").GetComponent<Text>().text = $"{deviceData.data[i].r1}";
- string dirStr = "-";
- if (i == 0)
- {
- dirStr = "→";
- lastWaterValue = float.Parse(deviceData.data[i].r1);
- }
- else
- {
- float currentWaterValue = float.Parse(deviceData.data[i].r1);
- if (currentWaterValue > lastWaterValue)
- {
- dirStr = "↑";
- }
- else if (currentWaterValue < lastWaterValue)
- {
- dirStr = "↓";
- }
- else
- {
- dirStr = "→";
- }
- lastWaterValue = currentWaterValue;
- }
- tempObj.transform.Find("trend").GetComponent<Text>().text = $"{dirStr}";
- listObj.Add(tempObj);
- creatCount++;
- if (creatCount >= 10)
- {
- creatCount = 0;
- yield return wait;
- }
- }
- }
- }
|