Example30_PieChart.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using System.Collections;
  2. using UnityEngine;
  3. using UnityEngine.EventSystems;
  4. using XCharts.Runtime;
  5. namespace XCharts.Example
  6. {
  7. [DisallowMultipleComponent]
  8. public class Example30_PieChart : MonoBehaviour
  9. {
  10. private PieChart chart;
  11. private Serie serie, serie1;
  12. private float m_RadiusSpeed = 100f;
  13. private float m_CenterSpeed = 1f;
  14. void Awake()
  15. {
  16. LoopDemo();
  17. }
  18. private void OnEnable()
  19. {
  20. LoopDemo();
  21. }
  22. void LoopDemo()
  23. {
  24. StopAllCoroutines();
  25. StartCoroutine(PieDemo());
  26. }
  27. IEnumerator PieDemo()
  28. {
  29. StartCoroutine(PieAdd());
  30. yield return new WaitForSeconds(2);
  31. StartCoroutine(PieShowLabel());
  32. yield return new WaitForSeconds(4);
  33. StartCoroutine(Doughnut());
  34. yield return new WaitForSeconds(3);
  35. StartCoroutine(DoublePie());
  36. yield return new WaitForSeconds(2);
  37. StartCoroutine(RosePie());
  38. yield return new WaitForSeconds(5);
  39. LoopDemo();
  40. }
  41. IEnumerator PieAdd()
  42. {
  43. chart = gameObject.GetComponent<PieChart>();
  44. if (chart == null) chart = gameObject.AddComponent<PieChart>();
  45. chart.GetChartComponent<Title>().text = "PieChart - 饼图";
  46. chart.GetChartComponent<Title>().subText = "基础饼图";
  47. var legend = chart.GetChartComponent<Legend>();
  48. legend.show = true;
  49. legend.location.align = Location.Align.TopLeft;
  50. legend.location.top = 60;
  51. legend.location.left = 2;
  52. legend.itemWidth = 70;
  53. legend.itemHeight = 20;
  54. legend.orient = Orient.Vertical;
  55. chart.RemoveData();
  56. serie = chart.AddSerie<Bar>("访问来源");
  57. serie.radius[0] = 0;
  58. serie.radius[1] = 110;
  59. serie.center[0] = 0.5f;
  60. serie.center[1] = 0.4f;
  61. chart.AddData(0, 335, "直接访问");
  62. chart.AddData(0, 310, "邮件营销");
  63. chart.AddData(0, 243, "联盟广告");
  64. chart.AddData(0, 135, "视频广告");
  65. chart.AddData(0, 1548, "搜索引擎");
  66. chart.onPointerClickPie = delegate(PointerEventData e, int serieIndex, int dataIndex)
  67. {
  68. };
  69. yield return new WaitForSeconds(1);
  70. }
  71. IEnumerator PieShowLabel()
  72. {
  73. chart.GetChartComponent<Title>().subText = "显示文本标签";
  74. serie.AddExtraComponent<LabelStyle>();
  75. serie.label.show = true;
  76. chart.RefreshChart();
  77. yield return new WaitForSeconds(1);
  78. serie.labelLine.lineType = LabelLine.LineType.Curves;
  79. chart.RefreshChart();
  80. yield return new WaitForSeconds(1);
  81. serie.labelLine.lineType = LabelLine.LineType.HorizontalLine;
  82. chart.RefreshChart();
  83. yield return new WaitForSeconds(1);
  84. serie.labelLine.lineType = LabelLine.LineType.BrokenLine;
  85. chart.RefreshChart();
  86. yield return new WaitForSeconds(1);
  87. serie.labelLine.show = false;
  88. chart.RefreshChart();
  89. }
  90. IEnumerator Doughnut()
  91. {
  92. chart.GetChartComponent<Title>().subText = "圆环图";
  93. serie.radius[0] = 2f;
  94. while (serie.radius[0] < serie.radius[1] * 0.7f)
  95. {
  96. serie.radius[0] += m_RadiusSpeed * Time.deltaTime;
  97. chart.RefreshChart();
  98. yield return null;
  99. }
  100. serie.gap = 1f;
  101. chart.RefreshChart();
  102. yield return new WaitForSeconds(1);
  103. serie.data[0].selected = true;
  104. chart.RefreshChart();
  105. yield return new WaitForSeconds(1);
  106. serie.gap = 0f;
  107. serie.data[0].selected = false;
  108. chart.RefreshChart();
  109. yield return new WaitForSeconds(1);
  110. }
  111. IEnumerator DoublePie()
  112. {
  113. chart.GetChartComponent<Title>().subText = "多图组合";
  114. serie1 = chart.AddSerie<Pie>("访问来源2");
  115. chart.AddData(1, 335, "直达");
  116. chart.AddData(1, 679, "营销广告");
  117. chart.AddData(1, 1548, "搜索引擎");
  118. serie1.radius[0] = 0;
  119. serie1.radius[1] = 2f;
  120. serie1.center[0] = 0.5f;
  121. serie1.center[1] = 0.4f;
  122. chart.RefreshChart();
  123. while (serie1.radius[1] < serie.radius[0] * 0.75f)
  124. {
  125. serie1.radius[1] += m_RadiusSpeed * Time.deltaTime;
  126. chart.RefreshChart();
  127. yield return null;
  128. }
  129. serie1.label.show = true;
  130. serie1.label.position = LabelStyle.Position.Inside;
  131. serie1.label.textStyle.color = Color.white;
  132. serie1.label.textStyle.fontSize = 14;
  133. chart.RefreshChart();
  134. yield return new WaitForSeconds(1);
  135. }
  136. IEnumerator RosePie()
  137. {
  138. chart.GetChartComponent<Title>().subText = "玫瑰图";
  139. chart.GetChartComponent<Legend>().show = false;
  140. serie1.ClearData();
  141. serie.ClearData();
  142. serie1.radius = serie.radius = new float[2] { 0, 80 };
  143. serie1.label.position = LabelStyle.Position.Outside;
  144. serie1.labelLine.lineType = LabelLine.LineType.Curves;
  145. serie1.label.textStyle.color = Color.clear;
  146. for (int i = 0; i < 2; i++)
  147. {
  148. chart.AddData(i, 10, "rose1");
  149. chart.AddData(i, 5, "rose2");
  150. chart.AddData(i, 15, "rose3");
  151. chart.AddData(i, 25, "rose4");
  152. chart.AddData(i, 20, "rose5");
  153. chart.AddData(i, 35, "rose6");
  154. chart.AddData(i, 30, "rose7");
  155. chart.AddData(i, 40, "rose8");
  156. }
  157. while (serie.center[0] > 0.25f || serie1.center[0] < 0.7f)
  158. {
  159. if (serie.center[0] > 0.25f) serie.center[0] -= m_CenterSpeed * Time.deltaTime;
  160. if (serie1.center[0] < 0.7f) serie1.center[0] += m_CenterSpeed * Time.deltaTime;
  161. chart.RefreshChart();
  162. yield return null;
  163. }
  164. yield return new WaitForSeconds(1);
  165. while (serie.radius[0] > 3f)
  166. {
  167. serie.radius[0] -= m_RadiusSpeed * Time.deltaTime;
  168. serie1.radius[0] -= m_RadiusSpeed * Time.deltaTime;
  169. chart.RefreshChart();
  170. yield return null;
  171. }
  172. serie.radius[0] = 0;
  173. serie1.radius[0] = 0;
  174. serie.pieRoseType = RoseType.Area;
  175. serie1.pieRoseType = RoseType.Radius;
  176. chart.RefreshChart();
  177. yield return new WaitForSeconds(1);
  178. }
  179. }
  180. }