unity3d:折线图,读取csv数据,每s生成一段线

1.读取csv表数据,点的数据随机,在3000以上
2.x方向,y方向的最大值根据表读取
3.每1s显示当前的点的数据
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

using System.Collections;
using System.Collections.Generic;
using SpringGUI;
using UnityEngine;
using Vectrosity;

public class ChartMgr : MonoBehaviour {


    public class TestData
    {
        public float xValue { get; set; }
        public float yValue { get; set; }

        public TestData(float x, float y)
        {
            xValue = x;
            yValue = y;
        }
    }

    public LineChart LineChart = null;

    public LineChart LineChart1 = null; //第二张表
    public List<TestData> m_listData = new List<TestData>();

    public List<TestData> m_listData0 = new List<TestData>();

    public List<TestData> m_listData1 = new List<TestData>();
    public List<TestData> m_listData2 = new List<TestData>();
    public List<TestData> m_listData3 = new List<TestData>();
    public List<float> m_listTime = new List<float>();
    float m_maxTime = 0;
    float m_maxData0 = 0;//表1 的最大值
    float m_maxData1 = 0;//表2 的最大值
    float m_time = 0;

    Coroutine m_cor0;
    Coroutine m_cor1;
    Coroutine m_cor2;
    Coroutine m_cor3;

    private VectorLine line0;
    private VectorLine line1;
    private VectorLine line2;
    private VectorLine line3;

    public Camera m_cam;
    public void Awake()
    {



    }

    private void Start()
    {
        //SetLine(line0, new Color32(255, 237, 0, 235));
        //SetLine(line1, new Color32(73, 223, 255, 235));
        //SetLine(line2, new Color32(255, 237, 0, 235));

        //SetLine(line3, new Color32(73, 223, 255, 235));
    }
    VectorLine SetLine(VectorLine line, Color32 color,string name)
    {
        VectorLine.Destroy(ref line);

        line = new VectorLine(name, new List<Vector2>(), 2, LineType.Continuous, Joins.Fill);
        line.drawTransform = transform;
        line.color = color;
        return line;

    }

    public void ShowChart()
    {
        line0 = SetLine(line0, new Color32(255, 237, 0, 235), "line0");
        line1 = SetLine(line1, new Color32(73, 223, 255, 235), "line1");
        line2 = SetLine(line2, new Color32(255, 237, 0, 235), "line2");
        line3 = SetLine(line3, new Color32(73, 223, 255, 235), "line3");

        LineChart.RemoveLine(new int[] { 0,1});
        LineChart1.RemoveLine(new int[] { 0, 1 });
        ReadData();
        List<TestData> listData = new List<TestData>();
        LineChart.Inject<TestData>(listData);
        LineChart.Inject<TestData>(listData);

        LineChart1.Inject<TestData>(listData);
        LineChart1.Inject<TestData>(listData);

        float width = LineChart.GetComponent<RectTransform>().sizeDelta.x;
        float height = LineChart.GetComponent<RectTransform>().sizeDelta.y;
        LineChart.m_unitXNum = 10;
        LineChart.m_unitYNum = 500;
        LineChart.LineChartBasis.MeshCellXSize = width / m_maxTime * LineChart.m_unitXNum;
        LineChart.LineChartBasis.MeshCellYSize = height / m_maxData0 * LineChart.m_unitYNum;

        float width1 = LineChart1.GetComponent<RectTransform>().sizeDelta.x;
        float height1 = LineChart.GetComponent<RectTransform>().sizeDelta.y;
        LineChart1.m_unitXNum = 10;
        LineChart1.m_unitYNum = 50;
        LineChart1.LineChartBasis.MeshCellXSize = width1 / m_maxTime * LineChart1.m_unitXNum;
        LineChart1.LineChartBasis.MeshCellYSize = height1 / m_maxData1 * LineChart1.m_unitYNum;


        LineChart.ShowUnit();
        LineChart1.ShowUnit();
        m_time = Time.time;
        if (m_cor0 != null)
        {
            StopCoroutine(m_cor0);
        }
        m_cor0 = StartCoroutine(YieldCreateLineNew(LineChart, line0, m_listData0));

        if (m_cor1 != null)
        {
            StopCoroutine(m_cor1);
        }
        m_cor1 = StartCoroutine(YieldCreateLineNew(LineChart, line1, m_listData1));

        if (m_cor2 != null)
        {
            StopCoroutine(m_cor2);
        }
        m_cor2 = StartCoroutine(YieldCreateLineNew(LineChart1, line2, m_listData2));

        if (m_cor3 != null)
        {
            StopCoroutine(m_cor3);
        }
        m_cor3 = StartCoroutine(YieldCreateLineNew(LineChart1, line3, m_listData3));
    }

    IEnumerator YieldCreateLineVec(LineChart lineChart, VectorLine line, List<TestData> listDataTrue)
    {
        float i = 0;
        List<TestData> listData = new List<TestData>();
        int idx = 0;//当前索引
        float step = 1.0f;
        //Vector2 charPos = Wrold2ScreenPos(m_cam, LineChart.transform.position);
        Vector2 charPos = LineChart.transform.position;
        for (int cnt = 0; cnt < listDataTrue.Count; cnt++)
        {
            Vector2 offset = GetPos(new Vector2(listDataTrue[cnt].xValue, listDataTrue[cnt].yValue), lineChart.GetComponent<RectTransform>());
            Vector2 screenPos = offset + charPos - new Vector2(Screen.width/2,Screen.height/2);
            line.points2.Add(screenPos);
            if (line.points2.Count >= 2)
            {
                //line.points2[line.points2.Count - 1] = mousePos;
                line.Draw();
            }

            if (cnt < listDataTrue.Count - 1)
            {
                yield return new WaitForSecondsRealtime(m_listTime[cnt]);
            }
        }

        

        float totalTime = Time.time - m_time;
        Debug.Log(totalTime);
        yield return null;
    }

    IEnumerator YieldCreateLineNew(LineChart lineChartTmp, VectorLine line, List<TestData> listDataTrue)
    {
        float i = 0;
        List<TestData> listData = new List<TestData>();
        int idx = 0;//当前索引
        float step = 1.0f;
        Vector2 charPos = lineChartTmp.transform.position;
        while (i < m_maxTime)
        {


            for (; idx < listDataTrue.Count; idx++)
            {
                if (listDataTrue[idx].xValue * m_maxTime >= i && listDataTrue[idx].xValue * m_maxTime <= i + step)
                {
                    Vector2 offset = GetPos(new Vector2(listDataTrue[idx].xValue, listDataTrue[idx].yValue), lineChartTmp.GetComponent<RectTransform>());
                    Vector2 screenPos = offset + charPos - new Vector2(Screen.width / 2, Screen.height / 2);
                    line.points2.Add(screenPos);

                }
                if (listDataTrue[idx].xValue * m_maxTime > i + step)
                {
                    break;
                }
            }
           
            if (line.points2.Count >= 2)
            {
                //line.points2[line.points2.Count - 1] = mousePos;
                line.Draw();
            }

            i += step;
            yield return new WaitForSeconds(step);
        }
        float totalTime = Time.time - m_time;
        Debug.Log(totalTime);
    }

    IEnumerator YieldCreateLine(LineChart lineChart, int id, List<TestData> listDataTrue)
    {
        float i = 0;
        List<TestData> listData = new List<TestData>();
        int idx = 0;//当前索引
        float step = 1.0f;

        while (i < m_maxTime)
        {
 
            
            for (; idx < listDataTrue.Count; idx++)
            {
                if (listDataTrue[idx].xValue * m_maxTime >= i && listDataTrue[idx].xValue * m_maxTime <= i + step)
                {
                    listData.Add(listDataTrue[idx]);
                }
                if (listDataTrue[idx].xValue * m_maxTime > i + step)
                {
                    break;
                }
            }
            lineChart.Replace(id, listData);
            i += step;
            yield return new WaitForSeconds(step);
        }
        float totalTime = Time.time - m_time;
        Debug.Log(totalTime);

    }


    public void ReadData()
    {
        m_listData0.Clear();
        m_listData1.Clear();
        m_listData2.Clear();
        m_listData3.Clear();
        m_listTime.Clear();
        List<string[]> list = CSVMgr.GetData(Application.streamingAssetsPath + "/shuju.csv");
        Debug.Log(list.Count);
        for (int i = 0; i < list.Count; i++)
        {
            
            
            float line0 = float.Parse(list[i][1]);
            float line1 = float.Parse(list[i][2]);
            float line2 = float.Parse(list[i][3]);
            float line3 = float.Parse(list[i][4]);

            if (line0 > m_maxData0)
            {
                m_maxData0 = line0;
            }
            if (line1 > m_maxData0)
            {
                m_maxData0 = line1;
            }

            if (line2 > m_maxData1)
            {
                m_maxData1 = line2;
            }
            if (line3 > m_maxData1)
            {
                m_maxData1 = line3;
            }

            if (i < list.Count - 1)
            {
                float time = float.Parse(list[i][0]);
                float timeNext = float.Parse(list[i + 1][0]);
                m_listTime.Add(timeNext - time);
            }
            //m_listData0.Add(new TestData(time, line0));
            //m_listData1.Add(new TestData(time, line1));
            //m_listData2.Add(new TestData(time, line2));
            //m_listData3.Add(new TestData(time, line3));
        }

        m_maxTime = float.Parse(list[list.Count-1][0]);

        for (int i = 0; i < list.Count; i++)
        {
            float time = float.Parse(list[i][0]) / m_maxTime;
            float line0 = float.Parse(list[i][1]) / m_maxData0;
            float line1 = float.Parse(list[i][2]) / m_maxData0;
            float line2 = float.Parse(list[i][3]) / m_maxData1;
            float line3 = float.Parse(list[i][4]) / m_maxData1;

            m_listData0.Add(new TestData(time, line0));
            m_listData1.Add(new TestData(time, line1));
            m_listData2.Add(new TestData(time, line2));
            m_listData3.Add(new TestData(time, line3));
        }

        Debug.Log("最大时间 :" + m_maxTime);
        Debug.Log("表1最大值 :" + m_maxData0);
        Debug.Log("表2最大值 :" + m_maxData1);
    }


    Vector2 World2CanvasPos(Canvas canvas, Vector3 world)
    {
        Vector2 pos;
        RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas.transform as RectTransform, world, canvas.GetComponent<Camera>(), out pos);
        return pos;
    }

    Vector2 Wrold2ScreenPos(Camera cam, Vector3 world)
    {
        Vector2 pos;
        pos = cam.WorldToScreenPoint(world);
        return pos;
    }

    public Vector2 GetPos(Vector2 pos,RectTransform rect)
    {
        pos += new Vector2(-0.5f, -0.5f);

        return new Vector2(pos.x * rect.sizeDelta.x, pos.y * rect.sizeDelta.y);
    }

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

四夕立羽

你的鼓励将是我创作的最大动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值