XChart设置折线图自动填充数据

1、时间递增

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TimeAdd : MonoBehaviour
{
    public static PlayerManager instance;
    public float RunTime = 0f;
    public string CurrTime = "00:00";
    public List<string> CurrStrs = new List<string>();
    private string OldCurr = "00:00";

    private void Awake()
    {
        instance = this;

    }
    public void Start()
    {
        REFLASH();
    }

   
    private void REFLASH()
    {
        RunTime = 0;
        OldCurr = "00:00";
        CurrTime = "00:00";
        CurrStrs.Clear();
    }
    public void FixedUpdate()
    {
        RunTime += 0.02f;

    }
    void Update()
    {
        GetTime();

    }

    int TimeINT;
    int Min;
    int Scend;
    public void GetTime()
    {
        TimeINT = (int)RunTime;
        Min = TimeINT / 60;
        Scend = TimeINT % 60;
        if (Scend < 10)
        {
            CurrTime = Min.ToString() + ":0" + Scend.ToString();
        }
        else
        {
            CurrTime = Min.ToString() + ":" + Scend.ToString();
        }
        if (Min < 10)
        {
            CurrTime = "0" + CurrTime;
        }
        if (OldCurr != CurrTime)
        {
            SetTime();
            OldCurr = CurrTime;
        }
    }
    public void SetTime()
    {
        if (CurrStrs.Count < 8)
        {
            CurrStrs.Add(CurrTime);
        }
        else
        {

            for (int i = 0; i < CurrStrs.Count - 1; i++)
            {
                int k = i + 1;
                CurrStrs[i] = CurrStrs[k];
            }
            CurrStrs[7] = CurrTime;
        }
    }


}

2、折线图数据填充

using System;
using System.Collections;
using System.Collections.Generic;
using Unity.Mathematics;
using UnityEngine;
using XCharts.Runtime;

public class XChartData : MonoBehaviour
{
    public int Index;
    public int MaxCount = 8;
    public LineChart chart;
    public List<double> DataChart;

    System.Random random=new System.Random();
    // Start is called before the first frame update
    void Start()
    {
        REFLAH();
    }
    private void REFLAH()
    {
        DataChart.Clear();
        chart.ClearData();
        Index = 0;
    }

    float refTime=1f;
    // Update is called once per frame
    void Update()
    {
        refTime -= Time.deltaTime;
        if (refTime <= 0)
        {
          double aa =   random.NextDouble()*5+4;
            ReviceFuction(aa.ToString("f2"));
            refTime = 1f;
        }
    }

    double value;
    private void ReviceFuction(string current)
    {

        List<string> st = PlayerManager.instance.CurrStrs;
        if (st.Count < 1)
        {
            return;
        }
        value = double.Parse(current);

        //先计算X轴 X轴有8个
        if (Index < MaxCount)
        {
            chart.AddXAxisData(st[st.Count - 1]);
            chart.AddData(0, value);
            Index++;
            DataChart.Add(value);


        }
        else
        {
            for (int i = 0; i < MaxCount; i++)
            {
                if (st.Count <= MaxCount)
                {
                    chart.UpdateXAxisData(i, st[i]);
                }
                else
                {
                    chart.UpdateXAxisData(i, st[st.Count - MaxCount + i]);
                }
            }


        }
        if (Index == MaxCount)
        {
            for (int i = 0; i < DataChart.Count - 1; i++)
            {
                DataChart[i] = DataChart[i + 1];

                chart.UpdateData(0, i, DataChart[i]);
            }
            DataChart[MaxCount - 1] = value;
            chart.UpdateData(0, MaxCount - 1, value);

        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

杰尼杰尼丶

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

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

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

打赏作者

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

抵扣说明:

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

余额充值