echart2.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. // 立即执行函数,防止变量污染 (function() {})();
  2. // 柱状图模块1
  3. (function() {
  4. // 1.实例化对象
  5. var myChart = echarts.init(document.querySelector(".bar .chart"));
  6. function randomData() {
  7. now = new Date(+now + oneDay);
  8. value = value + Math.random() * 21 - 10;
  9. return {
  10. name: now.toString(),
  11. value: [
  12. [now.getFullYear(), now.getMonth() + 1, now.getDate()].join('/'),
  13. Math.round(value)
  14. ]
  15. };
  16. }
  17. var data = [];
  18. var now = +new Date(2020, 9, 3);
  19. var oneDay = 24 * 3600 * 1000;
  20. var value = Math.random() * 1000;
  21. for (var i = 0; i < 1000; i++) {
  22. data.push(randomData());
  23. }
  24. option = {
  25. color: ['green'],
  26. grid: {
  27. top: 6,
  28. // left: 0,
  29. bottom: 4,
  30. },
  31. tooltip: {
  32. trigger: 'axis',
  33. formatter: function(params) {
  34. params = params[0];
  35. var date = new Date(params.name);
  36. return date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear() + ' : ' + params.value[1];
  37. },
  38. axisPointer: {
  39. animation: false
  40. }
  41. },
  42. xAxis: {
  43. type: 'time',
  44. splitLine: {
  45. show: false
  46. },
  47. axisLine: {
  48. lineStyle: {
  49. color: "rgba(255,255,255,.0)"
  50. }
  51. },
  52. axisLabel: {
  53. textStyle: {
  54. color: "rgba(255,255,255,.6)",
  55. fontSize: 12
  56. },
  57. formatter: '{value}'
  58. },
  59. },
  60. yAxis: {
  61. type: 'value',
  62. boundaryGap: [0, '100%'],
  63. splitLine: {
  64. show: false
  65. },
  66. axisLine: {
  67. lineStyle: {
  68. color: "rgba(255,255,255,.2)"
  69. }
  70. },
  71. axisLabel: {
  72. textStyle: {
  73. color: "rgba(255,255,255,.6)",
  74. fontSize: 12
  75. },
  76. formatter: '{value}'
  77. },
  78. },
  79. series: [{
  80. name: '模拟数据',
  81. type: 'line',
  82. showSymbol: false,
  83. hoverAnimation: false,
  84. data: data
  85. }]
  86. };
  87. setInterval(function() {
  88. for (var i = 0; i < 5; i++) {
  89. data.shift();
  90. data.push(randomData());
  91. }
  92. myChart.setOption({
  93. series: [{
  94. data: data
  95. }]
  96. });
  97. }, 1000);
  98. // 3.把配置项给实例对象
  99. myChart.setOption(option);
  100. // 4.让图表随屏幕自适应
  101. window.addEventListener('resize', function() {
  102. myChart.resize();
  103. })
  104. })();
  105. // 柱状图模块2
  106. (function() {
  107. // 1.实例化对象
  108. var myChart = echarts.init(document.querySelector(".bar2 .chart"));
  109. function randomData() {
  110. now = new Date(+now + oneDay);
  111. value = value + Math.random() * 21 - 10;
  112. return {
  113. name: now.toString(),
  114. value: [
  115. [now.getFullYear(), now.getMonth() + 1, now.getDate()].join('/'),
  116. Math.round(value)
  117. ]
  118. };
  119. }
  120. var data = [];
  121. var now = +new Date(2020, 9, 3);
  122. var oneDay = 24 * 3600 * 1000;
  123. var value = Math.random() * 1000;
  124. for (var i = 0; i < 1000; i++) {
  125. data.push(randomData());
  126. }
  127. option = {
  128. color: ['green'],
  129. grid: {
  130. top: 6,
  131. bottom: 4,
  132. },
  133. tooltip: {
  134. trigger: 'axis',
  135. formatter: function(params) {
  136. params = params[0];
  137. var date = new Date(params.name);
  138. return date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear() + ' : ' + params.value[1];
  139. },
  140. axisPointer: {
  141. animation: false
  142. }
  143. },
  144. xAxis: {
  145. type: 'time',
  146. splitLine: {
  147. show: false
  148. },
  149. axisLine: {
  150. lineStyle: {
  151. color: "rgba(255,255,255,.0)"
  152. }
  153. },
  154. axisLabel: {
  155. textStyle: {
  156. color: "rgba(255,255,255,.6)",
  157. fontSize: 12
  158. },
  159. formatter: '{value}'
  160. }
  161. },
  162. yAxis: {
  163. type: 'value',
  164. boundaryGap: [0, '100%'],
  165. splitLine: {
  166. show: false
  167. },
  168. axisLine: {
  169. lineStyle: {
  170. color: "rgba(255,255,255,.2)"
  171. }
  172. },
  173. axisLabel: {
  174. textStyle: {
  175. color: "rgba(255,255,255,.6)",
  176. fontSize: 12
  177. },
  178. formatter: '{value}'
  179. }
  180. },
  181. series: [{
  182. name: '模拟数据',
  183. type: 'line',
  184. showSymbol: false,
  185. hoverAnimation: false,
  186. data: data
  187. }]
  188. };
  189. setInterval(function() {
  190. for (var i = 0; i < 5; i++) {
  191. data.shift();
  192. data.push(randomData());
  193. }
  194. myChart.setOption({
  195. series: [{
  196. data: data
  197. }]
  198. });
  199. }, 1000);
  200. // 3.把配置项给实例对象
  201. myChart.setOption(option);
  202. // 4.让图表随屏幕自适应
  203. window.addEventListener('resize', function() {
  204. myChart.resize();
  205. })
  206. })();
  207. // 折线图模块1
  208. (function() {
  209. var myChart = echarts.init(document.querySelector(".line .chart"));
  210. function randomData() {
  211. now = new Date(+now + oneDay);
  212. value = value + Math.random() * 21 - 10;
  213. return {
  214. name: now.toString(),
  215. value: [
  216. [now.getFullYear(), now.getMonth() + 1, now.getDate()].join('/'),
  217. Math.round(value)
  218. ]
  219. };
  220. }
  221. var data = [];
  222. var now = +new Date(2020, 9, 3);
  223. var oneDay = 24 * 3600 * 1000;
  224. var value = Math.random() * 1000;
  225. for (var i = 0; i < 1000; i++) {
  226. data.push(randomData());
  227. }
  228. option = {
  229. color: ['green'],
  230. grid: {
  231. top: 6,
  232. bottom: 4,
  233. },
  234. tooltip: {
  235. trigger: 'axis',
  236. formatter: function(params) {
  237. params = params[0];
  238. var date = new Date(params.name);
  239. return date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear() + ' : ' + params.value[1];
  240. },
  241. axisPointer: {
  242. animation: false
  243. }
  244. },
  245. xAxis: {
  246. type: 'time',
  247. splitLine: {
  248. show: false
  249. },
  250. axisLine: {
  251. lineStyle: {
  252. color: "rgba(255,255,255,.0)"
  253. }
  254. },
  255. axisLabel: {
  256. textStyle: {
  257. color: "rgba(255,255,255,.6)",
  258. fontSize: 12
  259. },
  260. formatter: '{value}'
  261. }
  262. },
  263. yAxis: {
  264. type: 'value',
  265. boundaryGap: [0, '100%'],
  266. splitLine: {
  267. show: false
  268. },
  269. axisLine: {
  270. lineStyle: {
  271. color: "rgba(255,255,255,.2)"
  272. }
  273. },
  274. axisLabel: {
  275. textStyle: {
  276. color: "rgba(255,255,255,.6)",
  277. fontSize: 12
  278. },
  279. formatter: '{value}'
  280. }
  281. },
  282. series: [{
  283. name: '模拟数据',
  284. type: 'line',
  285. showSymbol: false,
  286. hoverAnimation: false,
  287. data: data
  288. }]
  289. };
  290. setInterval(function() {
  291. for (var i = 0; i < 5; i++) {
  292. data.shift();
  293. data.push(randomData());
  294. }
  295. myChart.setOption({
  296. series: [{
  297. data: data
  298. }]
  299. });
  300. }, 1000);
  301. myChart.setOption(option);
  302. // 4.让图表随屏幕自适应
  303. window.addEventListener('resize', function() {
  304. myChart.resize();
  305. })
  306. // 5.点击切换2020 和 2021 的数据
  307. $('.line h2 a').on('click', function() {
  308. // console.log($(this).index());
  309. // 点击a 之后 根据当前a的索引号 找到对应的 yearData 相关对象
  310. // console.log(yearData[$(this).index()]);
  311. var obj = yearData[$(this).index()];
  312. option.series[0].data = obj.data[0];
  313. option.series[1].data = obj.data[1];
  314. // 选中年份高亮
  315. $('.line h2 a').removeClass('a-active');
  316. $(this).addClass('a-active');
  317. // 需要重新渲染
  318. myChart.setOption(option);
  319. })
  320. })();
  321. // 折线图模块2
  322. (function() {
  323. var myChart = echarts.init(document.querySelector('.line2 .chart'));
  324. function randomData() {
  325. now = new Date(+now + oneDay);
  326. value = value + Math.random() * 21 - 10;
  327. return {
  328. name: now.toString(),
  329. value: [
  330. [now.getFullYear(), now.getMonth() + 1, now.getDate()].join('/'),
  331. Math.round(value)
  332. ]
  333. };
  334. }
  335. var data = [];
  336. var now = +new Date(2020, 9, 3);
  337. var oneDay = 24 * 3600 * 1000;
  338. var value = Math.random() * 1000;
  339. for (var i = 0; i < 1000; i++) {
  340. data.push(randomData());
  341. }
  342. option = {
  343. color: ['green'],
  344. grid: {
  345. top: 6,
  346. bottom: 4,
  347. },
  348. tooltip: {
  349. trigger: 'axis',
  350. formatter: function(params) {
  351. params = params[0];
  352. var date = new Date(params.name);
  353. return date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear() + ' : ' + params.value[1];
  354. },
  355. axisPointer: {
  356. animation: false
  357. }
  358. },
  359. xAxis: {
  360. show: false,
  361. type: 'time',
  362. splitLine: {
  363. show: false
  364. },
  365. axisLine: {
  366. lineStyle: {
  367. color: "rgba(255,255,255,.0)"
  368. }
  369. },
  370. axisLabel: {
  371. textStyle: {
  372. color: "rgba(255,255,255,.6)",
  373. fontSize: 12
  374. },
  375. formatter: '{value}'
  376. },
  377. },
  378. yAxis: {
  379. type: 'value',
  380. boundaryGap: [0, '100%'],
  381. splitLine: {
  382. show: false
  383. },
  384. axisLine: {
  385. lineStyle: {
  386. color: "rgba(255,255,255,.2)"
  387. }
  388. },
  389. axisLabel: {
  390. textStyle: {
  391. color: "rgba(255,255,255,.6)",
  392. fontSize: 12
  393. },
  394. formatter: '{value}'
  395. }
  396. },
  397. series: [{
  398. name: '模拟数据',
  399. type: 'line',
  400. showSymbol: false,
  401. hoverAnimation: false,
  402. data: data
  403. }]
  404. };
  405. setInterval(function() {
  406. for (var i = 0; i < 5; i++) {
  407. data.shift();
  408. data.push(randomData());
  409. }
  410. myChart.setOption({
  411. series: [{
  412. data: data
  413. }]
  414. });
  415. }, 1000);
  416. myChart.setOption(option);
  417. window.addEventListener('resize', function() {
  418. myChart.resize();
  419. })
  420. })();