1. 程式人生 > 其它 >一段基於xCharts圖表外掛的程式碼

一段基於xCharts圖表外掛的程式碼

技術標籤:UnityUIxCharts

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using XCharts;
/// <summary>
/// 
/// </summary>
public class WS_LineCtrl : MonoBehaviour {

    public static WS_LineCtrl instance;//單例
    public Slider AcceleratorSlider;//油門slider
    public Text AcceleratorText;//油門
    public float AcceleratorValue;//油門值
    private bool StartTestbl = false;

    [Space(10)]//曲線表OBJ 用於縮放顯示
    public GameObject Voltageobj;
    public GameObject Currentobj;
    public GameObject Rotationobj;
    public GameObject Torqueobj;
    public GameObject Pullobj;
    public GameObject Temperatureobj;
    public GameObject JiangXiaoObj;  

    [Space(10)]
    public Button StartTest;//開始實驗按鈕
    public Button EndTest;//停止測試按鈕
    public Button SelectCurrent;//選中電流
    public Button SelectRotation;//選中轉速
    public Button SelectTorque;//選中扭矩
    public Button SelectPull;//選中拉力
    public Button SelectTemperature;//選中點選溫度
    public Button SelectJiangXiao;//選中顯示漿效
    public Button CurrentZero;//耗電清零
    public Button RotationZero;
    public Button PullZero;



    [Space(10)]
    public LineChart VoltageLineChart;//電壓圖表
    public LineChart CurrentLineChart;//電流圖表
    public LineChart RotationLineChart;//轉速圖表
    public LineChart TorqueLineChart;//扭矩圖表
    public LineChart PullLineChart;//拉力圖表
    public LineChart TemperatureLineChart;//溫度圖表
    public LineChart JiangXiaoLineChart;//漿效圖表

    [Space(10)]
    public Text CurrentText;//電流值
    public Text VoltageText;//電壓值
    public Text RotationText;//轉速值
    public Text TorqueText;//扭矩值
    public Text PullText;//拉力值
    public Text Temperature;//溫度值
    public Text JiangXiaoText;//漿效值


    /// <summary>
    /// 重啟
    /// </summary>
    private void OnEnable()
    {
        StartTestbl = false;
        AcceleratorValue = 0;
        CurrentLineChart.ClearData();
        VoltageLineChart.ClearData();
        RotationLineChart.ClearData();
        TorqueLineChart.ClearData();
        PullLineChart.ClearData();
        TemperatureLineChart.ClearData();
        JiangXiaoLineChart.ClearData();
    }

    private void Awake()
    {
        InitLineCharts();
        instance = this;
    }




    // Use this for initialization
    void Start () {



        StartTest.onClick.AddListener(StartTestOnClcik);
        EndTest.onClick.AddListener(EndTestOnClick);
        SelectCurrent.onClick.AddListener(SelectCurrentOnClick);
        SelectRotation.onClick.AddListener(SelectRotationOnClick);
        SelectTorque.onClick.AddListener(SelectTorqueOnClick);
        SelectPull.onClick.AddListener(SelectPullOnClick);
        SelectTemperature.onClick.AddListener(SelectTemperatureOnClick);
        SelectJiangXiao.onClick.AddListener(SelectJiangXiaoOnClick);
        CurrentZero.onClick.AddListener(CurrentToZero);
        RotationZero.onClick.AddListener(RotationToZero);
        PullZero.onClick.AddListener(PullToZero);

        SelectCurrentOnClick();


    }

    #region 按鈕事件
    void CurrentToZero()
    {
        CurrentLineChart.ClearData();
        CurrentLineChart.AddData(0, 0);
    }

    void RotationToZero()
    {
        RotationLineChart.ClearData();
        RotationLineChart.AddData(0, 0);
    }
    void PullToZero()
    {
        PullLineChart.ClearData();
        PullLineChart.AddData(0, 0);
    }



    void StartTestOnClcik()
    {
        StartTestbl = true;
    }

    public void EndTestOnClick() {
        StartTestbl = false;

    }




    void SelectCurrentOnClick()
    {
        SelectCurrent.gameObject.GetComponent<Image>().sprite = SelectCurrent.gameObject.GetComponent<WS_BtnSprite>().Selected;
        SelectRotation.gameObject.GetComponent<Image>().sprite = SelectRotation.gameObject.GetComponent<WS_BtnSprite>().Null;
        SelectTorque.gameObject.GetComponent<Image>().sprite = SelectTorque.gameObject.GetComponent<WS_BtnSprite>().Null;
        SelectPull.gameObject.GetComponent<Image>().sprite = SelectPull.gameObject.GetComponent<WS_BtnSprite>().Null;
        SelectTemperature.gameObject.GetComponent<Image>().sprite = SelectTemperature.gameObject.GetComponent<WS_BtnSprite>().Null;
        SelectJiangXiao.gameObject.GetComponent<Image>().sprite = SelectJiangXiao.gameObject.GetComponent<WS_BtnSprite>().Null;

        Voltageobj.transform.localScale = new Vector3(1, 1, 1);
        Currentobj.transform.localScale = new Vector3(1, 1, 1);
        Rotationobj.transform.localScale = new Vector3(1, 1, 1);
        Torqueobj.transform.localScale = new Vector3(0, 1, 1);
        Pullobj.transform.localScale = new Vector3(0, 1, 1);
        Temperatureobj.transform.localScale = new Vector3(0, 1, 1);
        JiangXiaoObj.transform.localScale = new Vector3(0, 1, 1);








    }

    void SelectRotationOnClick()
    {
        SelectCurrent.gameObject.GetComponent<Image>().sprite = SelectCurrent.gameObject.GetComponent<WS_BtnSprite>().Null;
        SelectRotation.gameObject.GetComponent<Image>().sprite = SelectRotation.gameObject.GetComponent<WS_BtnSprite>().Selected;
        SelectTorque.gameObject.GetComponent<Image>().sprite = SelectTorque.gameObject.GetComponent<WS_BtnSprite>().Null;
        SelectPull.gameObject.GetComponent<Image>().sprite = SelectPull.gameObject.GetComponent<WS_BtnSprite>().Null;
        SelectTemperature.gameObject.GetComponent<Image>().sprite = SelectTemperature.gameObject.GetComponent<WS_BtnSprite>().Null;
        SelectJiangXiao.gameObject.GetComponent<Image>().sprite = SelectJiangXiao.gameObject.GetComponent<WS_BtnSprite>().Null;

        Voltageobj.transform.localScale = new Vector3(1, 1, 1);
        Currentobj.transform.localScale = new Vector3(1, 1, 1);
        Rotationobj.transform.localScale = new Vector3(1, 1, 1);
        Torqueobj.transform.localScale = new Vector3(0, 1, 1);
        Pullobj.transform.localScale = new Vector3(0, 1, 1);
        Temperatureobj.transform.localScale = new Vector3(0, 1, 1);
        JiangXiaoObj.transform.localScale = new Vector3(0, 1, 1);


    }
    void SelectTorqueOnClick() {
        SelectCurrent.gameObject.GetComponent<Image>().sprite = SelectCurrent.gameObject.GetComponent<WS_BtnSprite>().Null;
        SelectRotation.gameObject.GetComponent<Image>().sprite = SelectRotation.gameObject.GetComponent<WS_BtnSprite>().Null;
        SelectTorque.gameObject.GetComponent<Image>().sprite = SelectTorque.gameObject.GetComponent<WS_BtnSprite>().Selected;
        SelectPull.gameObject.GetComponent<Image>().sprite = SelectPull.gameObject.GetComponent<WS_BtnSprite>().Null;
        SelectTemperature.gameObject.GetComponent<Image>().sprite = SelectTemperature.gameObject.GetComponent<WS_BtnSprite>().Null;
        SelectJiangXiao.gameObject.GetComponent<Image>().sprite = SelectJiangXiao.gameObject.GetComponent<WS_BtnSprite>().Null;

        Voltageobj.transform.localScale = new Vector3(0, 1, 1);
        Currentobj.transform.localScale = new Vector3(0, 1, 1);
        Rotationobj.transform.localScale = new Vector3(0, 1, 1);
        Torqueobj.transform.localScale = new Vector3(1, 1, 1);
        Pullobj.transform.localScale = new Vector3(1, 1, 1);
        Temperatureobj.transform.localScale = new Vector3(1, 1, 1);
        JiangXiaoObj.transform.localScale = new Vector3(0, 1, 1);

    }
    void SelectPullOnClick()
    {
        SelectCurrent.gameObject.GetComponent<Image>().sprite = SelectCurrent.gameObject.GetComponent<WS_BtnSprite>().Null;
        SelectRotation.gameObject.GetComponent<Image>().sprite = SelectRotation.gameObject.GetComponent<WS_BtnSprite>().Null;
        SelectTorque.gameObject.GetComponent<Image>().sprite = SelectTorque.gameObject.GetComponent<WS_BtnSprite>().Null;
        SelectPull.gameObject.GetComponent<Image>().sprite = SelectPull.gameObject.GetComponent<WS_BtnSprite>().Selected;
        SelectTemperature.gameObject.GetComponent<Image>().sprite = SelectTemperature.gameObject.GetComponent<WS_BtnSprite>().Null;
        SelectJiangXiao.gameObject.GetComponent<Image>().sprite = SelectJiangXiao.gameObject.GetComponent<WS_BtnSprite>().Null;

        Voltageobj.transform.localScale = new Vector3(0, 1, 1);
        Currentobj.transform.localScale = new Vector3(0, 1, 1);
        Rotationobj.transform.localScale = new Vector3(0, 1, 1);
        Torqueobj.transform.localScale = new Vector3(1, 1, 1);
        Pullobj.transform.localScale = new Vector3(1, 1, 1);
        Temperatureobj.transform.localScale = new Vector3(1, 1, 1);
        JiangXiaoObj.transform.localScale = new Vector3(0, 1, 1);
    }
    void SelectTemperatureOnClick()
    {
        SelectCurrent.gameObject.GetComponent<Image>().sprite = SelectCurrent.gameObject.GetComponent<WS_BtnSprite>().Null;
        SelectRotation.gameObject.GetComponent<Image>().sprite = SelectRotation.gameObject.GetComponent<WS_BtnSprite>().Null;
        SelectTorque.gameObject.GetComponent<Image>().sprite = SelectTorque.gameObject.GetComponent<WS_BtnSprite>().Null;
        SelectPull.gameObject.GetComponent<Image>().sprite = SelectPull.gameObject.GetComponent<WS_BtnSprite>().Null;
        SelectTemperature.gameObject.GetComponent<Image>().sprite = SelectTemperature.gameObject.GetComponent<WS_BtnSprite>().Selected;
        SelectJiangXiao.gameObject.GetComponent<Image>().sprite = SelectJiangXiao.gameObject.GetComponent<WS_BtnSprite>().Null;

        Voltageobj.transform.localScale = new Vector3(0, 1, 1);
        Currentobj.transform.localScale = new Vector3(0, 1, 1);
        Rotationobj.transform.localScale = new Vector3(0, 1, 1);
        Torqueobj.transform.localScale = new Vector3(1, 1, 1);
        Pullobj.transform.localScale = new Vector3(1, 1, 1);
        Temperatureobj.transform.localScale = new Vector3(1, 1, 1);
        JiangXiaoObj.transform.localScale = new Vector3(0, 1, 1);
    }

    void SelectJiangXiaoOnClick()
    {
        SelectCurrent.gameObject.GetComponent<Image>().sprite = SelectCurrent.gameObject.GetComponent<WS_BtnSprite>().Null;
        SelectRotation.gameObject.GetComponent<Image>().sprite = SelectRotation.gameObject.GetComponent<WS_BtnSprite>().Null;
        SelectTorque.gameObject.GetComponent<Image>().sprite = SelectTorque.gameObject.GetComponent<WS_BtnSprite>().Null;
        SelectPull.gameObject.GetComponent<Image>().sprite = SelectPull.gameObject.GetComponent<WS_BtnSprite>().Null;
        SelectTemperature.gameObject.GetComponent<Image>().sprite = SelectTemperature.gameObject.GetComponent<WS_BtnSprite>().Null;
        SelectJiangXiao.gameObject.GetComponent<Image>().sprite = SelectJiangXiao.gameObject.GetComponent<WS_BtnSprite>().Selected;

        Voltageobj.transform.localScale = new Vector3(0, 1, 1);
        Currentobj.transform.localScale = new Vector3(0, 1, 1);
        Rotationobj.transform.localScale = new Vector3(0, 1, 1);
        Torqueobj.transform.localScale = new Vector3(0, 1, 1);
        Pullobj.transform.localScale = new Vector3(0, 1, 1);
        Temperatureobj.transform.localScale = new Vector3(0, 1, 1);
        JiangXiaoObj.transform.localScale = new Vector3(1, 1, 1);
    }

    #endregion


    // Update is called once per frame
    void Update () {

        if (AcceleratorValue<=0)
        {
            AcceleratorValue = 0;
        }
        if (AcceleratorValue >= 100)
        {
            AcceleratorValue = 100;
        }


        AcceleratorSlider.value = AcceleratorValue / 100;
        AcceleratorText.text = ((int)AcceleratorValue).ToString()+"%";

        if (StartTestbl)
        {
            UpdateTheLineData();//每隔2秒更新下線的資料
        }

    }
    public void AcceleratorSliderChange()
    {
        AcceleratorValue = AcceleratorSlider.value*100;
    }
    private float timer = 0;


    /// <summary>
    /// 更新線的資料
    /// </summary>
    void UpdateTheLineData()
    {
        timer += Time.deltaTime;
        if (timer>=2.0f)
        {
            timer = 0;
            AddVoltagLineData();//新增一個電壓資料
            AddCurrentLineData();//新增一個電流資料
            AddRotationLineData();//新增一個轉速資料
            AddTorqueLineData();//新增一個扭矩資料
            AddPullLineData();//新增一個拉力資料
            AddTemperatureLineData();//新增一個溫度資料
            AddJiangXiaoLineData();//新增一個漿效資料
        }
    }
    /// <summary>
    /// 初始化圖表
    /// </summary>
    void InitLineCharts()
    {
        CurrentLineChart.title.show = true;
        CurrentLineChart.tooltip.show = true;
        CurrentLineChart.legend.show = false;
        CurrentLineChart.xAxises[0].show = true;
        CurrentLineChart.xAxises[1].show = false;
        CurrentLineChart.yAxises[0].show = true;
        CurrentLineChart.yAxises[1].show = false;
        CurrentLineChart.xAxises[0].type = Axis.AxisType.Value;
        CurrentLineChart.yAxises[0].type = Axis.AxisType.Value;
        CurrentLineChart.xAxises[0].splitNumber = 12;
        CurrentLineChart.xAxises[0].boundaryGap = false;
        CurrentLineChart.RemoveData();
        CurrentLineChart.AddSerie(SerieType.Line);
        CurrentLineChart.series.list[0].areaStyle.show = true;
        CurrentLineChart.series.list[0].areaStyle.opacity = 0.1f;
        CurrentLineChart.xAxis0.ceilRate = 10;
        //CurrentLineChart.AddData(0, 0);

        VoltageLineChart.title.show = true;
        VoltageLineChart.tooltip.show = false;
        VoltageLineChart.legend.show = false;
        VoltageLineChart.xAxises[0].show = true;
        VoltageLineChart.xAxises[1].show = false;
        VoltageLineChart.yAxises[0].show = true;
        VoltageLineChart.yAxises[1].show = false;
        VoltageLineChart.xAxises[0].type = Axis.AxisType.Value;
        VoltageLineChart.yAxises[0].type = Axis.AxisType.Value;
        VoltageLineChart.xAxises[0].splitNumber = 12;
        VoltageLineChart.xAxises[0].boundaryGap = false;
        VoltageLineChart.RemoveData();
        VoltageLineChart.AddSerie(SerieType.Line);
        VoltageLineChart.series.list[0].areaStyle.show = true;
        VoltageLineChart.series.list[0].areaStyle.opacity = 0.1f;
        VoltageLineChart.xAxis0.ceilRate = 10;
        //VoltageLineChart.AddData(0, 0);


        RotationLineChart.title.show = true;
        RotationLineChart.tooltip.show = false;
        RotationLineChart.legend.show = false;
        RotationLineChart.xAxises[0].show = true;
        RotationLineChart.xAxises[1].show = false;
        RotationLineChart.yAxises[0].show = true;
        RotationLineChart.yAxises[1].show = false;
        RotationLineChart.xAxises[0].type = Axis.AxisType.Value;
        RotationLineChart.yAxises[0].type = Axis.AxisType.Value;
        RotationLineChart.xAxises[0].splitNumber = 12;
        RotationLineChart.xAxises[0].boundaryGap = false;
        RotationLineChart.RemoveData();
        RotationLineChart.AddSerie(SerieType.Line);
        RotationLineChart.series.list[0].areaStyle.show = true;
        RotationLineChart.series.list[0].areaStyle.opacity = 0.1f;
        RotationLineChart.xAxis0.ceilRate = 10;
        //VoltageLineChart.AddData(0, 0);

        TorqueLineChart.title.show = true;
        TorqueLineChart.tooltip.show = false;
        TorqueLineChart.legend.show = false;
        TorqueLineChart.xAxises[0].show = true;
        TorqueLineChart.xAxises[1].show = false;
        TorqueLineChart.yAxises[0].show = true;
        TorqueLineChart.yAxises[1].show = false;
        TorqueLineChart.xAxises[0].type = Axis.AxisType.Value;
        TorqueLineChart.yAxises[0].type = Axis.AxisType.Value;
        TorqueLineChart.xAxises[0].splitNumber = 12;
        TorqueLineChart.xAxises[0].boundaryGap = false;
        TorqueLineChart.RemoveData();
        TorqueLineChart.AddSerie(SerieType.Line);
        TorqueLineChart.series.list[0].areaStyle.show = true;
        TorqueLineChart.series.list[0].areaStyle.opacity = 0.1f;
        TorqueLineChart.xAxis0.ceilRate = 10;
        //VoltageLineChart.AddData(0, 0);


        PullLineChart.title.show = true;
        PullLineChart.tooltip.show = false;
        PullLineChart.legend.show = false;
        PullLineChart.xAxises[0].show = true;
        PullLineChart.xAxises[1].show = false;
        PullLineChart.yAxises[0].show = true;
        PullLineChart.yAxises[1].show = false;
        PullLineChart.xAxises[0].type = Axis.AxisType.Value;
        PullLineChart.yAxises[0].type = Axis.AxisType.Value;
        PullLineChart.xAxises[0].splitNumber = 12;
        PullLineChart.xAxises[0].boundaryGap = false;
        PullLineChart.RemoveData();
        PullLineChart.AddSerie(SerieType.Line);
        PullLineChart.series.list[0].areaStyle.show = true;
        PullLineChart.series.list[0].areaStyle.opacity = 0.1f;
        PullLineChart.xAxis0.ceilRate = 10;
        //VoltageLineChart.AddData(0, 0);


        TemperatureLineChart.title.show = true;
        TemperatureLineChart.tooltip.show = false;
        TemperatureLineChart.legend.show = false;
        TemperatureLineChart.xAxises[0].show = true;
        TemperatureLineChart.xAxises[1].show = false;
        TemperatureLineChart.yAxises[0].show = true;
        TemperatureLineChart.yAxises[1].show = false;
        TemperatureLineChart.xAxises[0].type = Axis.AxisType.Value;
        TemperatureLineChart.yAxises[0].type = Axis.AxisType.Value;
        TemperatureLineChart.xAxises[0].splitNumber = 12;
        TemperatureLineChart.xAxises[0].boundaryGap = false;
        TemperatureLineChart.RemoveData();
        TemperatureLineChart.AddSerie(SerieType.Line);
        TemperatureLineChart.series.list[0].areaStyle.show = true;
        TemperatureLineChart.series.list[0].areaStyle.opacity = 0.1f;
        TemperatureLineChart.xAxis0.ceilRate = 10;
        //VoltageLineChart.AddData(0, 0);
        JiangXiaoLineChart.title.show = true;
        JiangXiaoLineChart.tooltip.show = false;
        JiangXiaoLineChart.legend.show = false;
        JiangXiaoLineChart.xAxises[0].show = true;
        JiangXiaoLineChart.xAxises[1].show = false;
        JiangXiaoLineChart.yAxises[0].show = true;
        JiangXiaoLineChart.yAxises[1].show = false;
        JiangXiaoLineChart.xAxises[0].type = Axis.AxisType.Value;
        JiangXiaoLineChart.yAxises[0].type = Axis.AxisType.Value;
        JiangXiaoLineChart.xAxises[0].splitNumber = 12;
        JiangXiaoLineChart.xAxises[0].boundaryGap = false;
        JiangXiaoLineChart.RemoveData();
        JiangXiaoLineChart.AddSerie(SerieType.Line);
        JiangXiaoLineChart.series.list[0].areaStyle.show = true;
        JiangXiaoLineChart.series.list[0].areaStyle.opacity = 0.1f;
        JiangXiaoLineChart.xAxis0.ceilRate = 10;
        //JiangXiaoLineChart.AddData(0, 0);


    }


    /// <summary>
    /// 根據油門值新增一個電流資料
    /// </summary>
    void AddCurrentLineData()
    {
        float value = 0;
        if (AcceleratorValue == 0)
        {
            value = 0;
        }
        else if (AcceleratorValue <= 10 && AcceleratorValue > 0)
        {
            value = Random.Range(0, 0.32f);
        }
        else if (AcceleratorValue <= 20 && AcceleratorValue > 10)
        {
            value = Random.Range(0.32f, 1.05f);
        }
        else if (AcceleratorValue <= 30 && AcceleratorValue > 20)
        {
            value = Random.Range(1.05f, 2.42f);
        }
        else if (AcceleratorValue <= 40 && AcceleratorValue > 30)
        {
            value = Random.Range(2.42f, 4.43f);
        }
        else if (AcceleratorValue <= 50 && AcceleratorValue > 40)
        {
            value = Random.Range(4.43f, 7.15f);
        }
        else if (AcceleratorValue <= 60 && AcceleratorValue > 50)
        {
            value = Random.Range(7.15f, 10.59f);
        }
        else if (AcceleratorValue <= 70 && AcceleratorValue > 60)
        {
            value = Random.Range(10.59f, 15.07f);
        }
        else if (AcceleratorValue <= 80 && AcceleratorValue > 70)
        {
            value = Random.Range(15.07f, 20.83f);
        }
        else if (AcceleratorValue <= 90 && AcceleratorValue > 80)
        {
            value = Random.Range(20.83f, 27.73f);
        }
        else if (AcceleratorValue < 100 && AcceleratorValue > 90)
        {
            value = Random.Range(27.73f, 36.16f);
        }
        else if (AcceleratorValue == 100)
        {
            value = Random.Range(35.56f, 36.16f);
        }

        CurrentLineChart.AddData(0, value);
        CurrentText.text = value.ToString("0.00")+"A";
    }


    /// <summary>
    /// 根據轉速值新增一個轉速資料
    /// </summary>
    void AddRotationLineData()
    {
        int value = 0;
        if (AcceleratorValue == 0)
        {
            value = 0;
        }
        else if (AcceleratorValue <= 10 && AcceleratorValue > 0)
        {
            value = Random.Range(100, 493);
        }
        else if (AcceleratorValue <= 20 && AcceleratorValue > 10)
        {
            value = Random.Range(493, 958);
        }
        else if (AcceleratorValue <= 30 && AcceleratorValue > 20)
        {
            value = Random.Range(958, 1410);
        }
        else if (AcceleratorValue <= 40 && AcceleratorValue > 30)
        {
            value = Random.Range(1410, 1869);
        }
        else if (AcceleratorValue <= 50 && AcceleratorValue > 40)
        {
            value = Random.Range(1869, 2262);
        }
        else if (AcceleratorValue <= 60 && AcceleratorValue > 50)
        {
            value = Random.Range(2262, 2621);
        }
        else if (AcceleratorValue <= 70 && AcceleratorValue > 60)
        {
            value = Random.Range(2621, 3010);
        }
        else if (AcceleratorValue <= 80 && AcceleratorValue > 70)
        {
            value = Random.Range(3010, 3361);
        }
        else if (AcceleratorValue <= 90 && AcceleratorValue > 80)
        {
            value = Random.Range(3361, 3698);
        }
        else if (AcceleratorValue < 100 && AcceleratorValue > 90)
        {
            value = Random.Range(3698, 4020);
        }
        else if (AcceleratorValue == 100)
        {
            value = Random.Range(3980, 4100);
        }

        RotationLineChart.AddData(0, value);
        RotationText.text = value.ToString("")+"(RPM)";
    }

    /// <summary>
    /// 新增一個電壓的資料
    /// </summary>
    void AddVoltagLineData()
    {
        float value = 0;
        if (StartTestbl)
        {
            value = 24.5f;
        }
        else
        {
            value = 0;
        }
        VoltageLineChart.AddData(0, value,value.ToString("0.00")+"V");
        VoltageText.text = value.ToString("0.00")+"V";



    }


    /// <summary>
    /// 根據油門值新增一個扭矩資料
    /// </summary>
    void AddTorqueLineData()
    {
        float value = 0;
        if (AcceleratorValue == 0)
        {
            value = 0;
        }
        else if (AcceleratorValue <= 10 && AcceleratorValue > 0)
        {
            value = Random.Range(0.001f, 0.008f);
        }
        else if (AcceleratorValue <= 20 && AcceleratorValue > 10)
        {
            value = Random.Range(0.008f, 0.07f);
        }
        else if (AcceleratorValue <= 30 && AcceleratorValue > 20)
        {
            value = Random.Range(0.07f, 0.17f);
        }
        else if (AcceleratorValue <= 40 && AcceleratorValue > 30)
        {
            value = Random.Range(0.17f, 0.29f);
        }
        else if (AcceleratorValue <= 50 && AcceleratorValue > 40)
        {
            value = Random.Range(0.29f, 0.43f);
        }
        else if (AcceleratorValue <= 60 && AcceleratorValue > 50)
        {
            value = Random.Range(0.43f, 0.6f);
        }
        else if (AcceleratorValue <= 70 && AcceleratorValue > 60)
        {
            value = Random.Range(0.6f, 0.81f);
        }
        else if (AcceleratorValue <= 80 && AcceleratorValue > 70)
        {
            value = Random.Range(0.81f, 1.05f);
        }
        else if (AcceleratorValue <= 90 && AcceleratorValue > 80)
        {
            value = Random.Range(1.05f, 1.28f);
        }
        else if (AcceleratorValue < 100 && AcceleratorValue > 90)
        {
            value = Random.Range(1.28f, 1.49f);
        }
        else if (AcceleratorValue == 100)
        {
            value = Random.Range(1.40f, 1.55f);
        }

        TorqueLineChart.AddData(0, value);
        TorqueText.text = value.ToString("0.000")+"N*m";
    }

    /// <summary>
    /// 根據油門值新增一個拉力資料
    /// </summary>
    void AddPullLineData()
    {
        int value = 0;
        if (AcceleratorValue == 0)
        {
            value = 0;
        }
        else if (AcceleratorValue <= 10 && AcceleratorValue > 0)
        {
            value = Random.Range(20, 58);
        }
        else if (AcceleratorValue <= 20 && AcceleratorValue > 10)
        {
            value = Random.Range(58, 245);
        }
        else if (AcceleratorValue <= 30 && AcceleratorValue > 20)
        {
            value = Random.Range(245, 565);
        }
        else if (AcceleratorValue <= 40 && AcceleratorValue > 30)
        {
            value = Random.Range(565, 1006);
        }
        else if (AcceleratorValue <= 50 && AcceleratorValue > 40)
        {
            value = Random.Range(1006, 1496);
        }
        else if (AcceleratorValue <= 60 && AcceleratorValue > 50)
        {
            value = Random.Range(1496, 2054);
        }
        else if (AcceleratorValue <= 70 && AcceleratorValue > 60)
        {
            value = Random.Range(2054, 2762);
        }
        else if (AcceleratorValue <= 80 && AcceleratorValue > 70)
        {
            value = Random.Range(2762, 3524);
        }
        else if (AcceleratorValue <= 90 && AcceleratorValue > 80)
        {
            value = Random.Range(3524, 4288);
        }
        else if (AcceleratorValue < 100 && AcceleratorValue > 90)
        {
            value = Random.Range(4288, 5047);
        }
        else if (AcceleratorValue == 100)
        {
            value = Random.Range(4800, 5200);
        }

        PullLineChart.AddData(0, value);
        PullText.text = value.ToString("")+"g";
    }


    /// <summary>
    /// 根據油門值新增一個電流資料
    /// </summary>
    void AddTemperatureLineData()
    {
        float value = 0;
        if (AcceleratorValue == 0)
        {
            value = 0;
        }
        else if (AcceleratorValue <= 10 && AcceleratorValue > 0)
        {
            value = Random.Range(25, 26.2f);
        }
        else if (AcceleratorValue <= 20 && AcceleratorValue > 10)
        {
            value = Random.Range(26.2f, 26.3f);
        }
        else if (AcceleratorValue <= 30 && AcceleratorValue > 20)
        {
            value = Random.Range(26.3f, 26.8f);
        }
        else if (AcceleratorValue <= 40 && AcceleratorValue > 30)
        {
            value = Random.Range(26.8f, 27.5f);
        }
        else if (AcceleratorValue <= 50 && AcceleratorValue > 40)
        {
            value = Random.Range(27.5f, 28.1f);
        }
        else if (AcceleratorValue <= 60 && AcceleratorValue > 50)
        {
            value = Random.Range(28.1f, 28.8f);
        }
        else if (AcceleratorValue <= 70 && AcceleratorValue > 60)
        {
            value = Random.Range(28.8f, 29.6f);
        }
        else if (AcceleratorValue <= 80 && AcceleratorValue > 70)
        {
            value = Random.Range(29.6f, 30.23f);
        }
        else if (AcceleratorValue <= 90 && AcceleratorValue > 80)
        {
            value = Random.Range(30.23f, 31.7f);
        }
        else if (AcceleratorValue < 100 && AcceleratorValue > 90)
        {
            value = Random.Range(31.7f, 32.2f);
        }
        else if (AcceleratorValue == 100)
        {
            value = Random.Range(31.9f, 32.5f);
        }

        TemperatureLineChart.AddData(0, value);
        Temperature.text = value.ToString("0.0")+ "℃";
    }

    /// <summary>
    /// 根據油門值新增一個漿效資料
    /// </summary>
    void AddJiangXiaoLineData()
    {
        float value = 0;
        if (AcceleratorValue == 0)
        {
            value = 0;
        }
        else if (AcceleratorValue <= 10 && AcceleratorValue > 0)//141
        {
            value = Random.Range(100,150);
        }
        else if (AcceleratorValue <= 20 && AcceleratorValue > 10)//36.2
        {
            value = Random.Range(30.2f,40.2f);
        }
        else if (AcceleratorValue <= 30 && AcceleratorValue > 20)//22.9
        {
            value = Random.Range(19.3f, 26.8f);
        }
        else if (AcceleratorValue <= 40 && AcceleratorValue > 30)//17.6
        {
            value = Random.Range(15.8f, 19.3f);
        }
        else if (AcceleratorValue <= 50 && AcceleratorValue > 40)//14.7
        {
            value = Random.Range(13.5f, 15.8f);
        }
        else if (AcceleratorValue <= 60 && AcceleratorValue > 50)//12.4
        {
            value = Random.Range(11.1f, 13.5f);
        }
        else if (AcceleratorValue <= 70 && AcceleratorValue > 60)//10.8
        {
            value = Random.Range(9.8f, 11.1f);
        }
        else if (AcceleratorValue <= 80 && AcceleratorValue > 70)//9.58
        {
            value = Random.Range(9.0f, 9.8f);
        }
        else if (AcceleratorValue <= 90 && AcceleratorValue > 80)//8.6
        {
            value = Random.Range(8.3f, 9.0f);
        }
        else if (AcceleratorValue < 100 && AcceleratorValue > 90)//8.07
        {
            value = Random.Range(8.0f, 8.3f);
        }
        else if (AcceleratorValue == 100)//8.07
        {
            value = Random.Range(8.0f, 8.1f);
        }

        JiangXiaoLineChart.AddData(0, value);
        JiangXiaoText.text = value.ToString("0.0") + "g/W";
    }
}

做的一個專案裡面xcharts程式碼片段