mycharts.js 18 KB

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