EchartRender.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. <template>
  2. <div class="echart-render chart-box">
  3. <h3 class="chart-title">{{ chartTitle }}</h3>
  4. <v-chart
  5. :options="chartOption"
  6. :init-options="initOptions"
  7. v-if="chartOption"
  8. autoresize
  9. ref="vueChart"
  10. ></v-chart>
  11. <p class="chart-none" v-else>暂无数据</p>
  12. </div>
  13. </template>
  14. <script>
  15. import echarts from "echarts/lib/echarts";
  16. export default {
  17. name: "echart-render",
  18. props: {
  19. chartType: {
  20. type: String,
  21. required: true,
  22. validator(value) {
  23. return (
  24. [
  25. "bar",
  26. "barReverse",
  27. "pie",
  28. "line",
  29. "darkLines",
  30. "barGroup",
  31. "lineGroup"
  32. ].indexOf(value) !== -1
  33. );
  34. }
  35. },
  36. rendererType: {
  37. type: String,
  38. default: "canvas",
  39. validator(val) {
  40. return ["canvas", "svg"].indexOf(val) !== -1;
  41. }
  42. },
  43. animationIsOpen: {
  44. type: Boolean,
  45. default: true
  46. },
  47. chartData: {
  48. type: [Object, Array],
  49. required: true
  50. },
  51. chartTitle: {
  52. type: String,
  53. default: ""
  54. },
  55. chartColor: {
  56. type: Array,
  57. default() {
  58. return [
  59. "#22C0FF",
  60. "#41CD7D",
  61. "#FF7786",
  62. "#4E7CFF",
  63. "#6d32f9",
  64. "#dd7755"
  65. ];
  66. }
  67. }
  68. },
  69. data() {
  70. return {
  71. chartOption: null,
  72. initOptions: { renderer: "canvas" }
  73. };
  74. },
  75. mounted() {
  76. if (this.rendererType) this.initOptions.renderer = this.rendererType;
  77. this.getOptions();
  78. },
  79. methods: {
  80. getOptions() {
  81. const name = this.chartType[0].toUpperCase() + this.chartType.slice(1);
  82. this.chartOption = this[`get${name}Option`](this.chartData);
  83. },
  84. getDataURL(options) {
  85. return this.$refs.vueChart && this.$refs.vueChart.getDataURL(options);
  86. },
  87. getLineOption(datas) {
  88. let labels = [],
  89. vals = [];
  90. datas.map(item => {
  91. labels.push(item.name);
  92. vals.push(item.value);
  93. });
  94. const linearColor = new echarts.graphic.LinearGradient(0, 1, 0, 0, [
  95. {
  96. offset: 1,
  97. color: "rgba(34,192,255,1)"
  98. },
  99. {
  100. offset: 0,
  101. color: "rgba(34,192,255,0)"
  102. }
  103. ]);
  104. return {
  105. animation: this.animationIsOpen,
  106. grid: {
  107. top: "10%",
  108. bottom: "12%",
  109. left: "5%",
  110. right: "5%"
  111. },
  112. tooltip: {
  113. show: true,
  114. formatter: function(params) {
  115. return params.value + "%";
  116. }
  117. },
  118. xAxis: {
  119. type: "category",
  120. data: labels,
  121. splitLine: {
  122. show: true,
  123. lineStyle: {
  124. color: "rgba(231,234,241,1)"
  125. }
  126. },
  127. axisLine: {
  128. lineStyle: {
  129. color: "rgba(231,234,241,1)"
  130. }
  131. },
  132. axisLabel: {
  133. color: "#7C86A3",
  134. fontSize: 12,
  135. fontWeight: "bold"
  136. },
  137. axisTick: {
  138. show: false
  139. }
  140. },
  141. yAxis: {
  142. type: "value",
  143. splitLine: {
  144. show: true,
  145. lineStyle: {
  146. color: "rgba(231,234,241,1)"
  147. }
  148. },
  149. axisLine: {
  150. lineStyle: {
  151. color: "rgba(231,234,241,1)"
  152. }
  153. },
  154. axisLabel: {
  155. fontSize: 12,
  156. color: "#7C86A3",
  157. formatter: function(value, index) {
  158. return value + "%";
  159. }
  160. },
  161. axisTick: {
  162. show: false
  163. }
  164. },
  165. series: [
  166. {
  167. name: "数量",
  168. type: "line",
  169. smooth: true,
  170. data: vals,
  171. itemStyle: {
  172. color: "rgba(34, 192, 255, 1)"
  173. },
  174. lineStyle: {
  175. color: "rgba(34, 192, 255, 1)"
  176. },
  177. areaStyle: {
  178. color: linearColor
  179. }
  180. }
  181. ]
  182. };
  183. },
  184. getDarkLinesOption(datas) {
  185. if (!datas.length) return;
  186. const labels = datas[0].data.map(item => item.name);
  187. const colors = [
  188. {
  189. color: "rgba(21, 91, 146,1)",
  190. lineColor: "rgba(21, 91, 146,0.6)",
  191. linearColor: new echarts.graphic.LinearGradient(0, 1, 0, 0, [
  192. {
  193. offset: 1,
  194. color: "rgba(21, 91, 146,1)"
  195. },
  196. {
  197. offset: 0,
  198. color: "rgba(21, 91, 146,0.2)"
  199. }
  200. ]),
  201. z: 4
  202. },
  203. {
  204. color: "rgba(204, 70, 53,1)",
  205. lineColor: "rgba(204, 70, 53,0.6)",
  206. linearColor: new echarts.graphic.LinearGradient(0, 1, 0, 0, [
  207. {
  208. offset: 1,
  209. color: "rgba(204, 70, 53,1)"
  210. },
  211. {
  212. offset: 0,
  213. color: "rgba(204, 70, 53,0)"
  214. }
  215. ]),
  216. z: 3
  217. }
  218. ];
  219. const series = datas.map(function(item, index) {
  220. const data = item.data.map(function(elem) {
  221. return elem.value;
  222. });
  223. const color = colors[index];
  224. return {
  225. name: item.name,
  226. type: "line",
  227. smooth: true,
  228. data: data,
  229. symbol: "circle",
  230. symbolSize: 6,
  231. z: color.z,
  232. itemStyle: {
  233. color: color.color
  234. },
  235. lineStyle: {
  236. color: color.lineColor
  237. },
  238. areaStyle: {
  239. color: color.linearColor
  240. }
  241. };
  242. });
  243. return {
  244. animation: this.animationIsOpen,
  245. grid: {
  246. top: "10%",
  247. bottom: "12%",
  248. left: "3.5%",
  249. right: "3%"
  250. },
  251. tooltip: {
  252. show: true,
  253. formatter: function(params) {
  254. return params.value + "%";
  255. }
  256. },
  257. legend: {
  258. data: datas.map(data => data.name),
  259. right: "3%",
  260. itemWidth: 14,
  261. textStyle: {
  262. color: "#9d9c9c",
  263. fontSize: 14
  264. }
  265. },
  266. xAxis: {
  267. type: "category",
  268. data: labels,
  269. splitLine: {
  270. show: true,
  271. lineStyle: {
  272. color: "#3d3f55"
  273. }
  274. },
  275. axisLine: {
  276. lineStyle: {
  277. color: "#3d3f55"
  278. }
  279. },
  280. axisLabel: {
  281. color: "#9d9c9c",
  282. fontSize: 12,
  283. fontWeight: "bold"
  284. },
  285. axisTick: {
  286. show: false
  287. }
  288. },
  289. yAxis: {
  290. type: "value",
  291. splitLine: {
  292. show: true,
  293. lineStyle: {
  294. color: "#3d3f55"
  295. }
  296. },
  297. axisLine: {
  298. lineStyle: {
  299. color: "#3d3f55"
  300. }
  301. },
  302. axisLabel: {
  303. fontSize: 12,
  304. color: "#9d9c9c",
  305. formatter: "{value}%"
  306. },
  307. axisTick: {
  308. show: false
  309. }
  310. },
  311. series: series
  312. };
  313. },
  314. getPieOption(datas) {
  315. if (!datas.chartLabels.length) return;
  316. const seriesData = datas.chartLabels.map(function(item, index) {
  317. return {
  318. name: item,
  319. value: datas.chartData[0][index]
  320. };
  321. });
  322. return {
  323. animation: this.animationIsOpen,
  324. grid: {
  325. top: "24%",
  326. bottom: "10%"
  327. },
  328. tooltip: {
  329. trigger: "item",
  330. formatter: "{a} <br/>{b} : {c} ({d}%)"
  331. },
  332. legend: {
  333. show: true,
  334. itemGap: 20,
  335. itemWidth: 20,
  336. textStyle: {
  337. fontSize: 16
  338. }
  339. },
  340. series: [
  341. {
  342. name: "数量",
  343. type: "pie",
  344. radius: "70%",
  345. data: seriesData,
  346. label: {
  347. show: false
  348. },
  349. itemStyle: {
  350. emphasis: {
  351. shadowBlur: 10,
  352. shadowOffsetX: 0,
  353. shadowColor: "rgba(0, 0, 0, 0.5)"
  354. }
  355. }
  356. }
  357. ]
  358. };
  359. },
  360. getBarOption(datas) {
  361. if (!datas.names.length) return;
  362. return {
  363. animation: this.animationIsOpen,
  364. color: this.chartColor,
  365. grid: {
  366. top: "20%",
  367. bottom: "10%",
  368. left: "8%",
  369. right: "5%"
  370. },
  371. tooltip: {
  372. show: true
  373. },
  374. xAxis: {
  375. type: "category",
  376. data: datas.names,
  377. axisLine: {
  378. lineStyle: {
  379. color: "rgba(231,234,241,1)"
  380. }
  381. },
  382. axisLabel: {
  383. color: "#7C86A3",
  384. fontSize: 12,
  385. fontWeight: "bold"
  386. },
  387. axisTick: {
  388. show: false
  389. }
  390. },
  391. yAxis: {
  392. type: "value",
  393. splitLine: {
  394. show: true,
  395. lineStyle: {
  396. color: "rgba(231,234,241,1)"
  397. }
  398. },
  399. axisLine: {
  400. lineStyle: {
  401. color: "rgba(231,234,241,1)"
  402. }
  403. },
  404. axisLabel: {
  405. color: "#7C86A3",
  406. fontSize: 12,
  407. fontWeight: "bold"
  408. },
  409. axisTick: {
  410. show: false
  411. }
  412. },
  413. series: [
  414. {
  415. name: this.chartTitle || "值",
  416. type: "bar",
  417. barMaxWidth: 24,
  418. data: datas.dataList,
  419. label: {
  420. show: true,
  421. position: "top",
  422. color: "#666"
  423. },
  424. itemStyle: {
  425. barBorderRadius: 4
  426. }
  427. }
  428. ]
  429. };
  430. },
  431. getBarReverseOption(datas) {
  432. if (!datas.names.length) return;
  433. return {
  434. animation: this.animationIsOpen,
  435. color: this.chartColor,
  436. grid: {
  437. top: "20%",
  438. bottom: "10%",
  439. left: "12%",
  440. right: "5%"
  441. },
  442. tooltip: {
  443. show: true
  444. },
  445. yAxis: {
  446. type: "category",
  447. data: datas.names,
  448. axisLine: {
  449. lineStyle: {
  450. color: "rgba(231,234,241,1)"
  451. }
  452. },
  453. splitLine: {
  454. show: true,
  455. lineStyle: {
  456. color: "rgba(231,234,241,1)"
  457. }
  458. },
  459. axisLabel: {
  460. color: "#7C86A3",
  461. fontSize: 12,
  462. fontWeight: "bold"
  463. },
  464. axisTick: {
  465. show: false
  466. }
  467. },
  468. xAxis: {
  469. type: "value",
  470. splitLine: {
  471. show: false,
  472. lineStyle: {
  473. color: "rgba(231,234,241,1)"
  474. }
  475. },
  476. axisLine: {
  477. lineStyle: {
  478. color: "rgba(231,234,241,1)"
  479. }
  480. },
  481. axisLabel: {
  482. color: "#7C86A3",
  483. fontSize: 12,
  484. fontWeight: "bold"
  485. },
  486. axisTick: {
  487. show: false
  488. }
  489. },
  490. series: [
  491. {
  492. name: this.chartTitle || "值",
  493. type: "bar",
  494. barMaxWidth: 24,
  495. data: datas.dataList,
  496. label: {
  497. show: true,
  498. position: "right",
  499. color: "#666"
  500. },
  501. itemStyle: {
  502. barBorderRadius: 4
  503. }
  504. }
  505. ]
  506. };
  507. },
  508. getBarGroupOption(datas) {
  509. if (!datas.names.length) return;
  510. const xAxis = datas.dataList.map(function(item) {
  511. return item.name;
  512. });
  513. const series = datas.names.map(function(name, index) {
  514. const data = datas.dataList.map(function(item) {
  515. return item.data[index];
  516. });
  517. return {
  518. name: name,
  519. type: "bar",
  520. data: data,
  521. barMaxWidth: 12,
  522. itemStyle: {
  523. barBorderRadius: 2
  524. }
  525. };
  526. });
  527. const options = {
  528. animation: this.animationIsOpen,
  529. color: this.chartColor,
  530. grid: {
  531. top: "20%",
  532. bottom: "10%",
  533. left: "8%",
  534. right: "5%"
  535. },
  536. tooltip: {
  537. show: true,
  538. trigger: "axis",
  539. axisPointer: {
  540. type: "shadow"
  541. },
  542. formatter: function(params) {
  543. const label = params[0].axisValueLabel;
  544. let infos = params.map(function(item) {
  545. return item.seriesName + ":" + item.value.toFixed(2) + "%";
  546. });
  547. infos.unshift(label);
  548. return infos.join("<br/>");
  549. }
  550. },
  551. legend: {
  552. data: datas.names,
  553. right: 0,
  554. itemWidth: 14,
  555. textStyle: {
  556. fontSize: 16
  557. }
  558. },
  559. xAxis: {
  560. type: "category",
  561. data: xAxis,
  562. axisLabel: {
  563. color: "#7C86A3",
  564. fontSize: 12,
  565. fontWeight: "bold"
  566. },
  567. axisTick: {
  568. show: false
  569. }
  570. },
  571. yAxis: {
  572. type: "value",
  573. splitLine: {
  574. show: true,
  575. lineStyle: {
  576. color: "rgba(231,234,241,1)"
  577. }
  578. },
  579. axisLine: {
  580. lineStyle: {
  581. color: "rgba(231,234,241,1)"
  582. }
  583. },
  584. axisLabel: {
  585. color: "#7C86A3",
  586. fontSize: 12,
  587. fontWeight: "bold",
  588. formatter: function(value, index) {
  589. return value + "%";
  590. }
  591. },
  592. axisTick: {
  593. show: false
  594. }
  595. },
  596. series: series
  597. };
  598. return options;
  599. },
  600. getLineGroupOption(datas) {
  601. if (!datas.length) return;
  602. const names = datas.map(function(item) {
  603. return item.name;
  604. });
  605. const xaxis = datas[0].dataList.map(function(item, index) {
  606. return index;
  607. });
  608. const series = datas.map(function(item) {
  609. return {
  610. name: item.name,
  611. type: "line",
  612. symbol: "circle",
  613. smooth: true,
  614. itemStyle: {
  615. emphasis: {
  616. color: "#333"
  617. }
  618. },
  619. data: item.dataList.map(function(num) {
  620. return num * num;
  621. })
  622. };
  623. });
  624. return {
  625. animation: this.animationIsOpen,
  626. grid: {
  627. top: "15%",
  628. bottom: "12%"
  629. },
  630. tooltip: {
  631. show: true,
  632. trigger: "axis",
  633. axisPointer: {
  634. type: "shadow"
  635. },
  636. formatter: function(params) {
  637. const label = params[0].axisValueLabel;
  638. let infos = params.map(function(item) {
  639. return item.seriesName + ":" + Math.sqrt(item.value);
  640. });
  641. infos.unshift(label);
  642. return infos.join("<br/>");
  643. }
  644. },
  645. legend: {
  646. data: names,
  647. right: 0,
  648. itemWidth: 14,
  649. textStyle: {
  650. fontSize: 16
  651. }
  652. },
  653. xAxis: {
  654. type: "category",
  655. data: xaxis,
  656. axisLabel: {
  657. show: false,
  658. fontSize: 14
  659. },
  660. axisTick: {
  661. show: false
  662. }
  663. },
  664. yAxis: {
  665. type: "value",
  666. interval: 1,
  667. splitLine: {
  668. show: false
  669. },
  670. axisLabel: {
  671. fontSize: 14,
  672. formatter: function(value) {
  673. const num = Math.sqrt(value);
  674. return num % 1 ? "" : parseInt(num);
  675. }
  676. },
  677. axisTick: {
  678. show: false
  679. }
  680. },
  681. dataZoom: [
  682. {
  683. type: "inside",
  684. start: 0,
  685. end: 30
  686. },
  687. {
  688. type: "slider",
  689. start: 0,
  690. end: 30
  691. }
  692. ],
  693. series: series
  694. };
  695. }
  696. }
  697. };
  698. </script>