Example40_Radar.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using XCharts.Runtime;
  5. namespace XCharts.Example
  6. {
  7. [DisallowMultipleComponent]
  8. public class Example40_Radar : MonoBehaviour
  9. {
  10. private RadarChart chart;
  11. private Serie serie, serie1;
  12. void Awake()
  13. {
  14. LoopDemo();
  15. }
  16. private void OnEnable()
  17. {
  18. LoopDemo();
  19. }
  20. void LoopDemo()
  21. {
  22. StopAllCoroutines();
  23. StartCoroutine(RadarDemo());
  24. }
  25. IEnumerator RadarDemo()
  26. {
  27. StartCoroutine(RadarAdd());
  28. yield return new WaitForSeconds(2);
  29. StartCoroutine(RadarUpdate());
  30. yield return new WaitForSeconds(2);
  31. StartCoroutine(RadarAddMultiple());
  32. yield return new WaitForSeconds(2);
  33. LoopDemo();
  34. }
  35. IEnumerator RadarAdd()
  36. {
  37. chart = gameObject.GetComponent<RadarChart>();
  38. if (chart == null) chart = gameObject.AddComponent<RadarChart>();
  39. chart.RemoveChartComponents<RadarCoord>();
  40. chart.RemoveData();
  41. chart.GetChartComponent<Title>().text = "RadarChart - 雷达图";
  42. chart.GetChartComponent<Title>().subText = "";
  43. var legend = chart.GetChartComponent<Legend>();
  44. legend.show = true;
  45. legend.location.align = Location.Align.TopLeft;
  46. legend.location.top = 60;
  47. legend.location.left = 2;
  48. legend.itemWidth = 70;
  49. legend.itemHeight = 20;
  50. legend.orient = Orient.Vertical;
  51. var radarCoord = chart.AddChartComponent<RadarCoord>();
  52. radarCoord.shape = RadarCoord.Shape.Polygon;
  53. radarCoord.center[0] = 0.5f;
  54. radarCoord.center[1] = 0.4f;
  55. radarCoord.radius = 0.4f;
  56. radarCoord.AddIndicator("indicator1", 0, 100);
  57. radarCoord.AddIndicator("indicator2", 0, 100);
  58. radarCoord.AddIndicator("indicator3", 0, 100);
  59. radarCoord.AddIndicator("indicator4", 0, 100);
  60. radarCoord.AddIndicator("indicator5", 0, 100);
  61. serie = chart.AddSerie<Radar>("test");
  62. serie.radarIndex = 0;
  63. chart.AddData(0, new List<double> { 10, 20, 60, 40, 20 }, "data1");
  64. chart.AddData(0, new List<double> { 40, 60, 90, 80, 70 }, "data2");
  65. yield return new WaitForSeconds(1);
  66. }
  67. IEnumerator RadarUpdate()
  68. {
  69. var radarCoord = chart.GetChartComponent<RadarCoord>();
  70. radarCoord.UpdateIndicator(0, "new1", 0, 100);
  71. chart.UpdateData(0, 0, new List<double> { 15, 30, 50, 60, 50 });
  72. chart.UpdateDataName(0, 0, "new1");
  73. yield return new WaitForSeconds(1);
  74. }
  75. IEnumerator RadarAddMultiple()
  76. {
  77. chart.RemoveChartComponents<RadarCoord>();
  78. chart.RemoveData();
  79. chart.GetChartComponent<Title>().text = "RadarChart - 多雷达图";
  80. chart.GetChartComponent<Title>().subText = "";
  81. var legend = chart.GetChartComponent<Legend>();
  82. legend.show = true;
  83. legend.location.align = Location.Align.TopLeft;
  84. legend.location.top = 60;
  85. legend.location.left = 2;
  86. legend.itemWidth = 70;
  87. legend.itemHeight = 20;
  88. legend.orient = Orient.Vertical;
  89. var radarCoord = chart.AddChartComponent<RadarCoord>();
  90. radarCoord.shape = RadarCoord.Shape.Polygon;
  91. radarCoord.center[0] = 0.25f;
  92. radarCoord.center[1] = 0.4f;
  93. radarCoord.radius = 0.25f;
  94. for (int i = 1; i <= 5; i++)
  95. {
  96. radarCoord.AddIndicator("radar1" + i, 0, 100);
  97. }
  98. var radarCoord2 = chart.AddChartComponent<RadarCoord>();
  99. radarCoord2.shape = RadarCoord.Shape.Polygon;
  100. radarCoord2.center[0] = 0.75f;
  101. radarCoord2.center[1] = 0.4f;
  102. radarCoord2.radius = 0.25f;
  103. for (int i = 1; i <= 5; i++)
  104. {
  105. radarCoord2.AddIndicator("radar2" + i, 0, 100);
  106. }
  107. serie = chart.AddSerie<Radar>("test1");
  108. serie.radarIndex = 0;
  109. chart.AddData(0, new List<double> { 10, 20, 60, 40, 20 }, "data1");
  110. serie1 = chart.AddSerie<Radar>("test2");
  111. serie1.radarIndex = 1;
  112. chart.AddData(1, new List<double> { 10, 20, 60, 40, 20 }, "data2");
  113. yield return new WaitForSeconds(1);
  114. }
  115. }
  116. }