forked from nimiq/nimiq-demo
-
Notifications
You must be signed in to change notification settings - Fork 16
/
script-chart-wealth-distribution.js
135 lines (114 loc) · 4.71 KB
/
script-chart-wealth-distribution.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
template.wealthDistributionChart = tmpl('template-wealth-distribution-chart');
async function _wealthDistribution(axisType = "linear", skipRender = false) {
if(!skipRender) {
// Only replace infobox contents when not coming from the scale toggle
$infobox.innerHTML = template.wealthDistributionChart();
window.scrollTo(0, $infobox.offsetTop - 100);
}
var rootKey = await $.accounts._tree._store.getRootKey();
var rootNode = await $.accounts._tree._store.get(rootKey);
var balances = [];
await _resolveAccountsTreeNode(rootNode, "", balances);
balances = balances.filter(balance => balance);
balances.sort((a, b) => a > b ? -1 : a < b ? 1 : 0);
balances = balances.map(balance => Nimiq.Policy.satoshisToCoins(balance));
var _renderWealthDisributionChart = function() {
var labels = [];
for (var i = 1; i <= balances.length; i++) { labels.push(i); }
labels = labels.map(label => label + '.');
try {
$infobox.removeChild($infobox.getElementsByClassName('blocklist-loader')[0]);
}
catch(e) {}
var time = new Date($.blockchain.head.timestamp * 1000);
$infobox.getElementsByClassName('chart-valid-info')[0].innerHTML = "Created at block #" + $.blockchain.head.height + " - " + time.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: 'line',
// The data for our dataset
data: {
labels: labels,
datasets: [{
label: "BetaNIM",
pointBackgroundColor: '#042146',
borderColor: '#042146',
data: balances
}]
},
// Configuration options go here
options: {
animation: {
duration: 0
},
legend: {
display: false
},
scales: {
yAxes: [{
ticks: {
callback: (value) => {
if(axisType === "linear") {
return value;
}
else if(axisType === "logarithmic") {
let label = Math.round(parseFloat(value) * Nimiq.Policy.SATOSHIS_PER_COIN) / Nimiq.Policy.SATOSHIS_PER_COIN;
if(label.toString()[0] !== "1" && label.toString().slice(-1) !== "1") return null;
return label;
}
}
},
type: axisType,
scaleLabel: {
display: true,
labelString: "Balance in NIM",
fontFamily: "'Source Sans Pro', sans-serif",
fontSize: 16
}
}],
xAxes: [{
scaleLabel: {
display: true,
labelString: "Accounts (ordered by balance)",
fontFamily: "'Source Sans Pro', sans-serif",
fontSize: 16
}
}]
}
}
});
$infobox.getElementsByTagName('input')[0].removeAttribute('disabled');
};
if(document.getElementById('chart-js-script')) {
_renderWealthDisributionChart();
}
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 = _renderWealthDisributionChart;
document.body.appendChild(scriptTag);
}
}
async function _resolveAccountsTreeNode(currentNode, currentPrefix, balances) {
if(!currentNode) return;
currentPrefix += currentNode.prefix;
if (currentNode.isTerminal()) {
balances.push(currentNode.account.balance.value);
}
else {
for (const childKey of currentNode.getChildren()) {
const node = await $.accounts._tree._store.get(childKey);
await _resolveAccountsTreeNode(node, currentPrefix, balances);
}
}
}
function _wealthDistributionSwitchScale(self) {
if(self.checked) {
_wealthDistribution("logarithmic", true);
}
else {
_wealthDistribution("linear", true);
}
}