暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Example00_CheatSheet.cs 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 Example00_CheatSheet : MonoBehaviour
  9. {
  10. private LineChart chart;
  11. private float speed = 100f;
  12. private void OnEnable()
  13. {
  14. StartCoroutine(CheatSheet());
  15. }
  16. IEnumerator CheatSheet()
  17. {
  18. StartCoroutine(InitChart());
  19. while (true)
  20. {
  21. StartCoroutine(ComponentTitle());
  22. yield return new WaitForSeconds(2);
  23. StartCoroutine(ComponentAxis());
  24. yield return new WaitForSeconds(2);
  25. StartCoroutine(ComponentGrid());
  26. yield return new WaitForSeconds(2);
  27. StartCoroutine(ComponentSerie());
  28. yield return new WaitForSeconds(4);
  29. StartCoroutine(ComponentLegend());
  30. yield return new WaitForSeconds(4);
  31. StartCoroutine(ComponentTheme());
  32. yield return new WaitForSeconds(4);
  33. StartCoroutine(ComponentDataZoom());
  34. yield return new WaitForSeconds(5);
  35. StartCoroutine(ComponentVisualMap());
  36. yield return new WaitForSeconds(3);
  37. }
  38. }
  39. IEnumerator InitChart()
  40. {
  41. chart = gameObject.GetComponent<LineChart>();
  42. if (chart == null) gameObject.AddComponent<LineChart>();
  43. chart.GetOrAddChartComponent<Title>().show = true;
  44. chart.EnsureChartComponent<Title>().text = "术语解析-组件";
  45. var grid = chart.GetOrAddChartComponent<GridCoord>();
  46. grid.bottom = 30;
  47. grid.right = 30;
  48. grid.left = 50;
  49. grid.top = 80;
  50. chart.RemoveChartComponent<VisualMap>();
  51. chart.RemoveData();
  52. chart.AddSerie<Bar>("Bar");
  53. chart.AddSerie<Line>("Line");
  54. for (int i = 0; i < 8; i++)
  55. {
  56. chart.AddXAxisData("x" + (i + 1));
  57. chart.AddData(0, Random.Range(10, 100));
  58. chart.AddData(1, Random.Range(30, 100));
  59. }
  60. yield return null;
  61. }
  62. IEnumerator ComponentTitle()
  63. {
  64. chart.EnsureChartComponent<Title>().text = "术语解析 - 组件";
  65. chart.EnsureChartComponent<Title>().subText = "Title 标题:可指定主标题和子标题";
  66. chart.EnsureChartComponent<XAxis>().show = true;
  67. chart.EnsureChartComponent<YAxis>().show = true;
  68. chart.EnsureChartComponent<Legend>().show = false;
  69. chart.series[0].show = false;
  70. chart.series[1].show = false;
  71. for (int i = 0; i < 4; i++)
  72. {
  73. chart.EnsureChartComponent<Title>().show = !chart.EnsureChartComponent<Title>().show;
  74. chart.RefreshChart();
  75. yield return new WaitForSeconds(0.2f);
  76. }
  77. chart.EnsureChartComponent<Title>().show = true;
  78. chart.RefreshChart();
  79. }
  80. IEnumerator ComponentAxis()
  81. {
  82. chart.EnsureChartComponent<Title>().subText = "Axis 坐标轴:配置X和Y轴的轴线、刻度、标签等样式外观配置";
  83. chart.series[0].show = false;
  84. chart.series[1].show = false;
  85. var xAxis = chart.EnsureChartComponent<XAxis>();
  86. var yAxis = chart.EnsureChartComponent<YAxis>();
  87. for (int i = 0; i < 4; i++)
  88. {
  89. xAxis.show = !xAxis.show;
  90. yAxis.show = !yAxis.show;
  91. chart.RefreshChart();
  92. yield return new WaitForSeconds(0.2f);
  93. }
  94. xAxis.show = true;
  95. yAxis.show = true;
  96. chart.RefreshChart();
  97. yield return new WaitForSeconds(1f);
  98. }
  99. IEnumerator ComponentGrid()
  100. {
  101. chart.EnsureChartComponent<Title>().subText = "Grid 网格:调整坐标系边距和颜色等";
  102. var grid = chart.EnsureChartComponent<GridCoord>();
  103. for (int i = 0; i < 4; i++)
  104. {
  105. grid.backgroundColor = i % 2 == 0 ? Color.clear : Color.grey;
  106. chart.RefreshChart();
  107. yield return new WaitForSeconds(0.2f);
  108. }
  109. grid.backgroundColor = Color.clear;
  110. chart.RefreshChart();
  111. yield return new WaitForSeconds(1f);
  112. }
  113. IEnumerator ComponentSerie()
  114. {
  115. chart.EnsureChartComponent<Title>().subText = "Serie 系列:调整坐标系边距和颜色等";
  116. chart.series[0].show = true;
  117. chart.series[1].show = true;
  118. chart.AnimationReset();
  119. chart.RefreshChart();
  120. yield return new WaitForSeconds(1.2f);
  121. for (int i = 0; i < 4; i++)
  122. {
  123. chart.series[0].show = !chart.series[0].show;
  124. chart.series[1].show = !chart.series[1].show;
  125. chart.RefreshChart();
  126. yield return new WaitForSeconds(0.2f);
  127. }
  128. chart.series[0].show = true;
  129. chart.series[1].show = true;
  130. chart.RefreshChart();
  131. yield return new WaitForSeconds(1f);
  132. }
  133. IEnumerator ComponentLegend()
  134. {
  135. chart.EnsureChartComponent<Title>().subText = "Legend 图例:展示不同系列的名字和颜色,可控制系列显示等";
  136. var legend = chart.EnsureChartComponent<Legend>();
  137. legend.show = true;
  138. var grid = chart.EnsureChartComponent<GridCoord>();
  139. grid.top = 80;
  140. legend.location.top = 50;
  141. chart.RefreshChart();
  142. yield return new WaitForSeconds(1f);
  143. for (int i = 0; i < 4; i++)
  144. {
  145. legend.show = !legend.show;
  146. chart.RefreshChart();
  147. yield return new WaitForSeconds(0.2f);
  148. }
  149. legend.show = true;
  150. chart.RefreshChart();
  151. yield return new WaitForSeconds(1f);
  152. chart.ClickLegendButton(0, "Line", false);
  153. yield return new WaitForSeconds(0.2f);
  154. chart.ClickLegendButton(0, "Line", true);
  155. yield return new WaitForSeconds(0.5f);
  156. chart.ClickLegendButton(1, "Bar", false);
  157. yield return new WaitForSeconds(0.2f);
  158. chart.ClickLegendButton(1, "Bar", true);
  159. yield return new WaitForSeconds(0.5f);
  160. }
  161. IEnumerator ComponentTheme()
  162. {
  163. chart.EnsureChartComponent<Title>().subText = "Theme 主题:可从全局上配置图表的颜色、字体等效果,支持默认主题切换";
  164. yield return new WaitForSeconds(1f);
  165. chart.EnsureChartComponent<Title>().subText = "Theme 主题:Light主题";
  166. chart.UpdateTheme(ThemeType.Light);
  167. yield return new WaitForSeconds(1f);
  168. chart.EnsureChartComponent<Title>().subText = "Theme 主题:Dark主题";
  169. chart.UpdateTheme(ThemeType.Dark);
  170. yield return new WaitForSeconds(1f);
  171. chart.EnsureChartComponent<Title>().subText = "Theme 主题:Default主题";
  172. chart.UpdateTheme(ThemeType.Default);
  173. yield return new WaitForSeconds(1f);
  174. }
  175. IEnumerator ComponentDataZoom()
  176. {
  177. chart.EnsureChartComponent<Title>().subText = "DataZoom 区域缩放:可通过拖、拽、缩小、放大来观察细节数据";
  178. var grid = chart.EnsureChartComponent<GridCoord>();
  179. grid.bottom = 70;
  180. var dataZoom = chart.GetOrAddChartComponent<DataZoom>();
  181. dataZoom.enable = true;
  182. dataZoom.supportInside = true;
  183. dataZoom.supportSlider = true;
  184. dataZoom.start = 0;
  185. dataZoom.end = 100;
  186. chart.RefreshChart();
  187. for (int i = 0; i < 4; i++)
  188. {
  189. dataZoom.supportSlider = !dataZoom.supportSlider;
  190. chart.RefreshChart();
  191. yield return new WaitForSeconds(0.2f);
  192. }
  193. dataZoom.supportSlider = true;
  194. chart.RefreshChart();
  195. yield return new WaitForSeconds(1f);
  196. while (dataZoom.start < 40)
  197. {
  198. dataZoom.start += speed * Time.deltaTime * 0.8f;
  199. chart.RefreshDataZoom();
  200. chart.RefreshChart();
  201. yield return null;
  202. }
  203. while (dataZoom.end > 60)
  204. {
  205. dataZoom.end -= speed * Time.deltaTime * 0.8f;
  206. chart.RefreshDataZoom();
  207. chart.RefreshChart();
  208. yield return null;
  209. }
  210. while (dataZoom.start > 0)
  211. {
  212. dataZoom.start -= speed * Time.deltaTime * 0.8f;
  213. dataZoom.end -= speed * Time.deltaTime * 0.8f;
  214. chart.RefreshDataZoom();
  215. chart.RefreshChart();
  216. yield return null;
  217. }
  218. while (dataZoom.end < 100)
  219. {
  220. dataZoom.start += speed * Time.deltaTime * 0.8f;
  221. dataZoom.end += speed * Time.deltaTime * 0.8f;
  222. chart.RefreshDataZoom();
  223. chart.RefreshChart();
  224. yield return null;
  225. }
  226. while (dataZoom.start > 0 || dataZoom.end < 100)
  227. {
  228. dataZoom.start -= speed * Time.deltaTime * 0.8f;
  229. dataZoom.end += speed * Time.deltaTime * 0.8f;
  230. chart.RefreshDataZoom();
  231. chart.RefreshChart();
  232. yield return null;
  233. }
  234. }
  235. IEnumerator ComponentVisualMap()
  236. {
  237. chart.EnsureChartComponent<Title>().subText = "VisualMap 视觉映射:可从全局上配置图表的颜色、字体等效果,支持默认主题切换";
  238. var visualMap = chart.GetOrAddChartComponent<VisualMap>();
  239. visualMap.show = true;
  240. visualMap.showUI = true;
  241. visualMap.orient = Orient.Vertical;
  242. visualMap.calculable = true;
  243. visualMap.min = 0;
  244. visualMap.max = 100;
  245. visualMap.range[0] = 0;
  246. visualMap.range[1] = 100;
  247. var colors = new List<string>
  248. {
  249. "#313695",
  250. "#4575b4",
  251. "#74add1",
  252. "#abd9e9",
  253. "#e0f3f8",
  254. "#ffffbf",
  255. "#fee090",
  256. "#fdae61",
  257. "#f46d43",
  258. "#d73027",
  259. "#a50026"
  260. };
  261. visualMap.AddColors(colors);
  262. var grid = chart.EnsureChartComponent<GridCoord>();
  263. grid.left = 80;
  264. grid.bottom = 100;
  265. chart.RefreshChart();
  266. yield return new WaitForSeconds(1f);
  267. while (visualMap.rangeMin < 40)
  268. {
  269. visualMap.rangeMin += speed * Time.deltaTime;
  270. chart.RefreshChart();
  271. yield return null;
  272. }
  273. while (visualMap.rangeMax > 60)
  274. {
  275. visualMap.rangeMax -= speed * Time.deltaTime;
  276. chart.RefreshChart();
  277. yield return null;
  278. }
  279. while (visualMap.rangeMin > 0 || visualMap.rangeMax < 100)
  280. {
  281. visualMap.rangeMin -= speed * Time.deltaTime;
  282. visualMap.rangeMax += speed * Time.deltaTime;
  283. chart.RefreshChart();
  284. yield return null;
  285. }
  286. }
  287. }
  288. }