Chart Graph动态添加数据
using System;
using ChartAndGraph;
using UnityEngine;
using UnityEngine.UI;
using Random = UnityEngine.Random;

public class RealTimeGraph : MonoBehaviour
{
    public GraphChart Graph;
    public int totalPoints = 5;
    float lastTime = 0f;

    public bool isCaton;

    public int normalMin;
    public int normalMax;
    [Space] public int catonMin;
    public float catonMax;

    public Text realTimeText;
    public Text realTimeText2;


    [Header("仪表盘")] public PointerController pointer;
    [Header("速度开关")] public bool one;
    public bool two;

    public float oneSpeed;
    public float twoSpeed;
    public float allSpeed;

    private float ping;

    [Header("动画")] public GameObject band;
    public GameObject wave;

    void Start()
    {
        if (Graph == null) // the ChartGraph info is obtained via the inspector
            return;
        Graph.CustomDateTimeFormat = (date) => { return date.ToString("HH:mm:ss"); };
        BatchGeneration(isCaton);
    }

    public void BatchGeneration(bool status)
    {
        Graph.DataSource.ClearCategory("Player 1");
        DateTime currentTime = DateTime.Now.AddSeconds(-totalPoints);
        for (int i = 0; i < totalPoints; i++) //add random points to the graph
        {
            Graph.DataSource.AddPointToCategoryRealtime("Player 1", currentTime.AddSeconds(i),
                0);
        }
        // if (status)
        // {
        //     for (int i = 0; i < totalPoints; i++) //add random points to the graph
        //     {
        //         Graph.DataSource.AddPointToCategoryRealtime("Player 1", currentTime.AddSeconds(i),
        //             Random.Range(catonMin * 1000, catonMax * 1000) * 0.001f);
        //     }
        // }
        // else
        // {
        //     for (int i = 0; i < totalPoints; i++) //add random points to the graph
        //     {
        //         Graph.DataSource.AddPointToCategoryRealtime("Player 1", currentTime.AddSeconds(i),
        //             Random.Range(normalMin, normalMax));
        //     }
        // }
    }

    public void SwitchNum(bool status)
    {
        if (status)
        {
            Graph.ScrollableData.VerticalViewSize = 3;
        }
        else
        {
            Graph.ScrollableData.VerticalViewSize = 30;
        }

        BatchGeneration(status);
    }

    void Update()
    {
        float time = Time.time;
        if (lastTime + .5f < time)
        {
            lastTime = time;
            if (one && two)
            {
                ping = Random.Range(allSpeed - 50, allSpeed + 50);
                Graph.DataSource.AddPointToCategoryRealtime("Player 1", DateTime.Now, ping,
                    .5f);
                realTimeText.text = $"{Math.Round(ping, 2)}";
                realTimeText2.text = $"{Math.Round(ping, 2)}";
                pointer.SetSpeed(ping);
            }
            else if (one)
            {
                ping = Random.Range(oneSpeed - 50, oneSpeed + 50);
                Graph.DataSource.AddPointToCategoryRealtime("Player 1", DateTime.Now, ping,
                    .5f);
                realTimeText.text = $"{Math.Round(ping, 2)}";
                realTimeText2.text = $"{Math.Round(ping, 2)}";
                pointer.SetSpeed(ping);
            }
            else if (two)
            {
                ping = Random.Range(twoSpeed - 50, twoSpeed + 50);
                Graph.DataSource.AddPointToCategoryRealtime("Player 1", DateTime.Now, ping,
                    .5f);
                realTimeText.text = $"{Math.Round(ping, 2)}";
                realTimeText2.text = $"{Math.Round(ping, 2)}";
                pointer.SetSpeed(ping);
            }

            else
            {
                Graph.DataSource.AddPointToCategoryRealtime("Player 1", DateTime.Now, 0,
                    .5f);
                realTimeText.text = $"0";
                realTimeText2.text = $"0";
                pointer.SetSpeed(0);
            }
        }
    }

    public void SetOne()
    {
        one = !one;
        band.SetActive(one);
    }

    public void SetTwo()
    {
        two = !two;
        wave.SetActive(two);
    }
}
上一篇
下一篇