WaterTrendPanel.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Threading.Tasks;
  5. using Bitsplash.DatePicker;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. using XCharts.Runtime;
  9. public class WaterTrendPanel : MonoBehaviour
  10. {
  11. public LineChart _LineChart;
  12. public Text nameText;
  13. public GameObject listItemOri;
  14. public List<GameObject> listObj = new List<GameObject>();
  15. public Button closeButton;
  16. public string currentStcd;
  17. public Transform listRoot;
  18. public DatePickerDropDownTextMeshPro DatePicker_Start;
  19. public DatePickerDropDownTextMeshPro DatePicker_End;
  20. public string startDateStr="";
  21. public string endDateStr="";
  22. private void Start()
  23. {
  24. DatePicker_Start = this.transform.Find("DatePicker_Start").GetComponent<DatePickerDropDownTextMeshPro>();
  25. DatePicker_End = this.transform.Find("DatePicker_End").GetComponent<DatePickerDropDownTextMeshPro>();
  26. DatePicker_Start.GetDateString += (string dateStr) =>
  27. {
  28. startDateStr = dateStr;
  29. CheckDate();
  30. };
  31. DatePicker_End.GetDateString += (string dateStr) =>
  32. {
  33. endDateStr = dateStr;
  34. CheckDate();
  35. };
  36. }
  37. public void Init()
  38. {
  39. _LineChart = this.transform.Find("LineChart").GetComponent<LineChart>();
  40. listRoot = this.transform.Find("ScrollView/Viewport/Content").transform;
  41. closeButton = this.transform.Find("CloseButton").GetComponent<Button>();
  42. nameText = this.transform.Find("nameText").GetComponent<Text>();
  43. closeButton.onClick.AddListener(() => { Hide(); });
  44. }
  45. public void CheckDate()
  46. {
  47. if (!startDateStr.Equals("") && !endDateStr.Equals(""))
  48. {
  49. var startD=DateTime.ParseExact(startDateStr,"yyyy-MM-dd",null);
  50. var endD= DateTime.ParseExact(endDateStr,"yyyy-MM-dd",null);
  51. var disD = endD - startD;
  52. Debug.Log(disD.TotalDays);
  53. if (disD.TotalDays > 180)
  54. {
  55. Debug.Log("时间间隔大于6个月");
  56. }
  57. }
  58. }
  59. public async Task Show(string stcd, string name)
  60. {
  61. currentStcd = stcd;
  62. name = name.Replace(" ", "");
  63. nameText.text = $"{name}站点水位趋势";
  64. this.gameObject.SetActive(true);
  65. string chartJsonStr = await HttpHelper._Instance.GetWaterTrend_Chart(stcd);
  66. SetChartLine(chartJsonStr);
  67. string listJsonStr = await HttpHelper._Instance.GetWaterTrend_List(stcd);
  68. StartCoroutine(CreatList(listJsonStr));
  69. }
  70. public void Hide()
  71. {
  72. this.gameObject.SetActive(false);
  73. }
  74. public void SetChartLine(string jsonData)
  75. {
  76. Debug.Log(jsonData);
  77. WaterTrendData_Chart tempData = Newtonsoft.Json.JsonConvert.DeserializeObject<WaterTrendData_Chart>(jsonData);
  78. XAxis tempXaxis = _LineChart.GetChartComponent<XAxis>();
  79. tempXaxis.data.Clear();
  80. for (int i = 0; i < tempData.data.Count; i++)
  81. {
  82. tempXaxis.AddData(tempData.data[i].key);
  83. }
  84. var tempSeries = _LineChart.series;
  85. SerieData[] tempDatas = new SerieData[tempData.data.Count];
  86. for (int i = 0; i < tempDatas.Length; i++)
  87. {
  88. tempDatas[i] = new SerieData();
  89. tempDatas[i].data = new List<double>();
  90. tempDatas[i].data.Add(i);
  91. tempDatas[i].data.Add(float.Parse(tempData.data[i].value));
  92. }
  93. tempSeries[0].data.Clear();
  94. tempSeries[0].data.AddRange(tempDatas);
  95. }
  96. IEnumerator CreatList(string jsonData)
  97. {
  98. Debug.Log(jsonData);
  99. var wait = new WaitForEndOfFrame();
  100. WaterTrendData_List tempData = Newtonsoft.Json.JsonConvert.DeserializeObject<WaterTrendData_List>(jsonData);
  101. if (listObj.Count > 0)
  102. {
  103. GameObject[] deleteObjs = listObj.ToArray();
  104. for (int i = 0; i < deleteObjs.Length; i++)
  105. {
  106. Destroy(deleteObjs[i]);
  107. }
  108. }
  109. listObj.Clear();
  110. int creatCount = 0;
  111. float lastWaterValue = 0;
  112. for (int i = 0; i < tempData.data.Count; i++)
  113. {
  114. GameObject tempObj = Instantiate(listItemOri, listRoot);
  115. tempObj.transform.Find("id").GetComponent<Text>().text = $"{i + 1}";
  116. tempObj.transform.Find("time").GetComponent<Text>().text = $"{tempData.data[i].updateTime}";
  117. tempObj.transform.Find("value").GetComponent<Text>().text = $"{tempData.data[i].dwz}";
  118. string dirStr = "-";
  119. if (i == 0)
  120. {
  121. dirStr = "→";
  122. lastWaterValue = float.Parse(tempData.data[i].dwz);
  123. }
  124. else
  125. {
  126. float currentWaterValue = float.Parse(tempData.data[i].dwz);
  127. if (currentWaterValue > lastWaterValue)
  128. {
  129. dirStr = "↑";
  130. }
  131. else if (currentWaterValue < lastWaterValue)
  132. {
  133. dirStr = "↓";
  134. }
  135. else
  136. {
  137. dirStr = "→";
  138. }
  139. lastWaterValue = currentWaterValue;
  140. }
  141. tempObj.transform.Find("trend").GetComponent<Text>().text = $"{dirStr}";
  142. listObj.Add(tempObj);
  143. creatCount++;
  144. if (creatCount >= 10)
  145. {
  146. creatCount = 0;
  147. yield return wait;
  148. }
  149. }
  150. }
  151. }
  152. [Serializable]
  153. public class WaterTrendData_Chart
  154. {
  155. public List<WaterCharData> data;
  156. }
  157. [Serializable]
  158. public class WaterCharData
  159. {
  160. public string key;
  161. public string value;
  162. }
  163. [Serializable]
  164. public class WaterTrendData_List
  165. {
  166. public List<WaterTrendData> data;
  167. }
  168. [Serializable]
  169. public class WaterTrendData
  170. {
  171. public string updateTime;
  172. public string dwz;
  173. }