Нет описания
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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) chart = gameObject.AddComponent<BarChart>();
  38. chart.EnsureChartComponent<Title>().text = "BarChart - 柱状图";
  39. chart.EnsureChartComponent<Title>().subText = "普通柱状图";
  40. var yAxis = chart.EnsureChartComponent<YAxis>();
  41. yAxis.minMaxType = Axis.AxisMinMaxType.Default;
  42. chart.RemoveData();
  43. serie = chart.AddSerie<Bar>("Bar1");
  44. for (int i = 0; i < m_DataNum; i++)
  45. {
  46. chart.AddXAxisData("x" + (i + 1));
  47. chart.AddData(0, UnityEngine.Random.Range(30, 90));
  48. }
  49. yield return new WaitForSeconds(1);
  50. }
  51. IEnumerator BarMutilSerie()
  52. {
  53. chart.EnsureChartComponent<Title>().subText = "多条柱状图";
  54. float now = serie.barWidth - 0.35f;
  55. while (serie.barWidth > 0.35f)
  56. {
  57. serie.barWidth -= now * Time.deltaTime;
  58. chart.RefreshChart();
  59. yield return null;
  60. }
  61. serie2 = chart.AddSerie<Bar>("Bar2");
  62. serie2.lineType = LineType.Normal;
  63. serie2.barWidth = 0.35f;
  64. for (int i = 0; i < m_DataNum; i++)
  65. {
  66. chart.AddData(1, UnityEngine.Random.Range(20, 90));
  67. }
  68. yield return new WaitForSeconds(1);
  69. }
  70. IEnumerator ZebraBar()
  71. {
  72. chart.EnsureChartComponent<Title>().subText = "斑马柱状图";
  73. serie.barType = BarType.Zebra;
  74. serie2.barType = BarType.Zebra;
  75. serie.barZebraWidth = serie.barZebraGap = 4;
  76. serie2.barZebraWidth = serie2.barZebraGap = 4;
  77. chart.RefreshChart();
  78. yield return new WaitForSeconds(1);
  79. }
  80. IEnumerator SameBarAndNotStack()
  81. {
  82. chart.EnsureChartComponent<Title>().subText = "非堆叠同柱";
  83. serie.barType = serie2.barType = BarType.Normal;
  84. serie.stack = "";
  85. serie2.stack = "";
  86. serie.barGap = -1;
  87. serie2.barGap = -1;
  88. yield return new WaitForSeconds(1);
  89. }
  90. IEnumerator SameBarAndStack()
  91. {
  92. chart.EnsureChartComponent<Title>().subText = "堆叠同柱";
  93. serie.barType = serie2.barType = BarType.Normal;
  94. serie.stack = "samename";
  95. serie2.stack = "samename";
  96. yield return new WaitForSeconds(1);
  97. float now = 0.6f - serie.barWidth;
  98. while (serie.barWidth < 0.6f)
  99. {
  100. serie.barWidth += now * Time.deltaTime;
  101. serie2.barWidth += now * Time.deltaTime;
  102. chart.RefreshChart();
  103. yield return null;
  104. }
  105. serie.barWidth = serie2.barWidth;
  106. chart.RefreshChart();
  107. yield return new WaitForSeconds(1);
  108. }
  109. IEnumerator SameBarAndPercentStack()
  110. {
  111. chart.EnsureChartComponent<Title>().subText = "百分比堆叠同柱";
  112. serie.barType = serie2.barType = BarType.Normal;
  113. serie.stack = "samename";
  114. serie2.stack = "samename";
  115. serie.barPercentStack = true;
  116. if (null == serie.label)
  117. {
  118. serie.AddExtraComponent<LabelStyle>();
  119. }
  120. serie.label.show = true;
  121. serie.label.position = LabelStyle.Position.Center;
  122. serie.label.textStyle.color = Color.white;
  123. serie.label.formatter = "{d:f0}%";
  124. if (null == serie2.label)
  125. {
  126. serie2.AddExtraComponent<LabelStyle>();
  127. }
  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. }