demo-c3.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. var chart = c3.generate({
  2. data: {
  3. columns: [
  4. ['data1', 30, 200, 100, 400, 150, 250, 50, 100, 250]
  5. ],
  6. type: 'spline',
  7. selection: {
  8. enabled: true
  9. }
  10. },
  11. color: {
  12. pattern: [color.primary._500, color.info._500, color.success._500, color.danger._500, color.warning._500]
  13. },
  14. grid: {
  15. x: {
  16. show: false
  17. },
  18. y: {
  19. show: true
  20. }
  21. }
  22. });
  23. var defaultMessage = $('#message').html(),
  24. currentIndex = 0,
  25. timer, duration = 1500,
  26. demos = [
  27. function () {
  28. chart.load({
  29. columns: [
  30. ['data2', 100, 30, 200, 320, 50, 150, 230, 80, 150]
  31. ],
  32. type: 'spline'
  33. })
  34. setMessage('Load data2');
  35. setProgressBar('4')
  36. },
  37. function () {
  38. chart.load({
  39. columns: [
  40. ['data3', 70, 90, 170, 220, 100, 110, 130, 40, 50]
  41. ]
  42. })
  43. setMessage('Load data3');
  44. setProgressBar('8')
  45. },
  46. function () {
  47. chart.select(['data1'], [2]);
  48. setMessage('Select point for index 2 of data1');
  49. setProgressBar('12')
  50. },
  51. function () {
  52. chart.select(['data1'], [4, 6]);
  53. setMessage('Select point for index 4,6 of data1');
  54. setProgressBar('16')
  55. },
  56. function () {
  57. chart.unselect();
  58. setMessage('Unselect points');
  59. setProgressBar('20')
  60. },
  61. function () {
  62. chart.focus('data2');
  63. setMessage('Focus on data2');
  64. setProgressBar('24')
  65. },
  66. function () {
  67. chart.focus('data3');
  68. setMessage('Focus on data3');
  69. setProgressBar('28')
  70. },
  71. function () {
  72. chart.revert();
  73. setMessage('Defocus');
  74. setProgressBar('32')
  75. },
  76. function () {
  77. chart.load({
  78. columns: [
  79. ['data1', 300, 230, 400, 520, 230, 250, 330, 280, 250]
  80. ]
  81. })
  82. setMessage('Update data1');
  83. setProgressBar('36')
  84. },
  85. function () {
  86. chart.load({
  87. columns: [
  88. ['data2', 30, 50, 90, 120, 40, 50, 80, 70, 50]
  89. ]
  90. })
  91. setMessage('Update data2');
  92. setProgressBar('40')
  93. },
  94. function () {
  95. chart.regions([{
  96. start: 1,
  97. end: 3
  98. }]);
  99. setMessage('Add region from 1 to 3');
  100. setProgressBar('44')
  101. },
  102. function () {
  103. chart.regions.add([{
  104. start: 6
  105. }]);
  106. setMessage('Add region from 6 to end');
  107. setProgressBar('48')
  108. },
  109. function () {
  110. chart.regions([]);
  111. setMessage('Clear regions');
  112. setProgressBar('52')
  113. },
  114. function () {
  115. chart.xgrids([{
  116. value: 1,
  117. text: 'Label 1'
  118. }, {
  119. value: 4,
  120. text: 'Label 4'
  121. }]);
  122. setMessage('Add x grid lines for 1, 4');
  123. setProgressBar('56')
  124. },
  125. function () {
  126. chart.ygrids.add([{
  127. value: 450,
  128. text: 'Label 450'
  129. }]);
  130. setMessage('Add y grid lines for 450');
  131. setProgressBar('60')
  132. },
  133. function () {
  134. chart.xgrids.remove({
  135. value: 1
  136. });
  137. chart.xgrids.remove({
  138. value: 4
  139. });
  140. setMessage('Remove grid lines for 1, 4');
  141. setProgressBar('64')
  142. },
  143. function () {
  144. chart.ygrids.remove({
  145. value: 450
  146. });
  147. setMessage('Remove grid line for 450');
  148. setProgressBar('68')
  149. },
  150. function () {
  151. chart.transform('bar');
  152. setMessage('Show as bar chart');
  153. setProgressBar('72')
  154. },
  155. function () {
  156. chart.groups([
  157. ['data2', 'data3']
  158. ]);
  159. setMessage('Grouping data2 and data3');
  160. setProgressBar('76')
  161. },
  162. function () {
  163. chart.groups([
  164. ['data1', 'data2', 'data3']
  165. ]);
  166. setMessage('Grouping data1, data2 and data3');
  167. },
  168. function () {
  169. chart.groups([
  170. ['data2', 'data3']
  171. ]);
  172. chart.transform('spline', 'data1');
  173. setMessage('Show data1 as spline');
  174. setProgressBar('80')
  175. },
  176. function () {
  177. chart.unload({
  178. ids: 'data3'
  179. });
  180. setMessage('Unload data3');
  181. setProgressBar('84')
  182. },
  183. function () {
  184. chart.unload({
  185. ids: 'data2'
  186. });
  187. setMessage('Unload data2');
  188. setProgressBar('88')
  189. },
  190. function () {
  191. chart.flow({
  192. columns: [
  193. ['data1', 390, 400, 200, 500]
  194. ],
  195. duration: 1000,
  196. });
  197. setMessage('Flow 4 data');
  198. setProgressBar('92')
  199. },
  200. function () {
  201. // wait for end of transition for flow
  202. },
  203. function () {
  204. chart.flow({
  205. columns: [
  206. ['data1', 190, 230]
  207. ],
  208. });
  209. setMessage('Flow 2 data');
  210. setProgressBar('96')
  211. },
  212. function () {
  213. // wait for end of transition for flow
  214. },
  215. function () {
  216. chart.transform('spline', ['data1', 'data2', 'data3']);
  217. chart.groups([
  218. ['data1'],
  219. ['data2'],
  220. ['data3']
  221. ]);
  222. chart.load({
  223. columns: [
  224. ['data1', 30, 200, 100, 400, 150, 250, 50, 100, 250]
  225. ]
  226. })
  227. setMessage('Finishing demo..');
  228. setProgressBar('100')
  229. stopDemo()
  230. }
  231. ];
  232. function setMessage(message) {
  233. document.getElementById('message').innerHTML = '<div id="demoMessage" class="shadow-lg fs-xl p-3 rounded fadeinup">' + message + '</div>';
  234. }
  235. function setProgressBar(percentage) {
  236. $('#demo-progress').css("width", percentage + "%");
  237. }
  238. function startDemo() {
  239. setMessage('Starting Demo...');
  240. timer = setInterval(function () {
  241. if (currentIndex == demos.length) currentIndex = 0;
  242. demos[currentIndex++]();
  243. }, duration);
  244. $('#playDemo').hide();
  245. $('#pauseDemo').show();
  246. }
  247. function stopDemo() {
  248. clearInterval(timer);
  249. document.getElementById('message').innerHTML = '<div id="demoMessage" class="shadow-lg fs-xl p-3 rounded fadeinup bg-success-500 text-center">Thanks for watching! <br> <button class="btn btn-xs btn-dark mt-2" onclick="startDemo();">Play again</button> </div>';
  250. $('#playDemo').hide();
  251. $('#pauseDemo').hide();
  252. };
  253. function pauseDemo() {
  254. clearInterval(timer);
  255. document.getElementById('message').innerHTML = '<div id="demoMessage" class="shadow-lg fs-xl p-3 rounded highlight"> Demo Paused </div>';
  256. $('#playDemo').show();
  257. $('#pauseDemo').hide();
  258. };