WaterTrendPanel.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. public void Init()
  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. currentStcd = stcd;
  28. name = name.Replace(" ", "");
  29. nameText.text = $"{name}站点水位趋势";
  30. this.gameObject.SetActive(true);
  31. string chartJsonStr = await HttpHelper._Instance.GetWaterTrend_Chart(stcd);
  32. SetChartLine(chartJsonStr);
  33. string listJsonStr = await HttpHelper._Instance.GetWaterTrend_List(stcd);
  34. StartCoroutine(CreatList(listJsonStr));
  35. }
  36. public void Hide()
  37. {
  38. this.gameObject.SetActive(false);
  39. }
  40. public void SetChartLine(string jsonData)
  41. {
  42. Debug.Log(jsonData);
  43. WaterTrendData_Chart tempData = Newtonsoft.Json.JsonConvert.DeserializeObject<WaterTrendData_Chart>(jsonData);
  44. XAxis tempXaxis = _LineChart.GetChartComponent<XAxis>();
  45. tempXaxis.data.Clear();
  46. for (int i = 0; i < tempData.data.Count; i++)
  47. {
  48. tempXaxis.AddData(tempData.data[i].key);
  49. }
  50. var tempSeries = _LineChart.series;
  51. SerieData[] tempDatas = new SerieData[tempData.data.Count];
  52. for (int i = 0; i < tempDatas.Length; i++)
  53. {
  54. tempDatas[i] = new SerieData();
  55. tempDatas[i].data = new List<double>();
  56. tempDatas[i].data.Add(i);
  57. tempDatas[i].data.Add(float.Parse(tempData.data[i].value));
  58. }
  59. tempSeries[0].data.Clear();
  60. tempSeries[0].data.AddRange(tempDatas);
  61. }
  62. IEnumerator CreatList(string jsonData)
  63. {
  64. Debug.Log(jsonData);
  65. var wait = new WaitForEndOfFrame();
  66. //todo 接口数据不一定是这个
  67. WaterTrendData_List tempData = Newtonsoft.Json.JsonConvert.DeserializeObject<WaterTrendData_List>(jsonData);
  68. if (listObj.Count > 0)
  69. {
  70. GameObject[] deleteObjs = listObj.ToArray();
  71. for (int i = 0; i < deleteObjs.Length; i++)
  72. {
  73. Destroy(deleteObjs[i]);
  74. }
  75. }
  76. listObj.Clear();
  77. int creatCount = 0;
  78. float lastWaterValue = 0;
  79. for (int i = 0; i < tempData.data.Count; i++)
  80. {
  81. GameObject tempObj = Instantiate(listItemOri, listRoot);
  82. tempObj.transform.Find("id").GetComponent<Text>().text = $"{i + 1}";
  83. tempObj.transform.Find("time").GetComponent<Text>().text = $"{tempData.data[i].updateTime}";
  84. tempObj.transform.Find("value").GetComponent<Text>().text = $"{tempData.data[i].dwz}";
  85. string dirStr = "-";
  86. if (i == 0)
  87. {
  88. dirStr = "→";
  89. lastWaterValue = float.Parse(tempData.data[i].dwz);
  90. }
  91. else
  92. {
  93. float currentWaterValue = float.Parse(tempData.data[i].dwz);
  94. if (currentWaterValue > lastWaterValue)
  95. {
  96. dirStr = "↑";
  97. }
  98. else if (currentWaterValue < lastWaterValue)
  99. {
  100. dirStr = "↓";
  101. }
  102. else
  103. {
  104. dirStr = "→";
  105. }
  106. lastWaterValue = currentWaterValue;
  107. }
  108. tempObj.transform.Find("trend").GetComponent<Text>().text = $"{dirStr}";
  109. listObj.Add(tempObj);
  110. creatCount++;
  111. if (creatCount >= 10)
  112. {
  113. creatCount = 0;
  114. yield return wait;
  115. }
  116. }
  117. }
  118. }
  119. [Serializable]
  120. public class WaterTrendData_Chart
  121. {
  122. public List<WaterCharData> data;
  123. }
  124. [Serializable]
  125. public class WaterCharData
  126. {
  127. public string key;
  128. public string value;
  129. }
  130. [Serializable]
  131. public class WaterTrendData_List
  132. {
  133. public List<WaterTrendData> data;
  134. }
  135. [Serializable]
  136. public class WaterTrendData
  137. {
  138. public string updateTime;
  139. public string dwz;
  140. }