@@ -48,17 +48,25 @@ const ChartVisualization = ({
48
48
49
49
// Processar dados selecionados para determinar o tipo de visualização
50
50
const chartData = useMemo ( ( ) => {
51
- if ( ! selectedData || selectedData . length === 0 || ! selectedCells ) {
51
+ // Validação inicial mais rigorosa
52
+ if ( ! selectedData || selectedData . length === 0 || ! selectedCells || ! data || ! Array . isArray ( data ) ) {
52
53
return null ;
53
54
}
54
55
55
56
const { rowStart, rowEnd, colStart, colEnd } = selectedCells ;
56
57
57
- // Verificar se temos dados válidos
58
- if ( rowStart === - 1 || colStart === - 1 ) {
58
+ // Verificar se temos dados válidos e se os índices são válidos
59
+ if ( rowStart === - 1 || colStart === - 1 || rowEnd >= data . length || rowStart < 0 ) {
59
60
return null ;
60
61
}
61
62
63
+ // Verificar se todos os índices de linha são válidos
64
+ for ( let rowIndex = rowStart ; rowIndex <= rowEnd ; rowIndex ++ ) {
65
+ if ( ! data [ rowIndex ] || ! data [ rowIndex ] . attributes ) {
66
+ return null ; // Dados inconsistentes, abortar
67
+ }
68
+ }
69
+
62
70
// Determinar se é time series de forma mais rigorosa
63
71
// Time series se: temos múltiplas colunas E pelo menos uma coluna é data
64
72
let isTimeSeries = false ;
@@ -86,7 +94,11 @@ const ChartVisualization = ({
86
94
const totalRows = Math . min ( 3 , rowEnd - rowStart + 1 ) ; // Verificar até 3 linhas
87
95
88
96
for ( let rowIndex = rowStart ; rowIndex < rowStart + totalRows ; rowIndex ++ ) {
89
- const value = data [ rowIndex ] ?. attributes [ columnName ] ;
97
+ // Verificar se o índice é válido antes de acessar
98
+ if ( rowIndex >= data . length || ! data [ rowIndex ] || ! data [ rowIndex ] . attributes ) {
99
+ continue ;
100
+ }
101
+ const value = data [ rowIndex ] . attributes [ columnName ] ;
90
102
if ( value instanceof Date ||
91
103
( typeof value === 'string' && ! isNaN ( Date . parse ( value ) ) && new Date ( value ) . getFullYear ( ) > 1900 ) ) {
92
104
dateCount ++ ;
@@ -123,8 +135,12 @@ const ChartVisualization = ({
123
135
const dataPoints = [ ] ;
124
136
125
137
for ( let rowIndex = rowStart ; rowIndex <= rowEnd ; rowIndex ++ ) {
126
- const timeValue = data [ rowIndex ] ?. attributes [ dateColumnName ] ;
127
- const numericValue = data [ rowIndex ] ?. attributes [ columnName ] ;
138
+ // Verificar se o índice é válido
139
+ if ( rowIndex >= data . length || ! data [ rowIndex ] || ! data [ rowIndex ] . attributes ) {
140
+ continue ;
141
+ }
142
+ const timeValue = data [ rowIndex ] . attributes [ dateColumnName ] ;
143
+ const numericValue = data [ rowIndex ] . attributes [ columnName ] ;
128
144
129
145
if ( timeValue && typeof numericValue === 'number' && ! isNaN ( numericValue ) ) {
130
146
dataPoints . push ( {
@@ -205,7 +221,11 @@ const ChartVisualization = ({
205
221
const columnLabels = [ ] ;
206
222
207
223
for ( let rowIndex = rowStart ; rowIndex <= rowEnd ; rowIndex ++ ) {
208
- const value = data [ rowIndex ] ?. attributes [ columnName ] ;
224
+ // Verificar se o índice é válido
225
+ if ( rowIndex >= data . length || ! data [ rowIndex ] || ! data [ rowIndex ] . attributes ) {
226
+ continue ;
227
+ }
228
+ const value = data [ rowIndex ] . attributes [ columnName ] ;
209
229
if ( typeof value === 'number' && ! isNaN ( value ) ) {
210
230
columnValues . push ( value ) ;
211
231
columnLabels . push ( `Row ${ rowIndex + 1 } ` ) ;
@@ -281,8 +301,12 @@ const ChartVisualization = ({
281
301
const columnName = order [ colStart ] ?. name ;
282
302
if ( columnName ) {
283
303
for ( let rowIndex = rowStart ; rowIndex <= rowEnd ; rowIndex ++ ) {
304
+ // Verificar se o índice é válido
305
+ if ( rowIndex >= data . length || ! data [ rowIndex ] || ! data [ rowIndex ] . attributes ) {
306
+ continue ;
307
+ }
284
308
labels . push ( `Row ${ rowIndex + 1 } ` ) ;
285
- const value = data [ rowIndex ] ? .attributes [ columnName ] ;
309
+ const value = data [ rowIndex ] . attributes [ columnName ] ;
286
310
dataPoints . push ( typeof value === 'number' && ! isNaN ( value ) ? value : 0 ) ;
287
311
}
288
312
}
@@ -353,6 +377,11 @@ const ChartVisualization = ({
353
377
} , [ selectedData , selectedCells , data , order , columns ] ) ;
354
378
355
379
const renderChart = ( ) => {
380
+ // Safety check to prevent crashes
381
+ if ( ! chartData ) {
382
+ return null ;
383
+ }
384
+
356
385
if ( chartData . type === 'timeSeries' ) {
357
386
return (
358
387
< Line
@@ -549,6 +578,18 @@ const ChartVisualization = ({
549
578
}
550
579
} ;
551
580
581
+ // Add null check to prevent runtime errors
582
+ if ( ! chartData ) {
583
+ return (
584
+ < div className = { styles . chartVisualization } >
585
+ < div className = { styles . noData } >
586
+ < p > No valid data selected for charting.</ p >
587
+ < p > Please select numeric or date columns to visualize.</ p >
588
+ </ div >
589
+ </ div >
590
+ ) ;
591
+ }
592
+
552
593
return (
553
594
< div className = { styles . chartVisualization } >
554
595
< div className = { styles . chartControls } >
0 commit comments