Example90_Candlestick.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using UnityEngine;
  2. using XCharts.Runtime;
  3. namespace XCharts.Example
  4. {
  5. [DisallowMultipleComponent]
  6. [ExecuteInEditMode]
  7. public class Example90_Candlestick : MonoBehaviour
  8. {
  9. private CandlestickChart chart;
  10. private float updateTime;
  11. public int dataCount = 100;
  12. void Awake()
  13. {
  14. chart = gameObject.GetComponent<CandlestickChart>();
  15. if (chart == null)
  16. {
  17. chart = gameObject.AddComponent<CandlestickChart>();
  18. }
  19. GenerateOHLC(dataCount);
  20. }
  21. void Update()
  22. {
  23. if (Input.GetKeyDown(KeyCode.Space))
  24. {
  25. AddData();
  26. }
  27. }
  28. void AddData() { }
  29. void GenerateOHLC(int count)
  30. {
  31. chart.ClearData();
  32. var xValue = System.DateTime.Now;
  33. var baseValue = Random.Range(0f, 1f) * 12000;
  34. var boxVals = new float[4];
  35. var dayRange = 12;
  36. for (int i = 0; i < count; i++)
  37. {
  38. baseValue = baseValue + Random.Range(0f, 1f) * 30 - 10;
  39. for (int j = 0; j < 4; j++)
  40. {
  41. boxVals[j] = (Random.Range(0f, 1f) - 0.5f) * dayRange + baseValue;
  42. }
  43. System.Array.Sort(boxVals);
  44. var openIdx = Mathf.RoundToInt(Random.Range(0f, 1f) * 3);
  45. var closeIdx = Mathf.RoundToInt(Random.Range(0f, 1f) * 2);
  46. if (openIdx == closeIdx)
  47. {
  48. closeIdx++;
  49. }
  50. //var volumn = boxVals[3]*(1000+Random.Range(0f,1f) * 500);
  51. var open = boxVals[openIdx];
  52. var close = boxVals[closeIdx];
  53. var lowest = boxVals[0];
  54. var heighest = boxVals[3];
  55. chart.AddXAxisData(i.ToString());
  56. chart.AddData(0, i, open, close, lowest, heighest);
  57. }
  58. }
  59. }
  60. }