This repository was archived by the owner on Dec 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsleepgraphs.html
More file actions
144 lines (126 loc) · 4.7 KB
/
Copy pathsleepgraphs.html
File metadata and controls
144 lines (126 loc) · 4.7 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
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
---
permalink: /sleepgraph/
search_exclude: true
---
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>User Sleep Graph</title>
<!-- Chart.js library -->
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<style>
/* Style the chart container */
#sleepChartContainer {
width: 2000px;
height: 2000px;
margin: 20px auto;
}
</style>
</head>
<body>
<!-- Chart container -->
<div id="sleepChartContainer">
<canvas id="sleepChart"></canvas>
</div>
<script type="module">
import { uri, options } from '{{site.baseurl}}/assets/js/api/config.js';
// Function to calculate total sleep duration from sleep data
function calculateTotalSleepDuration(sleepData) {
const totalDurationByDay = {};
for (let i = 0; i < sleepData.length; i++) {
const entry = sleepData[i];
const date = entry.sleepDate;
const duration = parseFloat(entry.sleepHours) || 0;
if (totalDurationByDay[date]) {
totalDurationByDay[date] += duration;
} else {
totalDurationByDay[date] = duration;
}
}
return totalDurationByDay;
}
// Function to determine color based on sleep quality and iterate through the qualities array
function getColor(qualities, index) {
for (let i = 0; i < qualities.length; i++) {
if (i === index) {
const quality = qualities[i].toLowerCase();
switch (quality) {
case 'excellent':
return 'rgba(75, 192, 192, 0.5)';
case 'good':
return 'rgba(54, 162, 235, 0.5)';
case 'fair':
return 'rgba(255, 206, 86, 0.5)';
case 'poor':
return 'rgba(255, 99, 132, 0.5)';
default:
return 'rgba(0, 0, 0, 0.5)';
}
}
}
}
// Fetch user data and create the chart
const userIDFromLocalStorage = localStorage.getItem('loggedInUserId');
fetch(`https://well.stu.nighthawkcodingsociety.com/api/users/${userIDFromLocalStorage}`)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(userData => {
console.log('Fetched User Data:', userData);
const sleepData = userData.sleep || [];
const totalDurationByDay = calculateTotalSleepDuration(sleepData);
console.log("Total duration by day:", totalDurationByDay);
const sleepDates = Object.keys(totalDurationByDay);
const sleepHours = Object.values(totalDurationByDay);
// Extract qualities from the sleepData array
const sleepQualities = sleepData.map(entry => entry.quality);
const colors = [];
for (let i = 0; i < sleepDates.length; i++) {
const date = sleepDates[i];
colors.push(getColor(sleepQualities, i)); // Call getColor function with qualities array and index
}
const ctx = document.getElementById('sleepChart').getContext('2d');
const sleepChart = new Chart(ctx, {
type: 'bar',
data: {
labels: sleepDates,
datasets: [{
label: 'Total Sleep Duration',
data: sleepHours,
backgroundColor: colors,
borderColor: colors.map(color => color.replace('0.5', '1')),
borderWidth: 1
}]
},
options: {
responsive: false,
maintainAspectRatio: false,
scales: {
y: {
beginAtZero: true,
title: {
display: true,
text: 'Sleep (Hours)'
}
},
x: {
title: {
display: true,
text: 'Date'
}
}
}
}
});
})
.catch(error => {
console.error('Error fetching data:', error);
});
</script>
</body>
</html>