-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
238 lines (216 loc) · 7.6 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<title>Dividends</title>
<link
rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
/>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<style>
body {
background-color: #f8f9fa;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
h1 {
color: #343a40;
margin-top: 20px;
font-weight: 700;
margin-bottom: 30px;
}
.container {
max-width: 800px;
margin: auto;
padding: 40px;
background-color: #ffffff;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.button-primary {
background-color: #007bff;
border: none;
padding: 10px 20px;
border-radius: 5px;
transition: background-color 0.3s ease;
}
.button-primary:hover {
background-color: #0056b3;
}
.card {
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
transition: transform 0.2s;
}
.card:hover {
transform: translateY(-5px);
}
</style>
</head>
<body>
<meta charset="UTF-8" />
<div class="container">
<h1 class="text-center">Dividends 🤑</h1>
<script>
function getLast5Dividends(data) {
const dates = Object.keys(data);
const last5Dates = dates.slice(0, 10);
const last5Dividends = last5Dates.map((date) => ({
date: date,
dividend: data[date],
}));
return last5Dividends;
}
const fileNames = [
"NVO",
"MSFT",
"ASML",
];
const requests = fileNames.map((fileName) => {
const xhr = new XMLHttpRequest();
xhr.open("GET", "data/" + fileName + "/daily.json", true);
xhr.send();
return xhr;
});
Promise.all(
requests.map(
(request) =>
new Promise((resolve, reject) => {
request.onreadystatechange = function () {
if (request.readyState === 4) {
if (request.status === 200) {
resolve(request.responseText);
} else {
reject(new Error(`Error loading ${request.responseURL}`));
}
}
};
})
)
)
.then((responses) => {
responses.forEach((response) => {
const data = JSON.parse(response);
const symbol = data["Meta Data"]["2. Symbol"];
const dividendData = {};
Object.keys(data["Time Series (Daily)"])
.slice(0, 5000)
.forEach((date) => {
const dividendAmount = parseFloat(
data["Time Series (Daily)"][date]["7. dividend amount"]
);
const year = date.slice(0, 4);
const d = new Date(date);
const month = d.getMonth() + 1;
const monthNames = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
];
const monthName = monthNames[month - 1];
if (dividendAmount > 0) {
dividendData[monthName + " " + year.toString()] =
dividendAmount;
}
});
const datasets = [
{
label: "Ex-Dividend " + symbol,
data: Object.values(dividendData).reverse(),
backgroundColor: "rgba(255, 99, 132, 0.2)",
borderColor: "rgba(255, 99, 132, 1)",
borderWidth: 1,
},
];
const dates = Object.keys(dividendData).reverse();
const parentDiv = document.createElement("div");
parentDiv.id = `${symbol}-dividends`;
parentDiv.style.display = "flex";
parentDiv.style.justifyContent = "center";
parentDiv.style.margin = "20px";
const canvas = document.createElement("canvas");
canvas.id = `${symbol}-chart`;
canvas.width = 800;
canvas.height = 450;
const canvasDiv = document.createElement("div");
canvasDiv.appendChild(canvas);
const ctx = canvas.getContext("2d");
const chart = new Chart(ctx, {
type: "bar",
data: {
labels: dates,
datasets: datasets,
},
options: {
scales: {
yAxes: [
{
ticks: {
beginAtZero: true,
},
},
],
],
responsive: false,
maintainAspectRatio: false,
},
});
const last5Dividends = getLast5Dividends(dividendData);
// create table element
const table = document.createElement("table");
// create table header row
const headerRow = document.createElement("tr");
const dateHeader = document.createElement("th");
dateHeader.textContent = "EX-DIVIDEND DATE";
const amountHeader = document.createElement("th");
amountHeader.textContent = "AMOUNT";
headerRow.appendChild(dateHeader);
headerRow.appendChild(amountHeader);
table.appendChild(headerRow);
// create table data rows
last5Dividends.forEach((dividend) => {
const row = document.createElement("tr");
const dateCell = document.createElement("td");
dateCell.textContent = dividend.date;
row.appendChild(dateCell);
const amountCell = document.createElement("td");
amountCell.textContent = dividend.dividend.toFixed(2);
row.appendChild(amountCell);
table.appendChild(row);
});
// add some styling to the table
table.style.borderCollapse = "collapse";
table.style.border = "1px solid #ddd";
table.style.fontFamily = "Arial, sans-serif";
const th = table.getElementsByTagName("th");
for (let i = 0; i < th.length; i++) {
th[i].style.backgroundColor = "#f2f2f2";
th[i].style.border = "1px solid #ddd";
th[i].style.padding = "8px";
th[i].style.textAlign = "left";
}
const td = table.getElementsByTagName("td");
for (let i = 0; i < td.length; i++) {
td[i].style.border = "1px solid #ddd";
td[i].style.padding = "8px";
}
const tableDiv = document.createElement("div");
tableDiv.appendChild(table);
parentDiv.appendChild(canvasDiv);
parentDiv.appendChild(tableDiv);
document.body.appendChild(parentDiv);
});
})
.catch((error) => {
console.error(error);
});
</script>
</div>
</body>
</html>