-
Notifications
You must be signed in to change notification settings - Fork 0
/
temp.js
121 lines (102 loc) · 3.26 KB
/
temp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// FETCH DATA USING API USING WEBSOCKET
let ws = new WebSocket("wss://stream.binance.com:9443/ws/etheur@trade");
let stockPriceElement = document.getElementById('stock-price')
let lastPrice = null;
var json1 = '';
var count = 0;
ws.onmessage = (event) => {
// console.log(event.data);
let stockObject = JSON.parse(event.data);
if (count == 0) {
json1 = '[' + event.data + ']';
$(document).change(function () {
$('#table1').DataTable({
"data": JSON.parse(json1),
"columns": [
{ "data": "e" },
{ "data": "E" },
{ "data": "s" },
{ "data": "t" },
{ "data": "p" },
{ "data": "q" },
{ "data": "b" },
{ "data": "a" },
{ "data": "T" },
{ "data": "m" },
{ "data": "M" }
]
});
});
count = count + 1;
}
else {
var json2 = json1.slice(0, json1.length - 1) + ',' + event.data + ']';
json1 = json2;
// console.log(json1)
console.log("i am in else loop")
setTable();
}
function setTable() {
console.log(json1)
$(document).ready(function () {
$('#table1').DataTable({
responsive: true,
destroy: true,
"data": JSON.parse(json1),
dom: 'lBfrtip',
buttons: [
'excel', 'pdf', 'print', 'copy'
],
"columns": [
{ "data": "e" },
{ "data": "E" },
{ "data": "s" },
{ "data": "t" },
{ "data": "p" },
{ "data": "q" },
{ "data": "b" },
{ "data": "a" },
{ "data": "T" },
{ "data": "m" },
{ "data": "M" }
]
});
});
}
let price = parseFloat(stockObject.p).toFixed(3);
stockPriceElement.innerText = price;
stockPriceElement.style.color = !lastPrice || lastPrice === price ? 'black' : price > lastPrice ? 'green' : 'red';
lastPrice = price;
let d = Date();
// // STORE DATA INTO TABLE
// let temp = document.createElement("tr");
// temp.innerHTML = `<td>${count++}</td><td> ${stockObject.p} </td> <td> ${d.slice(16, 24)} </td>`;
// document.getElementById('data1').appendChild(temp);
}
// FOR CHART let d = Date();
myChart.data.labels.push(d.slice(16, 24));
myChart.data.datasets[0].data.push(lastPrice)
myChart.update();
function chartopen() {
const ctx = document.getElementById('canvas').getContext('2d');
const myChart = new Chart(ctx, {
type: 'line',
data: {
labels: [],
datasets: [{
label: 'Price Of ETH',
data: [],
backgroundColor: 'transparent',
borderColor: 'Red',
borderWidth: 4
}]
},
options: {
scales: {
y: {
beginAtZero: true
}
}
}
});
}