DeviceTrendPanel.cs 9.1 KB

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