-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChart.js
More file actions
68 lines (59 loc) · 1.56 KB
/
Chart.js
File metadata and controls
68 lines (59 loc) · 1.56 KB
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
let logsChartInstance = null;
async function loadLogs() {
const user = auth.currentUser;
if (!user) return;
const snap = await db.collection("logs")
.where("userId", "==", user.uid)
.orderBy("date", "desc")
.limit(30)
.get();
const logs = [];
logsList.innerHTML = "";
snap.forEach(doc => {
const data = doc.data();
logs.push(data);
const li = document.createElement("li");
li.textContent = `${data.date} - [${data.tag}] ${data.title}`;
logsList.appendChild(li);
});
updateStreak(logs);
drawLogsChart(logs);
}
// compute consecutive streak ending today
function updateStreak(logs) {
const datesSet = new Set(logs.map(l => l.date));
let streak = 0;
let current = new Date();
while (true) {
const dStr = current.toISOString().slice(0,10);
if (datesSet.has(dStr)) {
streak++;
current.setDate(current.getDate() - 1);
} else {
break;
}
}
streakText.textContent = `Current streak: ${streak} day(s)`;
}
// Chart: count logs per day
function drawLogsChart(logs) {
const byDate = {};
logs.forEach(l => {
byDate[l.date] = (byDate[l.date] || 0) + 1;
});
const labels = Object.keys(byDate).sort();
const values = labels.map(d => byDate[d]);
const ctx = document.getElementById("logsChart").getContext("2d");
if (logsChartInstance) logsChartInstance.destroy();
logsChartInstance = new Chart(ctx, {
type: "bar",
data: {
labels,
datasets: [{
label: "Logs per day",
data: values,
backgroundColor: "rgba(37, 99, 235, 0.6)"
}]
}
});
}