WaterTrendPanel.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. var tempSeries = _LineChart.series;
  47. SerieData[] tempDatas = new SerieData[tempData.data.Count];
  48. for (int i = 0; i < tempDatas.Length; i++)
  49. {
  50. tempDatas[i] = new SerieData();
  51. tempDatas[i].data = new List<double>();
  52. tempDatas[i].data.Add(i);
  53. tempDatas[i].data.Add(tempData.data[i].value);
  54. }
  55. tempSeries[0].data.Clear();
  56. tempSeries[0].data.AddRange(tempDatas);
  57. XAxis tempXaxis = _LineChart.GetComponent<XAxis>();
  58. tempXaxis.data.Clear();
  59. for (int i = 0; i < tempData.data.Count; i++)
  60. {
  61. tempXaxis.AddData(tempData.data[i].key);
  62. }
  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. for (int i = 0; i < tempData.data.Count; i++)
  81. {
  82. GameObject tempObj = Instantiate(listItemOri, listRoot);
  83. tempObj.transform.Find("id").GetComponent<Text>().text = $"{i + 1}";
  84. tempObj.transform.Find("time").GetComponent<Text>().text = $"{tempData.data[i].time}";
  85. tempObj.transform.Find("value").GetComponent<Text>().text = $"{tempData.data[i].time}";
  86. string dirStr = "-";
  87. switch (tempData.data[i].dir)
  88. {
  89. case 0:
  90. dirStr = "→";
  91. break;
  92. case 1:
  93. dirStr = "↑";
  94. break;
  95. default:
  96. dirStr = "↓";
  97. break;
  98. }
  99. tempObj.transform.Find("trend").GetComponent<Text>().text = $"{dirStr}";
  100. listObj.Add(tempObj);
  101. creatCount++;
  102. if (creatCount >= 10)
  103. {
  104. creatCount = 0;
  105. yield return wait;
  106. }
  107. }
  108. }
  109. }
  110. [Serializable]
  111. public class WaterTrendData_Chart
  112. {
  113. public List<CharData_item> data;
  114. }
  115. [Serializable]
  116. public class WaterTrendData_List
  117. {
  118. public List<SWStationRecordData> data;
  119. }