dataDancing.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. require.config({
  2. paths:{
  3. echarts:'example/www/js'
  4. }
  5. });
  6. require(
  7. [
  8. 'echarts',
  9. 'echarts/chart/bar',
  10. 'echarts/chart/line',
  11. 'echarts/chart/scatter'
  12. ],
  13. function(ec) {
  14. myChart = ec.init(document.getElementById('main'));
  15. option = {
  16. title : {
  17. text: 'Data Dancing',
  18. x: 'center',
  19. textStyle: {
  20. fontSize: 28
  21. }
  22. },
  23. toolbox: {
  24. show : true,
  25. feature : {
  26. mark : {show: true},
  27. magicType: {show: true, type: ['line', 'bar', 'stack', 'tiled']},
  28. restore : {show: true},
  29. saveAsImage : {show: true}
  30. }
  31. },
  32. xAxis : [
  33. {
  34. type : 'category',
  35. axisLabel:{show:false},
  36. data : (function() {
  37. var res = [];
  38. for (var i = 0; i < 30; i++) {
  39. res.push(i);
  40. }
  41. return res;
  42. })()
  43. }
  44. ],
  45. yAxis : [
  46. {
  47. type : 'value',
  48. splitNumber:6,
  49. min:-30,
  50. max:30,
  51. power:1,
  52. axisLine:{show:false},
  53. axisLabel:{show:false},
  54. splitArea : {show : true}
  55. }
  56. ],
  57. animationDuration: 3000,
  58. animationEasing: 'BounceOut',
  59. addDataAnimation : false,
  60. series : [
  61. {
  62. name:'1',
  63. type:'bar',
  64. itemStyle: {normal: {areaStyle: {type: 'default'}}},
  65. symbol:'none',
  66. barMinHeight:0,
  67. data : dataDanceing[Math.floor(Math.random()*3)](1)
  68. },
  69. {
  70. name:'2',
  71. type:'bar',
  72. itemStyle: {normal: {areaStyle: {type: 'default'}}},
  73. symbol:'none',
  74. barMinHeight:0,
  75. data : dataDanceing[Math.floor(Math.random()*3)](-1)
  76. }
  77. ]
  78. };
  79. danceStep1 = option.series[0].data;
  80. danceStep2 = option.series[1].data;
  81. start(ecReady = true);
  82. }
  83. );
  84. // ------------------------------------
  85. var dataDanceing = [
  86. function(n) {
  87. var res = [];
  88. var p = Math.round(Math.random()*10) % 2 == 0;
  89. for (var i = 0; i < 30; i++) {
  90. res[p ? 'push' : 'unshift'](i * n);
  91. }
  92. //console.log('1--',res);
  93. return res;
  94. },
  95. function(n) {
  96. var res = [];
  97. var p = Math.round(Math.random()*10) % 2 == 0;
  98. for (var i = 0; i < 30; i++) {
  99. i % 2 == 0
  100. ? res[p ? 'push' : 'unshift']((p ? i : (30 - i)) * n)
  101. : res[p ? 'unshift' : 'push']((p ? i : (30 - i)) * n);
  102. }
  103. //console.log('2--',res);
  104. return res;
  105. },
  106. function(n) {
  107. var res = [];
  108. var p = Math.round(Math.random()*10) % 2 == 0;
  109. for (var i = 0; i < 60; i++) {
  110. res[p ? 'push' : 'unshift']((i - 30) * n);
  111. }
  112. //console.log('3--',res);
  113. return res;
  114. },
  115. function(n) {
  116. var res = [];
  117. var p = Math.round(Math.random()*10) % 2 == 0;
  118. for (var i = 0; i < 30; i++) {
  119. res[p ? 'push' : 'unshift']((i * n) * (i % 2 == 0 ? 1 : -1));
  120. }
  121. //console.log('3--',res);
  122. return res;
  123. }
  124. ];
  125. var myChart;
  126. var option;
  127. var ecReady = false;
  128. var password = false;
  129. var mReady = false;
  130. var danceStep1;
  131. var danceStep2;
  132. var timeTicket;
  133. var playing = false;
  134. function start(){
  135. if (ecReady && password && mReady && !playing) {
  136. if (!document.createElement('canvas').getContext) {
  137. alert('亲,换个浏览器吧');
  138. password = false;
  139. playing = false;
  140. return;
  141. }
  142. document.getElementById('main-wrap').className = 'ddshow';
  143. audioV3.play();
  144. playing = true;
  145. myChart.setOption(option, true);
  146. setTimeout(function(){
  147. timeTicket = setInterval(function(){
  148. if (danceStep1.length == 0 && playing) {
  149. danceStep1 = dataDanceing[Math.floor(Math.random()*dataDanceing.length)](Math.round(Math.random()*10)%2==0?1:-1);
  150. }
  151. if (danceStep2.length == 0 && playing) {
  152. danceStep2 = dataDanceing[Math.floor(Math.random()*dataDanceing.length)](Math.round(Math.random()*10)%2==0?1:-1);
  153. }
  154. if (danceStep1.length > 0 && danceStep2.length) {
  155. // 动态数据接口 addData
  156. myChart.addData([
  157. [
  158. 0, // 系列索引
  159. danceStep1.pop(), // 新增数据
  160. false, // 新增数据是否从队列头部插入
  161. false // 是否增加队列长度,false则自定删除原有数据,队头插入删队尾,队尾插入删队头
  162. ],
  163. [
  164. 1, // 系列索引
  165. danceStep2.pop(), // 新增数据
  166. false, // 新增数据是否从队列头部插入
  167. false, // 是否增加队列长度,false则自定删除原有数据,队头插入删队尾,队尾插入删队头
  168. ]
  169. ]);
  170. }
  171. else {
  172. clearInterval(timeTicket);
  173. setTimeout(function(){
  174. option.series[0].data = dataDanceing[1](1);
  175. option.series[1].data = dataDanceing[1](-1);
  176. var _backupSeries = option.series;
  177. var es = [[],[2,3,4,5,6,7,8],[2,5,8],[2,5,8],[],[3,4,5,6,7],[2,8],[2,8],[],[2,3,4,5,6,7,8],[5],[2,3,4,5,6,7,8],[],[4,5,6,7,8],[2,3,6],[4,5,6,7,8],[],[2,3,4,5,6,7,8],[2,5],[2,3,4,6,7,8],[],[2],[2,3,4,5,6,7,8],[2],[],[3,4,8],[2,5,8],[2,6,7]];
  178. var data= [];
  179. for (var i = 0, l = es.length; i < l; i++) {
  180. for (var j = 0, k = es[i].length; j < k; j++) {
  181. data.push([i + 1, 4.5 * (8 - es[i][j]) + 2, 1])
  182. }
  183. }
  184. option.series = [
  185. {
  186. type:'scatter',
  187. symbol:'rectangle',
  188. symbolSize:6,
  189. data: data.splice(0,13)
  190. },
  191. {
  192. type:'scatter',
  193. symbol:'circle',
  194. symbolSize:6,
  195. data: data
  196. },
  197. {
  198. type:'bar',
  199. itemStyle: {normal: {
  200. color: (function(){
  201. var zrColor = require('zrender/tool/color');
  202. return zrColor.getLinearGradient(
  203. 0, 200, 0, 400,
  204. [[0, 'rgba(144,238,144,0.8)'],[0.8, 'rgba(255,255,0,0.8)']]
  205. );
  206. })(),
  207. areaStyle: {type: 'default'}
  208. }},
  209. symbol:'none',
  210. barMinHeight:0,
  211. data : dataDanceing[1](-1)
  212. }
  213. ];
  214. option.title.text= 'Welcome';
  215. myChart.setOption(option, true);
  216. option.series = _backupSeries;
  217. option.title.text = 'Data Dancing';
  218. },500);
  219. }
  220. }, 100);
  221. },3800);
  222. }
  223. }
  224. // --------------------
  225. var audioV3 = document.getElementById('audioV3');
  226. audioV3.addEventListener && audioV3.addEventListener(
  227. 'ended',
  228. function(){
  229. playing = false;
  230. }
  231. )
  232. audioV3.addEventListener && audioV3.addEventListener(
  233. 'canplaythrough',
  234. function() {
  235. start(mReady = true);
  236. }
  237. );
  238. audioV3.src = 'asset/img/dataDancing.mp3';
  239. // -----------
  240. var k = [101,99,104,97,114,116,115];
  241. var progress = 0;
  242. document.body.addEventListener && document.body.addEventListener(
  243. 'keypress',
  244. function(p) {
  245. var curCode = p.keyCode;
  246. if (k[progress] == p.keyCode) {
  247. progress++;
  248. }
  249. else {
  250. progress = 0;
  251. }
  252. progress == k.length && start(password = true, progress = 0);
  253. }
  254. )