Newer
Older
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
export function renderSleepActivityVisualization() {
fetch("../static/js/final_combined_with_all_data.json") // Adapter le chemin si nécessaire
.then((response) => response.json())
.then((data) => {
const svg = d3
.select("#sleep-activity-visualization")
.append("svg")
.attr("width", 700)
.attr("height", 300);
const margin = { top: 20, right: 150, bottom: 50, left: 50 };
const width = +svg.attr("width") - margin.left - margin.right;
const height = +svg.attr("height") - margin.top - margin.bottom;
const g = svg
.append("g")
.attr("transform", `translate(${margin.left},${margin.top})`);
const tooltip = d3
.select("body")
.append("div")
.attr("class", "tooltip-sleepactivity");
const colorMap = {
Maya: "#0f7e06",
Corentin: "#1d38e3",
Anis: "#d6bff4",
Amira: "#7e09bd",
};
const getISOWeekNumber = (date) => {
const tempDate = new Date(date);
tempDate.setHours(0, 0, 0, 0);
tempDate.setDate(tempDate.getDate() + 4 - (tempDate.getDay() || 7));
const yearStart = new Date(tempDate.getFullYear(), 0, 1);
return Math.ceil(((tempDate - yearStart) / 86400000 + 1) / 7);
};
const filteredData = data.filter((d) => {
const date = new Date(d.date);
return date >= new Date("2023-10-01") && date <= new Date("2024-12-31");
});
const groupedData = d3.group(filteredData, (d) => {
const date = new Date(d.date);
const weekNumber = getISOWeekNumber(date);
return `${date.getFullYear()}-W${weekNumber}`;
});
const processedData = Array.from(groupedData, ([week, records]) => {
return records
.map((d) =>
[
{
name: "Anis",
steps: d.Steps_Anis,
sleep: d.Sleep_Anis,
calories: d.Calories_Anis,
},
{
name: "Maya",
steps: d.Steps_Maya,
sleep: d.Sleep_Maya,
calories: d.Calories_Maya,
},
{
name: "Corentin",
steps: d.Steps_Corentin,
sleep: d.Sleep_Corentin,
calories: d.Calories_Corentin,
},
{
name: "Amira",
steps: d.Steps_Amira,
sleep: d.Sleep_Amira,
calories: d.Calories_Amira,
},
].filter((d) => d.steps > 0 && d.sleep > 0)
)
.flat();
});
const x = d3
.scaleLinear()
.domain([0, Math.ceil(d3.max(processedData.flat(), (d) => d.steps))])
.range([0, width]);
const y = d3.scaleLinear().domain([0, 18]).range([height, 0]);
const radius = d3
.scaleSqrt()
.domain([0, Math.ceil(d3.max(processedData.flat(), (d) => d.calories))])
.range([3, 15]);
g.append("g")
.attr("transform", `translate(0,${height})`)
.call(d3.axisBottom(x).ticks(10))
.append("text")
.attr("fill", "black")
.attr("x", width / 2)
.attr("y", 40)
.attr("text-anchor", "middle")
.text("Nombres de pas");
g.append("g")
.call(d3.axisLeft(y))
.append("text")
.attr("fill", "black")
.attr("transform", "rotate(-90)")
.attr("x", -height / 2)
.attr("y", -40)
.attr("text-anchor", "middle")
.text("Nombres d'heurs de sommeil");
const legend = svg
.append("g")
.attr("transform", `translate(${width + 20}, 50)`);
legend
.selectAll("rect")
.data(Object.keys(colorMap))
.enter()
.append("rect")
.attr("x", 0)
.attr("y", (d, i) => i * 20)
.attr("width", 15)
.attr("height", 15)
.attr("fill", (d) => colorMap[d]);
legend
.selectAll("text")
.data(Object.keys(colorMap))
.enter()
.append("text")
.attr("x", 20)
.attr("y", (d, i) => i * 20 + 12)
.text((d) => d);
// Ajout de la légende pour la taille des bulles
legend
.append("text")
.attr("x", 0)
.attr("y", Object.keys(colorMap).length * 20 + 50)
.text("calories brûlées");
legend
.append("circle")
.attr("cx", 3)
.attr("cy", Object.keys(colorMap).length * 20 + 70)
.attr("r", radius(10)) // Taille de la bulle pour 10 calories (ajustez si nécessaire)
.attr("fill", "#ccc")
.attr("opacity", 0.7);
legend
.append("text")
.attr("x", 30)
.attr("y", Object.keys(colorMap).length * 20 + 75)
.text("3 calories");
legend
.append("circle")
.attr("cx", 10)
.attr("cy", Object.keys(colorMap).length * 20 + 100)
.attr("r", radius(100)) // Taille de la bulle pour 100 calories
.attr("fill", "#ccc")
.attr("opacity", 0.7);
legend
.append("text")
.attr("x", 30)
.attr("y", Object.keys(colorMap).length * 20 + 105)
.text("600 calories");
const slider = document.getElementById("date-slider");
const playButton = document.getElementById("play-button");
let playing = false;
let interval;
slider.max = processedData.length - 1;
const update = (index) => {
const currentData = processedData[index];
const weekLabel = Array.from(groupedData.keys())[index];
document.getElementById("date-label").textContent = weekLabel;
g.selectAll("circle").remove();
g.selectAll("circle")
.data(currentData)
.enter()
.append("circle")
.attr("cx", (d) => x(d.steps))
.attr("cy", (d) => y(d.sleep))
.attr("r", (d) => radius(d.calories))
.attr("fill", (d) => colorMap[d.name])
.attr("opacity", 0.7)
.on("mouseover", (event, d) => {
tooltip
.style("visibility", "visible")
.text(
`${d.name}: Pas: ${d.steps}, Sommeil: ${d.sleep}, Calories: ${d.calories}`
);
})
.on("mousemove", (event) => {
tooltip
.style("top", `${event.pageY - 10}px`)
.style("left", `${event.pageX + 10}px`);
})
.on("mouseout", () => {
tooltip.style("visibility", "hidden");
});
};
playButton.addEventListener("click", () => {
if (!playing) {
playing = true;
playButton.textContent = "Pause";
let index = 0;
interval = setInterval(() => {
if (index >= processedData.length) {
clearInterval(interval);
playButton.textContent = "Play";
playing = false;
} else {
slider.value = index;
update(index);
index++;
}
}, 800);
} else {
clearInterval(interval);
playButton.textContent = "Play";
playing = false;
}
});
slider.addEventListener("input", (event) => update(+event.target.value));
update(0);
})
.catch((error) => console.error("Error loading data:", error));
}