WaterTrendPanel.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. WaterTrendData_List tempData = Newtonsoft.Json.JsonConvert.DeserializeObject<WaterTrendData_List>(jsonData);
  67. if (listObj.Count > 0)
  68. {
  69. GameObject[] deleteObjs = listObj.ToArray();
  70. for (int i = 0; i < deleteObjs.Length; i++)
  71. {
  72. Destroy(deleteObjs[i]);
  73. }
  74. }
  75. listObj.Clear();
  76. int creatCount = 0;
  77. float lastWaterValue = 0;
  78. for (int i = 0; i < tempData.data.Count; i++)
  79. {
  80. GameObject tempObj = Instantiate(listItemOri, listRoot);
  81. tempObj.transform.Find("id").GetComponent<Text>().text = $"{i + 1}";
  82. tempObj.transform.Find("time").GetComponent<Text>().text = $"{tempData.data[i].updateTime}";
  83. tempObj.transform.Find("value").GetComponent<Text>().text = $"{tempData.data[i].dwz}";
  84. string dirStr = "-";
  85. if (i == 0)
  86. {
  87. dirStr = "→";
  88. lastWaterValue = float.Parse(tempData.data[i].dwz);
  89. }
  90. else
  91. {
  92. float currentWaterValue = float.Parse(tempData.data[i].dwz);
  93. if (currentWaterValue > lastWaterValue)
  94. {
  95. dirStr = "↑";
  96. }
  97. else if (currentWaterValue < lastWaterValue)
  98. {
  99. dirStr = "↓";
  100. }
  101. else
  102. {
  103. dirStr = "→";
  104. }
  105. lastWaterValue = currentWaterValue;
  106. }
  107. tempObj.transform.Find("trend").GetComponent<Text>().text = $"{dirStr}";
  108. listObj.Add(tempObj);
  109. creatCount++;
  110. if (creatCount >= 10)
  111. {
  112. creatCount = 0;
  113. yield return wait;
  114. }
  115. }
  116. }
  117. }
  118. [Serializable]
  119. public class WaterTrendData_Chart
  120. {
  121. public List<WaterCharData> data;
  122. }
  123. [Serializable]
  124. public class WaterCharData
  125. {
  126. public string key;
  127. public string value;
  128. }
  129. [Serializable]
  130. public class WaterTrendData_List
  131. {
  132. public List<WaterTrendData> data;
  133. }
  134. [Serializable]
  135. public class WaterTrendData
  136. {
  137. public string updateTime;
  138. public string dwz;
  139. }