| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 | using System;using System.Collections;using System.Collections.Generic;using System.Threading.Tasks;using UnityEngine;using UnityEngine.UI;using XCharts.Runtime;public class WaterTrendPanel : MonoBehaviour{    public LineChart _LineChart;    public Text nameText;    public GameObject listItemOri;    public List<GameObject> listObj = new List<GameObject>();    public Button closeButton;    public string currentStcd;    public Transform listRoot;    public void Init()    {        _LineChart = this.transform.Find("LineChart").GetComponent<LineChart>();        listRoot = this.transform.Find("ScrollView/Viewport/Content").transform;        closeButton = this.transform.Find("CloseButton").GetComponent<Button>();        nameText = this.transform.Find("nameText").GetComponent<Text>();        closeButton.onClick.AddListener(() => { Hide(); });    }    public async Task Show(string stcd, string name)    {        currentStcd = stcd;        name = name.Replace(" ", "");        nameText.text = $"{name}站点水位趋势";        this.gameObject.SetActive(true);        string chartJsonStr = await HttpHelper._Instance.GetWaterTrend_Chart(stcd);        SetChartLine(chartJsonStr);        string listJsonStr = await HttpHelper._Instance.GetWaterTrend_List(stcd);        StartCoroutine(CreatList(listJsonStr));    }    public void Hide()    {        this.gameObject.SetActive(false);    }    public void SetChartLine(string jsonData)    {        Debug.Log(jsonData);        WaterTrendData_Chart tempData = Newtonsoft.Json.JsonConvert.DeserializeObject<WaterTrendData_Chart>(jsonData);        XAxis tempXaxis = _LineChart.GetChartComponent<XAxis>();        tempXaxis.data.Clear();        for (int i = 0; i < tempData.data.Count; i++)        {            tempXaxis.AddData(tempData.data[i].key);        }        var tempSeries = _LineChart.series;        SerieData[] tempDatas = new SerieData[tempData.data.Count];        for (int i = 0; i < tempDatas.Length; i++)        {            tempDatas[i] = new SerieData();            tempDatas[i].data = new List<double>();            tempDatas[i].data.Add(i);            tempDatas[i].data.Add(float.Parse(tempData.data[i].value));        }        tempSeries[0].data.Clear();        tempSeries[0].data.AddRange(tempDatas);    }    IEnumerator CreatList(string jsonData)    {        Debug.Log(jsonData);        var wait = new WaitForEndOfFrame();        WaterTrendData_List tempData = Newtonsoft.Json.JsonConvert.DeserializeObject<WaterTrendData_List>(jsonData);        if (listObj.Count > 0)        {            GameObject[] deleteObjs = listObj.ToArray();            for (int i = 0; i < deleteObjs.Length; i++)            {                Destroy(deleteObjs[i]);            }        }        listObj.Clear();        int creatCount = 0;        float lastWaterValue = 0;        for (int i = 0; i < tempData.data.Count; i++)        {            GameObject tempObj = Instantiate(listItemOri, listRoot);            tempObj.transform.Find("id").GetComponent<Text>().text = $"{i + 1}";            tempObj.transform.Find("time").GetComponent<Text>().text = $"{tempData.data[i].updateTime}";            tempObj.transform.Find("value").GetComponent<Text>().text = $"{tempData.data[i].dwz}";            string dirStr = "-";            if (i == 0)            {                dirStr = "→";                lastWaterValue = float.Parse(tempData.data[i].dwz);            }            else            {                float currentWaterValue = float.Parse(tempData.data[i].dwz);                if (currentWaterValue > lastWaterValue)                {                    dirStr = "↑";                }                else if (currentWaterValue < lastWaterValue)                {                    dirStr = "↓";                }                else                {                    dirStr = "→";                }                lastWaterValue = currentWaterValue;            }            tempObj.transform.Find("trend").GetComponent<Text>().text = $"{dirStr}";            listObj.Add(tempObj);            creatCount++;            if (creatCount >= 10)            {                creatCount = 0;                yield return wait;            }        }    }}[Serializable]public class WaterTrendData_Chart{    public List<WaterCharData> data;}[Serializable]public class WaterCharData{    public string key;    public string value;}[Serializable]public class WaterTrendData_List{    public List<WaterTrendData> data;}[Serializable]public class WaterTrendData{    public string updateTime;    public string dwz;}
 |