UGUI XChart使用笔记8.29

日常记录

尝试进行刷新数据

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XCharts;

///
/// 区域数据
///
[System.Serializable]
public struct AreaData
{
public string areaName;
public float[] areaDatas;
}

public class LineWithDataTest : MonoBehaviour
{
public LineChart chart;

public AreaData[] areaData;

private int[] newDatas;

private bool isUpdate = true;

void Start()
{
    newDatas = new int[5];

    if (chart == null)
    {
        return;
    }

    for (int i = 0; i < areaData.Length; i++)
    {
        SetSeries(areaData[i].areaName);

        for (int j = 0; j < areaData[i].areaDatas.Length; j++)
        {
            //初次添加数据
            chart.AddData(areaData[i].areaName,areaData[i].areaDatas[j]);
        }
    }
}

private void Update()
{
    if (isUpdate)
    {
        StartCoroutine(waitForUpdateLine());
    }
}

/// <summary>
/// 等待刷新
/// </summary>
/// <returns></returns>
IEnumerator waitForUpdateLine()
{
    isUpdate = false;

    newDatas[0] = Random.Range(830, 900);
    newDatas[1] = Random.Range(800, 960);
    newDatas[2] = Random.Range(850, 930);
    newDatas[3] = Random.Range(900, 960);

    for (int i = 0; i < areaData.Length; i++)
    {
        for (int j = 0; j < areaData[i].areaDatas.Length; j++)
        {
            if (j <= areaData[i].areaDatas.Length - 2)
            {
                areaData[i].areaDatas[j] = areaData[i].areaDatas[j + 1];
            }
            if (j == areaData[i].areaDatas.Length - 1)
            {
                areaData[i].areaDatas[j] = newDatas[i];
            }
        }
    }

    yield return new WaitForSeconds(2.0f);

    for (int i = 0; i < areaData.Length; i++)
    {
        for (int j = 0; j < areaData[i].areaDatas.Length; j++)
        {
            chart.UpdateData(areaData[i].areaName, areaData[i].areaDatas[j], j);
        }
    }

    chart.RefreshChart();

    isUpdate = true;
}

/// <summary>
/// 线条设置
/// </summary>
/// <param name="name"></param>
private void SetSeries(string name)
{
    //添加线条
    chart.AddSerie(name, SerieType.Line);
    //设置线条平滑度(种类)
    chart.series.GetSerie(name).lineType = LineType.Smooth;
}

}

上一篇:Unity3D(二)UGUI中按钮的使用一(转)


下一篇:SIKI_Unity_3_进阶_UGUI整体解决方案(基础篇)