pieChart2.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <template>
  2. <div ref="pieChart2" :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.deviceTypeCount();
  35. },
  36. },
  37. },
  38. methods: {
  39. deviceTypeCount() {
  40. var _this = this;
  41. api
  42. .deviceTypeCount({
  43. site: _this.$store.state.siteId,
  44. })
  45. .then((requset) => {
  46. if (requset.status === "SUCCESS") {
  47. var data = requset.data;
  48. _this.initChart(
  49. data.epCount == null ? 0 : data.epCount,
  50. data.videoCount == null ? 0 : data.videoCount
  51. );
  52. } else {
  53. ElMessage.success({
  54. message: requset.msg,
  55. type: "success",
  56. });
  57. }
  58. });
  59. },
  60. //次数分布折线图
  61. initChart(epCount, videoCount) {
  62. var chart = echarts.init(this.$refs.pieChart2);
  63. var option;
  64. var pie = [
  65. {
  66. value: epCount,
  67. name: "电力",
  68. },
  69. {
  70. value: videoCount,
  71. name: "视频",
  72. },
  73. ];
  74. option = {
  75. grid: {},
  76. title: [
  77. {
  78. text: "{name|" + (epCount + videoCount) + "}\n{val|总数}",
  79. top: "center",
  80. left: "center",
  81. textStyle: {
  82. rich: {
  83. name: {
  84. fontSize: 30,
  85. fontWeight: "normal",
  86. color: "#FFFFFF",
  87. fontFamily: "impact",
  88. padding: [0, 0, 3, 0],
  89. },
  90. val: {
  91. fontSize: 14,
  92. fontWeight: "normal",
  93. color: "#FFFFFF",
  94. padding: [3, 0, 0, 0],
  95. },
  96. },
  97. },
  98. },
  99. ],
  100. tooltip: {
  101. // trigger: "item",
  102. trigger: "item",
  103. confine: true, //将此限制打开后tooltip将不再溢出
  104. formatter: function (params) {
  105. return (
  106. params.name +
  107. ":" +
  108. params.value +
  109. "<br>占比:" +
  110. params.percent.toFixed(2) +
  111. "%"
  112. );
  113. },
  114. },
  115. series: [
  116. {
  117. label: {
  118. normal: {
  119. show: true,
  120. textStyle: {
  121. color: "#fff",
  122. },
  123. formatter: " {b} ",
  124. },
  125. emphasis: {
  126. show: true,
  127. },
  128. },
  129. radius: ["43%", "65%"],
  130. type: "pie",
  131. data: pie,
  132. labelLine: {
  133. normal: {
  134. length: 3, //aa折线长度
  135. // length2: 1, //aa折线长度
  136. },
  137. },
  138. },
  139. ],
  140. color: ["#39FEFC", "#FC3535"],
  141. };
  142. chart.setOption(option, true);
  143. window.addEventListener("resize", () => {
  144. chart.resize();
  145. });
  146. this.chart = chart;
  147. },
  148. },
  149. };
  150. </script>