mycharts.js 23 KB

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