alarmRate.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <template>
  2. <div class="component_box">
  3. <div
  4. ref="chart"
  5. style="width: 100%; height: 100%"
  6. @mouseenter="handleMouseEnter"
  7. @mouseleave="handleMouseLeave"
  8. ></div>
  9. </div>
  10. </template>
  11. <script>
  12. import { alarmRate } from "@/api/iot/board";
  13. import * as echarts from "echarts";
  14. export default {
  15. props: {
  16. orgId: {
  17. type: String,
  18. isRequired: true,
  19. },
  20. },
  21. data() {
  22. return {
  23. data: {
  24. normalCount: 0,
  25. alarmCount: 0,
  26. },
  27. };
  28. },
  29. components: {},
  30. computed: {},
  31. watch: {
  32. orgId: {
  33. deep: true,
  34. handler(val) {
  35. this.resetTimer();
  36. this.getData();
  37. },
  38. },
  39. data: {
  40. deep: true,
  41. handler() {
  42. this.initMap();
  43. },
  44. },
  45. },
  46. created() {
  47. this.refreshTime = 1 * 60 * 1000;
  48. this.isMouseOver = false;
  49. },
  50. async mounted() {
  51. window.addEventListener("resize", this.windowResize);
  52. },
  53. beforeDestroy() {
  54. this.timer && clearInterval(this.timer);
  55. this.timer = null;
  56. window.removeEventListener("resize", this.windowResize);
  57. },
  58. methods: {
  59. handleClick() {
  60. this.resetTimer();
  61. this.getData();
  62. },
  63. handleMouseEnter() {
  64. this.isMouseOver = true;
  65. },
  66. handleMouseLeave() {
  67. this.isMouseOver = false;
  68. },
  69. async getData() {
  70. let r = (await alarmRate(this.orgId)).data;
  71. this.data = r;
  72. },
  73. windowResize() {
  74. this.myChart && this.myChart.resize();
  75. },
  76. initMap() {
  77. this.myChart && this.myChart.dispose();
  78. let c = this.$refs["chart"];
  79. // 基于准备好的dom,初始化echarts实例
  80. this.myChart = echarts.init(c);
  81. let option = {
  82. title: {
  83. text: "设备告警状态",
  84. top: 35,
  85. left: 20,
  86. textStyle: {
  87. fontSize: 18,
  88. color: "#fff",
  89. },
  90. },
  91. tooltip: {
  92. trigger: "item",
  93. formatter: "{a} <br/>{b}: {c} ({d}%)",
  94. },
  95. legend: {
  96. right: 20,
  97. top: 35,
  98. data: ["告警", "正常"],
  99. textStyle: {
  100. color: "#fff",
  101. },
  102. },
  103. series: [
  104. {
  105. name: "设备告警状态",
  106. type: "pie",
  107. radius: ["0", "55%"],
  108. center: ["50%", "60%"],
  109. // color: ['#e72325', '#98e002', '#2ca3fd'],
  110. color: ["#F6A645", "#13D0B2"],
  111. selectedMode: true,
  112. selectedOffset: 4,
  113. // avoidLabelOverlap:false,
  114. // padAngle: 5,
  115. // emphasis:{
  116. // label:{
  117. // show:true
  118. // }
  119. // },
  120. // itemStyle: {
  121. // borderWidth: 2,
  122. // borderColor: 'rgba(12,30,50,1)'
  123. // emphasis: {
  124. // shadowBlur: 10,
  125. // shadowOffsetX: 0,
  126. // shadowColor: "rgba(0,0,0,0,5)",
  127. // },
  128. // },
  129. label: {
  130. normal: {
  131. color: "inherit",
  132. formatter: (param) => {
  133. return `${param.name}\n${param.percent == null ? 0 : param.percent}%`;
  134. },
  135. },
  136. },
  137. data: [
  138. {
  139. value: this.data.alarmCount,
  140. name: "告警",
  141. selected: true,
  142. },
  143. {
  144. value: this.data.normalCount,
  145. name: "正常",
  146. },
  147. ],
  148. },
  149. ],
  150. };
  151. // 使用刚指定的配置项和数据显示图表。
  152. if (option && typeof option === "object") {
  153. this.myChart.setOption(option);
  154. }
  155. },
  156. resetTimer() {
  157. this.timer && clearInterval(this.timer);
  158. this.timer = setInterval(() => {
  159. if (this.isMouseOver) {
  160. return;
  161. }
  162. // this.activeName = getNextPeriod(this.types, this.activeName);
  163. this.getData();
  164. }, this.refreshTime);
  165. },
  166. },
  167. };
  168. </script>
  169. <style scoped src="./../css/index.css"></style>