Unity插件XCharts的代码测试简记

Unity插件XCharts的代码测试简记

    • 相关链接
    • 创建
    • 代码

相关链接

插件作者主页:

  • https://blog.csdn.net/monitor1394/article/details/102762068

插件下载链接:

  1. https://github.com/monitor1394/unity-ugui-XCharts:插件源项目
  2. https://download.csdn.net/download/f_957995490/12189881:我自己导出的unitypackage

创建

如下图指示的创建例子图表(线形图)。
Unity插件XCharts的代码测试简记_第1张图片

代码

编写代码:

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

public class XChartsTest : MonoBehaviour
{
    LineChart chart;
    string name1 = "test";
    string name2 = "test2";

    void Awake()
    {
        chart = GetComponent<LineChart>();
    }

    void Start()
    {
        chart.title.show = true;
        chart.title.text = "Line Text";

        chart.tooltip.show = true;

        chart.legend.show = true;
        chart.legend.data.Clear();
        chart.legend.data.Add(name1);
        chart.legend.data.Add(name2);

        chart.xAxises[0].show = true;
        chart.xAxises[1].show = false;
        chart.yAxises[0].show = true;
        chart.yAxises[1].show = false;

        chart.xAxises[0].type = Axis.AxisType.Value;
        chart.yAxises[0].type = Axis.AxisType.Category;


        int[] data1 = { 10, 20, 30, 10, 50 };
        int[] data2 = { 50, 60, 10, 50, 10 };
        chart.xAxises[0].splitNumber = data1.Length;

        chart.RemoveData();

        for (int i = 0; i < data1.Length; i++)
        {
            chart.AddYAxisData(i.ToString());
        }

        chart.AddSerie(SerieType.Line, name1);
        chart.AddSerie(SerieType.Line, name2);

        foreach (int i in data1)
        {
            chart.AddData(name1, i);
        }

        foreach (int i in data2)
        {
            chart.AddData(name2, i);
        }

        time = 0;
    }

    private float time;

    void Update()
    {
        if (2.0f <= time)
        {
            time = 0;
            float temp = Random.Range(10, 70);
            chart.UpdateData(name1, 2, temp);
        }
        time += Time.deltaTime;
    }
}

然后,挂在创建的线形图表上。
Unity插件XCharts的代码测试简记_第2张图片
运行之后,效果如下:
Unity插件XCharts的代码测试简记_第3张图片
当然图中的红线2号节点会每2秒钟运动一次。

你可能感兴趣的:(XCharts)