WaterTrendPanel.cs 13 KB

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