WaterTrendPanel.cs 13 KB

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