Example_Dynamic.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using UnityEngine;
  3. using XCharts.Runtime;
  4. namespace XCharts.Example
  5. {
  6. [DisallowMultipleComponent]
  7. //[ExecuteInEditMode]
  8. [RequireComponent(typeof(BaseChart))]
  9. public class Example_Dynamic : MonoBehaviour
  10. {
  11. public int maxCacheDataNumber = 100;
  12. public float initDataTime = 2;
  13. public bool insertDataToHead = false;
  14. private BaseChart chart;
  15. private float updateTime;
  16. private float initTime;
  17. private int initCount;
  18. private int count;
  19. private bool isInited;
  20. private DateTime timeNow;
  21. void Awake()
  22. {
  23. chart = gameObject.GetComponent<BaseChart>();
  24. var serie = chart.GetSerie(0);
  25. serie.symbol.show = false;
  26. serie.maxCache = maxCacheDataNumber;
  27. var xAxis = chart.GetOrAddChartComponent<XAxis>();
  28. xAxis.maxCache = maxCacheDataNumber;
  29. timeNow = DateTime.Now;
  30. timeNow = timeNow.AddSeconds(-maxCacheDataNumber);
  31. serie.insertDataToHead = insertDataToHead;
  32. xAxis.insertDataToHead = insertDataToHead;
  33. }
  34. void Update()
  35. {
  36. if (initCount < maxCacheDataNumber)
  37. {
  38. int count = (int) (maxCacheDataNumber / initDataTime * Time.deltaTime);
  39. for (int i = 0; i < count; i++)
  40. {
  41. timeNow = timeNow.AddSeconds(1);
  42. var category = timeNow.ToString("hh:mm:ss");
  43. var value = UnityEngine.Random.Range(60, 150);
  44. chart.AddXAxisData(category);
  45. chart.AddData(0, value);
  46. initCount++;
  47. if (initCount > maxCacheDataNumber) break;
  48. }
  49. chart.RefreshChart();
  50. }
  51. updateTime += Time.deltaTime;
  52. if (updateTime >= 1)
  53. {
  54. var category = DateTime.Now.ToString("hh:mm:ss");
  55. var value = UnityEngine.Random.Range(60, 150);
  56. updateTime = 0;
  57. count++;
  58. chart.AddXAxisData(category);
  59. chart.AddData(0, value);
  60. chart.RefreshChart();
  61. }
  62. }
  63. }
  64. }