two.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <template>
  2. <div
  3. id="two"
  4. style="width: 160px; height: 160px"
  5. ></div>
  6. </template>
  7. <script type="text/ecmascript-6">
  8. import { mapState } from "vuex";
  9. export default {
  10. data() {
  11. return {
  12. value:'',
  13. chartInstance: null,
  14. allData: null, // 服务器返回的数据
  15. };
  16. },
  17. computed: {
  18. ...mapState(["websocketIP"]),
  19. },
  20. mounted() {
  21. this.websocket = new WebSocket(`ws://${this.websocketIP}/hbase/ws/belt/43`);
  22. this.initWebSocket();
  23. this.initChart(); //函数调用
  24. // this.getData();
  25. this.updateChart();
  26. },
  27. methods: {
  28. initChart() {
  29. this.chartInstance = this.$echarts.init(
  30. document.getElementById("two")
  31. );
  32. },
  33. updateChart() {
  34. const option = {
  35. grid: {
  36. height: 160,
  37. left: 0,
  38. top: 0,
  39. bottom: 0,
  40. right: 0,
  41. },
  42. series: [
  43. {
  44. name: "仪表盘",
  45. type: "gauge",
  46. min: 0,
  47. max: 1200,
  48. splitNumber: 10,
  49. // "axisLine": {
  50. // "lineStyle": {
  51. // "color": [
  52. // [
  53. // 0.2,
  54. // "#5DD1FA"
  55. // ],
  56. // [
  57. // 1,
  58. // "#f7f9fc"
  59. // ]
  60. // ],
  61. // "width": 10
  62. // }
  63. // },
  64. axisLabel: {
  65. distance: -60,
  66. show: true,
  67. },
  68. axisTick: {
  69. show: true,
  70. },
  71. splitLine: {
  72. show: true,
  73. },
  74. itemStyle: {
  75. show: true,
  76. },
  77. title: {
  78. color: "#7f7f7f",
  79. fontSize: 16,
  80. fontWeight: "bolder",
  81. },
  82. detail: {
  83. offsetCenter: [0, 0],
  84. textStyle: {
  85. fontSize: "18",
  86. fontWeight: "bolder",
  87. color: "#F3DB5C",
  88. },
  89. },
  90. pointer: {
  91. show: true,
  92. },
  93. data: [
  94. {
  95. // name: "系统电压",
  96. value: this.value,
  97. itemStyle: { color: "#00ccee" },
  98. },
  99. ],
  100. },
  101. ],
  102. };
  103. this.chartInstance.setOption(option);
  104. },
  105. addData(data) {
  106. this.value = data.map((item) => item["jiedidianliu_after"])[0];
  107. this.chartInstance.setOption({
  108. series: [
  109. {
  110. data: [
  111. {
  112. value: this.value,
  113. },
  114. ],
  115. },
  116. ],
  117. });
  118. },
  119. initWebSocket() {
  120. // 连接错误
  121. this.websocket.onerror = () => {
  122. console.log(
  123. "WebSocket连接发生错误 状态码:" + this.websocket.readyState
  124. );
  125. };
  126. // 连接成功
  127. this.websocket.onopen = () => {
  128. console.log(
  129. "WebSocket连接成功 状态码:" + this.websocket.readyState
  130. );
  131. };
  132. // 收到消息的回调
  133. this.websocket.onmessage = (event) => {
  134. // console.log("onmessage", event);
  135. if (JSON.parse(event.data).length) {
  136. this.addData(JSON.parse(event.data));
  137. }
  138. };
  139. // 连接关闭的回调
  140. this.websocket.onclose = () => {
  141. console.log(
  142. "WebSocket连接关闭 状态码:" + this.websocket.readyState
  143. );
  144. };
  145. // 通过$once来监听定时器,在beforeDestroy钩子可以被清除。
  146. this.$once("hook:beforeDestroy", () => {
  147. this.websocket.close();
  148. });
  149. },
  150. },
  151. };
  152. </script>
  153. <style scoped>
  154. </style>