-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
51 lines (42 loc) · 1.36 KB
/
app.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
// 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;
let count = 1;
ws.onmessage = (event) => {
let stockObject = JSON.parse(event.data);
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();
myChart.data.labels.push(d.slice(16, 24));
myChart.data.datasets[0].data.push(lastPrice)
myChart.update();
// 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);
}
// SHOW DATA CHART
const ctx = document.getElementById('canvas').getContext('2d');
const myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: [],
datasets: [{
label: 'Price Of ETH',
data: [],
backgroundColor: 'transparent',
borderColor: 'Red',
borderWidth: 4
}]
},
options: {
scales: {
y: {
beginAtZero: true
}
}
}
});