volAndCurrent.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. <template>
  2. <div>
  3. <div class="select-veido">
  4. <el-select
  5. style="margin-top:10px"
  6. v-model="currentChartId"
  7. placeholder="请选择"
  8. @change="selectParm"
  9. >
  10. <el-option
  11. v-for="item in parmoptions"
  12. :key="item.id+''"
  13. :label="item.label"
  14. :value="item.id"
  15. >
  16. </el-option>
  17. </el-select>
  18. </div>
  19. <div
  20. ref="testLine"
  21. style="width: 450px; height: 200px; margin: 0px auto"
  22. ></div>
  23. </div>
  24. </template>
  25. <script>
  26. import { mapState } from "vuex";
  27. export default {
  28. data() {
  29. return {
  30. parmoptions: [
  31. {
  32. id: 1,
  33. label: "三相线电压",
  34. },
  35. {
  36. id: 2,
  37. label: "三相相电流",
  38. },
  39. ],
  40. title: "三相线电压",
  41. type: ["Uab_after", "Ubc_after", "Uca_after"],
  42. unit: "V",
  43. currentChartId: 1,
  44. xData: new Array(30).fill("-"),
  45. y1Data: new Array(30).fill("-"),
  46. y2Data: new Array(30).fill("-"),
  47. y3Data: new Array(30).fill("-"),
  48. count: 0,
  49. myChart: null,
  50. option: {
  51. color: ["#41D6C3", "#FA8072"],
  52. tooltip: {
  53. trigger: "axis",
  54. axisPointer: {
  55. type: "cross",
  56. label: {
  57. backgroundColor: "#6a7985",
  58. },
  59. },
  60. backgroundColor: "#fff",
  61. padding: 10,
  62. textStyle: {
  63. fontSize: 12,
  64. color: "#152934",
  65. lineHeight: 24,
  66. },
  67. extraCssText:
  68. "box-shadow: 0 0 3px rgba(0, 0, 0, 0.3); border-radius: 0;",
  69. formatter: (params) => {
  70. console.log("params", params);
  71. var result = `${this.title}:${params[0].data}${this.unit} <br/>`;
  72. return result;
  73. },
  74. },
  75. grid: {
  76. top: "15%",
  77. left: "10%",
  78. right: "10%",
  79. bottom: "1%",
  80. containLabel: true,
  81. },
  82. legend: {
  83. orient: "vertical",
  84. x: "left",
  85. y: "top",
  86. // data: ["抽气流量", "排气流量"],
  87. textStyle: {
  88. fontWeight: "normal",
  89. color: "#fff",
  90. },
  91. },
  92. yAxis: [
  93. {
  94. boundaryGap: [0, "100%"],
  95. name: this.unit,
  96. splitLine: {
  97. show: true,
  98. lineStyle: {
  99. type: "dotted",
  100. color: "rgba(155, 155, 155, 0.5)",
  101. },
  102. },
  103. axisLine: {
  104. show: false,
  105. },
  106. axisLabel: {
  107. color: "#fff",
  108. fontSize: 11,
  109. },
  110. axisTick: { show: false },
  111. type: "value",
  112. },
  113. ],
  114. xAxis: [
  115. {
  116. // type: "time", // x轴为 时间轴
  117. interval: 0,
  118. splitLine: { show: false },
  119. axisLine: {
  120. lineStyle: { width: 0 },
  121. },
  122. axisLabel: {
  123. color: "#fff",
  124. fontSize: 11,
  125. rotate: 30,
  126. },
  127. axisTick: { show: false },
  128. boundaryGap: false,
  129. data: [],
  130. },
  131. ],
  132. title: {
  133. text: this.title,
  134. padding: [5, 170],
  135. textStyle: {
  136. fontWeight: "normal", //标题颜色
  137. color: "#4ADEFE",
  138. fontSize: 15,
  139. },
  140. },
  141. series: [
  142. {
  143. name: this.type[0],
  144. type: "line",
  145. symbol: "circle", // 默认是空心圆(中间是白色的),改成实心圆
  146. showAllSymbol: true,
  147. symbolSize: 0,
  148. smooth: true,
  149. lineStyle: {
  150. normal: {
  151. width: 2,
  152. color: "rgba(255, 102, 102,1)", // 线条颜色
  153. },
  154. borderColor: "rgba(0,0,0,.4)",
  155. },
  156. itemStyle: {
  157. color: "rgba(255, 102, 102,1)",
  158. borderColor: "#646ace",
  159. borderWidth: 2,
  160. },
  161. tooltip: {
  162. show: true,
  163. },
  164. areaStyle: {
  165. //区域填充样式
  166. normal: {
  167. //线性渐变,前4个参数分别是x0,y0,x2,y2(范围0~1);相当于图形包围盒中的百分比。如果最后一个参数是‘true’,则该四个值是绝对像素位置。
  168. color: new echarts.graphic.LinearGradient(
  169. 0,
  170. 0,
  171. 0,
  172. 1,
  173. [
  174. {
  175. offset: 0,
  176. color: "rgba(255, 102, 102,.3)",
  177. },
  178. {
  179. offset: 1,
  180. color: "rgba(255, 102, 102, 0)",
  181. },
  182. ],
  183. false
  184. ),
  185. shadowColor: "rgba(255, 102, 102, 0.5)", //阴影颜色
  186. shadowBlur: 20, //shadowBlur设图形阴影的模糊大小。配合shadowColor,shadowOffsetX/Y, 设置图形的阴影效果。
  187. },
  188. },
  189. data: [],
  190. },
  191. {
  192. name: this.type[1],
  193. type: "line",
  194. symbol: "circle", // 默认是空心圆(中间是白色的),改成实心圆
  195. showAllSymbol: true,
  196. symbolSize: 0,
  197. smooth: true,
  198. lineStyle: {
  199. normal: {
  200. width: 2,
  201. color: "rgba(255, 255, 0,1)", // 线条颜色
  202. },
  203. borderColor: "rgba(0,0,0,.4)",
  204. },
  205. itemStyle: {
  206. color: "rgba(255, 255, 0,1)",
  207. borderColor: "#646ace",
  208. borderWidth: 2,
  209. },
  210. tooltip: {
  211. show: true,
  212. },
  213. areaStyle: {
  214. //区域填充样式
  215. normal: {
  216. //线性渐变,前4个参数分别是x0,y0,x2,y2(范围0~1);相当于图形包围盒中的百分比。如果最后一个参数是‘true’,则该四个值是绝对像素位置。
  217. color: new echarts.graphic.LinearGradient(
  218. 0,
  219. 0,
  220. 0,
  221. 1,
  222. [
  223. {
  224. offset: 0,
  225. color: "rgba(255, 255, 0,.3)",
  226. },
  227. {
  228. offset: 1,
  229. color: "rgba(255, 255, 0, 0)",
  230. },
  231. ],
  232. false
  233. ),
  234. shadowColor: "rgba(255, 255, 0, 0.5)", //阴影颜色
  235. shadowBlur: 20, //shadowBlur设图形阴影的模糊大小。配合shadowColor,shadowOffsetX/Y, 设置图形的阴影效果。
  236. },
  237. },
  238. data: [],
  239. },
  240. {
  241. name: this.type[2],
  242. type: "line",
  243. symbol: "circle", // 默认是空心圆(中间是白色的),改成实心圆
  244. showAllSymbol: true,
  245. symbolSize: 0,
  246. smooth: true,
  247. lineStyle: {
  248. normal: {
  249. width: 2,
  250. color: "rgba(0, 102, 153,1)", // 线条颜色
  251. },
  252. borderColor: "rgba(0,0,0,.4)",
  253. },
  254. itemStyle: {
  255. color: "rgba(0, 102, 153,1)",
  256. borderColor: "#3578e5",
  257. borderWidth: 2,
  258. },
  259. tooltip: {
  260. show: true,
  261. },
  262. areaStyle: {
  263. //区域填充样式
  264. normal: {
  265. //线性渐变,前4个参数分别是x0,y0,x2,y2(范围0~1);相当于图形包围盒中的百分比。如果最后一个参数是‘true’,则该四个值是绝对像素位置。
  266. color: new echarts.graphic.LinearGradient(
  267. 0,
  268. 0,
  269. 0,
  270. 1,
  271. [
  272. {
  273. offset: 0,
  274. color: "rgba(0, 102, 153,.3)",
  275. },
  276. {
  277. offset: 1,
  278. color: "rgba(0, 102, 153, 0)",
  279. },
  280. ],
  281. false
  282. ),
  283. shadowColor: "rgba(0, 102, 153, 0.5)", //阴影颜色
  284. shadowBlur: 20, //shadowBlur设图形阴影的模糊大小。配合shadowColor,shadowOffsetX/Y, 设置图形的阴影效果。
  285. },
  286. },
  287. data: [],
  288. },
  289. ],
  290. },
  291. };
  292. },
  293. computed: {
  294. ...mapState(["websocketIP"]),
  295. },
  296. methods: {
  297. idChange(id) {
  298. console.log("vol", id);
  299. // 数据清空
  300. this.xData = new Array(30).fill("-");
  301. this.y1Data = new Array(30).fill("-");
  302. this.y2Data = new Array(30).fill("-");
  303. this.y3Data = new Array(30).fill("-");
  304. // 关闭以前的websocket
  305. this.websocket.close();
  306. // 开启一个新的
  307. this.websocket = new WebSocket(
  308. `ws://${this.websocketIP}/hbase/ws/belt/62`
  309. );
  310. this.initWebSocket();
  311. },
  312. // 选择要展示的参数
  313. selectParm() {
  314. for (let i in this.parmoptions) {
  315. if (this.parmoptions[i].id == this.currentChartId) {
  316. this.title = this.parmoptions[i].label;
  317. break;
  318. }
  319. }
  320. switch (this.currentChartId) {
  321. // 三相线电压
  322. case "1":
  323. this.unit = "V";
  324. this.type = ["Uab_after", "Ubc_after", "Uca_after"];
  325. break;
  326. // 三相相电流
  327. case "2":
  328. this.unit = "A";
  329. this.type = ["Ia", "Ib", "Ic"];
  330. break;
  331. }
  332. this.count = 0;
  333. this.xData = new Array(30).fill("-");
  334. this.y1Data = new Array(30).fill("-");
  335. this.y2Data = new Array(30).fill("-");
  336. this.y3Data = new Array(30).fill("-");
  337. },
  338. addData(data) {
  339. this.websocket;
  340. // 先扩展数组,然后删除多余元素
  341. console.log("data", data);
  342. if (this.count < 30) {
  343. for (let i = 0; i < data.length; i++) {
  344. this.y1Data[this.count] = data[i][this.type[0]];
  345. this.y2Data[this.count] = data[i][this.type[1]];
  346. this.y3Data[this.count] = data[i][this.type[2]];
  347. this.xData[this.count] = data[i]["date"].split(" ")[1];
  348. }
  349. this.count++;
  350. } else {
  351. for (let i = 0; i < data.length; i++) {
  352. // 删除数组中的第一个项目
  353. this.y1Data.shift();
  354. this.y1Data.push(data[i][this.type[0]]);
  355. this.y2Data.shift();
  356. this.y2Data.push(data[i][this.type[1]]);
  357. this.y3Data.shift();
  358. this.y3Data.push(data[i][this.type[2]]);
  359. this.xData.shift();
  360. this.xData.push(data[i]["date"].split(" ")[1]);
  361. }
  362. }
  363. this.myChart.setOption({
  364. xAxis: [
  365. {
  366. data: this.xData,
  367. },
  368. ],
  369. series: [
  370. {
  371. name: this.unit,
  372. data: this.y1Data,
  373. },
  374. {
  375. name: this.unit,
  376. data: this.y2Data,
  377. },
  378. {
  379. name: this.unit,
  380. data: this.y3Data,
  381. },
  382. ],
  383. });
  384. // console.log("this.paiqidata", this.paiqidata);
  385. // console.log("this.chouqidata", this.chouqidata);
  386. // console.log("this.xData", this.xData);
  387. },
  388. initWebSocket() {
  389. // 连接错误
  390. this.websocket.onerror = () => {
  391. console.log(
  392. "WebSocket连接发生错误 状态码:" + this.websocket.readyState
  393. );
  394. };
  395. // 连接成功
  396. this.websocket.onopen = () => {
  397. console.log(
  398. "WebSocket连接成功 状态码:" + this.websocket.readyState
  399. );
  400. };
  401. // 收到消息的回调
  402. this.websocket.onmessage = (event) => {
  403. // console.log("onmessage", event);
  404. if (JSON.parse(event.data).length) {
  405. this.addData(JSON.parse(event.data));
  406. }
  407. };
  408. // 连接关闭的回调
  409. this.websocket.onclose = () => {
  410. console.log(
  411. "WebSocket连接关闭 状态码:" + this.websocket.readyState
  412. );
  413. };
  414. // 通过$once来监听定时器,在beforeDestroy钩子可以被清除。
  415. this.$once("hook:beforeDestroy", () => {
  416. this.websocket.close();
  417. });
  418. },
  419. },
  420. mounted() {
  421. // 先画图
  422. this.myChart = this.$echarts.init(this.$refs.testLine);
  423. this.myChart.setOption(this.option);
  424. this.websocket = new WebSocket(`ws://${this.websocketIP}/hbase/ws/belt/62`);
  425. this.initWebSocket();
  426. },
  427. };
  428. </script>
  429. <style scoped>
  430. .select-veido .el-select {
  431. width: 120px;
  432. margin-left: 10px;
  433. }
  434. .select-veido >>> .el-input--small .el-input__inner {
  435. background: none;
  436. }
  437. .select-veido >>> .el-input__inner {
  438. border: 0;
  439. }
  440. </style>