forked from sisou/nimiq-watch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script-chart-transactions-per-block.js
160 lines (142 loc) · 6.42 KB
/
script-chart-transactions-per-block.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
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
template.transactionsPerBlockChart = tmpl('template-transactions-per-block-chart');
async function _transactionsPerBlock(range, skipRender) {
range = range || 'month';
skipRender = skipRender || false;
if(!skipRender) {
// Only replace infobox contents when not coming from the range toggle
$infobox.innerHTML = template.transactionsPerBlockChart();
window.scrollTo(0, $infobox.offsetTop - 100);
}
fetch(apiUrl + '/statistics/transactions/' + range).then(function(response) {
response.json().then(function(data) { // { h: block_height, t: timestamp, v: value, c: count }
var transactionCount = data.map(function(block) { return block['c']; });
var transactionValue = data.map(function(block) { return block['v']; });
var timestamp = data.map(function(block) { return block['t']; });
// Fill empty times
var i = 0
var gap;
var now = Date.now() / 1000;
switch(range) {
case 'day':
gap = 15 * 60; // 15 minutes
break;
case 'week':
gap = 2 * 60 * 60 // 2 hours
break;
case 'month':
gap = 8 * 60 * 60; // 8 hours
break;
default: // 'year'
gap = 4 * 24 * 60 * 60; // 4 days
break;
}
while(timestamp[i]) {
if(
(!timestamp[i + 1] && now - timestamp[i] > gap * 1.8)
|| (timestamp[i + 1] > timestamp[i] + gap * 1.8)
) {
// Add missing time
timestamp.splice(i + 1, 0, timestamp[i] + gap);
transactionCount.splice(i + 1, 0, 0);
transactionValue.splice(i + 1, 0, 0);
}
i++;
}
var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
var timelabel = timestamp.map(function(time) {
var date = new Date(time * 1000);
return date.getDate() + ". " + months[date.getMonth()] + " " + date.toTimeString().slice(0, 5);
});
transactionValue = transactionValue.map(value => Nimiq.Policy.satoshisToCoins(value));
var _renderTransactionsPerBlockChart = function() {
try {
$infobox.removeChild($infobox.getElementsByClassName('blocklist-loader')[0]);
}
catch(e) {}
$infobox.getElementsByClassName('chart-valid-info')[0].innerHTML = "Created " + (new Date()).toLocaleString();
if(window.chart) window.chart.destroy();
var ctx = $infobox.getElementsByTagName('canvas')[0].getContext('2d');
window.chart = new Chart(ctx, {
// The type of chart we want to create
type: 'bar',
// The data for our dataset
data: {
labels: timelabel,
datasets: [
{
label: "Transaction Volume",
backgroundColor: '#F6AE2D',
pointBackgroundColor: 'transparent',
borderColor: '#F6AE2D',
pointRadius: 3,
borderWidth: 2,
fill: false,
data: transactionValue,
type: 'line',
yAxisID: 'transactionValue'
},
{
label: "Transactions",
backgroundColor: '#042146',
borderColor: '#042146',
data: transactionCount,
yAxisID: 'transactionCount'
}]
},
// Configuration options go here
options: {
legend: {
// display: false
reverse: true,
labels: {
fontFamily: "'Source Sans Pro', sans-serif",
fontSize: 16,
boxWidth: 16
}
},
scales: {
yAxes: [
{
id: 'transactionValue',
position: 'right',
scaleLabel: {
display: true,
labelString: "Transaction Volume",
fontFamily: "'Source Sans Pro', sans-serif",
fontSize: 16
}
},
{
id: 'transactionCount',
position: 'left',
scaleLabel: {
display: true,
labelString: "Transactions",
fontFamily: "'Source Sans Pro', sans-serif",
fontSize: 16
}
}
]
},
animation: {
duration: 0,
},
}
});
};
if(document.getElementById('chart-js-script')) {
_renderTransactionsPerBlockChart();
}
else {
var scriptTag = document.createElement('script');
scriptTag.id = "chart-js-script";
scriptTag.src = "https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js";
scriptTag.onload = _renderTransactionsPerBlockChart;
document.body.appendChild(scriptTag);
}
});
});
}
function switchTransactionsPerBlockRange(range) {
_transactionsPerBlock(range, true);
}