EchartRender.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. <template>
  2. <div class="echart-render chart-box">
  3. <h3 class="chart-title">{{ chartTitle }}</h3>
  4. <v-chart
  5. :options="chartOption"
  6. v-if="chartOption"
  7. autoresize
  8. ref="vueChart"
  9. ></v-chart>
  10. <p class="chart-none" v-else>暂无数据</p>
  11. </div>
  12. </template>
  13. <script>
  14. import echarts from "echarts/lib/echarts";
  15. export default {
  16. name: "echart-render",
  17. props: {
  18. chartType: {
  19. type: String,
  20. required: true,
  21. validator(value) {
  22. return (
  23. ["bar", "barReverse", "pie", "line", "barGroup", "lineGroup"].indexOf(
  24. value
  25. ) !== -1
  26. );
  27. }
  28. },
  29. chartData: {
  30. type: [Object, Array],
  31. required: true
  32. },
  33. chartTitle: {
  34. type: String,
  35. default: ""
  36. },
  37. chartColor: {
  38. type: Array,
  39. default() {
  40. return [
  41. "#22C0FF",
  42. "#41CD7D",
  43. "#FF7786",
  44. "#4E7CFF",
  45. "#6d32f9",
  46. "#dd7755"
  47. ];
  48. }
  49. }
  50. },
  51. data() {
  52. return {
  53. chartOption: null
  54. };
  55. },
  56. mounted() {
  57. this.getOptions();
  58. },
  59. methods: {
  60. getOptions() {
  61. const name = this.chartType[0].toUpperCase() + this.chartType.slice(1);
  62. this.chartOption = this[`get${name}Option`](this.chartData);
  63. },
  64. getLineOption(datas) {
  65. let labels = [],
  66. vals = [];
  67. datas.map(item => {
  68. labels.push(item.name);
  69. vals.push(item.value);
  70. });
  71. const linearColor = new echarts.graphic.LinearGradient(0, 1, 0, 0, [
  72. {
  73. offset: 1,
  74. color: "rgba(34,192,255,1)"
  75. },
  76. {
  77. offset: 0,
  78. color: "rgba(34,192,255,0)"
  79. }
  80. ]);
  81. return {
  82. grid: {
  83. top: "10%",
  84. bottom: "12%",
  85. left: "5%",
  86. right: "5%"
  87. },
  88. tooltip: {
  89. show: true,
  90. formatter: function(params) {
  91. return params.value + "%";
  92. }
  93. },
  94. xAxis: {
  95. type: "category",
  96. data: labels,
  97. splitLine: {
  98. show: true,
  99. lineStyle: {
  100. color: "rgba(231,234,241,1)"
  101. }
  102. },
  103. axisLine: {
  104. lineStyle: {
  105. color: "rgba(231,234,241,1)"
  106. }
  107. },
  108. axisLabel: {
  109. color: "#7C86A3",
  110. fontSize: 12,
  111. fontWeight: "bold"
  112. },
  113. axisTick: {
  114. show: false
  115. }
  116. },
  117. yAxis: {
  118. type: "value",
  119. splitLine: {
  120. show: true,
  121. lineStyle: {
  122. color: "rgba(231,234,241,1)"
  123. }
  124. },
  125. axisLine: {
  126. lineStyle: {
  127. color: "rgba(231,234,241,1)"
  128. }
  129. },
  130. axisLabel: {
  131. fontSize: 12,
  132. color: "#7C86A3",
  133. formatter: function(value, index) {
  134. return value + "%";
  135. }
  136. },
  137. axisTick: {
  138. show: false
  139. }
  140. },
  141. series: [
  142. {
  143. name: "数量",
  144. type: "line",
  145. smooth: true,
  146. data: vals,
  147. itemStyle: {
  148. color: "rgba(34, 192, 255, 1)"
  149. },
  150. lineStyle: {
  151. color: "rgba(34, 192, 255, 1)"
  152. },
  153. areaStyle: {
  154. color: linearColor
  155. }
  156. }
  157. ]
  158. };
  159. },
  160. getPieOption(datas) {
  161. if (!datas.chartLabels.length) return;
  162. const seriesData = datas.chartLabels.map(function(item, index) {
  163. return {
  164. name: item,
  165. value: datas.chartData[0][index]
  166. };
  167. });
  168. return {
  169. grid: {
  170. top: "24%",
  171. bottom: "10%"
  172. },
  173. tooltip: {
  174. trigger: "item",
  175. formatter: "{a} <br/>{b} : {c} ({d}%)"
  176. },
  177. legend: {
  178. show: true,
  179. itemGap: 20,
  180. itemWidth: 20,
  181. textStyle: {
  182. fontSize: 16
  183. }
  184. },
  185. series: [
  186. {
  187. name: "数量",
  188. type: "pie",
  189. radius: "70%",
  190. data: seriesData,
  191. label: {
  192. show: false
  193. },
  194. itemStyle: {
  195. emphasis: {
  196. shadowBlur: 10,
  197. shadowOffsetX: 0,
  198. shadowColor: "rgba(0, 0, 0, 0.5)"
  199. }
  200. }
  201. }
  202. ]
  203. };
  204. },
  205. getBarOption(datas) {
  206. if (!datas.names.length) return;
  207. return {
  208. color: this.chartColor,
  209. grid: {
  210. top: "20%",
  211. bottom: "10%",
  212. left: "8%",
  213. right: "5%"
  214. },
  215. tooltip: {
  216. show: true
  217. },
  218. xAxis: {
  219. type: "category",
  220. data: datas.names,
  221. axisLine: {
  222. lineStyle: {
  223. color: "rgba(231,234,241,1)"
  224. }
  225. },
  226. axisLabel: {
  227. color: "#7C86A3",
  228. fontSize: 12,
  229. fontWeight: "bold"
  230. },
  231. axisTick: {
  232. show: false
  233. }
  234. },
  235. yAxis: {
  236. type: "value",
  237. splitLine: {
  238. show: true,
  239. lineStyle: {
  240. color: "rgba(231,234,241,1)"
  241. }
  242. },
  243. axisLine: {
  244. lineStyle: {
  245. color: "rgba(231,234,241,1)"
  246. }
  247. },
  248. axisLabel: {
  249. color: "#7C86A3",
  250. fontSize: 12,
  251. fontWeight: "bold"
  252. },
  253. axisTick: {
  254. show: false
  255. }
  256. },
  257. series: [
  258. {
  259. name: this.chartTitle || "值",
  260. type: "bar",
  261. barWidth: 16,
  262. data: datas.dataList,
  263. itemStyle: {
  264. barBorderRadius: 4
  265. }
  266. }
  267. ]
  268. };
  269. },
  270. getBarReverseOption(datas) {
  271. if (!datas.names.length) return;
  272. return {
  273. color: this.chartColor,
  274. grid: {
  275. top: "20%",
  276. bottom: "10%",
  277. left: "12%",
  278. right: "5%"
  279. },
  280. tooltip: {
  281. show: true
  282. },
  283. yAxis: {
  284. type: "category",
  285. data: datas.names,
  286. axisLine: {
  287. lineStyle: {
  288. color: "rgba(231,234,241,1)"
  289. }
  290. },
  291. splitLine: {
  292. show: true,
  293. lineStyle: {
  294. color: "rgba(231,234,241,1)"
  295. }
  296. },
  297. axisLabel: {
  298. color: "#7C86A3",
  299. fontSize: 12,
  300. fontWeight: "bold"
  301. },
  302. axisTick: {
  303. show: false
  304. }
  305. },
  306. xAxis: {
  307. type: "value",
  308. splitLine: {
  309. show: false,
  310. lineStyle: {
  311. color: "rgba(231,234,241,1)"
  312. }
  313. },
  314. axisLine: {
  315. lineStyle: {
  316. color: "rgba(231,234,241,1)"
  317. }
  318. },
  319. axisLabel: {
  320. color: "#7C86A3",
  321. fontSize: 12,
  322. fontWeight: "bold"
  323. },
  324. axisTick: {
  325. show: false
  326. }
  327. },
  328. series: [
  329. {
  330. name: this.chartTitle || "值",
  331. type: "bar",
  332. barWidth: 16,
  333. data: datas.dataList,
  334. itemStyle: {
  335. barBorderRadius: 4
  336. }
  337. }
  338. ]
  339. };
  340. },
  341. getBarGroupOption(datas) {
  342. if (!datas.names.length) return;
  343. const xAxis = datas.dataList.map(function(item) {
  344. return item.name;
  345. });
  346. const series = datas.names.map(function(name, index) {
  347. const data = datas.dataList.map(function(item) {
  348. return item.data[index];
  349. });
  350. return {
  351. name: name,
  352. type: "bar",
  353. data: data,
  354. barMaxWidth: 8,
  355. itemStyle: {
  356. barBorderRadius: 2
  357. }
  358. };
  359. });
  360. const options = {
  361. color: this.chartColor,
  362. grid: {
  363. top: "20%",
  364. bottom: "10%",
  365. left: "8%",
  366. right: "5%"
  367. },
  368. tooltip: {
  369. show: true,
  370. trigger: "axis",
  371. axisPointer: {
  372. type: "shadow"
  373. },
  374. formatter: function(params) {
  375. const label = params[0].axisValueLabel;
  376. let infos = params.map(function(item) {
  377. return item.seriesName + ":" + item.value.toFixed(2) + "%";
  378. });
  379. infos.unshift(label);
  380. return infos.join("<br/>");
  381. }
  382. },
  383. legend: {
  384. data: datas.names,
  385. right: 0,
  386. itemWidth: 14,
  387. textStyle: {
  388. fontSize: 16
  389. }
  390. },
  391. xAxis: {
  392. type: "category",
  393. data: xAxis,
  394. axisLabel: {
  395. color: "#7C86A3",
  396. fontSize: 12,
  397. fontWeight: "bold"
  398. },
  399. axisTick: {
  400. show: false
  401. }
  402. },
  403. yAxis: {
  404. type: "value",
  405. splitLine: {
  406. show: true,
  407. lineStyle: {
  408. color: "rgba(231,234,241,1)"
  409. }
  410. },
  411. axisLine: {
  412. lineStyle: {
  413. color: "rgba(231,234,241,1)"
  414. }
  415. },
  416. axisLabel: {
  417. color: "#7C86A3",
  418. fontSize: 12,
  419. fontWeight: "bold",
  420. formatter: function(value, index) {
  421. return value + "%";
  422. }
  423. },
  424. axisTick: {
  425. show: false
  426. }
  427. },
  428. series: series
  429. };
  430. return options;
  431. },
  432. getLineGroupOption(datas) {
  433. if (!datas.length) return;
  434. const names = datas.map(function(item) {
  435. return item.name;
  436. });
  437. const xaxis = datas[0].dataList.map(function(item, index) {
  438. return index;
  439. });
  440. const series = datas.map(function(item) {
  441. return {
  442. name: item.name,
  443. type: "line",
  444. symbol: "circle",
  445. smooth: true,
  446. itemStyle: {
  447. emphasis: {
  448. color: "#333"
  449. }
  450. },
  451. data: item.dataList.map(function(num) {
  452. return num * num;
  453. })
  454. };
  455. });
  456. return {
  457. grid: {
  458. top: "15%",
  459. bottom: "12%"
  460. },
  461. tooltip: {
  462. show: true,
  463. trigger: "axis",
  464. axisPointer: {
  465. type: "shadow"
  466. },
  467. formatter: function(params) {
  468. const label = params[0].axisValueLabel;
  469. let infos = params.map(function(item) {
  470. return item.seriesName + ":" + Math.sqrt(item.value);
  471. });
  472. infos.unshift(label);
  473. return infos.join("<br/>");
  474. }
  475. },
  476. legend: {
  477. data: names,
  478. right: 0,
  479. itemWidth: 14,
  480. textStyle: {
  481. fontSize: 16
  482. }
  483. },
  484. xAxis: {
  485. type: "category",
  486. data: xaxis,
  487. axisLabel: {
  488. show: false,
  489. fontSize: 14
  490. },
  491. axisTick: {
  492. show: false
  493. }
  494. },
  495. yAxis: {
  496. type: "value",
  497. interval: 1,
  498. splitLine: {
  499. show: false
  500. },
  501. axisLabel: {
  502. fontSize: 14,
  503. formatter: function(value) {
  504. const num = Math.sqrt(value);
  505. return num % 1 ? "" : parseInt(num);
  506. }
  507. },
  508. axisTick: {
  509. show: false
  510. }
  511. },
  512. dataZoom: [
  513. {
  514. type: "inside",
  515. start: 0,
  516. end: 30
  517. },
  518. {
  519. type: "slider",
  520. start: 0,
  521. end: 30
  522. }
  523. ],
  524. series: series
  525. };
  526. }
  527. }
  528. };
  529. </script>