mycharts.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. /**
  2. * echarts简单封装
  3. * 冯海夫
  4. * 20201008
  5. */
  6. var ChartsService = {
  7. // 通用的柱状图
  8. // divId:待显示的div id, data:json对象
  9. initCommonBar: function (divId, data) {
  10. var myChart = echarts.init(document.getElementById(divId));
  11. const xDataList = [];
  12. const yDataList = [];
  13. for (var val in data) {
  14. xDataList.push(val);
  15. yDataList.push(data[val]);
  16. }
  17. var option = {
  18. color: ['#37a2da'],
  19. grid:{
  20. left:"20%",
  21. right:"10%",
  22. bottom:"25%",
  23. top:"10%"
  24. },
  25. xAxis: {
  26. type: 'category',
  27. data: xDataList,
  28. axisLabel: {
  29. show: true,
  30. textStyle: {
  31. color: '#ffffff', //更改坐标轴文字颜色
  32. fontSize: 14 //更改坐标轴文字大小
  33. },
  34. interval:0,
  35. rotate:30
  36. },
  37. axisLine: {
  38. lineStyle: {
  39. type: 'solid',
  40. color: '#37a2da', //左边线的颜色
  41. width: '1' //坐标线的宽度
  42. }
  43. },
  44. splitLine: {
  45. show: false
  46. }
  47. },
  48. yAxis: {
  49. type: 'value',
  50. axisLabel: {
  51. show: true,
  52. textStyle: {
  53. color: '#ffffff', //更改坐标轴文字颜色
  54. fontSize: 14 //更改坐标轴文字大小
  55. }
  56. },
  57. axisLine: {
  58. lineStyle: {
  59. type: 'solid',
  60. color: '#37a2da', //左边线的颜色
  61. width: '1' //坐标线的宽度
  62. }
  63. },
  64. splitLine: {
  65. show: true,
  66. lineStyle: {
  67. color: ['#37a2da'],
  68. width: 1,
  69. type: 'solid'
  70. }
  71. }
  72. },
  73. series: [{
  74. data: yDataList,
  75. type: 'bar',
  76. showBackground: false,
  77. backgroundStyle: {
  78. color: 'rgba(220, 220, 220, 0.8)'
  79. },
  80. itemStyle: {
  81. normal: {
  82. label: {
  83. show: true,
  84. position: 'top',
  85. textStyle: {
  86. color: '#ffffff'
  87. }
  88. }
  89. }
  90. },
  91. }]
  92. };
  93. // 使用刚指定的配置项和数据显示图表。
  94. myChart.setOption(option);
  95. window.addEventListener("resize", function () {
  96. myChart.resize();
  97. });
  98. },
  99. // 带阴影柱状图柱状图
  100. // divId:待显示的div id, data:json对象
  101. initBarWithShade: function (divId, data) {
  102. var myChart = echarts.init(document.getElementById(divId));
  103. const xDataList = [];
  104. const yDataList = [];
  105. for (var val in data) {
  106. xDataList.push(val);
  107. yDataList.push(data[val]);
  108. }
  109. var option = {
  110. color: ['#37a2da'],
  111. grid:{
  112. left:"20%",
  113. right:"10%",
  114. bottom:"25%",
  115. top:"10%"
  116. },
  117. xAxis: {
  118. type: 'category',
  119. data: xDataList,
  120. axisLabel: {
  121. show: true,
  122. textStyle: {
  123. color: '#ffffff', //更改坐标轴文字颜色
  124. fontSize: 14 //更改坐标轴文字大小
  125. },
  126. interval:0,
  127. rotate:30
  128. },
  129. axisLine: {
  130. lineStyle: {
  131. type: 'solid',
  132. color: '#37a2da', //左边线的颜色
  133. width: '1' //坐标线的宽度
  134. }
  135. },
  136. splitLine: {
  137. show: false
  138. }
  139. },
  140. yAxis: {
  141. type: 'value',
  142. axisLabel: {
  143. show: true,
  144. textStyle: {
  145. color: '#ffffff', //更改坐标轴文字颜色
  146. fontSize: 14 //更改坐标轴文字大小
  147. }
  148. },
  149. axisLine: {
  150. lineStyle: {
  151. type: 'solid',
  152. color: '#37a2da', //左边线的颜色
  153. width: '1' //坐标线的宽度
  154. }
  155. },
  156. splitLine: {
  157. show: true,
  158. lineStyle: {
  159. color: ['#37a2da'],
  160. width: 1,
  161. type: 'solid'
  162. }
  163. }
  164. },
  165. series: [{
  166. data: yDataList,
  167. type: 'bar',
  168. showBackground: true,
  169. backgroundStyle: {
  170. color: 'rgba(220, 220, 220, 0.8)'
  171. },
  172. itemStyle: {
  173. normal: {
  174. label: {
  175. show: true,
  176. position: 'top',
  177. textStyle: {
  178. color: '#ffffff'
  179. }
  180. }
  181. }
  182. },
  183. }]
  184. };
  185. // 使用刚指定的配置项和数据显示图表。
  186. myChart.setOption(option);
  187. window.addEventListener("resize", function () {
  188. myChart.resize();
  189. });
  190. },
  191. // 上下两端都有数字的柱状图,算比例
  192. // divId:待显示的div id, data:json数组,label,sum1:下柱数,sum2:总熟
  193. initBarWithTwoNumberRate: function (divId, data) {
  194. var myChart = echarts.init(document.getElementById(divId));
  195. const xDataList = [];
  196. const yDataList = [];
  197. const yDataList2 = []
  198. $.each(data, function (index, item) {
  199. xDataList.push(item.label);
  200. const sum1 = (item.sum1 / item.sum2 * 100).toFixed(0);
  201. yDataList.push(sum1);
  202. yDataList2.push(100 - sum1);
  203. });
  204. var option = {
  205. color: ['#37a2da', '#ffd85c'],
  206. grid:{
  207. left:"20%",
  208. right:"10%",
  209. bottom:"25%",
  210. top:"10%"
  211. },
  212. xAxis: {
  213. type: 'category',
  214. data: xDataList,
  215. axisLabel: {
  216. show: true,
  217. textStyle: {
  218. color: '#ffffff', //更改坐标轴文字颜色
  219. fontSize: 14 //更改坐标轴文字大小
  220. },
  221. interval:0,
  222. rotate:30
  223. },
  224. axisLine: {
  225. lineStyle: {
  226. type: 'solid',
  227. color: '#37a2da', //左边线的颜色
  228. width: '1' //坐标线的宽度
  229. }
  230. },
  231. splitLine: {
  232. show: false
  233. }
  234. },
  235. yAxis: {
  236. type: 'value',
  237. axisLabel: {
  238. show: true,
  239. textStyle: {
  240. color: '#ffffff', //更改坐标轴文字颜色
  241. fontSize: 14 //更改坐标轴文字大小
  242. }
  243. },
  244. axisLine: {
  245. lineStyle: {
  246. type: 'solid',
  247. color: '#37a2da', //左边线的颜色
  248. width: '1' //坐标线的宽度
  249. }
  250. },
  251. splitLine: {
  252. show: true,
  253. lineStyle: {
  254. color: ['#37a2da'],
  255. width: 1,
  256. type: 'solid'
  257. }
  258. }
  259. },
  260. series: [{
  261. name: '打卡',
  262. type: 'bar',
  263. stack: '总量',
  264. label: {
  265. show: true,
  266. position: 'insideRight'
  267. },
  268. data: yDataList
  269. },
  270. {
  271. name: '未打卡',
  272. type: 'bar',
  273. stack: '总量',
  274. label: {
  275. show: true,
  276. position: 'insideRight'
  277. },
  278. data: yDataList2
  279. }
  280. ]
  281. };
  282. // 使用刚指定的配置项和数据显示图表。
  283. myChart.setOption(option);
  284. window.addEventListener("resize", function () {
  285. myChart.resize();
  286. });
  287. },
  288. // 折线图,带阴影
  289. // divId:待显示的div id, data:json对象
  290. initLineWithShade: function (divId, data) {
  291. var myChart = echarts.init(document.getElementById(divId));
  292. const xDataList = [];
  293. const yDataList = [];
  294. for (var val in data) {
  295. xDataList.push(val);
  296. yDataList.push(data[val]);
  297. }
  298. var option = {
  299. color: ['#37a2da'],
  300. grid:{
  301. left:"20%",
  302. right:"10%",
  303. bottom:"25%",
  304. top:"10%"
  305. },
  306. xAxis: {
  307. type: 'category',
  308. boundaryGap: false,
  309. data: xDataList,
  310. axisLabel: {
  311. show: true,
  312. textStyle: {
  313. color: '#ffffff', //更改坐标轴文字颜色
  314. fontSize: 14 //更改坐标轴文字大小
  315. },
  316. interval:0,
  317. rotate:30
  318. },
  319. axisLine: {
  320. lineStyle: {
  321. type: 'solid',
  322. color: '#37a2da', //左边线的颜色
  323. width: '1' //坐标线的宽度
  324. }
  325. },
  326. splitLine: {
  327. show: false
  328. }
  329. },
  330. yAxis: {
  331. type: 'value',
  332. axisLabel: {
  333. show: true,
  334. textStyle: {
  335. color: '#ffffff', //更改坐标轴文字颜色
  336. fontSize: 14 //更改坐标轴文字大小
  337. }
  338. },
  339. axisLine: {
  340. lineStyle: {
  341. type: 'solid',
  342. color: '#37a2da', //左边线的颜色
  343. width: '1' //坐标线的宽度
  344. }
  345. },
  346. splitLine: {
  347. show: true,
  348. lineStyle: {
  349. color: ['#37a2da'],
  350. width: 1,
  351. type: 'solid'
  352. }
  353. }
  354. },
  355. series: [{
  356. data: yDataList,
  357. type: 'line',
  358. areaStyle: {},
  359. itemStyle: {
  360. normal: {
  361. label: {
  362. show: true,
  363. position: 'top',
  364. textStyle: {
  365. color: '#ffffff'
  366. }
  367. }
  368. }
  369. }
  370. }]
  371. };
  372. // 使用刚指定的配置项和数据显示图表。
  373. myChart.setOption(option);
  374. window.addEventListener("resize", function () {
  375. myChart.resize();
  376. });
  377. },
  378. // 通用的饼图
  379. // divId:待显示的div id, data:json对象
  380. initCommonPie: function (divId, data) {
  381. var myChart = echarts.init(document.getElementById(divId));
  382. const chartData = [];
  383. var total = 0;
  384. for (var val in data) {
  385. total += data[val] * 1;
  386. }
  387. for (var val in data) {
  388. const desc = val + " " + data[val] + " " + (data[val] * 1 / total * 100).toFixed(1) + "%";
  389. chartData.push({
  390. value: data[val],
  391. name: desc
  392. });
  393. }
  394. var option = {
  395. color: ['#37a2da', '#ffd85c', '#67e0e3', '#ff9f7f'],
  396. title: {
  397. text: '',
  398. subtext: '',
  399. left: 'center'
  400. },
  401. tooltip: {
  402. trigger: 'item',
  403. formatter: '{a} <br/>{b} : {c} ({d}%)'
  404. },
  405. series: [{
  406. name: '',
  407. type: 'pie',
  408. radius: '100%',
  409. center: ['50%', '50%'],
  410. data: chartData,
  411. label: {
  412. normal: {
  413. show: true,
  414. position: 'inner'
  415. }
  416. },
  417. emphasis: {
  418. itemStyle: {
  419. shadowBlur: 0,
  420. shadowOffsetX: 0,
  421. shadowColor: 'rgba(0, 0, 0, 0.5)'
  422. }
  423. }
  424. }]
  425. };
  426. // 使用刚指定的配置项和数据显示图表。
  427. myChart.setOption(option);
  428. window.addEventListener("resize", function () {
  429. myChart.resize();
  430. });
  431. },
  432. // 仪表盘
  433. // divId:待显示的div id, data:json对象:name,value
  434. initCommonPanel: function (divId, data) {
  435. var myChart = echarts.init(document.getElementById(divId));
  436. const dataList = [];
  437. dataList.push(data);
  438. var option = {
  439. tooltip: {
  440. formatter: '{a} <br/>{b} : {c}%'
  441. },
  442. series: [{
  443. radius:"100%",
  444. name: '',
  445. type: 'gauge',
  446. detail: {
  447. formatter: '{value}%'
  448. },
  449. data: dataList,
  450. title: {
  451. textStyle: {
  452. color: '#ffffff', //更改坐标轴文字颜色
  453. fontSize: 12 //更改坐标轴文字大小
  454. }
  455. }
  456. }]
  457. };
  458. // 使用刚指定的配置项和数据显示图表。
  459. myChart.setOption(option);
  460. window.addEventListener("resize", function () {
  461. myChart.resize();
  462. });
  463. },
  464. // 柱状图和折线图对吧
  465. // divId:待显示的div id, data:json数组:label-x坐标名称,label1-y1名称,label2-y2名称,sum1-y1数量,sum2-y2数量
  466. initBarAndLine: function (divId, data) {
  467. var myChart = echarts.init(document.getElementById(divId));
  468. if (data.length == 0)
  469. return;
  470. const yLabel1 = data[0].label1;
  471. const yLabel2 = data[0].label2;
  472. const xDataList = [];
  473. const yDataList1 = [];
  474. const yDataList2 = [];
  475. $.each(data, function (index, item) {
  476. xDataList.push(item.label);
  477. yDataList1.push(item.sum1);
  478. yDataList2.push(item.sum2);
  479. });
  480. var option = {
  481. color: ['#ff9f7f', '#ffd85c'],
  482. grid:{
  483. left:"20%",
  484. right:"10%",
  485. bottom:"25%",
  486. top:"10%"
  487. },
  488. tooltip: {
  489. trigger: 'axis'
  490. },
  491. legend: {
  492. data: [{
  493. name: yLabel1,
  494. textStyle: {
  495. color: '#ffffff'
  496. }
  497. },
  498. {
  499. name: yLabel2,
  500. textStyle: {
  501. color: '#ffffff'
  502. }
  503. }
  504. ]
  505. },
  506. xAxis: {
  507. type: 'category',
  508. boundaryGap: false,
  509. data: xDataList,
  510. axisLabel: {
  511. show: true,
  512. textStyle: {
  513. color: '#ffffff', //更改坐标轴文字颜色
  514. fontSize: 14 //更改坐标轴文字大小
  515. },
  516. interval:0,
  517. rotate:30
  518. },
  519. axisLine: {
  520. lineStyle: {
  521. type: 'solid',
  522. color: '#37a2da', //左边线的颜色
  523. width: '1' //坐标线的宽度
  524. }
  525. },
  526. splitLine: {
  527. show: false
  528. }
  529. },
  530. yAxis: {
  531. type: 'value',
  532. axisLabel: {
  533. show: true,
  534. textStyle: {
  535. color: '#ffffff', //更改坐标轴文字颜色
  536. fontSize: 14 //更改坐标轴文字大小
  537. }
  538. },
  539. axisLine: {
  540. lineStyle: {
  541. type: 'solid',
  542. color: '#37a2da', //左边线的颜色
  543. width: '1' //坐标线的宽度
  544. }
  545. },
  546. splitLine: {
  547. show: true,
  548. lineStyle: {
  549. color: ['#37a2da'],
  550. width: 1,
  551. type: 'solid'
  552. }
  553. }
  554. },
  555. series: [{
  556. name: yLabel1,
  557. type: 'line',
  558. stack: '总量',
  559. label: {
  560. show: true
  561. },
  562. data: yDataList1
  563. },
  564. {
  565. name: yLabel2,
  566. type: 'bar',
  567. stack: '总量',
  568. label: {
  569. show: true
  570. },
  571. data: yDataList2
  572. }
  573. ]
  574. };
  575. // 使用刚指定的配置项和数据显示图表。
  576. myChart.setOption(option);
  577. window.addEventListener("resize", function () {
  578. myChart.resize();
  579. });
  580. },
  581. }