1
- import React from 'react' ;
1
+ import React , { useState , useEffect } from 'react' ;
2
2
import Chart from 'components/Chart/Chart.react' ;
3
3
import { ChartColorSchemes } from 'lib/Constants' ;
4
4
import styles from './GraphPanel.scss' ;
@@ -28,25 +28,48 @@ export default function GraphPanel({ selectedCells, order, data, columns, width
28
28
const { rowStart, rowEnd, colStart, colEnd } = selectedCells ;
29
29
const columnNames = order . slice ( colStart , colEnd + 1 ) . map ( o => o . name ) ;
30
30
const columnTypes = columnNames . map ( name => columns [ name ] ?. type ) ;
31
- const timeSeries =
32
- columnTypes . length > 1 &&
33
- columnTypes [ 0 ] === 'Date' &&
34
- columnTypes . slice ( 1 ) . every ( t => t === 'Number' ) ;
31
+
32
+ const initialUseXAxis =
33
+ columnNames . length > 1 &&
34
+ ( columnTypes [ 0 ] === 'Date' || columnTypes [ 0 ] === 'Number' ) &&
35
+ columnTypes . slice ( 1 ) . some ( t => t === 'Number' ) ;
36
+
37
+ const [ useXAxis , setUseXAxis ] = useState ( initialUseXAxis ) ;
38
+
39
+ useEffect ( ( ) => {
40
+ setUseXAxis ( initialUseXAxis ) ;
41
+ } , [ selectedCells ?. rowStart , selectedCells ?. rowEnd , selectedCells ?. colStart , selectedCells ?. colEnd ] ) ;
35
42
36
43
const chartData = { } ;
37
- if ( timeSeries ) {
44
+
45
+ if ( useXAxis ) {
46
+ let seriesIndex = 0 ;
38
47
for ( let j = 1 ; j < columnNames . length ; j ++ ) {
39
- chartData [ columnNames [ j ] ] = { color : ChartColorSchemes [ j - 1 ] , points : [ ] } ;
48
+ if ( columnTypes [ j ] === 'Number' ) {
49
+ chartData [ columnNames [ j ] ] = {
50
+ color : ChartColorSchemes [ seriesIndex ] ,
51
+ points : [ ] ,
52
+ } ;
53
+ seriesIndex ++ ;
54
+ }
40
55
}
41
56
for ( let i = rowStart ; i <= rowEnd ; i ++ ) {
42
57
const row = data [ i ] ;
43
58
if ( ! row ) continue ;
44
- const ts = parseDate ( row . attributes [ columnNames [ 0 ] ] ) ;
45
- if ( ts === null ) continue ;
59
+ let x = row . attributes [ columnNames [ 0 ] ] ;
60
+ if ( columnTypes [ 0 ] === 'Date' ) {
61
+ x = parseDate ( x ) ;
62
+ } else if ( typeof x === 'string' ) {
63
+ x = parseFloat ( x ) ;
64
+ }
65
+ if ( typeof x !== 'number' || isNaN ( x ) ) {
66
+ continue ;
67
+ }
46
68
for ( let j = 1 ; j < columnNames . length ; j ++ ) {
69
+ if ( columnTypes [ j ] !== 'Number' ) continue ;
47
70
const val = row . attributes [ columnNames [ j ] ] ;
48
71
if ( typeof val === 'number' && ! isNaN ( val ) ) {
49
- chartData [ columnNames [ j ] ] . points . push ( [ ts , val ] ) ;
72
+ chartData [ columnNames [ j ] ] . points . push ( [ x , val ] ) ;
50
73
}
51
74
}
52
75
}
@@ -62,7 +85,8 @@ export default function GraphPanel({ selectedCells, order, data, columns, width
62
85
for ( let i = rowStart ; i <= rowEnd ; i ++ , x ++ ) {
63
86
const row = data [ i ] ;
64
87
if ( ! row ) continue ;
65
- columnNames . forEach ( col => {
88
+ columnNames . forEach ( ( col , idx ) => {
89
+ if ( columnTypes [ idx ] !== 'Number' ) return ;
66
90
const val = row . attributes [ col ] ;
67
91
if ( typeof val === 'number' && ! isNaN ( val ) ) {
68
92
chartData [ col ] . points . push ( [ x , val ] ) ;
@@ -78,6 +102,16 @@ export default function GraphPanel({ selectedCells, order, data, columns, width
78
102
const chartWidth = width - 20 ;
79
103
return (
80
104
< div className = { styles . graphPanel } >
105
+ < div className = { styles . options } >
106
+ < label >
107
+ < input
108
+ type = "checkbox"
109
+ checked = { useXAxis }
110
+ onChange = { ( ) => setUseXAxis ( ! useXAxis ) }
111
+ /> { ' ' }
112
+ Use first column as X-axis
113
+ </ label >
114
+ </ div >
81
115
< Chart width = { chartWidth } height = { 400 } data = { chartData } />
82
116
</ div >
83
117
) ;
0 commit comments