DeviceTrendPanel.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 = "水位/m";
  114. break;
  115. case GongChengType.shenYaJi:
  116. typeText = "渗压/KPa";
  117. break;
  118. case GongChengType.jingLiShuiZhunYi:
  119. typeText = "位移/mm";
  120. break;
  121. case GongChengType.wenDu:
  122. typeText = "温度/\u2103";
  123. break;
  124. case GongChengType.yinZhangXianYi:
  125. typeText = "位移/mm";
  126. break;
  127. case GongChengType.chuiXianZuoBiaoYi:
  128. typeText = "位移/mm";
  129. break;
  130. case GongChengType.tuYaLiJi:
  131. typeText = "应力/MPa";
  132. break;
  133. case GongChengType.yingLiJi:
  134. typeText = "应力/MPa";
  135. break;
  136. case GongChengType.sanXiangWeiCuoJi:
  137. typeText = "位移/mm";
  138. break;
  139. case GongChengType.erXiangCeFengJi:
  140. typeText = "位移/mm";
  141. break;
  142. case GongChengType.weiYiJi:
  143. typeText = "位移/mm";
  144. break;
  145. }
  146. YAxis tempXaxis = _LineChart.GetChartComponent<YAxis>();
  147. var tempSeries = _LineChart.series;
  148. tempSeries[0].serieName = typeText;
  149. tempXaxis.axisName.name = typeText+" "+DateTime.Now.ToString("yyyy");
  150. nameText.text = $"{_name} {typeText}趋势";
  151. string endTime = DateTime.Now.ToString("yyyy-MM-dd");
  152. string startTime = DateTime.Now.AddMonths(-3).ToString("yyyy-MM-dd");
  153. startDateStr = startTime;
  154. endDateStr = endTime;
  155. DatePicker_Start.SetLabelText(startTime);
  156. DatePicker_End.SetLabelText(endTime);
  157. string getJsonStr = await HttpHelper._Instance.GetDeviceTrend(currentType, gid, sid, startTime, endTime);
  158. GetTrend(getJsonStr);
  159. }
  160. public void Hide()
  161. {
  162. GCJKLayer._Instance.RemovePanel(this);
  163. DestroyImmediate(this.gameObject);
  164. }
  165. /// <summary>
  166. /// 获取时间范围内的趋势图
  167. /// </summary>
  168. public void GetTrend(string jsonStr)
  169. {
  170. try
  171. {
  172. Debug.Log("GetTrend requestData:" + jsonStr);
  173. DeviceTrendRequest requestData = JsonConvert.DeserializeObject<DeviceTrendRequest>(jsonStr);
  174. XAxis tempXaxis = _LineChart.GetChartComponent<XAxis>();
  175. tempXaxis.data.Clear();
  176. for (int i = 0; i < requestData.data.Length; i++)
  177. {
  178. var tempTimeDate = DateTime.ParseExact(requestData.data[i].timeGroup, "yyyy-MM-dd HH:mm:ss",
  179. CultureInfo.InvariantCulture);
  180. tempXaxis.AddData(tempTimeDate.ToString("MM-dd"));
  181. }
  182. var tempSeries = _LineChart.series;
  183. SerieData[] tempDatas = new SerieData[requestData.data.Length];
  184. string typeText = "";
  185. switch (currentGongChengType)
  186. {
  187. case GongChengType.shuiWei:
  188. typeText = "水位/m";
  189. break;
  190. case GongChengType.shenYaJi:
  191. typeText = "渗压/KPa";
  192. break;
  193. case GongChengType.jingLiShuiZhunYi:
  194. typeText = "位移/mm";
  195. break;
  196. case GongChengType.wenDu:
  197. typeText = "温度/\u2103";
  198. break;
  199. case GongChengType.yinZhangXianYi:
  200. typeText = "位移/mm";
  201. break;
  202. case GongChengType.chuiXianZuoBiaoYi:
  203. typeText = "位移/mm";
  204. break;
  205. case GongChengType.tuYaLiJi:
  206. typeText = "应力/MPa";
  207. break;
  208. case GongChengType.yingLiJi:
  209. typeText = "应力/MPa";
  210. break;
  211. case GongChengType.sanXiangWeiCuoJi:
  212. typeText = "位移/mm";
  213. break;
  214. case GongChengType.erXiangCeFengJi:
  215. typeText = "位移/mm";
  216. break;
  217. case GongChengType.weiYiJi:
  218. typeText = "位移/mm";
  219. break;
  220. }
  221. tempSeries[0].serieName = typeText;
  222. string startTimeStr=DateTime.Parse(startDateStr).ToString("yyyy");
  223. string endTimeStr=DateTime.Parse(endDateStr).ToString("yyyy");
  224. string finalStr = startTimeStr.Equals(endDateStr) ? startTimeStr : $"{startTimeStr}-{endTimeStr}";
  225. tempXaxis.axisName.name = typeText+" "+finalStr;
  226. for (int i = 0; i < tempDatas.Length; i++)
  227. {
  228. tempDatas[i] = new SerieData();
  229. tempDatas[i].data = new List<double>();
  230. tempDatas[i].data.Add(i);
  231. //Debug.Log(requestData.data[i].r1);
  232. float tempValue = float.Parse(float.Parse(requestData.data[i].r1).ToString("0.00"));
  233. //Debug.Log(tempValue);
  234. tempDatas[i].data.Add(tempValue);
  235. }
  236. tempSeries[0].data.Clear();
  237. tempSeries[0].data.AddRange(tempDatas);
  238. StartCoroutine(CreatList(requestData));
  239. }
  240. catch (Exception e)
  241. {
  242. Debug.LogError(e.ToString());
  243. throw;
  244. }
  245. }
  246. IEnumerator CreatList(DeviceTrendRequest deviceData)
  247. {
  248. var wait = new WaitForEndOfFrame();
  249. if (listObj.Count > 0)
  250. {
  251. GameObject[] deleteObjs = listObj.ToArray();
  252. for (int i = 0; i < deleteObjs.Length; i++)
  253. {
  254. Destroy(deleteObjs[i]);
  255. }
  256. }
  257. listObj.Clear();
  258. int creatCount = 0;
  259. float lastWaterValue = 0;
  260. for (int i = 0; i < deviceData.data.Length; i++)
  261. {
  262. float value = float.Parse(float.Parse(deviceData.data[i].r1).ToString("F2"));
  263. GameObject tempObj = Instantiate(listItemOri, listRoot);
  264. tempObj.transform.Find("id").GetComponent<Text>().text = $"{i + 1}";
  265. tempObj.transform.Find("time").GetComponent<Text>().text = $"{deviceData.data[i].timeGroup}";
  266. tempObj.transform.Find("value").GetComponent<Text>().text = $"{value}";
  267. string dirStr = "-";
  268. if (i == 0)
  269. {
  270. dirStr = "→";
  271. lastWaterValue = value;
  272. }
  273. else
  274. {
  275. float currentWaterValue = value;
  276. if (currentWaterValue > lastWaterValue)
  277. {
  278. dirStr = "↑";
  279. }
  280. else if (currentWaterValue < lastWaterValue)
  281. {
  282. dirStr = "↓";
  283. }
  284. else
  285. {
  286. dirStr = "→";
  287. }
  288. lastWaterValue = currentWaterValue;
  289. }
  290. tempObj.transform.Find("trend").GetComponent<Text>().text = $"{dirStr}";
  291. listObj.Add(tempObj);
  292. creatCount++;
  293. if (creatCount >= 10)
  294. {
  295. creatCount = 0;
  296. yield return wait;
  297. }
  298. }
  299. }
  300. }