DeviceTrendPanel.cs 9.3 KB

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