DeviceTrendPanel.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Threading.Tasks;
  5. using Bitsplash.DatePicker;
  6. using Newtonsoft.Json;
  7. using UnityEngine;
  8. using UnityEngine.UI;
  9. using XCharts.Runtime;
  10. public class DeviceTrendPanel : MonoBehaviour
  11. {
  12. public LineChart _LineChart;
  13. public Text nameText;
  14. public GameObject listItemOri;
  15. public List<GameObject> listObj = new List<GameObject>();
  16. public Transform listRoot;
  17. public Button closeButton;
  18. public Button resetButton;
  19. public Button checkButton;
  20. public Text tipsText;
  21. public DatePickerDropDownTextMeshPro DatePicker_Start;
  22. public DatePickerDropDownTextMeshPro DatePicker_End;
  23. public string startDateStr = "";
  24. public string endDateStr = "";
  25. public int currentType = 0; //0补元 1套口
  26. public string _sid;
  27. public string _gid;
  28. private void Awake()
  29. {
  30. _LineChart = this.transform.Find("LineChart").GetComponent<LineChart>();
  31. listRoot = this.transform.Find("ScrollView/Viewport/Content").transform;
  32. closeButton = this.transform.Find("CloseButton").GetComponent<Button>();
  33. nameText = this.transform.Find("nameText").GetComponent<Text>();
  34. checkButton = this.transform.Find("CheckButton").GetComponent<Button>();
  35. tipsText = this.transform.Find("tipsText").GetComponent<Text>();
  36. tipsText.enabled = false;
  37. checkButton.onClick.AddListener(() => { GetData(); });
  38. closeButton.onClick.AddListener(() =>
  39. {
  40. Hide();
  41. if (GCJKLayer._Instance.currentSelectItem != null)
  42. {
  43. GCJKLayer._Instance.currentSelectItem.SetHighLight(false);
  44. GCJKLayer._Instance.currentSelectItem = null;
  45. }
  46. });
  47. }
  48. private void Start()
  49. {
  50. DatePicker_Start = this.transform.Find("DatePicker_Start").GetComponent<DatePickerDropDownTextMeshPro>();
  51. DatePicker_End = this.transform.Find("DatePicker_End").GetComponent<DatePickerDropDownTextMeshPro>();
  52. DatePicker_Start.GetDateString += (string dateStr) =>
  53. {
  54. startDateStr = dateStr;
  55. CheckDate();
  56. };
  57. DatePicker_End.GetDateString += (string dateStr) =>
  58. {
  59. endDateStr = dateStr;
  60. CheckDate();
  61. };
  62. Hide();
  63. }
  64. public void CheckDate()
  65. {
  66. if (!startDateStr.Equals("") && !endDateStr.Equals(""))
  67. {
  68. var startD = DateTime.ParseExact(startDateStr, "yyyy-MM-dd", null);
  69. var endD = DateTime.ParseExact(endDateStr, "yyyy-MM-dd", null);
  70. var disD = endD - startD;
  71. Debug.Log(disD.TotalDays);
  72. if (disD.TotalDays > 183)
  73. {
  74. Debug.Log("时间间隔大于6个月");
  75. checkButton.gameObject.SetActive(false);
  76. tipsText.enabled = true;
  77. }
  78. else
  79. {
  80. tipsText.enabled = false;
  81. checkButton.gameObject.SetActive(true);
  82. }
  83. }
  84. }
  85. public async Task GetData()
  86. {
  87. string getJsonStr =
  88. await HttpHelper._Instance.GetDeviceTrend(currentType, _gid, _sid, startDateStr, endDateStr);
  89. GetTrend(getJsonStr);
  90. }
  91. public async Task Show(string name, GongChengType type, string gid, string sid)
  92. {
  93. _gid = gid;
  94. _sid = sid;
  95. name = name.Replace(" ", "");
  96. string typeText = "";
  97. switch (type)
  98. {
  99. case GongChengType.shuiWei:
  100. typeText = "水位";
  101. break;
  102. case GongChengType.shuiYa:
  103. typeText = "水压";
  104. break;
  105. case GongChengType.weiYi:
  106. typeText = "位移";
  107. break;
  108. }
  109. YAxis tempXaxis = _LineChart.GetChartComponent<YAxis>();
  110. tempXaxis.axisName.name = typeText;
  111. nameText.text = $"{name} {typeText}趋势";
  112. this.gameObject.SetActive(true);
  113. string endTime = DateTime.Now.ToString("yyyy-MM-dd");
  114. string startTime = DateTime.Now.AddMonths(-3).ToString("yyyy-MM-dd");
  115. startDateStr = startTime;
  116. endDateStr = endTime;
  117. DatePicker_Start.SetLabelText(startTime);
  118. DatePicker_End.SetLabelText(endTime);
  119. string getJsonStr = await HttpHelper._Instance.GetDeviceTrend(currentType, gid, sid, startTime, endTime);
  120. GetTrend(getJsonStr);
  121. }
  122. public void Hide()
  123. {
  124. this.gameObject.SetActive(false);
  125. }
  126. /// <summary>
  127. /// 获取时间范围内的趋势图
  128. /// </summary>
  129. public void GetTrend(string jsonStr)
  130. {
  131. try
  132. {
  133. Debug.Log("GetTrend requestData:" + jsonStr);
  134. DeviceTrendRequest requestData = JsonConvert.DeserializeObject<DeviceTrendRequest>(jsonStr);
  135. XAxis tempXaxis = _LineChart.GetChartComponent<XAxis>();
  136. tempXaxis.data.Clear();
  137. for (int i = 0; i < requestData.data.Length; i++)
  138. {
  139. tempXaxis.AddData(requestData.data[i].timeGroup);
  140. }
  141. var tempSeries = _LineChart.series;
  142. SerieData[] tempDatas = new SerieData[requestData.data.Length];
  143. for (int i = 0; i < tempDatas.Length; i++)
  144. {
  145. tempDatas[i] = new SerieData();
  146. tempDatas[i].data = new List<double>();
  147. tempDatas[i].data.Add(i);
  148. tempDatas[i].data.Add(float.Parse(requestData.data[i].r1));
  149. }
  150. tempSeries[0].data.Clear();
  151. tempSeries[0].data.AddRange(tempDatas);
  152. StartCoroutine(CreatList(requestData));
  153. }
  154. catch (Exception e)
  155. {
  156. Debug.LogError(e.ToString());
  157. throw;
  158. }
  159. }
  160. IEnumerator CreatList(DeviceTrendRequest deviceData)
  161. {
  162. var wait = new WaitForEndOfFrame();
  163. if (listObj.Count > 0)
  164. {
  165. GameObject[] deleteObjs = listObj.ToArray();
  166. for (int i = 0; i < deleteObjs.Length; i++)
  167. {
  168. Destroy(deleteObjs[i]);
  169. }
  170. }
  171. listObj.Clear();
  172. int creatCount = 0;
  173. float lastWaterValue = 0;
  174. for (int i = 0; i < deviceData.data.Length; i++)
  175. {
  176. GameObject tempObj = Instantiate(listItemOri, listRoot);
  177. tempObj.transform.Find("id").GetComponent<Text>().text = $"{i + 1}";
  178. tempObj.transform.Find("time").GetComponent<Text>().text = $"{deviceData.data[i].timeGroup}";
  179. tempObj.transform.Find("value").GetComponent<Text>().text = $"{deviceData.data[i].r1}";
  180. string dirStr = "-";
  181. if (i == 0)
  182. {
  183. dirStr = "→";
  184. lastWaterValue = float.Parse(deviceData.data[i].r1);
  185. }
  186. else
  187. {
  188. float currentWaterValue = float.Parse(deviceData.data[i].r1);
  189. if (currentWaterValue > lastWaterValue)
  190. {
  191. dirStr = "↑";
  192. }
  193. else if (currentWaterValue < lastWaterValue)
  194. {
  195. dirStr = "↓";
  196. }
  197. else
  198. {
  199. dirStr = "→";
  200. }
  201. lastWaterValue = currentWaterValue;
  202. }
  203. tempObj.transform.Find("trend").GetComponent<Text>().text = $"{dirStr}";
  204. listObj.Add(tempObj);
  205. creatCount++;
  206. if (creatCount >= 10)
  207. {
  208. creatCount = 0;
  209. yield return wait;
  210. }
  211. }
  212. }
  213. }