WaterTrendPanel.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Threading.Tasks;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. using XCharts.Runtime;
  8. public class WaterTrendPanel : MonoBehaviour
  9. {
  10. public LineChart _LineChart;
  11. public Text nameText;
  12. public GameObject listItemOri;
  13. public List<GameObject> listObj = new List<GameObject>();
  14. public Button closeButton;
  15. public string currentStcd;
  16. public Transform listRoot;
  17. private void Awake()
  18. {
  19. _LineChart = this.transform.Find("LineChart").GetComponent<LineChart>();
  20. listRoot = this.transform.Find("ScrollView/Viewport/Content").transform;
  21. closeButton = this.transform.Find("CloseButton").GetComponent<Button>();
  22. nameText = this.transform.Find("nameText").GetComponent<Text>();
  23. closeButton.onClick.AddListener(() => { Hide(); });
  24. }
  25. public async Task Show(string stcd, string name)
  26. {
  27. //todo 先写死了
  28. stcd = "61017467";
  29. currentStcd = stcd;
  30. name = name.Replace(" ", "");
  31. nameText.text = $"{name}站点水位趋势";
  32. this.gameObject.SetActive(true);
  33. string chartJsonStr = await HttpHelper._Instance.GetWaterTrend_Chart(stcd);
  34. SetChartLine(chartJsonStr);
  35. string listJsonStr = await HttpHelper._Instance.GetWaterTrend_List(stcd);
  36. StartCoroutine(CreatList(listJsonStr));
  37. }
  38. public void Hide()
  39. {
  40. this.gameObject.SetActive(false);
  41. }
  42. public void SetChartLine(string jsonData)
  43. {
  44. Debug.Log(jsonData);
  45. WaterTrendData_Chart tempData = Newtonsoft.Json.JsonConvert.DeserializeObject<WaterTrendData_Chart>(jsonData);
  46. XAxis tempXaxis = _LineChart.GetChartComponent<XAxis>();
  47. tempXaxis.data.Clear();
  48. for (int i = 0; i < tempData.data.Count; i++)
  49. {
  50. tempXaxis.AddData(tempData.data[i].key);
  51. }
  52. var tempSeries = _LineChart.series;
  53. SerieData[] tempDatas = new SerieData[tempData.data.Count];
  54. for (int i = 0; i < tempDatas.Length; i++)
  55. {
  56. tempDatas[i] = new SerieData();
  57. tempDatas[i].data = new List<double>();
  58. tempDatas[i].data.Add(i);
  59. tempDatas[i].data.Add(float.Parse(tempData.data[i].value));
  60. }
  61. tempSeries[0].data.Clear();
  62. tempSeries[0].data.AddRange(tempDatas);
  63. }
  64. IEnumerator CreatList(string jsonData)
  65. {
  66. Debug.Log(jsonData);
  67. var wait = new WaitForEndOfFrame();
  68. //todo 接口数据不一定是这个
  69. WaterTrendData_List tempData = Newtonsoft.Json.JsonConvert.DeserializeObject<WaterTrendData_List>(jsonData);
  70. if (listObj.Count > 0)
  71. {
  72. GameObject[] deleteObjs = listObj.ToArray();
  73. for (int i = 0; i < deleteObjs.Length; i++)
  74. {
  75. Destroy(deleteObjs[i]);
  76. }
  77. }
  78. listObj.Clear();
  79. int creatCount = 0;
  80. float lastWaterValue = 0;
  81. for (int i = 0; i < tempData.data.Count; i++)
  82. {
  83. GameObject tempObj = Instantiate(listItemOri, listRoot);
  84. tempObj.transform.Find("id").GetComponent<Text>().text = $"{i + 1}";
  85. tempObj.transform.Find("time").GetComponent<Text>().text = $"{tempData.data[i].updateTime}";
  86. tempObj.transform.Find("value").GetComponent<Text>().text = $"{tempData.data[i].dwz}";
  87. string dirStr = "-";
  88. if (i == 0)
  89. {
  90. dirStr = "→";
  91. lastWaterValue = float.Parse(tempData.data[i].dwz);
  92. }
  93. else
  94. {
  95. float currentWaterValue = float.Parse(tempData.data[i].dwz);
  96. if (currentWaterValue > lastWaterValue)
  97. {
  98. dirStr = "↑";
  99. }
  100. else if (currentWaterValue < lastWaterValue)
  101. {
  102. dirStr = "↓";
  103. }
  104. else
  105. {
  106. dirStr = "→";
  107. }
  108. lastWaterValue = currentWaterValue;
  109. }
  110. tempObj.transform.Find("trend").GetComponent<Text>().text = $"{dirStr}";
  111. listObj.Add(tempObj);
  112. creatCount++;
  113. if (creatCount >= 10)
  114. {
  115. creatCount = 0;
  116. yield return wait;
  117. }
  118. }
  119. }
  120. }
  121. [Serializable]
  122. public class WaterTrendData_Chart
  123. {
  124. public List<WaterCharData> data;
  125. }
  126. [Serializable]
  127. public class WaterCharData
  128. {
  129. public string key;
  130. public string value;
  131. }
  132. [Serializable]
  133. public class WaterTrendData_List
  134. {
  135. public List<WaterTrendData> data;
  136. }
  137. [Serializable]
  138. public class WaterTrendData
  139. {
  140. public string updateTime;
  141. public string dwz;
  142. }