WaterTrendPanel.cs 14 KB

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