@@ -39,41 +39,41 @@ const generateVerticalStackedBars = <RawDatum extends Record<string, unknown>>(
39
39
} : Params < RawDatum , string , number > ,
40
40
barWidth : number ,
41
41
reverse : boolean
42
- ) => {
42
+ ) : ComputedBarDatum < RawDatum > [ ] => {
43
43
const getY = ( d : StackDatum < RawDatum > ) => yScale ( d [ reverse ? 0 : 1 ] )
44
44
const getHeight = ( d : StackDatum < RawDatum > , y : number ) => ( yScale ( d [ reverse ? 1 : 0 ] ) ?? 0 ) - y
45
45
46
- const bars = flattenDeep (
47
- stackedData . map ( stackedDataItem =>
48
- xScale . domain ( ) . map ( ( index , i ) => {
49
- const d = stackedDataItem [ i ]
50
- const x = xScale ( getIndex ( d . data ) ) ?? 0
51
- const y = ( getY ( d ) ?? 0 ) + innerPadding * 0.5
52
- const barHeight = getHeight ( d , y ) - innerPadding
53
- const [ rawValue , value ] = coerceValue ( d . data [ stackedDataItem . key ] )
54
-
55
- const barData = {
56
- id : stackedDataItem . key ,
57
- value : rawValue === null ? rawValue : value ,
58
- formattedValue : formatValue ( value ) ,
59
- hidden : false ,
60
- index : i ,
61
- indexValue : index ,
62
- data : filterNullValues ( d . data ) ,
63
- }
64
-
65
- return {
66
- key : `${ stackedDataItem . key } .${ index } ` ,
67
- data : barData ,
68
- x ,
69
- y ,
70
- width : barWidth ,
71
- height : barHeight ,
72
- color : getColor ( barData ) ,
73
- label : getTooltipLabel ( barData ) ,
74
- }
46
+ const bars : ComputedBarDatum < RawDatum > [ ] = [ ]
47
+ stackedData . forEach ( stackedDataItem =>
48
+ xScale . domain ( ) . forEach ( ( index , i ) => {
49
+ const d = stackedDataItem [ i ]
50
+ const x = xScale ( getIndex ( d . data ) ) ?? 0
51
+ const y = ( getY ( d ) ?? 0 ) + innerPadding * 0.5
52
+ const barHeight = getHeight ( d , y ) - innerPadding
53
+ const [ rawValue , value ] = coerceValue ( d . data [ stackedDataItem . key ] )
54
+
55
+ const barData : ComputedDatum < RawDatum > = {
56
+ id : stackedDataItem . key ,
57
+ value : rawValue === null ? rawValue : value ,
58
+ formattedValue : formatValue ( value ) ,
59
+ hidden : false ,
60
+ index : i ,
61
+ indexValue : index ,
62
+ data : filterNullValues ( d . data ) ,
63
+ }
64
+
65
+ bars . push ( {
66
+ key : `${ stackedDataItem . key } .${ index } ` ,
67
+ index : bars . length ,
68
+ data : barData ,
69
+ x ,
70
+ y ,
71
+ width : barWidth ,
72
+ height : barHeight ,
73
+ color : getColor ( barData ) ,
74
+ label : getTooltipLabel ( barData ) ,
75
75
} )
76
- )
76
+ } )
77
77
)
78
78
79
79
return bars
@@ -95,41 +95,41 @@ const generateHorizontalStackedBars = <RawDatum extends Record<string, unknown>>
95
95
} : Params < RawDatum , number , string > ,
96
96
barHeight : number ,
97
97
reverse : boolean
98
- ) => {
98
+ ) : ComputedBarDatum < RawDatum > [ ] => {
99
99
const getX = ( d : StackDatum < RawDatum > ) => xScale ( d [ reverse ? 1 : 0 ] )
100
100
const getWidth = ( d : StackDatum < RawDatum > , x : number ) => ( xScale ( d [ reverse ? 0 : 1 ] ) ?? 0 ) - x
101
101
102
- const bars = flattenDeep (
103
- stackedData . map ( stackedDataItem =>
104
- yScale . domain ( ) . map ( ( index , i ) => {
105
- const d = stackedDataItem [ i ]
106
- const y = yScale ( getIndex ( d . data ) ) ?? 0
107
- const x = ( getX ( d ) ?? 0 ) + innerPadding * 0.5
108
- const barWidth = getWidth ( d , x ) - innerPadding
109
- const [ rawValue , value ] = coerceValue ( d . data [ stackedDataItem . key ] )
110
-
111
- const barData = {
112
- id : stackedDataItem . key ,
113
- value : rawValue === null ? rawValue : value ,
114
- formattedValue : formatValue ( value ) ,
115
- hidden : false ,
116
- index : i ,
117
- indexValue : index ,
118
- data : filterNullValues ( d . data ) ,
119
- }
120
-
121
- return {
122
- key : `${ stackedDataItem . key } .${ index } ` ,
123
- data : barData ,
124
- x ,
125
- y ,
126
- width : barWidth ,
127
- height : barHeight ,
128
- color : getColor ( barData ) ,
129
- label : getTooltipLabel ( barData ) ,
130
- }
102
+ const bars : ComputedBarDatum < RawDatum > [ ] = [ ]
103
+ stackedData . forEach ( stackedDataItem =>
104
+ yScale . domain ( ) . forEach ( ( index , i ) => {
105
+ const d = stackedDataItem [ i ]
106
+ const y = yScale ( getIndex ( d . data ) ) ?? 0
107
+ const x = ( getX ( d ) ?? 0 ) + innerPadding * 0.5
108
+ const barWidth = getWidth ( d , x ) - innerPadding
109
+ const [ rawValue , value ] = coerceValue ( d . data [ stackedDataItem . key ] )
110
+
111
+ const barData : ComputedDatum < RawDatum > = {
112
+ id : stackedDataItem . key ,
113
+ value : rawValue === null ? rawValue : value ,
114
+ formattedValue : formatValue ( value ) ,
115
+ hidden : false ,
116
+ index : i ,
117
+ indexValue : index ,
118
+ data : filterNullValues ( d . data ) ,
119
+ }
120
+
121
+ bars . push ( {
122
+ key : `${ stackedDataItem . key } .${ index } ` ,
123
+ index : bars . length ,
124
+ data : barData ,
125
+ x ,
126
+ y ,
127
+ width : barWidth ,
128
+ height : barHeight ,
129
+ color : getColor ( barData ) ,
130
+ label : getTooltipLabel ( barData ) ,
131
131
} )
132
- )
132
+ } )
133
133
)
134
134
135
135
return bars
0 commit comments