DeviceTrendPanel.cs 7.0 KB

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