@@ -444,11 +444,11 @@ <h3 class="font-semibold mb-3 text-vallabus">
444
444
</ button >
445
445
</ div >
446
446
447
- <!-- Evolución por día -->
447
+ <!-- Evolución por semana -->
448
448
< div class ="bg-gray-50 p-4 rounded-lg sm:col-span-2 ">
449
449
< h3 class ="font-semibold mb-3 text-vallabus ">
450
450
< i data-lucide ="calendar " class ="inline mr-2 "> </ i >
451
- Evolución por día
451
+ Evolución por semana
452
452
</ h3 >
453
453
< canvas id ="complaintsChart "> </ canvas >
454
454
</ div >
@@ -798,7 +798,7 @@ <h3 class="text-lg font-semibold">${nombre}, ¡muchas gracias!</h3>
798
798
async function fetchStats ( ) {
799
799
try {
800
800
const response = await fetch ( 'https://datos.vallabus.com/reclamaciones/stats_reclamaciones.json' ) ;
801
- statsData = await response . json ( ) ;
801
+ const statsData = await response . json ( ) ;
802
802
803
803
// Actualizar el DOM con los datos
804
804
document . getElementById ( 'total-reclamaciones' ) . textContent = statsData . totales . total_reclamaciones ;
@@ -808,7 +808,7 @@ <h3 class="text-lg font-semibold">${nombre}, ¡muchas gracias!</h3>
808
808
renderList ( statsData . totales . barrios , 'barriosList' ) ;
809
809
renderList ( statsData . totales . lineas , 'lineasList' ) ;
810
810
811
- // Preparar los datos para el gráfico
811
+ // Preparar datos para el gráfico
812
812
const complaintsData = {
813
813
labels : [ ] ,
814
814
datasets : [ {
@@ -820,12 +820,60 @@ <h3 class="text-lg font-semibold">${nombre}, ¡muchas gracias!</h3>
820
820
} ]
821
821
} ;
822
822
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 ) ;
829
877
}
830
878
831
879
// Dibujar el gráfico
@@ -834,25 +882,17 @@ <h3 class="text-lg font-semibold">${nombre}, ¡muchas gracias!</h3>
834
882
type : 'line' ,
835
883
data : complaintsData ,
836
884
options : {
837
- scales : {
838
- y : {
839
- beginAtZero : true
840
- }
841
- } ,
885
+ scales : { y : { beginAtZero : true } } ,
842
886
plugins : {
843
887
legend : {
844
888
display : true ,
845
889
position : 'bottom' ,
846
- labels : {
847
- color : '#333' ,
848
- font : {
849
- size : 12
850
- }
851
- }
890
+ labels : { color : '#333' , font : { size : 12 } }
852
891
}
853
892
}
854
893
}
855
894
} ) ;
895
+
856
896
} catch ( error ) {
857
897
console . error ( 'Error fetching data:' , error ) ;
858
898
}
0 commit comments