Skip to content

Commit aadd116

Browse files
authored
Merge pull request #324 from VallaBus/beta
reclama - gráfico por semana
2 parents 246e8fc + f1c15c1 commit aadd116

File tree

1 file changed

+61
-21
lines changed

1 file changed

+61
-21
lines changed

reclama/index.html

+61-21
Original file line numberDiff line numberDiff line change
@@ -444,11 +444,11 @@ <h3 class="font-semibold mb-3 text-vallabus">
444444
</button>
445445
</div>
446446

447-
<!-- Evolución por día -->
447+
<!-- Evolución por semana -->
448448
<div class="bg-gray-50 p-4 rounded-lg sm:col-span-2">
449449
<h3 class="font-semibold mb-3 text-vallabus">
450450
<i data-lucide="calendar" class="inline mr-2"></i>
451-
Evolución por día
451+
Evolución por semana
452452
</h3>
453453
<canvas id="complaintsChart"></canvas>
454454
</div>
@@ -798,7 +798,7 @@ <h3 class="text-lg font-semibold">${nombre}, ¡muchas gracias!</h3>
798798
async function fetchStats() {
799799
try {
800800
const response = await fetch('https://datos.vallabus.com/reclamaciones/stats_reclamaciones.json');
801-
statsData = await response.json();
801+
const statsData = await response.json();
802802

803803
// Actualizar el DOM con los datos
804804
document.getElementById('total-reclamaciones').textContent = statsData.totales.total_reclamaciones;
@@ -808,7 +808,7 @@ <h3 class="text-lg font-semibold">${nombre}, ¡muchas gracias!</h3>
808808
renderList(statsData.totales.barrios, 'barriosList');
809809
renderList(statsData.totales.lineas, 'lineasList');
810810

811-
// Preparar los datos para el gráfico
811+
// Preparar datos para el gráfico
812812
const complaintsData = {
813813
labels: [],
814814
datasets: [{
@@ -820,12 +820,60 @@ <h3 class="text-lg font-semibold">${nombre}, ¡muchas gracias!</h3>
820820
}]
821821
};
822822

823-
// Llenar los datos del gráfico desde el historial
824-
for (const fecha in statsData.historico) {
825-
const [year, month, day] = fecha.split('-');
826-
const formattedDate = `${day}/${month}`;
827-
complaintsData.labels.push(formattedDate);
828-
complaintsData.datasets[0].data.push(statsData.historico[fecha].total_reclamaciones);
823+
// Funciones auxiliares
824+
function getISOWeek(date) {
825+
const d = new Date(date);
826+
d.setHours(0, 0, 0, 0);
827+
d.setDate(d.getDate() + 4 - (d.getDay() || 7)); // Ajustar al jueves
828+
const yearStart = new Date(d.getFullYear(), 0, 1);
829+
const weekNo = Math.ceil((((d - yearStart) / 86400000) + 1) / 7);
830+
return { isoYear: d.getFullYear(), isoWeek: weekNo };
831+
}
832+
833+
function getWeekRange(date) {
834+
const d = new Date(date);
835+
const day = d.getDay();
836+
const diff = d.getDate() - day + (day === 0 ? -6 : 1); // Lunes anterior
837+
const start = new Date(d.setDate(diff));
838+
const end = new Date(start);
839+
end.setDate(start.getDate() + 6);
840+
return { start, end };
841+
}
842+
843+
function formatWeekLabel(start, end) {
844+
const format = (date) => `${String(date.getDate()).padStart(2, '0')}/${String(date.getMonth() + 1).padStart(2, '0')}`;
845+
return `${format(start)} - ${format(end)}`;
846+
}
847+
848+
// Procesar fechas ordenadas
849+
const historicoDates = Object.keys(statsData.historico).sort();
850+
let currentWeekKey = null;
851+
let currentWeekTotal = 0;
852+
let currentLabel = '';
853+
854+
for (const fecha of historicoDates) {
855+
const date = new Date(fecha);
856+
const { isoYear, isoWeek } = getISOWeek(date);
857+
const weekKey = `${isoYear}-${isoWeek}`;
858+
859+
if (weekKey !== currentWeekKey) {
860+
if (currentWeekKey !== null) {
861+
complaintsData.labels.push(currentLabel);
862+
complaintsData.datasets[0].data.push(currentWeekTotal);
863+
}
864+
currentWeekKey = weekKey;
865+
const weekRange = getWeekRange(date);
866+
currentLabel = formatWeekLabel(weekRange.start, weekRange.end);
867+
currentWeekTotal = statsData.historico[fecha].total_reclamaciones;
868+
} else {
869+
currentWeekTotal = statsData.historico[fecha].total_reclamaciones;
870+
}
871+
}
872+
873+
// Añadir la última semana
874+
if (currentWeekKey !== null) {
875+
complaintsData.labels.push(currentLabel);
876+
complaintsData.datasets[0].data.push(currentWeekTotal);
829877
}
830878

831879
// Dibujar el gráfico
@@ -834,25 +882,17 @@ <h3 class="text-lg font-semibold">${nombre}, ¡muchas gracias!</h3>
834882
type: 'line',
835883
data: complaintsData,
836884
options: {
837-
scales: {
838-
y: {
839-
beginAtZero: true
840-
}
841-
},
885+
scales: { y: { beginAtZero: true } },
842886
plugins: {
843887
legend: {
844888
display: true,
845889
position: 'bottom',
846-
labels: {
847-
color: '#333',
848-
font: {
849-
size: 12
850-
}
851-
}
890+
labels: { color: '#333', font: { size: 12 } }
852891
}
853892
}
854893
}
855894
});
895+
856896
} catch (error) {
857897
console.error('Error fetching data:', error);
858898
}

0 commit comments

Comments
 (0)