| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337 | using System;using System.Collections;using System.Collections.Generic;using System.Globalization;using System.Threading.Tasks;using Bitsplash.DatePicker;using Newtonsoft.Json;using UnityEngine;using UnityEngine.UI;using XCharts.Runtime;public class DeviceTrendPanel : MonoBehaviour{    public RectTransform rect;    public LineChart _LineChart;    public Text nameText;    public GameObject listItemOri;    public List<GameObject> listObj = new List<GameObject>();    public Transform listRoot;    public Button closeButton;    public Button resetButton;    public Button checkButton;        public Text tipsText;    public DatePickerDropDownTextMeshPro DatePicker_Start;    public DatePickerDropDownTextMeshPro DatePicker_End;    public string startDateStr = "";    public string endDateStr = "";    public int currentType = 0; //0补元 1套口    public string _sid;    public string _gid;    public string dataName;        private void Awake()    {        rect = this.GetComponent<RectTransform>();        _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>();        checkButton = this.transform.Find("CheckButton").GetComponent<Button>();        tipsText = this.transform.Find("tipsText").GetComponent<Text>();        tipsText.enabled = false;        checkButton.onClick.AddListener(() => { GetData(); });        closeButton.onClick.AddListener(() =>        {            Hide();            // if (GCJKLayer._Instance.currentSelectItem != null)            // {            //     GCJKLayer._Instance.currentSelectItem.SetHighLight(false);            //     GCJKLayer._Instance.currentSelectItem = null;            // }        });    }    public void InitDatePicker()    {        DatePicker_Start = this.transform.Find("DatePicker_Start").GetComponent<DatePickerDropDownTextMeshPro>();        DatePicker_End = this.transform.Find("DatePicker_End").GetComponent<DatePickerDropDownTextMeshPro>();        DatePicker_Start.GetDateString += (string dateStr) =>        {            startDateStr = dateStr;            CheckDate();        };        DatePicker_End.GetDateString += (string dateStr) =>        {            endDateStr = dateStr;            CheckDate();        };    }    public void CheckDate()    {        if (!startDateStr.Equals("") && !endDateStr.Equals(""))        {            var startD = DateTime.ParseExact(startDateStr, "yyyy-MM-dd", null);            var endD = DateTime.ParseExact(endDateStr, "yyyy-MM-dd", null);            var disD = endD - startD;            Debug.Log(disD.TotalDays);            if (disD.TotalDays > 183)            {                Debug.Log("时间间隔大于6个月");                checkButton.gameObject.SetActive(false);                tipsText.enabled = true;            }            else            {                tipsText.enabled = false;                checkButton.gameObject.SetActive(true);            }        }    }    public void OnDrag()    {        var mousePos = Input.mousePosition;        mousePos.x -= Screen.width * 0.5f;        mousePos.y -= Screen.height * 0.5f;        rect.anchoredPosition = mousePos;    }        public async Task GetData()    {        string getJsonStr =            await HttpHelper._Instance.GetDeviceTrend(currentType, _gid, _sid, startDateStr, endDateStr);        GetTrend(getJsonStr);    }    private GongChengType currentGongChengType;        public async Task Show(string _name, GongChengType type, string gid, string sid)    {        dataName = _name;        _gid = gid;        _sid = sid;        _name = _name.Replace(" ", "");        string typeText = "";        currentGongChengType = type;        switch (currentGongChengType)        {            case GongChengType.shuiWei:                typeText = "水位/m";                break;            case GongChengType.shenYaJi:                typeText = "渗压/KPa";                break;            case GongChengType.jingLiShuiZhunYi:                typeText = "位移/mm";                break;            case GongChengType.wenDu:                typeText = "温度/\u2103";                break;            case GongChengType.yinZhangXianYi:                typeText = "位移/mm";                break;            case GongChengType.chuiXianZuoBiaoYi:                typeText = "位移/mm";                break;            case GongChengType.tuYaLiJi:                typeText = "应力/MPa";                break;            case GongChengType.yingLiJi:                typeText = "应力/MPa";                break;            case GongChengType.sanXiangWeiCuoJi:                typeText = "位移/mm";                break;            case GongChengType.erXiangCeFengJi:                typeText = "位移/mm";                break;            case GongChengType.weiYiJi:                typeText = "位移/mm";                break;        }        YAxis tempXaxis = _LineChart.GetChartComponent<YAxis>();        var tempSeries = _LineChart.series;        tempSeries[0].serieName = typeText;        tempXaxis.axisName.name = typeText+" "+DateTime.Now.ToString("yyyy");        nameText.text = $"{_name} {typeText}趋势";        string endTime = DateTime.Now.ToString("yyyy-MM-dd");        string startTime = DateTime.Now.AddMonths(-3).ToString("yyyy-MM-dd");        startDateStr = startTime;        endDateStr = endTime;        DatePicker_Start.SetLabelText(startTime);        DatePicker_End.SetLabelText(endTime);        string getJsonStr = await HttpHelper._Instance.GetDeviceTrend(currentType, gid, sid, startTime, endTime);        GetTrend(getJsonStr);    }    public void Hide()    {        GCJKLayer._Instance.RemovePanel(this);        DestroyImmediate(this.gameObject);    }    /// <summary>    /// 获取时间范围内的趋势图    /// </summary>    public void GetTrend(string jsonStr)    {        try        {            Debug.Log("GetTrend requestData:" + jsonStr);            DeviceTrendRequest requestData = JsonConvert.DeserializeObject<DeviceTrendRequest>(jsonStr);            XAxis tempXaxis = _LineChart.GetChartComponent<XAxis>();            tempXaxis.data.Clear();            var finalData = new DeviceTrendData[requestData.data.Length];            for (int i = 0; i < finalData.Length; i++)            {                finalData[i] = requestData.data[requestData.data.Length - 1 - i];            }                        for (int i = 0; i < finalData.Length; i++)            {                var tempTimeDate = DateTime.ParseExact(finalData[i].timeGroup, "yyyy-MM-dd HH:mm:ss",                    CultureInfo.InvariantCulture);                tempXaxis.AddData(tempTimeDate.ToString("MM-dd"));            }                        var tempSeries = _LineChart.series;            SerieData[] tempDatas = new SerieData[finalData.Length];                        string typeText = "";            switch (currentGongChengType)            {                case GongChengType.shuiWei:                    typeText = "水位/m";                    break;                case GongChengType.shenYaJi:                    typeText = "渗压/KPa";                    break;                case GongChengType.jingLiShuiZhunYi:                    typeText = "位移/mm";                    break;                case GongChengType.wenDu:                    typeText = "温度/\u2103";                    break;                case GongChengType.yinZhangXianYi:                    typeText = "位移/mm";                    break;                case GongChengType.chuiXianZuoBiaoYi:                    typeText = "位移/mm";                    break;                case GongChengType.tuYaLiJi:                    typeText = "应力/MPa";                    break;                case GongChengType.yingLiJi:                    typeText = "应力/MPa";                    break;                case GongChengType.sanXiangWeiCuoJi:                    typeText = "位移/mm";                    break;                case GongChengType.erXiangCeFengJi:                    typeText = "位移/mm";                    break;                case GongChengType.weiYiJi:                    typeText = "位移/mm";                    break;            }            tempSeries[0].serieName = typeText;                        string startTimeStr=DateTime.Parse(startDateStr).ToString("yyyy");            string endTimeStr=DateTime.Parse(endDateStr).ToString("yyyy");            string finalStr = startTimeStr.Equals(endDateStr) ? startTimeStr : $"{startTimeStr}-{endTimeStr}";            tempXaxis.axisName.name = typeText+" "+finalStr;                        for (int i = 0; i < tempDatas.Length; i++)            {                tempDatas[i] = new SerieData();                tempDatas[i].data = new List<double>();                tempDatas[i].data.Add(i);                //Debug.Log(requestData.data[i].r1);                double tempValue = double.Parse(float.Parse(finalData[i].r1).ToString("0.00"));                                //Debug.Log(tempValue * 100);                tempDatas[i].data.Add(tempValue);            }            tempSeries[0].data.Clear();            tempSeries[0].data.AddRange(tempDatas);            StartCoroutine(CreatList(requestData,typeText));        }        catch (Exception e)        {            Debug.LogError(e.ToString());            throw;        }    }    IEnumerator CreatList(DeviceTrendRequest deviceData,string unit)    {        var wait = new WaitForEndOfFrame();        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 < deviceData.data.Length; i++)        {            float value = float.Parse(float.Parse(deviceData.data[i].r1).ToString("F2"));            GameObject tempObj = Instantiate(listItemOri, listRoot);            tempObj.transform.Find("unit").GetComponent<Text>().text = $"{unit}";            tempObj.transform.Find("time").GetComponent<Text>().text = $"{deviceData.data[i].timeGroup}";            tempObj.transform.Find("value").GetComponent<Text>().text = $"{value}";            string dirStr = "-";            if (i == 0)            {                dirStr = "→";                lastWaterValue = value;            }            else            {                float currentWaterValue = value;                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;            }        }    }}
 |