WaterTrendPanel.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. string endTime = DateTime.Now.ToString("yyyy-MM-dd");
  185. string startTime = DateTime.Now.AddMonths(-3).ToString("yyyy-MM-dd");
  186. startDateStr = startTime;
  187. endDateStr = endTime;
  188. DatePicker_Start.SetLabelText(startTime);
  189. DatePicker_End.SetLabelText(endTime);
  190. currentType = type;
  191. if (type == 0)
  192. {
  193. threePanel.gameObject.SetActive(true);
  194. typeTitle.text = "站点水位趋势";
  195. }
  196. else {
  197. threePanel.gameObject.SetActive(false);
  198. typeTitle.text = "站点降雨趋势";
  199. }
  200. currentStcd = stcd;
  201. Debug.Log(stcd);
  202. currentName = name;
  203. ChangePanel(0);
  204. name = name.Replace(" ", "");
  205. nameText.text = $"站点:{name}";
  206. this.gameObject.SetActive(true);
  207. string chartJsonStr = "";
  208. if (type == 0) {
  209. _LineChart.GetChartComponent<YAxis>().axisName.name = "水位(m)";
  210. chartJsonStr = await HttpHelper._Instance.GetWaterTrend_Chart(stcd, startDateStr, endDateStr);
  211. }
  212. else{
  213. _LineChart.GetChartComponent<YAxis>().axisName.name = "降雨量(mm)";
  214. chartJsonStr = await HttpHelper._Instance.GetDropTrend_Chart(stcd, startDateStr, endDateStr);
  215. }
  216. if (stcd == "61017810")
  217. {
  218. bythreePanel.gameObject.SetActive(true);
  219. taokouthreePanel.gameObject.SetActive(false);
  220. }
  221. else {
  222. bythreePanel.gameObject.SetActive(false);
  223. taokouthreePanel.gameObject.SetActive(true);
  224. }
  225. SetChartLine(chartJsonStr);
  226. StartCoroutine(CreatList(chartJsonStr));
  227. }
  228. public async Task ShowInfo()
  229. {
  230. idText.text = "-";
  231. stnmText.text = "-";
  232. stcdText.text = "-";
  233. stlcText.text = "-";
  234. sttpText.text = "-";
  235. addvcdText.text = "-";
  236. bsnmText.text = "-";
  237. hnnmText.text = "-";
  238. rvnmText.text = "-";
  239. lgtdText.text = "-";
  240. lttdText.text = "-";
  241. dtmnmText.text = "-";
  242. dtmelText.text = "-";
  243. atcunitText.text = "-";
  244. localityText.text = "-";
  245. frgrdText.text = "-";
  246. usflText.text = "-";
  247. string infoJsonStr = await HttpHelper._Instance.GetWaterTrend_Info(currentStcd);
  248. Debug.Log(infoJsonStr);
  249. WaterTrendData_Info waterTrendData_Info = JsonConvert.DeserializeObject<WaterTrendData_Info>(infoJsonStr);
  250. Debug.Log(waterTrendData_Info);
  251. idText.text = waterTrendData_Info.data.id;
  252. stnmText.text = waterTrendData_Info.data.stnm;
  253. stcdText.text = waterTrendData_Info.data.stcd;
  254. stlcText.text = waterTrendData_Info.data.stlc;
  255. sttpText.text = waterTrendData_Info.data.sttp;
  256. addvcdText.text = waterTrendData_Info.data.addvcd;
  257. bsnmText.text = waterTrendData_Info.data.bsnm;
  258. hnnmText.text = waterTrendData_Info.data.hnnm;
  259. rvnmText.text = waterTrendData_Info.data.rvnm;
  260. lgtdText.text = waterTrendData_Info.data.lgtd;
  261. lttdText.text = waterTrendData_Info.data.lttd;
  262. dtmnmText.text = waterTrendData_Info.data.dtmnm;
  263. dtmelText.text = waterTrendData_Info.data.dtmel;
  264. atcunitText.text = waterTrendData_Info.data.atcunit;
  265. localityText.text = waterTrendData_Info.data.locality;
  266. frgrdText.text = waterTrendData_Info.data.frgrd;
  267. usflText.text = waterTrendData_Info.data.usfl;
  268. }
  269. public void Hide()
  270. {
  271. this.gameObject.SetActive(false);
  272. }
  273. public void SetChartLine(string jsonData)
  274. {
  275. WaterTrendData_Chart tempData = Newtonsoft.Json.JsonConvert.DeserializeObject<WaterTrendData_Chart>(jsonData);
  276. tempData.data.Reverse();
  277. XAxis tempXaxis = _LineChart.GetChartComponent<XAxis>();
  278. tempXaxis.data.Clear();
  279. for (int i = 0; i < tempData.data.Count; i++)
  280. {
  281. tempXaxis.AddData(tempData.data[i].time);
  282. }
  283. var tempSeries = _LineChart.series;
  284. tempSeries[0].serieName = "水位(m)";
  285. SerieData[] tempDatas = new SerieData[tempData.data.Count];
  286. for (int i = 0; i < tempDatas.Length; i++)
  287. {
  288. tempDatas[i] = new SerieData();
  289. tempDatas[i].data = new List<double>();
  290. tempDatas[i].data.Add(i);
  291. string valueTex = tempData.data[i].value;
  292. if (valueTex == "-") {
  293. valueTex = "-1";
  294. }
  295. double tempValue = double.Parse(float.Parse(valueTex).ToString("0.00"));
  296. //if (tempValue < 0) {
  297. // tempValue = 24.99f;
  298. //}
  299. tempDatas[i].data.Add(tempValue);
  300. }
  301. tempSeries[0].data.Clear();
  302. tempSeries[0].data.AddRange(tempDatas);
  303. }
  304. IEnumerator CreatList(string jsonData)
  305. {
  306. var wait = new WaitForEndOfFrame();
  307. WaterTrendData_Chart tempData = Newtonsoft.Json.JsonConvert.DeserializeObject<WaterTrendData_Chart>(jsonData);
  308. if (listObj.Count > 0)
  309. {
  310. GameObject[] deleteObjs = listObj.ToArray();
  311. for (int i = 0; i < deleteObjs.Length; i++)
  312. {
  313. Destroy(deleteObjs[i]);
  314. }
  315. }
  316. listObj.Clear();
  317. int creatCount = 0;
  318. for (int i = 0; i < tempData.data.Count; i++)
  319. {
  320. GameObject tempObj = Instantiate(listItemOri, listRoot);
  321. tempObj.transform.Find("id").GetComponent<Text>().text = $"{i + 1}";
  322. tempObj.transform.Find("time").GetComponent<Text>().text = $"{tempData.data[i].time}";
  323. tempObj.transform.Find("value").GetComponent<Text>().text = $"{tempData.data[i].value}";
  324. string dirStr = "-";
  325. if (tempData.data[i].trend == 1)
  326. {
  327. dirStr = "↑";
  328. tempObj.transform.Find("trend").GetComponent<Text>().color = Color.red;
  329. }
  330. else if (tempData.data[i].trend == 2)
  331. {
  332. dirStr = "→";
  333. tempObj.transform.Find("trend").GetComponent<Text>().color = Color.blue;
  334. }
  335. else if (tempData.data[i].trend == 3)
  336. {
  337. dirStr = "↓";
  338. tempObj.transform.Find("trend").GetComponent<Text>().color = Color.green;
  339. }
  340. else
  341. {
  342. dirStr = "-";
  343. tempObj.transform.Find("trend").GetComponent<Text>().color = Color.white;
  344. }
  345. tempObj.transform.Find("trend").GetComponent<Text>().text = $"{dirStr}";
  346. listObj.Add(tempObj);
  347. creatCount++;
  348. if (creatCount >= 10)
  349. {
  350. creatCount = 0;
  351. yield return wait;
  352. }
  353. }
  354. }
  355. }
  356. [Serializable]
  357. public class WaterTrendData_Chart
  358. {
  359. public int code;
  360. public string msg;
  361. public List<WaterCharData> data;
  362. }
  363. [Serializable]
  364. public class WaterCharData
  365. {
  366. public string time;
  367. public int trend;
  368. public string value;
  369. }
  370. [Serializable]
  371. public class WaterTrendData_Info
  372. {
  373. public int code;
  374. public string msg;
  375. public WaterInfoData data;
  376. }
  377. [Serializable]
  378. public class WaterInfoData
  379. {
  380. public string id { get; set; }
  381. public string addvcd { get; set; }
  382. public string atcunit { get; set; }
  383. public string bsnm { get; set; }
  384. public string dtmel { get; set; }
  385. public string dtmnm { get; set; }
  386. public string frgrd { get; set; }
  387. public string hnnm { get; set; }
  388. public string lgtd { get; set; }
  389. public string locality { get; set; }
  390. public string lttd { get; set; }
  391. public string rvnm { get; set; }
  392. public string stcd { get; set; }
  393. public string stlc { get; set; }
  394. public string stnm { get; set; }
  395. public string sttp { get; set; }
  396. public string usfl { get; set; }
  397. }