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