WaterTrendPanel.cs 14 KB

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