alarmRate.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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: 18,
  85. left: '3%',
  86. textStyle: {
  87. color: "#fff",
  88. fontWeight:'normal',
  89. fontSize:16
  90. },
  91. },
  92. tooltip: {
  93. trigger: "item",
  94. formatter: "{a} <br/>{b}: {c} ({d}%)",
  95. },
  96. legend: {
  97. right: 20,
  98. top: 18,
  99. data: ["告警", "正常"],
  100. textStyle: {
  101. color: "#fff",
  102. },
  103. },
  104. series: [
  105. {
  106. name: "设备告警状态",
  107. type: "pie",
  108. radius: ["0", "55%"],
  109. center: ["50%", "60%"],
  110. // color: ['#e72325', '#98e002', '#2ca3fd'],
  111. color: ["#F6A645", "#13D0B2"],
  112. selectedMode: true,
  113. selectedOffset: 4,
  114. // avoidLabelOverlap:false,
  115. // padAngle: 5,
  116. // emphasis:{
  117. // label:{
  118. // show:true
  119. // }
  120. // },
  121. // itemStyle: {
  122. // borderWidth: 2,
  123. // borderColor: 'rgba(12,30,50,1)'
  124. // emphasis: {
  125. // shadowBlur: 10,
  126. // shadowOffsetX: 0,
  127. // shadowColor: "rgba(0,0,0,0,5)",
  128. // },
  129. // },
  130. label: {
  131. color: "inherit",
  132. formatter: (param) => {
  133. return `${param.name}\n${
  134. param.percent == null ? 0 : param.percent
  135. }%`;
  136. },
  137. },
  138. data: [
  139. {
  140. value: this.data.alarmCount,
  141. name: "告警",
  142. selected: true,
  143. },
  144. {
  145. value: this.data.normalCount,
  146. name: "正常",
  147. },
  148. ],
  149. },
  150. ],
  151. };
  152. // 使用刚指定的配置项和数据显示图表。
  153. if (option && typeof option === "object") {
  154. this.myChart.setOption(option);
  155. }
  156. },
  157. resetTimer() {
  158. this.timer && clearInterval(this.timer);
  159. this.timer = setInterval(() => {
  160. if (this.isMouseOver) {
  161. return;
  162. }
  163. // this.activeName = getNextPeriod(this.types, this.activeName);
  164. this.getData();
  165. }, this.refreshTime);
  166. },
  167. },
  168. };
  169. </script>
  170. <style scoped src="./../css/index.css"></style>