mycharts.js 20 KB

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