WaterTrendPanel.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Threading.Tasks;
  5. using Best.HTTP.JSON;
  6. using Bitsplash.DatePicker;
  7. using Newtonsoft.Json;
  8. using UnityEngine;
  9. using UnityEngine.UI;
  10. using XCharts.Runtime;
  11. public class WaterTrendPanel : MonoBehaviour
  12. {
  13. public LineChart _LineChart;
  14. public Text nameText;
  15. public GameObject listItemOri;
  16. public List<GameObject> listObj = new List<GameObject>();
  17. public Button closeButton;
  18. public string currentStcd;
  19. public string currentName;
  20. public Transform listRoot;
  21. public DatePickerDropDownTextMeshPro DatePicker_Start;
  22. public DatePickerDropDownTextMeshPro DatePicker_End;
  23. public string startDateStr = "";
  24. public string endDateStr = "";
  25. public Button lineChartBtn;
  26. public Button infoBtn;
  27. public RectTransform lineChartPanel;
  28. public RectTransform infoPanel;
  29. public int currentType;
  30. public Text typeTitle;
  31. public Text idText;
  32. public Text stnmText;
  33. public Text stcdText;
  34. public Text stlcText;
  35. public Text sttpText;
  36. public Text addvcdText;
  37. public Text bsnmText;
  38. public Text hnnmText;
  39. public Text rvnmText;
  40. public Text lgtdText;
  41. public Text lttdText;
  42. public Text dtmnmText;
  43. public Text dtmelText;
  44. public Text atcunitText;
  45. public Text localityText;
  46. public Text frgrdText;
  47. public Text usflText;
  48. public Button searchButton;
  49. public GameObject threePanel;
  50. public GameObject bythreePanel;
  51. public GameObject taokouthreePanel;
  52. public void Init()
  53. {
  54. _LineChart = this.transform.Find("linePanel/LineChart").GetComponent<LineChart>();
  55. listRoot = this.transform.Find("linePanel/ScrollView/Viewport/Content").transform;
  56. closeButton = this.transform.Find("CloseButton").GetComponent<Button>();
  57. nameText = this.transform.Find("nameText").GetComponent<Text>();
  58. closeButton.onClick.AddListener(() => {
  59. print(111);
  60. Hide();
  61. });
  62. }
  63. private void Awake()
  64. {
  65. // // Get today's date
  66. // DateTime today = DateTime.Today;
  67. //
  68. // // Get the date 3 months ago
  69. // DateTime startDate = today.AddMonths(-3);
  70. //
  71. // // Format the dates to string
  72. // startDateStr = startDate.ToString("yyyy-MM-dd");
  73. // endDateStr = today.ToString("yyyy-MM-dd");
  74. DatePicker_Start = this.transform.Find("linePanel/DatePicker_Start").GetComponent<DatePickerDropDownTextMeshPro>();
  75. DatePicker_End = this.transform.Find("linePanel/DatePicker_End").GetComponent<DatePickerDropDownTextMeshPro>();
  76. // DatePicker_Start.SetLabelText(startDateStr);
  77. // DatePicker_End.SetLabelText(endDateStr);
  78. lineChartBtn.onClick.AddListener(() =>
  79. {
  80. if (lineChartBtn.GetComponent<CanvasGroup>().alpha < 1)
  81. {
  82. ChangePanel(0);
  83. ShowTrend(currentStcd, currentName,currentType);
  84. }
  85. });
  86. infoBtn.onClick.AddListener(() =>
  87. {
  88. if (infoBtn.GetComponent<CanvasGroup>().alpha < 1)
  89. {
  90. ChangePanel(1);
  91. ShowInfo();
  92. }
  93. });
  94. searchButton.onClick.AddListener(() =>
  95. {
  96. ShowTrend(currentStcd, currentName, currentType);
  97. });
  98. }
  99. private void Start()
  100. {
  101. DatePicker_Start.GetDateString += (string dateStr) =>
  102. {
  103. startDateStr = dateStr;
  104. CheckDate();
  105. };
  106. DatePicker_End.GetDateString += (string dateStr) =>
  107. {
  108. endDateStr = dateStr;
  109. CheckDate();
  110. };
  111. }
  112. public void CheckDate()
  113. {
  114. if (!startDateStr.Equals("") && !endDateStr.Equals(""))
  115. {
  116. var startD = DateTime.ParseExact(startDateStr, "yyyy-MM-dd", null);
  117. var endD = DateTime.ParseExact(endDateStr, "yyyy-MM-dd", null);
  118. var disD = endD - startD;
  119. Debug.Log(disD.TotalDays);
  120. if (disD.TotalDays > 183)
  121. {
  122. Debug.Log("时间间隔大于6个月");
  123. }
  124. }
  125. }
  126. /// <summary>
  127. /// 获取时间范围内的趋势图
  128. /// </summary>
  129. /// <param name="type"></param>
  130. /// <param name="gid"></param>
  131. /// <param name="sid"></param>
  132. /// <param name="startTime"></param>
  133. /// <param name="endTime"></param>
  134. public async Task GetTrend(int type, string gid, string sid)
  135. {
  136. string getJsonStr = await HttpHelper._Instance.GetDeviceTrend(type, gid, sid, startDateStr, endDateStr);
  137. try
  138. {
  139. Debug.Log("GetTrend requestData:" + getJsonStr);
  140. DeviceTrendRequest requestData = JsonConvert.DeserializeObject<DeviceTrendRequest>(getJsonStr);
  141. XAxis tempXaxis = _LineChart.GetChartComponent<XAxis>();
  142. tempXaxis.data.Clear();
  143. for (int i = 0; i < requestData.data.Length; i++)
  144. {
  145. tempXaxis.AddData(requestData.data[i].timeGroup);
  146. }
  147. var tempSeries = _LineChart.series;
  148. SerieData[] tempDatas = new SerieData[requestData.data.Length];
  149. for (int i = 0; i < tempDatas.Length; i++)
  150. {
  151. tempDatas[i] = new SerieData();
  152. tempDatas[i].data = new List<double>();
  153. tempDatas[i].data.Add(i);
  154. tempDatas[i].data.Add(float.Parse(requestData.data[i].r1));
  155. }
  156. tempSeries[0].data.Clear();
  157. tempSeries[0].data.AddRange(tempDatas);
  158. }
  159. catch (Exception e)
  160. {
  161. Debug.Log(e.ToString());
  162. throw;
  163. }
  164. }
  165. public void ChangePanel(int panelIndex = 0)
  166. {
  167. if (panelIndex == 0)
  168. {
  169. lineChartPanel.gameObject.SetActive(true);
  170. infoPanel.gameObject.SetActive(false);
  171. lineChartBtn.GetComponent<CanvasGroup>().alpha = 1;
  172. infoBtn.GetComponent<CanvasGroup>().alpha = 0.5f;
  173. }
  174. else
  175. {
  176. lineChartPanel.gameObject.SetActive(false);
  177. infoPanel.gameObject.SetActive(true);
  178. lineChartBtn.GetComponent<CanvasGroup>().alpha = 0.5f;
  179. infoBtn.GetComponent<CanvasGroup>().alpha = 1;
  180. }
  181. }
  182. public async Task ShowTrend(string stcd, string name,int type)
  183. {
  184. var tempSeries = _LineChart.series;
  185. tempSeries[0].data.Clear();
  186. if (listObj.Count > 0)
  187. {
  188. GameObject[] deleteObjs = listObj.ToArray();
  189. for (int i = 0; i < deleteObjs.Length; i++)
  190. {
  191. Destroy(deleteObjs[i]);
  192. }
  193. }
  194. listObj.Clear();
  195. string endTime = DateTime.Now.ToString("yyyy-MM-dd");
  196. string startTime = DateTime.Now.AddMonths(-3).ToString("yyyy-MM-dd");
  197. startDateStr = startTime;
  198. endDateStr = endTime;
  199. DatePicker_Start.SetLabelText(startTime);
  200. DatePicker_End.SetLabelText(endTime);
  201. currentType = type;
  202. if (type == 0)
  203. {
  204. threePanel.gameObject.SetActive(true);
  205. typeTitle.text = "站点水位趋势";
  206. }
  207. else {
  208. threePanel.gameObject.SetActive(false);
  209. typeTitle.text = "站点降雨趋势";
  210. }
  211. currentStcd = stcd;
  212. Debug.Log(stcd);
  213. currentName = name;
  214. ChangePanel(0);
  215. name = name.Replace(" ", "");
  216. nameText.text = $"站点:{name}";
  217. this.gameObject.SetActive(true);
  218. string chartJsonStr = "";
  219. if (type == 0) {
  220. _LineChart.GetChartComponent<YAxis>().axisName.name = "水位(m)";
  221. chartJsonStr = await HttpHelper._Instance.GetWaterTrend_Chart(stcd, startDateStr, endDateStr);
  222. }
  223. else{
  224. _LineChart.GetChartComponent<YAxis>().axisName.name = "降雨量(mm)";
  225. chartJsonStr = await HttpHelper._Instance.GetDropTrend_Chart(stcd, startDateStr, endDateStr);
  226. }
  227. if (stcd == "61017810")
  228. {
  229. bythreePanel.gameObject.SetActive(true);
  230. taokouthreePanel.gameObject.SetActive(false);
  231. }
  232. else {
  233. bythreePanel.gameObject.SetActive(false);
  234. taokouthreePanel.gameObject.SetActive(true);
  235. }
  236. SetChartLine(chartJsonStr);
  237. StartCoroutine(CreatList(chartJsonStr));
  238. }
  239. public async Task ShowInfo()
  240. {
  241. idText.text = "-";
  242. stnmText.text = "-";
  243. stcdText.text = "-";
  244. stlcText.text = "-";
  245. sttpText.text = "-";
  246. addvcdText.text = "-";
  247. bsnmText.text = "-";
  248. hnnmText.text = "-";
  249. rvnmText.text = "-";
  250. lgtdText.text = "-";
  251. lttdText.text = "-";
  252. dtmnmText.text = "-";
  253. dtmelText.text = "-";
  254. atcunitText.text = "-";
  255. localityText.text = "-";
  256. frgrdText.text = "-";
  257. usflText.text = "-";
  258. string infoJsonStr = await HttpHelper._Instance.GetWaterTrend_Info(currentStcd);
  259. Debug.Log(infoJsonStr);
  260. WaterTrendData_Info waterTrendData_Info = JsonConvert.DeserializeObject<WaterTrendData_Info>(infoJsonStr);
  261. Debug.Log(waterTrendData_Info);
  262. idText.text = waterTrendData_Info.data.id;
  263. stnmText.text = waterTrendData_Info.data.stnm;
  264. stcdText.text = waterTrendData_Info.data.stcd;
  265. stlcText.text = waterTrendData_Info.data.stlc;
  266. sttpText.text = waterTrendData_Info.data.sttp;
  267. addvcdText.text = waterTrendData_Info.data.addvcd;
  268. bsnmText.text = waterTrendData_Info.data.bsnm;
  269. hnnmText.text = waterTrendData_Info.data.hnnm;
  270. rvnmText.text = waterTrendData_Info.data.rvnm;
  271. lgtdText.text = waterTrendData_Info.data.lgtd;
  272. lttdText.text = waterTrendData_Info.data.lttd;
  273. dtmnmText.text = waterTrendData_Info.data.dtmnm;
  274. dtmelText.text = waterTrendData_Info.data.dtmel;
  275. atcunitText.text = waterTrendData_Info.data.atcunit;
  276. localityText.text = waterTrendData_Info.data.locality;
  277. frgrdText.text = waterTrendData_Info.data.frgrd;
  278. usflText.text = waterTrendData_Info.data.usfl;
  279. }
  280. public void Hide()
  281. {
  282. this.gameObject.SetActive(false);
  283. }
  284. public void SetChartLine(string jsonData)
  285. {
  286. WaterTrendData_Chart tempData = Newtonsoft.Json.JsonConvert.DeserializeObject<WaterTrendData_Chart>(jsonData);
  287. tempData.data.Reverse();
  288. XAxis tempXaxis = _LineChart.GetChartComponent<XAxis>();
  289. tempXaxis.data.Clear();
  290. for (int i = 0; i < tempData.data.Count; i++)
  291. {
  292. tempXaxis.AddData(tempData.data[i].time);
  293. }
  294. var tempSeries = _LineChart.series;
  295. tempSeries[0].serieName = "水位(m)";
  296. SerieData[] tempDatas = new SerieData[tempData.data.Count];
  297. for (int i = 0; i < tempDatas.Length; i++)
  298. {
  299. tempDatas[i] = new SerieData();
  300. tempDatas[i].data = new List<double>();
  301. tempDatas[i].data.Add(i);
  302. string valueTex = tempData.data[i].value;
  303. if (valueTex == "-") {
  304. valueTex = "-1";
  305. }
  306. double tempValue = double.Parse(float.Parse(valueTex).ToString("0.00"));
  307. //if (tempValue < 0) {
  308. // tempValue = 24.99f;
  309. //}
  310. tempDatas[i].data.Add(tempValue);
  311. }
  312. tempSeries[0].data.Clear();
  313. tempSeries[0].data.AddRange(tempDatas);
  314. }
  315. IEnumerator CreatList(string jsonData)
  316. {
  317. var wait = new WaitForEndOfFrame();
  318. WaterTrendData_Chart tempData = Newtonsoft.Json.JsonConvert.DeserializeObject<WaterTrendData_Chart>(jsonData);
  319. int creatCount = 0;
  320. for (int i = 0; i < tempData.data.Count; i++)
  321. {
  322. GameObject tempObj = Instantiate(listItemOri, listRoot);
  323. tempObj.transform.Find("id").GetComponent<Text>().text = $"{i + 1}";
  324. tempObj.transform.Find("time").GetComponent<Text>().text = $"{tempData.data[i].time}";
  325. tempObj.transform.Find("value").GetComponent<Text>().text = $"{tempData.data[i].value}";
  326. string dirStr = "-";
  327. if (tempData.data[i].trend == 1)
  328. {
  329. dirStr = "↑";
  330. tempObj.transform.Find("trend").GetComponent<Text>().color = Color.red;
  331. }
  332. else if (tempData.data[i].trend == 2)
  333. {
  334. dirStr = "→";
  335. tempObj.transform.Find("trend").GetComponent<Text>().color = Color.blue;
  336. }
  337. else if (tempData.data[i].trend == 3)
  338. {
  339. dirStr = "↓";
  340. tempObj.transform.Find("trend").GetComponent<Text>().color = Color.green;
  341. }
  342. else
  343. {
  344. dirStr = "-";
  345. tempObj.transform.Find("trend").GetComponent<Text>().color = Color.white;
  346. }
  347. tempObj.transform.Find("trend").GetComponent<Text>().text = $"{dirStr}";
  348. listObj.Add(tempObj);
  349. creatCount++;
  350. if (creatCount >= 10)
  351. {
  352. creatCount = 0;
  353. yield return wait;
  354. }
  355. }
  356. }
  357. }
  358. [Serializable]
  359. public class WaterTrendData_Chart
  360. {
  361. public int code;
  362. public string msg;
  363. public List<WaterCharData> data;
  364. }
  365. [Serializable]
  366. public class WaterCharData
  367. {
  368. public string time;
  369. public int trend;
  370. public string value;
  371. }
  372. [Serializable]
  373. public class WaterTrendData_Info
  374. {
  375. public int code;
  376. public string msg;
  377. public WaterInfoData data;
  378. }
  379. [Serializable]
  380. public class WaterInfoData
  381. {
  382. public string id { get; set; }
  383. public string addvcd { get; set; }
  384. public string atcunit { get; set; }
  385. public string bsnm { get; set; }
  386. public string dtmel { get; set; }
  387. public string dtmnm { get; set; }
  388. public string frgrd { get; set; }
  389. public string hnnm { get; set; }
  390. public string lgtd { get; set; }
  391. public string locality { get; set; }
  392. public string lttd { get; set; }
  393. public string rvnm { get; set; }
  394. public string stcd { get; set; }
  395. public string stlc { get; set; }
  396. public string stnm { get; set; }
  397. public string sttp { get; set; }
  398. public string usfl { get; set; }
  399. }