-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
182 lines (173 loc) · 4.9 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link
rel="shortcut icon"
href="./assets/images/favicon-32x32.png"
type="image/x-icon"
/>
<link rel="stylesheet" href="css/style.css" />
<title>Frontendmentor | Expense chart</title>
</head>
<body>
<main>
<div class="head__container">
<div class="head__container__balance">
<p class="name">My balance</p>
<h2 class="head__balance">$921.48</h2>
</div>
<svg
width="72"
height="48"
viewBox="0 0 72 48"
xmlns="http://www.w3.org/2000/svg"
>
<g fill="none" fill-rule="evenodd">
<circle fill="#F43F5E" cx="48" cy="24" r="24" />
<circle stroke="#F43F5E" stroke-width="2" cx="24" cy="24" r="23" />
</g>
</svg>
</div>
<div class="board">
<h2 id="title" class="board__title amount" style="--i: 1">
Spending - Last 7 days
</h2>
<canvas
id="chart"
class="board__chart"
aria-label="title"
role="img"
></canvas>
<hr class="board__line" />
<div>
<p class="board__total">Total this month</p>
<div class="board__summary">
<h1 class="board__amount amount" style="--i: 2">
$<span class="board__total__amount amount">478.33</span>
</h1>
<div>
<p class="board__percentage amount" style="--i: 3">+2.4%</p>
<p class="board__total">from last month</p>
</div>
</div>
</div>
</div>
</main>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script>
const canvas = document.getElementById('chart');
Chart.defaults.defaultFontFamily = 'DM Sans';
Chart.defaults.color = '#60A5FA';
Chart.defaults.font.size = 15;
if (window.screen.width <= 400) {
canvas.height = 200;
}
const customTooltip = {
caretSize: '',
backgroundColor: '#F43F5E',
yAlign: 'bottom',
displayColors: false,
padding: 7.2,
bodyFont: {
font: 'DM Sans',
size: 15,
},
callbacks: {
title: function (context) {
return '';
},
label: function (context) {
let label = context.dataset.raw || '';
if (label) {
label += ': ';
}
if (context.parsed.y !== null) {
label += new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
}).format(context.parsed.y);
}
return label;
},
},
};
async function init() {
try {
const path = './data/data.json';
const request = new Request(path);
const resp = await fetch(request);
const data = await resp.json();
const currentDay = data.map((item, index) => {
const d = new Date();
let day = d.getDay();
if (index + 1 === day) {
return '#F43F5E';
} else {
return '#A78BFA';
}
});
new Chart(canvas, {
type: 'bar',
data: {
labels: data.map((row) => row.day),
datasets: [
{
data: data.map((row) => row.amount),
backgroundColor: currentDay,
borderRadius: 4,
borderSkipped: false,
// barPercentage: 2,
// categoryPercentage: 1.0,
},
],
},
options: {
plugins: {
legend: {
display: false,
},
tooltip: customTooltip,
},
scales: {
y: {
ticks: {
padding: 0,
display: false,
},
grid: {
display: false,
},
border: {
display: false,
},
},
x: {
grid: {
display: false,
},
border: {
display: false,
},
},
},
layout: {
padding: {
top: 25,
left: 0,
bottom: 0,
right: 0,
},
},
},
});
} catch (error) {
console.log(error);
}
}
init();
</script>
</body>
</html>