Example20_BarChart.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using System.Collections;
  2. using UnityEngine;
  3. using XCharts.Runtime;
  4. namespace XCharts.Example
  5. {
  6. [DisallowMultipleComponent]
  7. public class Example20_BarChart : MonoBehaviour
  8. {
  9. private BarChart chart;
  10. private Serie serie, serie2;
  11. private int m_DataNum = 5;
  12. void Awake()
  13. {
  14. LoopDemo();
  15. }
  16. private void OnEnable()
  17. {
  18. LoopDemo();
  19. }
  20. void LoopDemo()
  21. {
  22. StopAllCoroutines();
  23. StartCoroutine(PieDemo());
  24. }
  25. IEnumerator PieDemo()
  26. {
  27. StartCoroutine(AddSimpleBar());
  28. yield return new WaitForSeconds(2);
  29. StartCoroutine(BarMutilSerie());
  30. yield return new WaitForSeconds(3);
  31. StartCoroutine(ZebraBar());
  32. yield return new WaitForSeconds(3);
  33. StartCoroutine(SameBarAndNotStack());
  34. yield return new WaitForSeconds(3);
  35. StartCoroutine(SameBarAndStack());
  36. yield return new WaitForSeconds(3);
  37. StartCoroutine(SameBarAndPercentStack());
  38. yield return new WaitForSeconds(10);
  39. LoopDemo();
  40. }
  41. IEnumerator AddSimpleBar()
  42. {
  43. chart = gameObject.GetComponent<BarChart>();
  44. if (chart == null) chart = gameObject.AddComponent<BarChart>();
  45. chart.GetChartComponent<Title>().text = "BarChart - 柱状图";
  46. chart.GetChartComponent<Title>().subText = "普通柱状图";
  47. var yAxis = chart.GetChartComponent<YAxis>();
  48. yAxis.minMaxType = Axis.AxisMinMaxType.Default;
  49. chart.RemoveData();
  50. serie = chart.AddSerie<Bar>("Bar1");
  51. for (int i = 0; i < m_DataNum; i++)
  52. {
  53. chart.AddXAxisData("x" + (i + 1));
  54. chart.AddData(0, UnityEngine.Random.Range(30, 90));
  55. }
  56. yield return new WaitForSeconds(1);
  57. }
  58. IEnumerator BarMutilSerie()
  59. {
  60. chart.GetChartComponent<Title>().subText = "多条柱状图";
  61. float now = serie.barWidth - 0.35f;
  62. while (serie.barWidth > 0.35f)
  63. {
  64. serie.barWidth -= now * Time.deltaTime;
  65. chart.RefreshChart();
  66. yield return null;
  67. }
  68. serie2 = chart.AddSerie<Bar>("Bar2");
  69. serie2.lineType = LineType.Normal;
  70. serie2.barWidth = 0.35f;
  71. for (int i = 0; i < m_DataNum; i++)
  72. {
  73. chart.AddData(1, UnityEngine.Random.Range(20, 90));
  74. }
  75. yield return new WaitForSeconds(1);
  76. }
  77. IEnumerator ZebraBar()
  78. {
  79. chart.GetChartComponent<Title>().subText = "斑马柱状图";
  80. serie.barType = BarType.Zebra;
  81. serie2.barType = BarType.Zebra;
  82. serie.barZebraWidth = serie.barZebraGap = 4;
  83. serie2.barZebraWidth = serie2.barZebraGap = 4;
  84. chart.RefreshChart();
  85. yield return new WaitForSeconds(1);
  86. }
  87. IEnumerator SameBarAndNotStack()
  88. {
  89. chart.GetChartComponent<Title>().subText = "非堆叠同柱";
  90. serie.barType = serie2.barType = BarType.Normal;
  91. serie.stack = "";
  92. serie2.stack = "";
  93. serie.barGap = -1;
  94. serie2.barGap = -1;
  95. yield return new WaitForSeconds(1);
  96. }
  97. IEnumerator SameBarAndStack()
  98. {
  99. chart.GetChartComponent<Title>().subText = "堆叠同柱";
  100. serie.barType = serie2.barType = BarType.Normal;
  101. serie.stack = "samename";
  102. serie2.stack = "samename";
  103. yield return new WaitForSeconds(1);
  104. float now = 0.6f - serie.barWidth;
  105. while (serie.barWidth < 0.6f)
  106. {
  107. serie.barWidth += now * Time.deltaTime;
  108. serie2.barWidth += now * Time.deltaTime;
  109. chart.RefreshChart();
  110. yield return null;
  111. }
  112. serie.barWidth = serie2.barWidth;
  113. chart.RefreshChart();
  114. yield return new WaitForSeconds(1);
  115. }
  116. IEnumerator SameBarAndPercentStack()
  117. {
  118. chart.GetChartComponent<Title>().subText = "百分比堆叠同柱";
  119. serie.barType = serie2.barType = BarType.Normal;
  120. serie.stack = "samename";
  121. serie2.stack = "samename";
  122. serie.barPercentStack = true;
  123. serie.AddExtraComponent<LabelStyle>();
  124. serie.label.show = true;
  125. serie.label.position = LabelStyle.Position.Center;
  126. serie.label.textStyle.color = Color.white;
  127. serie.label.formatter = "{d:f0}%";
  128. serie2.label.show = true;
  129. serie2.label.position = LabelStyle.Position.Center;
  130. serie2.label.textStyle.color = Color.white;
  131. serie2.label.formatter = "{d:f0}%";
  132. serie2.labelDirty = true;
  133. chart.RefreshChart();
  134. yield return new WaitForSeconds(1);
  135. }
  136. }
  137. }