DeviceTrendPanel.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. var tempSeries = _LineChart.series;
  111. tempSeries[0].serieName = typeText;
  112. tempXaxis.axisName.name = typeText;
  113. nameText.text = $"{name} {typeText}趋势";
  114. this.gameObject.SetActive(true);
  115. string endTime = DateTime.Now.ToString("yyyy-MM-dd");
  116. string startTime = DateTime.Now.AddMonths(-3).ToString("yyyy-MM-dd");
  117. startDateStr = startTime;
  118. endDateStr = endTime;
  119. DatePicker_Start.SetLabelText(startTime);
  120. DatePicker_End.SetLabelText(endTime);
  121. string getJsonStr = await HttpHelper._Instance.GetDeviceTrend(currentType, gid, sid, startTime, endTime);
  122. GetTrend(getJsonStr);
  123. }
  124. public void Hide()
  125. {
  126. this.gameObject.SetActive(false);
  127. }
  128. /// <summary>
  129. /// 获取时间范围内的趋势图
  130. /// </summary>
  131. public void GetTrend(string jsonStr)
  132. {
  133. try
  134. {
  135. Debug.Log("GetTrend requestData:" + jsonStr);
  136. DeviceTrendRequest requestData = JsonConvert.DeserializeObject<DeviceTrendRequest>(jsonStr);
  137. XAxis tempXaxis = _LineChart.GetChartComponent<XAxis>();
  138. tempXaxis.data.Clear();
  139. for (int i = 0; i < requestData.data.Length; i++)
  140. {
  141. tempXaxis.AddData(requestData.data[i].timeGroup);
  142. }
  143. var tempSeries = _LineChart.series;
  144. SerieData[] tempDatas = new SerieData[requestData.data.Length];
  145. for (int i = 0; i < tempDatas.Length; i++)
  146. {
  147. tempDatas[i] = new SerieData();
  148. tempDatas[i].data = new List<double>();
  149. tempDatas[i].data.Add(i);
  150. tempDatas[i].data.Add(float.Parse(requestData.data[i].r1));
  151. }
  152. tempSeries[0].data.Clear();
  153. tempSeries[0].data.AddRange(tempDatas);
  154. StartCoroutine(CreatList(requestData));
  155. }
  156. catch (Exception e)
  157. {
  158. Debug.LogError(e.ToString());
  159. throw;
  160. }
  161. }
  162. IEnumerator CreatList(DeviceTrendRequest deviceData)
  163. {
  164. var wait = new WaitForEndOfFrame();
  165. if (listObj.Count > 0)
  166. {
  167. GameObject[] deleteObjs = listObj.ToArray();
  168. for (int i = 0; i < deleteObjs.Length; i++)
  169. {
  170. Destroy(deleteObjs[i]);
  171. }
  172. }
  173. listObj.Clear();
  174. int creatCount = 0;
  175. float lastWaterValue = 0;
  176. for (int i = 0; i < deviceData.data.Length; i++)
  177. {
  178. GameObject tempObj = Instantiate(listItemOri, listRoot);
  179. tempObj.transform.Find("id").GetComponent<Text>().text = $"{i + 1}";
  180. tempObj.transform.Find("time").GetComponent<Text>().text = $"{deviceData.data[i].timeGroup}";
  181. tempObj.transform.Find("value").GetComponent<Text>().text = $"{deviceData.data[i].r1}";
  182. string dirStr = "-";
  183. if (i == 0)
  184. {
  185. dirStr = "→";
  186. lastWaterValue = float.Parse(deviceData.data[i].r1);
  187. }
  188. else
  189. {
  190. float currentWaterValue = float.Parse(deviceData.data[i].r1);
  191. if (currentWaterValue > lastWaterValue)
  192. {
  193. dirStr = "↑";
  194. }
  195. else if (currentWaterValue < lastWaterValue)
  196. {
  197. dirStr = "↓";
  198. }
  199. else
  200. {
  201. dirStr = "→";
  202. }
  203. lastWaterValue = currentWaterValue;
  204. }
  205. tempObj.transform.Find("trend").GetComponent<Text>().text = $"{dirStr}";
  206. listObj.Add(tempObj);
  207. creatCount++;
  208. if (creatCount >= 10)
  209. {
  210. creatCount = 0;
  211. yield return wait;
  212. }
  213. }
  214. }
  215. }