pieChart.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <template>
  2. <div ref="pieChart" :style="{ height: height, width: width }"></div>
  3. </template>
  4. <script>
  5. import * as echarts from "echarts";
  6. import api from "../../../../api/Overview/index";
  7. import { ElMessage } from "element-plus";
  8. export default {
  9. props: {
  10. width: {
  11. type: String,
  12. default: "100%",
  13. },
  14. height: {
  15. type: String,
  16. default: "100%",
  17. },
  18. },
  19. data() {
  20. return {};
  21. },
  22. mounted() {},
  23. beforeUnmount() {
  24. window.removeEventListener("resize", this.chart);
  25. },
  26. watch: {
  27. /**
  28. * @监听vuex存储值变化 用于调用api
  29. */
  30. "$store.state.siteId": {
  31. immediate: true, // 首次加载的时候执行函数
  32. deep: true, // 深入观察,监听数组值,对象属性值的变化
  33. handler: function () {
  34. this.count();
  35. },
  36. },
  37. },
  38. methods: {
  39. count() {
  40. var _this = this;
  41. api
  42. .count({
  43. siteId: _this.$store.state.siteId,
  44. })
  45. .then((requset) => {
  46. if (requset.status === "SUCCESS") {
  47. var data = requset.data;
  48. // console.log(data);
  49. _this.initChart(data);
  50. } else {
  51. ElMessage.success({
  52. message: requset.msg,
  53. type: "success",
  54. });
  55. }
  56. });
  57. },
  58. //次数分布折线图
  59. initChart(list) {
  60. var chart = echarts.init(this.$refs.pieChart);
  61. var option;
  62. var pie = [];
  63. var totalNum = 0;
  64. list.map((val) => {
  65. totalNum += val.count;
  66. pie.push({ value: val.count, name: val.name });
  67. return;
  68. });
  69. option = {
  70. grid: {},
  71. title: [
  72. {
  73. text: "{name|" + totalNum + "}\n{val|总数}",
  74. top: "center",
  75. left: "center",
  76. textStyle: {
  77. rich: {
  78. name: {
  79. fontSize: 30,
  80. fontWeight: "normal",
  81. color: "#FFFFFF",
  82. fontFamily: "impact",
  83. padding: [0, 0, 3, 0],
  84. },
  85. val: {
  86. fontSize: 14,
  87. fontWeight: "normal",
  88. color: "#FFFFFF",
  89. padding: [3, 0, 0, 0],
  90. },
  91. },
  92. },
  93. },
  94. ],
  95. tooltip: {
  96. // trigger: "item",
  97. trigger: "item",
  98. confine: true, //将此限制打开后tooltip将不再溢出
  99. formatter: function (params) {
  100. return (
  101. params.name +
  102. ":" +
  103. params.value +
  104. "<br>占比:" +
  105. params.percent.toFixed(2) +
  106. "%"
  107. );
  108. },
  109. },
  110. series: [
  111. {
  112. label: {
  113. normal: {
  114. show: true,
  115. textStyle: {
  116. color: "#fff",
  117. },
  118. formatter: " {b} ",
  119. },
  120. emphasis: {
  121. show: true,
  122. },
  123. },
  124. radius: ["43%", "60%"],
  125. type: "pie",
  126. data: pie,
  127. labelLine: {
  128. normal: {
  129. length: 3, //aa折线长度
  130. // length2: 1, //aa折线长度
  131. },
  132. },
  133. },
  134. ],
  135. color: ["#39BBFE", "#FCCB35", "#FC7735"],
  136. };
  137. chart.setOption(option, true);
  138. window.addEventListener("resize", () => {
  139. chart.resize();
  140. });
  141. this.chart = chart;
  142. },
  143. },
  144. };
  145. </script>