@@ -60,47 +60,70 @@ const ChartVisualization = ({
6060 }
6161
6262 // Determinar se é time series de forma mais rigorosa
63- // Time series só se: primeira coluna é data E temos múltiplas colunas E a primeira coluna É REALMENTE data
64- const firstColumnName = order [ colStart ] ?. name ;
63+ // Time series se: temos múltiplas colunas E pelo menos uma coluna é data
6564 let isTimeSeries = false ;
65+ let dateColumnName = null ;
66+ let dateColumnIndex = - 1 ;
6667
67- // Só considerar time series se temos múltiplas colunas E a primeira coluna é explicitamente data/datetime
68- if ( colEnd > colStart && firstColumnName && columns ) {
69- // Verificar primeiro o tipo da coluna no schema
70- const firstColumnType = columns [ firstColumnName ] ?. type ;
71- const isDateColumn = firstColumnType === 'Date' ||
72- / ^ ( d a t e | t i m e | c r e a t e d | u p d a t e d | w h e n | a t ) $ / i. test ( firstColumnName ) ||
73- firstColumnName . toLowerCase ( ) . includes ( 'date' ) ||
74- firstColumnName . toLowerCase ( ) . includes ( 'time' ) ;
75-
76- if ( isDateColumn ) {
77- // Verificar se a primeira coluna contém realmente datas válidas
78- let dateCount = 0 ;
79- const totalRows = Math . min ( 3 , rowEnd - rowStart + 1 ) ; // Verificar até 3 linhas
80-
81- for ( let rowIndex = rowStart ; rowIndex < rowStart + totalRows ; rowIndex ++ ) {
82- const value = data [ rowIndex ] ?. attributes [ firstColumnName ] ;
83- if ( value instanceof Date ||
84- ( typeof value === 'string' && ! isNaN ( Date . parse ( value ) ) && new Date ( value ) . getFullYear ( ) > 1900 ) ) {
85- dateCount ++ ;
86- }
68+ // Procurar por qualquer coluna de data na seleção (não apenas a primeira)
69+ if ( colEnd > colStart && columns ) {
70+ for ( let colIndex = colStart ; colIndex <= colEnd ; colIndex ++ ) {
71+ const columnName = order [ colIndex ] ?. name ;
72+ if ( ! columnName ) {
73+ continue ;
8774 }
8875
89- isTimeSeries = dateCount >= totalRows * 0.6 ; // 60% devem ser datas válidas (mais permissivo)
76+ // Verificar o tipo da coluna no schema
77+ const columnType = columns [ columnName ] ?. type ;
78+ const isDateColumn = columnType === 'Date' ||
79+ / ^ ( d a t e | t i m e | c r e a t e d | u p d a t e d | w h e n | a t ) $ / i. test ( columnName ) ||
80+ columnName . toLowerCase ( ) . includes ( 'date' ) ||
81+ columnName . toLowerCase ( ) . includes ( 'time' ) ;
82+
83+ if ( isDateColumn ) {
84+ // Verificar se a coluna contém realmente datas válidas
85+ let dateCount = 0 ;
86+ const totalRows = Math . min ( 3 , rowEnd - rowStart + 1 ) ; // Verificar até 3 linhas
87+
88+ for ( let rowIndex = rowStart ; rowIndex < rowStart + totalRows ; rowIndex ++ ) {
89+ const value = data [ rowIndex ] ?. attributes [ columnName ] ;
90+ if ( value instanceof Date ||
91+ ( typeof value === 'string' && ! isNaN ( Date . parse ( value ) ) && new Date ( value ) . getFullYear ( ) > 1900 ) ) {
92+ dateCount ++ ;
93+ }
94+ }
95+
96+ if ( dateCount >= totalRows * 0.6 ) { // 60% devem ser datas válidas
97+ isTimeSeries = true ;
98+ dateColumnName = columnName ;
99+ dateColumnIndex = colIndex ;
100+ break ; // Encontrou uma coluna de data válida
101+ }
102+ }
90103 }
91104 }
92105
93106 if ( isTimeSeries && colEnd > colStart ) {
94- // Time Series: primeira coluna é data, outras são números
107+ // Time Series: usar a coluna de data encontrada , outras são números
95108 const datasets = [ ] ;
109+ let datasetIndex = 0 ;
110+
111+ // Criar um dataset para cada coluna numérica (exceto a coluna de data)
112+ for ( let colIndex = colStart ; colIndex <= colEnd ; colIndex ++ ) {
113+ // Pular a coluna de data
114+ if ( colIndex === dateColumnIndex ) {
115+ continue ;
116+ }
96117
97- // Criar um dataset para cada coluna numérica
98- for ( let colIndex = colStart + 1 ; colIndex <= colEnd ; colIndex ++ ) {
99118 const columnName = order [ colIndex ] ?. name ;
119+ if ( ! columnName ) {
120+ continue ;
121+ }
122+
100123 const dataPoints = [ ] ;
101124
102125 for ( let rowIndex = rowStart ; rowIndex <= rowEnd ; rowIndex ++ ) {
103- const timeValue = data [ rowIndex ] ?. attributes [ firstColumnName ] ;
126+ const timeValue = data [ rowIndex ] ?. attributes [ dateColumnName ] ;
104127 const numericValue = data [ rowIndex ] ?. attributes [ columnName ] ;
105128
106129 if ( timeValue && typeof numericValue === 'number' && ! isNaN ( numericValue ) ) {
@@ -115,10 +138,11 @@ const ChartVisualization = ({
115138 datasets . push ( {
116139 label : columnName ,
117140 data : dataPoints ,
118- borderColor : `hsl(${ ( colIndex - colStart ) * 60 } , 70%, 50%)` ,
119- backgroundColor : `hsla(${ ( colIndex - colStart ) * 60 } , 70%, 50%, 0.1)` ,
141+ borderColor : `hsl(${ datasetIndex * 60 } , 70%, 50%)` ,
142+ backgroundColor : `hsla(${ datasetIndex * 60 } , 70%, 50%, 0.1)` ,
120143 tension : 0.1
121144 } ) ;
145+ datasetIndex ++ ;
122146 }
123147 }
124148
@@ -139,7 +163,7 @@ const ChartVisualization = ({
139163 } ,
140164 title : {
141165 display : true ,
142- text : firstColumnName
166+ text : dateColumnName
143167 }
144168 } ,
145169 y : {
@@ -172,7 +196,9 @@ const ChartVisualization = ({
172196
173197 for ( let colIndex = colStart ; colIndex <= colEnd ; colIndex ++ ) {
174198 const columnName = order [ colIndex ] ?. name ;
175- if ( ! columnName ) continue ;
199+ if ( ! columnName ) {
200+ continue ;
201+ }
176202
177203 // Coletar todos os valores desta coluna
178204 const columnValues = [ ] ;
0 commit comments