Skip to content
This repository was archived by the owner on Mar 19, 2021. It is now read-only.

Commit 8a68419

Browse files
committed
图表正常显示
1 parent 51100d9 commit 8a68419

File tree

1 file changed

+32
-22
lines changed

1 file changed

+32
-22
lines changed

frontend/src/components/Chart.vue

+32-22
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
<template>
2-
<div>
3-
<div id="chart" style="width: 100%; height: 600px;"></div>
2+
<v-card>
3+
<div id="chart" style="width: 100%; height: 500px;"></div>
44
<Notice ref="notice" />
5-
</div>
5+
</v-card>
66
</template>
7-
87
<script>
98
import timechart from "timechart";
109
import Notice from "@/components/Notice.vue";
@@ -13,9 +12,9 @@ export default {
1312
Notice
1413
},
1514
data: () => ({
16-
el: null,
15+
ws: null,
1716
chart: null,
18-
cColors: 0,
17+
iColor: 0,
1918
colors: [
2019
"#F44336",
2120
"#9C27B0",
@@ -26,30 +25,41 @@ export default {
2625
"#795548"
2726
]
2827
}),
28+
created() {
29+
this.initWS();
30+
},
31+
destroyed() {
32+
this.ws.close();
33+
},
2934
mounted() {
30-
this.el = document.getElementById("chart");
31-
this.chart = new timechart(this.el, {
35+
const el = document.getElementById("chart");
36+
this.chart = new timechart(el, {
3237
baseTime: Date.now() - performance.now(),
3338
series: [],
3439
xRange: { min: 0, max: 20 * 1000 },
3540
realTime: true
3641
});
37-
var ws = new WebSocket("ws://" + window.location.host + "/ws");
38-
ws.onopen = function() {
42+
},
43+
methods: {
44+
initWS() {
45+
this.ws = new WebSocket("ws://" + window.location.host + "/ws");
46+
this.ws.onopen = this.WSonopen;
47+
this.ws.onclose = this.WSclose;
48+
this.ws.onmessage = this.WSonmessage;
49+
this.ws.onerror = this.WSonerror;
50+
},
51+
WSonopen() {
3952
this.$refs.notice.show("连接成功", 0);
40-
};
41-
ws.onclose = function() {
53+
},
54+
WSclose() {
4255
this.$refs.notice.show("连接断开", 1);
43-
ws = null;
44-
};
45-
ws.onmessage = function(evt) {
56+
},
57+
WSonmessage(evt) {
4658
this.praseWS(evt.data);
47-
};
48-
ws.onerror = function(evt) {
59+
},
60+
WSonerror(evt) {
4961
console.log("ERROR: " + evt.data);
50-
};
51-
},
52-
methods: {
62+
},
5363
praseWS(data) {
5464
if (data != "") {
5565
const jsonWS = JSON.parse(data);
@@ -65,10 +75,10 @@ export default {
6575
} else {
6676
this.chart.options.series.push({
6777
name: jsonWS.DataPack[i].Name,
68-
color: this.colors[this.cColors],
78+
color: this.colors[this.iColor],
6979
data: [{ x: jsonWS.DataPack[i].Tick, y: jsonWS.DataPack[i].Data }]
7080
});
71-
this.cColors++;
81+
this.iColor++;
7282
}
7383
}
7484
this.chart.update();

0 commit comments

Comments
 (0)