Newer
Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
members.forEach((member, i) => {
legend.append("rect")
.attr("x", i * 100 - (members.length * 50)) // Espacement horizontal entre les rectangles
.attr("y", 0)
.attr("width", 15)
.attr("height", 15)
.attr("fill", colorMap[member]);
legend.append("text")
.attr("x", i * 100 - (members.length * 50) + 20) // Texte à côté du rectangle
.attr("y", 12)
.text(member)
.style("font-size", "12px")
.attr("text-anchor", "start");
});
// Ajouter un élément pour "Pas de données"
legend.append("rect")
.attr("x", members.length * 100 - (members.length * 50)) // Position pour le rectangle gris
.attr("y", 0)
.attr("width", 15)
.attr("height", 15)
.attr("fill", "lightgrey");
legend.append("text")
.attr("x", members.length * 100 - (members.length * 50) + 20) // Texte à côté du rectangle gris
.attr("y", 12)
.text("Pas de données")
.style("font-size", "12px")
.attr("text-anchor", "start");
// Barres pour chaque membre
members.forEach((member, i) => {
svg.selectAll(`.bar-sleep-${member}`)
.data(aggregatedData)
.enter()
.append("rect")
.attr("x", d => xScale(d.month) + i * (xScale.bandwidth() / members.length))
.attr("y", d => {
const value = d[`Sleep_${member}`];
return value === -1.0 ? yScale(2) : yScale(value); // Placer les -1.0 à une hauteur fixe, ici 2 heures
})
.attr("width", xScale.bandwidth() / members.length)
.attr("height", d => {
const value = d[`Sleep_${member}`];
return value === -1.0 ? height - yScale(2) : height - yScale(value); // Barres grisées si -1.0
})
.attr("fill", d => {
const value = d[`Sleep_${member}`];
return value === -1.0 ? "#D3D3D3" : colorMap[member]; // Gris pour -1.0
})
.on("mouseover", function(event, d) {
tooltip.transition().duration(200).style("opacity", .9); // Transition d'apparition du tooltip
tooltip.html(`Mois : ${d.month}<br>Sommeil moyen : ${d[`Sleep_${member}`] === -1.0 ? "Pas de données" : d[`Sleep_${member}`].toFixed(2)} heures`) // Affichage du tooltip
.style("left", (event.pageX + 5) + "px")
.style("top", (event.pageY - 28) + "px");
})
.on("mouseout", function() {
tooltip.transition().duration(500).style("opacity", 0); // Transition de disparition du tooltip
})
.on("click", (event, d) => {
const memberColor = colorMap[member]; // Récupérer la couleur du membre
showDetailChart(d.month, member, selectedYear, memberColor); // Passer la couleur à la fonction showDetailChart
});
});
}
// Fonction pour afficher les détails
function showDetailChart(month, member, year, memberColor) {
// Affiche le modal
const modal = d3.select("#modal");
modal.style("display", "block");
// Fermer le modal
modal.select(".close").on("click", () => {
modal.style("display", "none");
d3.select("#detail-visualization").selectAll("*").remove();
});
const detailContainer = d3.select("#detail-visualization");
detailContainer.selectAll("*").remove();
const filteredData = data.filter(d => formatYear(d.date) === year.toString() && formatMonth(d.date) === month);
const dailyData = d3.groups(filteredData, d => formatDay(d.date)).map(([day, records]) => ({
day: day,
value: d3.mean(records, d => d[`Sleep_${member}`] || 0)
}));
const detailSvg = detailContainer.append("svg")
.attr("width", 600)
.attr("height", 400)
.append("g")
.attr("transform", "translate(50, 50)");
const xScale = d3.scaleBand()
.domain(dailyData.map(d => d.day))
.range([0, 500])
.padding(0.1);
const yScale = d3.scaleLinear()
.domain([0, d3.max(dailyData, d => d.value)]).nice()
.range([300, 0]);
// Ajout de l'axe X
detailSvg.append("g")
.attr("transform", "translate(0, 300)")
.call(d3.axisBottom(xScale));
// Ajout de l'axe Y
detailSvg.append("g")
.call(d3.axisLeft(yScale));
// Titre du graphique
detailSvg.append("text")
.attr("x", 250)
.attr("y", -20)
.attr("text-anchor", "middle")
.style("font-size", "16px")
.text(`${member} - Sommeil du mois de ${month} (${year})`);
// Ajouter la légende de l'axe X (Jours)
detailSvg.append("text")
.attr("x", 250)
.attr("y", 340)
.attr("text-anchor", "middle")
.style("font-size", "12px")
.text("Jours du mois");
// Ajouter la légende de l'axe Y (Heures de sommeil)
detailSvg.append("text")
.attr("transform", "rotate(-90)")
.attr("x", -200)
.attr("y", -40)
.attr("text-anchor", "middle")
.style("font-size", "12px")
.text("Heures de sommeil moyen");
// Dessin des barres
detailSvg.selectAll(".bar")
.data(dailyData)
.enter()
.append("rect")
.attr("x", d => xScale(d.day))
.attr("y", d => {
const value = d.value;
return value < 0 || value === null ? yScale(2) : yScale(value);
})
.attr("width", xScale.bandwidth())
.attr("height", d => {
const value = d.value;
return value < 0 || value === null ? 300 - yScale(2) : 300 - yScale(value);
})
.attr("fill", d => {
const value = d.value;
return value < 0 || value === null ? "lightgrey" : memberColor;
})
.on("mouseover", function(event, d) {
tooltip.transition().duration(200).style("opacity", 1);
tooltip.html(`
<div style="text-align: center;">
<strong>Jour :</strong> ${d.day}<br>
<strong>Sommeil :</strong> ${d.value === -1.0 || d.value === null ? "Pas de données" : d.value.toFixed(2)} heures
</div>
`);
})
.on("mousemove", function(event) {
tooltip
.style("left", (event.pageX + 15) + "px") // Décalage pour positionner le tooltip
.style("top", (event.pageY + 15) + "px");
})
.on("mouseout", function() {
tooltip.transition().duration(500).style("opacity", 0);
});
}
// Initialisation de la visualisation
updateVisualization(years[3]);
// Mettre à jour la visualisation lorsque le slider est déplacé
rangeSlider.on("input", function() {
const selectedYear = years[this.value];
yearDisplay.text(selectedYear);
updateVisualization(selectedYear);
});
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
// Visu 5
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")
.style("position", "absolute")
.style("visibility", "hidden")
.style("background", "#fff")
.style("border", "1px solid #ccc")
.style("padding", "5px")
.style("border-radius", "4px")
.style("font-size", "12px");
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("Steps");
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("Sleep (hours)");
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);
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}: Steps: ${d.steps}, Sleep: ${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++;
}
} 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));
}
// Visu 6
function renderRadialDistanceChart() {
fetch("../static/js/final_combined_with_all_data.json")
.then((response) => response.json())
.then((data) => {
const width = 300;
const height = 300;
const innerRadius = 30;
const outerRadius = Math.min(width, height) / 2 - 20;
// Filter data
const filteredData = data.filter((d) => {
const date = new Date(d.date);
return date >= new Date("2023-10-01") && date <= new Date("2024-12-31");
});
// Group data by ISO week
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]) => {
const aggregated = {
week: week,
year: week.split("-")[0],
Distance_Anis: d3.sum(records, (d) => (d.Distance_Anis > 0 ? d.Distance_Anis : 0)),
Distance_Maya: d3.sum(records, (d) => (d.Distance_Maya > 0 ? d.Distance_Maya : 0)),
Distance_Corentin: d3.sum(records, (d) => (d.Distance_Corentin > 0 ? d.Distance_Corentin : 0)),
Distance_Amira: d3.sum(records, (d) => (d.Distance_Amira > 0 ? d.Distance_Amira : 0)),
Sleep_Anis: d3.mean(records, (d) => (d.Sleep_Anis > 0 ? d.Sleep_Anis : 0)),
Sleep_Maya: d3.mean(records, (d) => (d.Sleep_Maya > 0 ? d.Sleep_Maya : 0)),
Sleep_Corentin: d3.mean(records, (d) => (d.Sleep_Corentin > 0 ? d.Sleep_Corentin : 0)),
Sleep_Amira: d3.mean(records, (d) => (d.Sleep_Amira > 0 ? d.Sleep_Amira : 0)),
};
return aggregated;
});
const users = ["Anis", "Maya", "Corentin", "Amira"];
users.forEach((user) => {
const personKey = `Distance_${user}`;
const sleepKey = `Sleep_${user}`;
d3.select(`#chart-${user}`).html(""); // Clear the previous chart
const svg = d3.select(`#chart-${user}`)
.append("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", [-width / 2, -height / 2, width, height])
.attr("style", "width: 100%; height: auto; font: 10px sans-serif;");
// Tooltip
const tooltip = d3.select("body").append("div")
.attr("class", "tooltip-radial")
.style("opacity", 0)
.style("position", "absolute")
.style("background", "rgba(0, 0, 0, 0.7)")
.style("color", "white")
.style("padding", "8px")
.style("border-radius", "4px")
.style("pointer-events", "none");
// Scales
const x = d3.scaleBand()
.domain(processedData.map((d) => d.week))
.range([0, 2 * Math.PI])
.align(0);
const y = d3.scaleRadial()
.domain([0, d3.max(processedData, (d) => d[personKey])])
.range([innerRadius, outerRadius]);
const color = d3.scaleLinear()
.domain([0, d3.max(processedData, (d) => d[sleepKey])])
.range(["lightblue", "darkblue"]);
// Bars
svg.append("g")
.selectAll("path")
.data(processedData)
.join("path")
.attr("d", d3.arc()
.innerRadius(innerRadius)
.outerRadius((d) => d[personKey] > 0 ? y(d[personKey]) : y(10))
.startAngle((d) => x(d.week))
.endAngle((d) => x(d.week) + x.bandwidth())
.padAngle(0.02)
.padRadius(innerRadius))
.attr("fill", (d) => d[personKey] > 0 ? color(d[sleepKey]) : "#ccc")
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
.on("mouseover", function (event, d) {
tooltip.transition().duration(200).style("opacity", 0.9);
tooltip.html(`
<strong>${user}</strong><br>
Semaine : ${d.week}<br>
Distance : ${d[personKey] > 0 ? d[personKey].toFixed(2) : "N/A"} km<br>
Sommeil : ${d[sleepKey] > 0 ? d[sleepKey].toFixed(2) + "h" : "N/A"}
`)
.style("left", `${event.pageX + 10}px`)
.style("top", `${event.pageY - 28}px`);
})
.on("mousemove", function (event) {
tooltip.style("left", `${event.pageX + 10}px`).style("top", `${event.pageY - 28}px`);
})
.on("mouseout", function () {
tooltip.transition().duration(500).style("opacity", 0);
});
// Week Labels with grouped years
svg.append("g")
.selectAll("g")
.data(processedData)
.join("g")
.attr("transform", (d) => {
const midAngle = (x(d.week) + x.bandwidth() / 2) * 180 / Math.PI - 90; // Angle médian
const radius = outerRadius + 10; // Position juste en dehors des barres
return `
rotate(${midAngle})
translate(${radius},0)
`;
})
.call((g) => {
g.append("text")
.text((d, i) => {
// Affiche l'année une seule fois pour la première semaine de chaque année
if (i === 0 || d.year !== processedData[i - 1].year) {
return `${d.year} ${d.week.split("-")[1]}`; // Année et numéro de semaine
}
return `${d.week.split("-")[1]}`; // Numéro de semaine
})
.attr("text-anchor", "middle")
.style("font-size", "6px") // Réduction de la taille pour ne pas encombrer
.style("fill", "#666"); // Couleur discrète pour les étiquettes
});
// Add user label
svg.append("text")
.attr("text-anchor", "middle")
.attr("dy", "0.5em")
.style("font-size", "10px")
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
.style("font-weight", "bold")
.text(user);
// Radial circles
const distanceTicks = y.ticks(5);
const circleGroup = svg.append("g");
circleGroup.selectAll("circle")
.data(distanceTicks)
.join("circle")
.attr("r", (d) => y(d))
.attr("fill", "none")
.attr("stroke", "#ccc")
.attr("stroke-dasharray", "4 2");
circleGroup.selectAll("text")
.data(distanceTicks)
.join("text")
.attr("x", 0)
.attr("y", (d) => -y(d))
.attr("dy", "-0.3em")
.attr("text-anchor", "middle")
.text((d) => `${d.toFixed(0)} km`);
const defs = svg.append("defs");
const gradient = defs.append("linearGradient")
.attr("id", "gradient")
.attr("x1", "0%")
.attr("y1", "0%")
.attr("x2", "100%")
.attr("y2", "0%");
gradient.append("stop").attr("offset", "0%").attr("stop-color", "lightblue");
gradient.append("stop").attr("offset", "100%").attr("stop-color", "darkblue");
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
const legendTooltip = d3.select("body").append("div")
.attr("class", "tooltip-legend")
.style("position", "absolute")
.style("padding", "10px")
.style("background", "#fff")
.style("border", "1px solid #ccc")
.style("border-radius", "5px")
.style("box-shadow", "0 4px 8px rgba(0, 0, 0, 0.1)") // Ajout d'un effet d'ombre
.style("font-size", "12px") // Police ajustée
.style("display", "none");
// Extraction des valeurs dynamiques pour Min et Max
const sleepMin = d3.min(processedData, (d) => d[sleepKey]);
const sleepMax = d3.max(processedData, (d) => d[sleepKey]);
// Afficher la légende au survol
svg.on("mouseover", (event) => {
legendTooltip.style("display", "block")
.style("left", `${event.pageX + 10}px`)
.style("top", `${event.pageY}px`)
.html(`
<strong>Durée de sommeil</strong><br>
<div style="width: 100px; height: 10px; background: linear-gradient(lightblue, darkblue); margin-top: 5px;"></div>
<div style="display: flex; justify-content: space-between; margin-top: 5px;">
<small>${sleepMin.toFixed(1)}h</small>
<small>${sleepMax.toFixed(1)}h</small>
</div>
<div style="width: 100px; height: 10px; background: #ccc; margin-top: 10px;"></div>
<small style="display: block; text-align: center; margin-top: 5px;">Valeurs manquantes</small>
`);
});
// Masquer la légende
svg.on("mouseout", () => {
legendTooltip.style("display", "none");
});
})
.catch((error) => console.error("Error loading data:", error));
function getISOWeekNumber(date) {
const tempDate = new Date(date);
tempDate.setDate(tempDate.getDate() + 4 - (tempDate.getDay() || 7));
const yearStart = new Date(tempDate.getFullYear(), 0, 1);
return Math.ceil(((tempDate - yearStart) / 86400000 + 1) / 7);
}
document.addEventListener("DOMContentLoaded", function () {
renderStepsVisualization();
renderDistanceVisualization();
renderCaloriesVisualization();
renderSleepActivityVisualization();
renderRadialDistanceChart()