DeviceTrendPanel.cs 7.9 KB

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