1
1
<template >
2
- <div >
3
- <div id =" chart" style =" width : 100% ; height : 600 px ;" ></div >
2
+ <v-card >
3
+ <div id =" chart" style =" width : 100% ; height : 500 px ;" ></div >
4
4
<Notice ref =" notice" />
5
- </div >
5
+ </v-card >
6
6
</template >
7
-
8
7
<script >
9
8
import timechart from " timechart" ;
10
9
import Notice from " @/components/Notice.vue" ;
@@ -13,9 +12,9 @@ export default {
13
12
Notice
14
13
},
15
14
data : () => ({
16
- el : null ,
15
+ ws : null ,
17
16
chart: null ,
18
- cColors : 0 ,
17
+ iColor : 0 ,
19
18
colors: [
20
19
" #F44336" ,
21
20
" #9C27B0" ,
@@ -26,30 +25,41 @@ export default {
26
25
" #795548"
27
26
]
28
27
}),
28
+ created () {
29
+ this .initWS ();
30
+ },
31
+ destroyed () {
32
+ this .ws .close ();
33
+ },
29
34
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, {
32
37
baseTime: Date .now () - performance .now (),
33
38
series: [],
34
39
xRange: { min: 0 , max: 20 * 1000 },
35
40
realTime: true
36
41
});
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 () {
39
52
this .$refs .notice .show (" 连接成功" , 0 );
40
- };
41
- ws . onclose = function () {
53
+ },
54
+ WSclose () {
42
55
this .$refs .notice .show (" 连接断开" , 1 );
43
- ws = null ;
44
- };
45
- ws .onmessage = function (evt ) {
56
+ },
57
+ WSonmessage (evt ) {
46
58
this .praseWS (evt .data );
47
- };
48
- ws . onerror = function (evt ) {
59
+ },
60
+ WSonerror (evt ) {
49
61
console .log (" ERROR: " + evt .data );
50
- };
51
- },
52
- methods: {
62
+ },
53
63
praseWS (data ) {
54
64
if (data != " " ) {
55
65
const jsonWS = JSON .parse (data);
@@ -65,10 +75,10 @@ export default {
65
75
} else {
66
76
this .chart .options .series .push ({
67
77
name: jsonWS .DataPack [i].Name ,
68
- color: this .colors [this .cColors ],
78
+ color: this .colors [this .iColor ],
69
79
data: [{ x: jsonWS .DataPack [i].Tick , y: jsonWS .DataPack [i].Data }]
70
80
});
71
- this .cColors ++ ;
81
+ this .iColor ++ ;
72
82
}
73
83
}
74
84
this .chart .update ();
0 commit comments