@@ -48,17 +48,25 @@ const ChartVisualization = ({
4848
4949 // Processar dados selecionados para determinar o tipo de visualização
5050 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 ) ) {
5253 return null ;
5354 }
5455
5556 const { rowStart, rowEnd, colStart, colEnd } = selectedCells ;
5657
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 ) {
5960 return null ;
6061 }
6162
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+
6270 // Determinar se é time series de forma mais rigorosa
6371 // Time series se: temos múltiplas colunas E pelo menos uma coluna é data
6472 let isTimeSeries = false ;
@@ -86,7 +94,11 @@ const ChartVisualization = ({
8694 const totalRows = Math . min ( 3 , rowEnd - rowStart + 1 ) ; // Verificar até 3 linhas
8795
8896 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 ] ;
90102 if ( value instanceof Date ||
91103 ( typeof value === 'string' && ! isNaN ( Date . parse ( value ) ) && new Date ( value ) . getFullYear ( ) > 1900 ) ) {
92104 dateCount ++ ;
@@ -123,8 +135,12 @@ const ChartVisualization = ({
123135 const dataPoints = [ ] ;
124136
125137 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 ] ;
128144
129145 if ( timeValue && typeof numericValue === 'number' && ! isNaN ( numericValue ) ) {
130146 dataPoints . push ( {
@@ -205,7 +221,11 @@ const ChartVisualization = ({
205221 const columnLabels = [ ] ;
206222
207223 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 ] ;
209229 if ( typeof value === 'number' && ! isNaN ( value ) ) {
210230 columnValues . push ( value ) ;
211231 columnLabels . push ( `Row ${ rowIndex + 1 } ` ) ;
@@ -281,8 +301,12 @@ const ChartVisualization = ({
281301 const columnName = order [ colStart ] ?. name ;
282302 if ( columnName ) {
283303 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+ }
284308 labels . push ( `Row ${ rowIndex + 1 } ` ) ;
285- const value = data [ rowIndex ] ? .attributes [ columnName ] ;
309+ const value = data [ rowIndex ] . attributes [ columnName ] ;
286310 dataPoints . push ( typeof value === 'number' && ! isNaN ( value ) ? value : 0 ) ;
287311 }
288312 }
@@ -353,6 +377,11 @@ const ChartVisualization = ({
353377 } , [ selectedData , selectedCells , data , order , columns ] ) ;
354378
355379 const renderChart = ( ) => {
380+ // Safety check to prevent crashes
381+ if ( ! chartData ) {
382+ return null ;
383+ }
384+
356385 if ( chartData . type === 'timeSeries' ) {
357386 return (
358387 < Line
@@ -549,6 +578,18 @@ const ChartVisualization = ({
549578 }
550579 } ;
551580
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+
552593 return (
553594 < div className = { styles . chartVisualization } >
554595 < div className = { styles . chartControls } >
0 commit comments