-
Notifications
You must be signed in to change notification settings - Fork 0
/
portfolio_template.html
413 lines (360 loc) · 14.9 KB
/
portfolio_template.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Portfolio Analysis</title>
<!-- Styles -->
<link id="tabulator-css" href="css/tabulator_midnight.min.css" rel="stylesheet">
<style>
body {
font-family: 'SF Pro Text', sans-serif;
margin: 0;
padding: 0;
background-color: #fafafa;
color: #333;
transition: background-color 0.3s, color 0.3s;
}
body.dark-mode {
background-color: #1c1c1e;
color: #f5f5f7;
}
h1 {
text-align: center;
margin-top: 40px;
color: #007aff;
font-family: 'SF Pro Display', sans-serif;
font-weight: 600;
letter-spacing: -0.5px;
}
#portfolio {
text-align: center;
width: 90%;
margin: auto;
border-radius: 12px;
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
background-color: #fff;
padding: 20px;
}
.positive-change {
color: #00FF00;
}
.negative-change {
color: #FF3333;
}
.dark-mode #portfolio {
background-color: #2c2c2e;
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.5);
}
.dark-mode-toggle {
position: absolute;
top: 20px;
right: 20px;
cursor: pointer;
color: inherit;
font-weight: 500;
}
.dark-mode-toggle .icon {
font-size: 24px;
transition: transform 0.3s ease;
}
.dark-mode-toggle.dark-mode .icon {
color: #fff;
}
#pie-chart-container, #line-chart-container {
text-align: center;
width: 90%;
max-width: 600px;
margin: 40px auto;
border-radius: 12px;
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
background-color: #fff;
padding: 20px;
}
.dark-mode #pie-chart-container, .dark-mode #line-chart-container {
background-color: #2c2c2e;
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.5);
}
#pie-chart, #line-chart {
max-width: 100%;
height: auto;
}
.chart-container {
margin-top: 20px;
}
</style>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<style>
@font-face {
font-family: 'SF Pro Display';
src: url('fonts/SF-Pro-Display-Regular.otf') format('opentype');
font-weight: normal;
font-style: normal;
}
@font-face {
font-family: 'SF Pro Display';
src: url('fonts/SF-Pro-Display-Bold.otf') format('opentype');
font-weight: bold;
font-style: normal;
}
@font-face {
font-family: 'SF Pro Text';
src: url('fonts/SF-Pro-Text-Regular.otf') format('opentype');
font-weight: normal;
font-style: normal;
}
@font-face {
font-family: 'SF Pro Text';
src: url('fonts/SF-Pro-Text-Bold.otf') format('opentype');
font-weight: bold;
font-style: normal;
}
</style>
</head>
<body class="dark-mode">
<h1>Portfolio Analysis</h1>
<!-- Dark Mode Toggle -->
<div class="dark-mode-toggle" aria-label="Toggle dark mode">
<span class="icon" role="img" aria-hidden="true">🌕</span>
</div>
<br><br>
<!-- Total Portfolio Value -->
<div id="portfolio-value"></div>
<br>
<!-- Portfolio -->
<div id="portfolio"></div>
<!-- Pie Chart Container -->
<div id="pie-chart-container" class="chart-container">
<canvas id="pie-chart" width="400" height="400" aria-label="Pie Chart"></canvas>
</div>
<!-- Line Chart Container -->
<div id="line-chart-container" class="chart-container">
<canvas id="line-chart" width="400" height="400" aria-label="Line Chart"></canvas>
</div>
<!-- External Scripts -->
<script src="js/jquery.min.js"></script>
<script src="js/tabulator.min.js"></script>
<script src="js/chart.min.js"></script>
<!-- Script -->
<script>
const coinGrouping = {
tether: ['tether', 'usd-coin', 'dai', 'usdd', 'aave-v3-usdt', 'binance-usd', 'aave-polygon-usdc', 'aave-polygon-usdt', 'bridged-tether-starkgate', 'nostra-uno', 'bridged-usd-coin-starkgate'],
celestia: ['celestia', 'stride-staked-tia'],
ethereum: ['ethereum', 'staked-ether', 'rocket-pool-eth']
};
let groupStableCoins = true; // Variable to control whether to group stable coins or not
function toggleDarkMode() {
$("body").toggleClass("dark-mode");
$(".dark-mode-toggle .icon").toggleClass("dark-mode");
// Toggle between light and dark mode CSS for the table
const tabulatorCSS = document.getElementById('tabulator-css');
if ($("body").hasClass("dark-mode")) {
tabulatorCSS.href = "css/tabulator_midnight.min.css";
} else {
tabulatorCSS.href = "css/tabulator_simple.min.css";
}
}
function displayPortfolioValue(portfolioValue, portfolioHistory) {
// Ensure portfolioValue is valid
if (!portfolioValue || typeof portfolioValue !== 'object' || !portfolioValue.coins) {
console.error('Invalid portfolio data.');
return;
}
// Ensure portfolioHistory is valid
if (!portfolioHistory || typeof portfolioHistory !== 'object' || !portfolioHistory.data) {
console.error('Invalid portfolio history data.');
return;
}
let tableData = [];
let totalValue = 0;
for (let coinId in portfolioValue.coins) {
let details = portfolioValue.coins[coinId];
let amount = details.amount;
let price = details.price;
let value = amount * price;
totalValue += value;
let price_change_1h = details.price_change_1h;
let price_change_24h = details.price_change_24h;
let price_change_7d = details.price_change_7d;
let price_change_14d = details.price_change_14d;
let price_change_30d = details.price_change_30d;
let price_change_60d = details.price_change_60d;
let price_change_200d = details.price_change_200d;
let price_change_1y = details.price_change_1y;
tableData.push({
Cryptocurrency: coinId,
Amount: amount,
'Price (USD)': price.toFixed(2),
'price_change_1h': price_change_1h.toFixed(2),
'price_change_24h': price_change_24h.toFixed(2),
'price_change_7d': price_change_7d.toFixed(2),
'price_change_14d': price_change_14d.toFixed(2),
'price_change_30d': price_change_30d.toFixed(2),
'price_change_60d': price_change_60d.toFixed(2),
'price_change_200d': price_change_200d.toFixed(2),
'price_change_1y': price_change_1y.toFixed(2),
'Value (USD)': value.toFixed(2)
});
}
let table = new Tabulator("#portfolio", {
data: tableData,
layout: "fitColumns",
columns: [
{ title: "Coin", field: "Cryptocurrency" },
{ title: "Amount", field: "Amount" },
{ title: "Price (USD)", field: "Price (USD)" },
{ title: "1H", field: "price_change_1h", formatter: priceChangeFormatter, sorter: "number" },
{ title: "24H", field: "price_change_24h", formatter: priceChangeFormatter, sorter: "number" },
{ title: "7D", field: "price_change_7d", formatter: priceChangeFormatter, sorter: "number" },
{ title: "14D", field: "price_change_14d", formatter: priceChangeFormatter, sorter: "number" },
{ title: "30D", field: "price_change_30d", formatter: priceChangeFormatter, sorter: "number" },
{ title: "60D", field: "price_change_60d", formatter: priceChangeFormatter, sorter: "number" },
{ title: "200D", field: "price_change_200d", formatter: priceChangeFormatter, sorter: "number" },
{ title: "1Y", field: "price_change_1y", formatter: priceChangeFormatter, sorter: "number" },
{ title: "Value (USD)", field: "Value (USD)" }
],
initialSort: [{ column: "price_change_24h", dir: "desc" }],
headerVertical: false // Add this line to remove the vertical header row
});
let totalPortfolioValue = totalValue.toFixed(2);
$("#portfolio-value").append(`<p style="text-align:center; font-size: 24px; font-weight: 600;">Total Portfolio Value: $${totalPortfolioValue}</p>`);
// Generate Pie Chart
generatePieChart(portfolioValue);
// Generate Line Chart
generateLineChart(portfolioHistory);
}
// Formatter function for price change columns
function priceChangeFormatter(cell, formatterParams, onRendered) {
let value = parseFloat(cell.getValue());
let formattedValue = value.toFixed(2);
let className = value > 0 ? 'positive-change' : 'negative-change';
return `<span class="${className}">${formattedValue}</span>`;
}
function generatePieChart(portfolioValue) {
let ctx = document.getElementById('pie-chart').getContext('2d');
let labels = [];
let data = [];
if (groupStableCoins) {
let stableCoinTotal = 0;
for (let coinId in portfolioValue.coins) {
if (isStableCoin(coinId)) {
stableCoinTotal += portfolioValue.coins[coinId].amount * portfolioValue.coins[coinId].price;
} else {
labels.push(coinId);
let amount = portfolioValue.coins[coinId].amount;
let price = portfolioValue.coins[coinId].price;
let value = amount * price;
data.push(value);
}
}
if (stableCoinTotal > 0) {
labels.push('Stable Coins');
data.push(stableCoinTotal);
}
} else {
for (let coinId in portfolioValue.coins) {
labels.push(coinId);
let amount = portfolioValue.coins[coinId].amount;
let price = portfolioValue.coins[coinId].price;
let value = amount * price;
data.push(value);
}
}
// Array of colors for the pie chart
let backgroundColors = [
'#ff3b30', // Red
'#ff9500', // Orange
'#ffcc00', // Yellow
'#4cd964', // Green
'#5ac8fa', // Blue
'#5856d6', // Indigo
'#ff6a80', // Pink
'#ffcc99', // Peach
'#a7ffeb', // Light Blue
'#dabaff' // Lavender
];
new Chart(ctx, {
type: 'pie',
data: {
labels: labels,
datasets: [{
label: 'Cryptocurrency Distribution',
data: data,
backgroundColor: backgroundColors.slice(0, labels.length), // Ensure number of colors matches number of labels
borderWidth: 1
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
plugins: {
title: {
display: true,
text: 'Cryptocurrency Distribution',
font: {
size: 18,
family: "'SF Pro Display', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif"
}
}
}
}
});
}
function generateLineChart(portfolioHistory) {
let ctx = document.getElementById('line-chart').getContext('2d');
let labels = [];
let data = [];
for (let key in portfolioHistory.data) {
let portfolioData = portfolioHistory.data[key].portfolio_data;
let totalValue = calculateTotalPortfolioValue(portfolioData);
let timestamp = portfolioHistory.data[key].last_updated;
labels.unshift(timestamp);
data.unshift(totalValue);
}
new Chart(ctx, {
type: 'line',
data: {
labels: labels,
datasets: [{
label: 'Total Portfolio Value',
data: data,
backgroundColor: 'rgba(54, 162, 235, 0.2)',
borderColor: 'rgba(54, 162, 235, 1)',
borderWidth: 1
}]
},
options: {
scales: {
y: {
beginAtZero: true
}
}
}
});
}
function calculateTotalPortfolioValue(portfolioValue) {
let totalValue = 0;
for (let coinId in portfolioValue.coins) {
let details = portfolioValue.coins[coinId];
let amount = details.amount;
let price = details.price;
totalValue += amount * price;
}
return totalValue;
}
function isStableCoin(coinId) {
const stable_coin_list = coinGrouping.tether;
return stable_coin_list.includes(coinId);
}
window.onload = function() {
let portfolioValue = {{portfolio_value}};
let portfolioHistory = {{portfolio_history}};
displayPortfolioValue(portfolioValue, portfolioHistory);
};
$(document).ready(function() {
$(".dark-mode-toggle").click(toggleDarkMode);
});
</script>
</body>
</html>