-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex1.html
266 lines (237 loc) · 7.35 KB
/
index1.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
<!DOCTYPE html>
<html>
<body>
<head>
<title id="title">Ticker</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script>
</head>
<body>
<br>
<style>
div {float: left;}
.stats {
margin-left: 5px;
margin-right: 5px
}
#ltc {margin-left: 5px;}
.chart {max-width: 900px;}
</style>
<div>Current Price:</div><div class="stats" id="current">Checking... Pls wait a moment.</div>
<br>
<div>High:</div><div class="stats" id="high">Checking...</div>
<br>
<div>Low:</div><div class="stats" id="low">Checking...</div>
<br>
<div>Volume_24hr:</div><div class="stats" id="volume">Checking...</div>
<br>
<div>Bids (buy orders w/in $1.50):</div><div class="stats" id="bids">Checking...</div>
<br>
<div>Asks (sell orders w/in $1.50):</div><div class="stats" id="asks">Checking...</div>
<br>
<div>BTC:</div><div class="stats" id="btc">Checking...</div><div>/ LTC:</div><div class="stats" id="ltc">Checking...</div>
<br>
<hr>
<br>
<div class="chart">
<canvas id="eth-price" width="800" height="450"></canvas>
</div>
<br>
<br>
<div class="chart">
<canvas id="eth-orders" width="800" height="450"></canvas>
</div>
<br>
<br>
<div class="chart">
<canvas id="eth-volume" width="800" height="450"></canvas>
</div>
<script>
var priceArr = [];
var timeStamps = [];
var volume = [];
var current = [];
var high;
var low;
//Populates Historical Data for Graph (Last 60 minutes), refreshes with new data hereafter
setTimeout(function(){
fetch('chart', {method: 'get'}).then(res => res.json()).then(function(data, err) {
var oneHundred = data.slice(0, 99).reverse();
oneHundred.forEach( el => {
timeStamps.push(new Date(el[0] * 1000).toLocaleTimeString());
priceArr.push( el[4] );
volume.push( el[5] );
});
high = Math.max(...priceArr);
low = Math.min(...priceArr);
document.getElementById('high').innerHTML = high;
document.getElementById('low').innerHTML = low;
document.getElementById('current').innerHTML = priceArr[priceArr.length-1];
document.getElementById('title').innerHTML = priceArr[priceArr.length-1];
});
//Draws initial Historical Price chart, then get overriden by the interval call to /chart path below
setTimeout(function(){
new Chart(document.getElementById("eth-price"), {
type: 'line',
data: {
labels: timeStamps,
datasets: [{
data: priceArr,
label: "ETH",
borderColor: "#3e95cd",
fill: false
}]
},
options: {
title: {
display: true,
text: 'ETH Price'
}
}
});
//Draws Volumes Chart and stays static at the moment, not refreshed for now
new Chart(document.getElementById("eth-volume"), {
type: 'bar',
data: {
labels: timeStamps,
datasets: [{
data: volume,
label: "ETH",
borderColor: "#3e95cd",
fill: true
}]
},
options: {
title: {
display: true,
text: 'ETH 5m Volume'
}
}
});
},2000);
},1000);
//Updates the title prices with current price every 3 seconds
setInterval(function(){
fetch('price', {method: 'get'}).then(data => data.json()).then(function(data, err) {
var midETH = data.eth;
var eth = JSON.parse(data.allCoins['eth']);
var btc = JSON.parse(data.allCoins['btc']);
var ltc = JSON.parse(data.allCoins['ltc']);
document.getElementById('title').innerHTML = midETH;
document.getElementById('current').innerHTML = midETH;
document.getElementById('btc').innerHTML = formatCommas(parseFloat(btc.price).toFixed(2));
document.getElementById('ltc').innerHTML = parseFloat(ltc.price).toFixed(2);
document.getElementById('volume').innerHTML = formatCommas(parseInt(eth.volume));
priceArr[priceArr.length-1] = midETH;
current = midETH;
if (midETH > high) document.getElementById('high').innerHTML = midETH;
if (midETH < low) document.getElementById('low').innerHTML = midETH;
});
//Updates only the latest data point on the the Price chart to keep it real-time
setTimeout(function(){
new Chart(document.getElementById("eth-price"), {
type: 'line',
data: {
labels: timeStamps,
datasets: [{
data: priceArr,
label: "ETH",
borderColor: "#3e95cd",
fill: false
}]
},
options: {
title: {
display: true,
text: 'ETH Price'
},
animation: {
duration: 0
}
}
});
},1000);
}, 3000);
//Refreshes the overall Historical Price Chart every 5 minutes:
setInterval(function(){
fetch('chart', {method: 'get'}).then(data => data.json()).then(function(data, err) {
var oneHundred = data.slice(0, 99).reverse();
timeStamps = [];
priceArr = [];
oneHundred.forEach( el => {
timeStamps.push(new Date(el[0] * 1000).toLocaleTimeString());
priceArr.push( el[4] );
});
high = Math.max(...priceArr);
low = Math.min(...priceArr);
if (high > current) document.getElementById('high').innerHTML = high;
if (low < current) document.getElementById('low').innerHTML = low;
});
new Chart(document.getElementById("eth-price"), {
type: 'line',
data: {
labels: timeStamps,
datasets: [{
data: priceArr,
label: "ETH",
borderColor: "#3e95cd",
fill: false
}]
},
options: {
title: {
display: true,
text: 'ETH Price'
}
}
});
}, 300000);
//Updates Bid / Ask orders every 3 seconds, specify the $ range below:
var orderBookChart;
setInterval(function(){
fetch('bidAsk', {method: 'get'}).then(data => data.json()).then(function(data, err) {
document.getElementById('asks').innerHTML = data.askTotal;
document.getElementById('bids').innerHTML = data.bidTotal;
if ((data.askTotal - data.bidTotal) > 150) {
document.getElementById('asks').style.color = 'red';
} else if ((data.bidTotal - data.askTotal) > 150) {
document.getElementById('bids').style.color = 'green';
} else {
document.getElementById('asks').style.color = 'black';
document.getElementById('bids').style.color = 'black';
}
orderBookChart = data.chartData;
});
//draws orderbook chart
setTimeout(function(){
new Chart(document.getElementById("eth-orders"), {
type: 'line',
data: {
labels: orderBookChart[1],
datasets: [{
data: orderBookChart[0],
label: "ETH Order Book",
borderColor: "#3e95cd",
fill: false
}]
},
options: {
title: {
display: true,
text: 'ETH Order Book'
},
animation: {
duration: 0
}
}
});
},1000);
}, 3000);
//adds commas per thousands to values, accounts for decimal values.
function formatCommas(x) {
var wholeNumbers = x.toString().split(".");
wholeNumbers[0] = wholeNumbers[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
return wholeNumbers.join(".");
}
</script>
</body>
</html>