mycharts.js 23 KB

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