forked from ehicks05/cryptick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
charts.html
375 lines (325 loc) · 12.3 KB
/
charts.html
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link id="styleLink" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.1/css/bulma.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script>
<style>
.red {color: red;transition: color .3s;}
.green {color: #00C000;transition: color .3s;}
#btcGraphWrapper, #ethGraphWrapper {
height: 300px;
width: 100%;
min-width: 100%;
/*overflow-x: scroll;*/
}
canvas {
padding: 10px;
position: relative;
/*overflow-x: scroll;*/
width: 100%;
}
</style>
</head>
<body>
<span id="lightBulb" style="cursor: pointer;">💡</span>
<a href="index.html" style="cursor: pointer;">🏠</a>
<section class="section">
<div class="container">
<header>BTC</header>
<div style="font-size: 4em;" class="title is-1" id="price-btc"></div>
<div class="subtitle is-3" id="time-btc"></div>
<p>
<span id="timeSince-btc"></span>
<br><span id="socketStatus-btc"></span>
</p>
<div class="has-text-danger" id="error-btc" style="display:none;"></div>
<div id="btcGraphWrapper">
<canvas id="btcGraphContainer" width="300" height="200"></canvas>
</div>
</div>
</section>
<section class="section">
<div class="container">
<header>ETH</header>
<div style="font-size: 4em;" class="title is-1" id="price-eth"></div>
<div class="subtitle is-3" id="time-eth"></div>
<p>
<span id="timeSince-eth"></span>
<br/><span id="socketStatus-eth"></span>
</p>
<div class="has-text-danger" id="error-eth" style="display:none;"></div>
<div id="ethGraphWrapper">
<canvas id="ethGraphContainer" width="300" height="200"></canvas>
</div>
</div>
</section>
<script>
document.getElementById('lightBulb').addEventListener('click', function (e)
{
var defaultURL = "https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.1/css/bulma.min.css";
var link = document.getElementById("styleLink"); //Fetch the link by its ID
if (link.getAttribute("href") === defaultURL)
link.setAttribute("href", "css/dark.css"); //Change its href attribute
else
link.setAttribute("href", defaultURL); //Change its href attribute
});
var subscribeMessage = {
"type": "subscribe",
"product_ids": [
"BTC-USD",
"ETH-USD"
],
"channels": [
"ticker"
]
};
var bitcoinData = {
"name": "BTC-USD",
"lastPrice": 0,
"lastUpdate": 0,
"timeSinceId": "timeSince-btc",
"timeId": "time-btc",
"priceId": "price-btc",
"errorId": "error-btc",
"socketStatusId": "socketStatus-btc"
};
var ethData = {
"name": "ETH-USD",
"lastPrice": 0,
"lastUpdate": 0,
"timeSinceId": "timeSince-eth",
"timeId": "time-eth",
"priceId": "price-eth",
"errorId": "error-eth",
"socketStatusId": "socketStatus-eth"
};
var cbSocket = new WebSocket("wss://ws-feed.pro.coinbase.com");
function connect(socket, subscribeMessage)
{
socket.onopen = () => socket.send(JSON.stringify(subscribeMessage));
socket.onmessage = event => {
var response = JSON.parse(event.data);
if (response.type === 'ticker')
{
var cryptoElement = response.product_id === 'BTC-USD' ? bitcoinData : ethData;
cryptoElement.lastUpdate = new Date();
var price = response.price;
if (cryptoElement.name === 'BTC-USD')
updateBTCGraph(price, new Date().toLocaleTimeString());
if (cryptoElement.name === 'ETH-USD')
updateETHGraph(price, new Date().toLocaleTimeString());
price = "$" + Number.parseFloat(price).toFixed(2);
flashPriceColorChange(price, cryptoElement.lastPrice, document.getElementById(cryptoElement.priceId));
cryptoElement.lastPrice = price;
document.getElementById(cryptoElement.priceId).innerText = price;
}
if (response.type === "error")
{
document.getElementById(cryptoElement.errorId).style.display = "block";
document.getElementById(cryptoElement.errorId).innerText = event.data;
}
};
socket.onclose = event =>
{
console.log('Socket is closed. Reconnect will be attempted in 1 second', event.reason);
setTimeout(() =>
{
connect(socket, subscribeMessage)
}, 1000);
}
}
connect(cbSocket, subscribeMessage);
var currentTime = new Date();
var socketReadyState = 0;
setInterval(() =>
{
currentTime = new Date();
var timeSinceLastUpdateBTC = getTimeSinceLastUpdate(currentTime, bitcoinData.lastUpdate);
socketReadyState = cbSocket.readyState;
document.getElementById(bitcoinData.timeId).innerText = currentTime.toLocaleTimeString();
document.getElementById(bitcoinData.timeSinceId).innerText = "Seconds since last update: " + timeSinceLastUpdateBTC;
document.getElementById(bitcoinData.socketStatusId).innerText = "socket " + getSocketStatus(socketReadyState);
var timeSinceLastUpdateETH = getTimeSinceLastUpdate(currentTime, ethData.lastUpdate);
document.getElementById(ethData.timeId).innerText = currentTime.toLocaleTimeString();
document.getElementById(ethData.timeSinceId).innerText = "Seconds since last update: " + timeSinceLastUpdateETH;
document.getElementById(ethData.socketStatusId).innerText = "socket " + getSocketStatus(socketReadyState);
}, 100);
function getTimeSinceLastUpdate(currentTime, lastUpdate)
{
return Math.round(Math.max(currentTime - lastUpdate, 0) / 1000);
}
function getSocketStatus(readyState)
{
if (readyState === 0) return "connecting";
if (readyState === 1) return "open";
if (readyState === 2) return "closing";
if (readyState === 3) return "closed";
}
function flashPriceColorChange(newPrice, lastPrice, priceElement)
{
if (newPrice > lastPrice)
priceElement.classList.add('green');
if (newPrice < lastPrice)
priceElement.classList.add('red');
setTimeout(() => priceElement.classList.remove('green', 'red'), 500)
}
function updateETHGraph(newPrice, newTime)
{
if (ethConfig.data.datasets[0].data.length > 50)
ethConfig.data.datasets[0].data.shift();
if (ethConfig.data.labels.length > 50)
ethConfig.data.labels.shift();
ethConfig.data.datasets[0].data.push(newPrice);
ethConfig.data.labels.push(newTime);
let min = Math.min(...ethConfig.data.datasets[0].data)-2;
let max = Math.max(...ethConfig.data.datasets[0].data)+2;
let range = 1 + max - min;
ethConfig.options.scales.yAxes[0].ticks.stepSize = range / 10;
console.log(`eth: ${min} - ${max} = ${Math.abs(min - max)}`);
ethConfig.options.scales.yAxes[0].ticks.suggestedMin = min;
ethConfig.options.scales.yAxes[0].ticks.suggestedaxn = max;
window.myLine2.update();
}
function updateBTCGraph(newPrice, newTime)
{
if (btcConfig.data.datasets[0].data.length > 50)
btcConfig.data.datasets[0].data.shift();
if (btcConfig.data.labels.length > 50)
btcConfig.data.labels.shift();
btcConfig.data.datasets[0].data.push(newPrice);
btcConfig.data.labels.push(newTime);
let min = Math.min(...btcConfig.data.datasets[0].data)-.25;
let max = Math.max(...btcConfig.data.datasets[0].data)+.25;
let range = 1 + max - min;
btcConfig.options.scales.yAxes[0].ticks.stepSize = range / 10;
console.log(`btc: ${min} - ${max} = ${Math.abs(min - max)}`);
btcConfig.options.scales.yAxes[0].ticks.suggestedMin = min;
btcConfig.options.scales.yAxes[0].ticks.suggestedaxn = max;
window.myLine.update();
}
var ethConfig = {
type: 'line',
response: true,
maintainAspectRatio: false,
data:
{
labels: [],
datasets: [{
label: "USD $",
fill: false,
lineTension: 0,
data: [],
pointBorderWidth: 5,
pointBackgroundColor: '#9ab9ea',
pointBorderColor: '#331530',
borderColor: '#7ec631',
}]
},
options:
{
responsive: true,
maintainAspectRatio: false,
title: {
display: true,
text: 'Etherium Price Graph'
},
hover: {
mode: 'nearest',
intersect: true,
},
scales: {
yAxes: [{
scaleLabel:
{
display:true,
labelString: 'USD $'
},
ticks: {
// suggestedMin: 100,
// suggestedMax: 200,
// stepSize: .10
}
}],
xAxes: [{
display: true,
scaleLabel: {
display: true,
labelString: 'Something'
},
ticks: {
stepSize: 1,
beginAtZero: true,
}
}]
}
}
};
var btcConfig = {
type: 'line',
responsive: true,
maintainAspectRatio: false,
data:
{
labels: [],
datasets: [{
label: "USD $",
fill: false,
lineTension: 0,
data: [],
pointBorderWidth: 5,
pointBackgroundColor: '#9ab9ea',
pointBorderColor: '#331530',
borderColor: '#7ec631',
}]
},
options:
{
responsive: true,
maintainAspectRatio: false,
title: {
display: true,
text: 'Bitcoin Price Graph'
},
hover: {
mode: 'nearest',
intersect: true,
},
scales: {
yAxes: [{
scaleLabel:
{
display:true,
labelString: 'USD $'
},
ticks: {
// suggestedMin: 4000,
// suggestedMax: 7000,
// stepSize: .10
}
}],
xAxes: [{
display: true,
scaleLabel: {
display: true,
labelString: 'Something'
},
ticks: {
stepSize: 1,
beginAtZero: true,
}
}]
}
}
};
setTimeout(createCharts, 1000);
createCharts();
function createCharts()
{
var btcGraph = document.getElementById('btcGraphContainer').getContext('2d');
window.myLine = new Chart(btcGraph, btcConfig);
var ethGraph = document.getElementById('ethGraphContainer').getContext('2d');
window.myLine2 = new Chart(ethGraph, ethConfig);
}
</script>
</body>
</html>