|
@@ -1,47 +1,48 @@
|
|
|
<template>
|
|
|
- <!-- 两条曲线封装 且需要两个websocket连接 -->
|
|
|
- <div>
|
|
|
- <div ref="testLine" style="width: 500px; height: 200px; margin: 0px auto;padding-top:8px"></div>
|
|
|
- </div>
|
|
|
+ <!-- 两条曲线封装 且需要两个websocket连接 -->
|
|
|
+ <div>
|
|
|
+ <div
|
|
|
+ ref="testLine"
|
|
|
+ style="width: 500px; height: 200px; margin: 0px auto;padding-top:8px"
|
|
|
+ ></div>
|
|
|
+ </div>
|
|
|
</template>
|
|
|
|
|
|
<script>
|
|
|
- import {
|
|
|
- mapState
|
|
|
- } from "vuex";
|
|
|
+import { mapState } from "vuex";
|
|
|
|
|
|
- export default {
|
|
|
- props: {
|
|
|
- title: {
|
|
|
- type: String,
|
|
|
- default: ''
|
|
|
- },
|
|
|
- id: {
|
|
|
- type: Number,
|
|
|
- required: true
|
|
|
- },
|
|
|
- unit: {
|
|
|
- type: String,
|
|
|
- default: ''
|
|
|
- },
|
|
|
- type: {
|
|
|
- type: Array,
|
|
|
- required: true
|
|
|
- },
|
|
|
- legend: {
|
|
|
- type: Array,
|
|
|
- required: true
|
|
|
- },
|
|
|
- },
|
|
|
- data() {
|
|
|
- return {
|
|
|
- currentChartId: 1,
|
|
|
- xData: new Array(30).fill("-"),
|
|
|
- y1Data: new Array(30).fill("-"),
|
|
|
- y2Data: new Array(30).fill("-"),
|
|
|
- count: 0,
|
|
|
- myChart: null,
|
|
|
- option: {
|
|
|
+export default {
|
|
|
+ props: {
|
|
|
+ title: {
|
|
|
+ type: String,
|
|
|
+ default: "",
|
|
|
+ },
|
|
|
+ id: {
|
|
|
+ type: Number,
|
|
|
+ required: true,
|
|
|
+ },
|
|
|
+ unit: {
|
|
|
+ type: String,
|
|
|
+ default: "",
|
|
|
+ },
|
|
|
+ type: {
|
|
|
+ type: Array,
|
|
|
+ required: true,
|
|
|
+ },
|
|
|
+ legend: {
|
|
|
+ type: Array,
|
|
|
+ required: true,
|
|
|
+ },
|
|
|
+ },
|
|
|
+ data() {
|
|
|
+ return {
|
|
|
+ currentChartId: 1,
|
|
|
+ xData: new Array(30).fill("-"),
|
|
|
+ y1Data: new Array(30).fill("-"),
|
|
|
+ y2Data: new Array(30).fill("-"),
|
|
|
+ count: 0,
|
|
|
+ myChart: null,
|
|
|
+ option: {
|
|
|
tooltip: {
|
|
|
trigger: "axis",
|
|
|
backgroundColor: "transparent",
|
|
@@ -142,8 +143,7 @@
|
|
|
// icon:'rect',
|
|
|
itemGap: 25,
|
|
|
// itemWidth: 18,
|
|
|
- icon:
|
|
|
- "path://M0 2a2 2 0 0 1 2 -2h14a2 2 0 0 1 2 2v0a2 2 0 0 1 -2 2h-14a2 2 0 0 1 -2 -2z",
|
|
|
+ icon: "path://M0 2a2 2 0 0 1 2 -2h14a2 2 0 0 1 2 2v0a2 2 0 0 1 -2 2h-14a2 2 0 0 1 -2 -2z",
|
|
|
|
|
|
data: this.legend,
|
|
|
},
|
|
@@ -152,7 +152,7 @@
|
|
|
left: "10%",
|
|
|
right: "10%",
|
|
|
bottom: "1%",
|
|
|
- containLabel: true
|
|
|
+ containLabel: true,
|
|
|
},
|
|
|
yAxis: [
|
|
|
{
|
|
@@ -200,7 +200,7 @@
|
|
|
textStyle: {
|
|
|
fontWeight: "normal", //标题颜色
|
|
|
color: "#4ADEFE",
|
|
|
- fontSize:15
|
|
|
+ fontSize: 15,
|
|
|
},
|
|
|
},
|
|
|
|
|
@@ -305,122 +305,133 @@
|
|
|
},
|
|
|
],
|
|
|
},
|
|
|
- };
|
|
|
- },
|
|
|
- computed: {
|
|
|
- ...mapState(["websocketIP"]),
|
|
|
- },
|
|
|
- methods: {
|
|
|
- idChange(id) {
|
|
|
- // console.log('this.legend',this.legend[0]);
|
|
|
- // console.log("sep", id);
|
|
|
- // 数据清空
|
|
|
- this.xData = new Array(30).fill("-");
|
|
|
- this.y1Data = new Array(30).fill("-");
|
|
|
- this.y2Data = new Array(30).fill("-");
|
|
|
- // 关闭以前的websocket
|
|
|
- this.websocket.close();
|
|
|
- // 开启一个新的
|
|
|
- this.websocket = new WebSocket(
|
|
|
- `ws://${this.websocketIP}/hbase/ws/belt/${this.id}`
|
|
|
- );
|
|
|
- this.initWebSocket();
|
|
|
- },
|
|
|
+ };
|
|
|
+ },
|
|
|
+ computed: {
|
|
|
+ ...mapState(["websocketIP"]),
|
|
|
+ },
|
|
|
+ methods: {
|
|
|
+ idChange(id) {
|
|
|
+ // console.log('this.legend',this.legend[0]);
|
|
|
+ // console.log("sep", id);
|
|
|
+ // 数据清空
|
|
|
+ this.xData = new Array(30).fill("-");
|
|
|
+ this.y1Data = new Array(30).fill("-");
|
|
|
+ this.y2Data = new Array(30).fill("-");
|
|
|
+ // 关闭以前的websocket
|
|
|
+ this.websocket.close();
|
|
|
+ // 开启一个新的
|
|
|
+ this.websocket = new WebSocket(
|
|
|
+ `ws://${this.websocketIP}/hbase/ws/belt/${this.id}`
|
|
|
+ );
|
|
|
+ this.initWebSocket();
|
|
|
+ },
|
|
|
|
|
|
- addData(data) {
|
|
|
- this.websocket;
|
|
|
- // 先扩展数组,然后删除多余元素
|
|
|
- // console.log("data", data);
|
|
|
- if (this.count < 30) {
|
|
|
- for (let i = 0; i < data.length; i++) {
|
|
|
- this.y1Data[this.count] = data[i][this.type[0]];
|
|
|
- this.y2Data[this.count] = data[i][this.type[1]];
|
|
|
- this.xData[this.count] = data[i]["date"].split(" ")[1];
|
|
|
- }
|
|
|
- this.count++;
|
|
|
- } else {
|
|
|
- for (let i = 0; i < data.length; i++) {
|
|
|
- // 删除数组中的第一个项目
|
|
|
- this.y1Data.shift();
|
|
|
- this.y1Data.push(data[i][this.type[0]]);
|
|
|
- this.y2Data.shift();
|
|
|
- this.y2Data.push(data[i][this.type[1]]);
|
|
|
- this.xData.shift();
|
|
|
- this.xData.push(data[i]["date"].split(" ")[1]);
|
|
|
- }
|
|
|
- }
|
|
|
- this.myChart.setOption({
|
|
|
- xAxis: [{
|
|
|
- data: this.xData,
|
|
|
- }, ],
|
|
|
- series: [{
|
|
|
- name: this.legend[0],
|
|
|
- data: this.y1Data,
|
|
|
- },
|
|
|
- {
|
|
|
- name: this.legend[1],
|
|
|
- data: this.y2Data,
|
|
|
- }
|
|
|
- ],
|
|
|
- });
|
|
|
- // console.log("this.paiqidata", this.paiqidata);
|
|
|
- // console.log("this.chouqidata", this.chouqidata);
|
|
|
- // console.log("this.xData", this.xData);
|
|
|
- },
|
|
|
- initWebSocket() {
|
|
|
- // 连接错误
|
|
|
- this.websocket.onerror = () => {
|
|
|
- console.log(
|
|
|
- "WebSocket连接发生错误 状态码:" + this.websocket.readyState
|
|
|
- );
|
|
|
- };
|
|
|
- // 连接成功
|
|
|
- this.websocket.onopen = () => {
|
|
|
- console.log(
|
|
|
- "WebSocket连接成功 状态码:" + this.websocket.readyState
|
|
|
- );
|
|
|
- };
|
|
|
- // 收到消息的回调
|
|
|
- this.websocket.onmessage = (event) => {
|
|
|
- console.log("@@@@", event);
|
|
|
-
|
|
|
- if (JSON.parse(event.data).length) {
|
|
|
- this.addData(JSON.parse(event.data));
|
|
|
- }
|
|
|
- };
|
|
|
- // 连接关闭的回调
|
|
|
- this.websocket.onclose = () => {
|
|
|
- console.log(
|
|
|
- "WebSocket连接关闭 状态码:" + this.websocket.readyState
|
|
|
- );
|
|
|
- };
|
|
|
- // 通过$once来监听定时器,在beforeDestroy钩子可以被清除。
|
|
|
- this.$once("hook:beforeDestroy", () => {
|
|
|
- this.websocket.close();
|
|
|
- });
|
|
|
- },
|
|
|
- },
|
|
|
- mounted() {
|
|
|
- console.log("@@@@", this.id);
|
|
|
- // 先画图
|
|
|
- this.myChart = this.$echarts.init(this.$refs.testLine);
|
|
|
- this.myChart.setOption(this.option);
|
|
|
- this.websocket = new WebSocket(`ws://${this.websocketIP}/hbase/ws/belt/${this.id}`);
|
|
|
- this.initWebSocket();
|
|
|
- },
|
|
|
- };
|
|
|
+ addData(data) {
|
|
|
+ this.websocket;
|
|
|
+ // 先扩展数组,然后删除多余元素
|
|
|
+ // console.log("data", data);
|
|
|
+ if (this.count < 30) {
|
|
|
+ for (let i = 0; i < data.length; i++) {
|
|
|
+ this.y1Data[this.count] = data[i][this.type[0]];
|
|
|
+ this.y2Data[this.count] = data[i][this.type[1]];
|
|
|
+ this.xData[this.count] = data[i]["date"].split(" ")[1];
|
|
|
+ }
|
|
|
+ this.count++;
|
|
|
+ } else {
|
|
|
+ for (let i = 0; i < data.length; i++) {
|
|
|
+ // 删除数组中的第一个项目
|
|
|
+ this.y1Data.shift();
|
|
|
+ this.y1Data.push(data[i][this.type[0]]);
|
|
|
+ this.y2Data.shift();
|
|
|
+ this.y2Data.push(data[i][this.type[1]]);
|
|
|
+ this.xData.shift();
|
|
|
+ this.xData.push(data[i]["date"].split(" ")[1]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ this.myChart.setOption({
|
|
|
+ xAxis: [
|
|
|
+ {
|
|
|
+ data: this.xData,
|
|
|
+ },
|
|
|
+ ],
|
|
|
+ series: [
|
|
|
+ {
|
|
|
+ name: this.legend[0],
|
|
|
+ data: this.y1Data,
|
|
|
+ },
|
|
|
+ {
|
|
|
+ name: this.legend[1],
|
|
|
+ data: this.y2Data,
|
|
|
+ },
|
|
|
+ ],
|
|
|
+ });
|
|
|
+ // console.log("this.paiqidata", this.paiqidata);
|
|
|
+ // console.log("this.chouqidata", this.chouqidata);
|
|
|
+ // console.log("this.xData", this.xData);
|
|
|
+ },
|
|
|
+ initWebSocket() {
|
|
|
+ // 连接错误
|
|
|
+ this.websocket.onerror = () => {
|
|
|
+ console.log(
|
|
|
+ "WebSocket连接发生错误 状态码:" + this.websocket.readyState
|
|
|
+ );
|
|
|
+ };
|
|
|
+ // 连接成功
|
|
|
+ this.websocket.onopen = () => {
|
|
|
+ console.log(
|
|
|
+ "WebSocket连接成功 状态码:" + this.websocket.readyState
|
|
|
+ );
|
|
|
+ };
|
|
|
+ // 收到消息的回调
|
|
|
+ this.websocket.onmessage = (event) => {
|
|
|
+ console.log("@@@@", event);
|
|
|
+
|
|
|
+ if (JSON.parse(event.data).length) {
|
|
|
+ this.addData(JSON.parse(event.data));
|
|
|
+ }
|
|
|
+ };
|
|
|
+ // 连接关闭的回调
|
|
|
+ this.websocket.onclose = () => {
|
|
|
+ console.log(
|
|
|
+ "WebSocket连接关闭 状态码:" + this.websocket.readyState
|
|
|
+ );
|
|
|
+ };
|
|
|
+ // 通过$once来监听定时器,在beforeDestroy钩子可以被清除。
|
|
|
+ this.$once("hook:beforeDestroy", () => {
|
|
|
+ this.websocket.close();
|
|
|
+ });
|
|
|
+ },
|
|
|
+ clearData() {
|
|
|
+ this.count = 0;
|
|
|
+ this.xData = new Array(30).fill("-");
|
|
|
+ this.y1Data = new Array(30).fill("-");
|
|
|
+ this.y2Data = new Array(30).fill("-");
|
|
|
+ },
|
|
|
+ },
|
|
|
+ mounted() {
|
|
|
+ console.log("@@@@", this.id);
|
|
|
+ // 先画图
|
|
|
+ this.myChart = this.$echarts.init(this.$refs.testLine);
|
|
|
+ this.myChart.setOption(this.option);
|
|
|
+ this.websocket = new WebSocket(
|
|
|
+ `ws://${this.websocketIP}/hbase/ws/belt/${this.id}`
|
|
|
+ );
|
|
|
+ this.initWebSocket();
|
|
|
+ },
|
|
|
+};
|
|
|
</script>
|
|
|
<style scoped>
|
|
|
- .select-veido .el-select {
|
|
|
- width: 100px;
|
|
|
- margin-left: 10px;
|
|
|
- }
|
|
|
+.select-veido .el-select {
|
|
|
+ width: 100px;
|
|
|
+ margin-left: 10px;
|
|
|
+}
|
|
|
|
|
|
- .select-veido>>>.el-input--small .el-input__inner {
|
|
|
- background: none;
|
|
|
- }
|
|
|
+.select-veido >>> .el-input--small .el-input__inner {
|
|
|
+ background: none;
|
|
|
+}
|
|
|
|
|
|
- .select-veido>>>.el-input__inner {
|
|
|
- border: 0;
|
|
|
- }
|
|
|
+.select-veido >>> .el-input__inner {
|
|
|
+ border: 0;
|
|
|
+}
|
|
|
</style>
|