1
1
import { useMemo , useCallback , useState } from 'react'
2
+ import { scaleLinear } from 'd3-scale'
2
3
import { useTheme , usePropertyAccessor , useValueFormatter } from '@nivo/core'
3
4
import { useInheritedColor , getContinuousColorScale } from '@nivo/colors'
4
5
import { AnnotationMatcher , useAnnotations } from '@nivo/annotations'
8
9
HeatMapCommonProps ,
9
10
HeatMapDataProps ,
10
11
HeatMapDatum ,
12
+ SizeVariationConfig ,
11
13
} from './types'
12
14
import { commonDefaultProps } from './defaults'
13
15
import { computeCells } from './compute'
@@ -43,15 +45,6 @@ export const useComputeCells = <Datum extends HeatMapDatum, ExtraProps extends o
43
45
[ data , width , height , xInnerPadding , xOuterPadding , yInnerPadding , yOuterPadding ]
44
46
)
45
47
46
- /*
47
- const computeX = (column: number, cellWidth: number, padding: number) => {
48
- return column * cellWidth + cellWidth * 0.5 + padding * column + padding
49
- }
50
- const computeY = (row: number, cellHeight: number, padding: number) => {
51
- return row * cellHeight + cellHeight * 0.5 + padding * row + padding
52
- }
53
- */
54
-
55
48
const isHoverTargetByType = {
56
49
cell : < Datum extends HeatMapDatum > (
57
50
cell : Omit <
@@ -83,58 +76,23 @@ const isHoverTargetByType = {
83
76
) => cell . serieId === current . serieId || cell . data . x === current . data . x ,
84
77
}
85
78
86
- /*
87
- const computeCells = <Datum extends object>({
88
- data,
89
- keys,
90
- getIndex,
91
- xScale,
92
- yScale,
93
- sizeScale,
94
- cellOpacity,
95
- cellWidth,
96
- cellHeight,
97
- colorScale,
98
- nanColor,
99
- getLabel,
100
- getLabelTextColor,
101
- }: {
102
- data: HeatMapDataProps<Datum>['data']
103
- keys: HeatMapCommonProps<Datum>['keys']
104
- getIndex: (datum: Datum) => string | number
105
- }) => {
106
- const cells = []
107
- data.forEach(datum => {
108
- keys.forEach(key => {
109
- const value = datum[key]
110
- const label = getLabel(datum, key)
111
- const index = getIndex(datum)
112
- const sizeMultiplier = sizeScale ? sizeScale(value) : 1
113
- const width = sizeMultiplier * cellWidth
114
- const height = sizeMultiplier * cellHeight
79
+ const useSizeScale = (
80
+ size : false | SizeVariationConfig ,
81
+ min : number ,
82
+ max : number
83
+ ) : ( ( value : number | null ) => number ) =>
84
+ useMemo ( ( ) => {
85
+ if ( ! size ) return ( ) => 1
115
86
116
- const cell = {
117
- id: `${key}.${index}`,
118
- xKey: key,
119
- yKey: index,
120
- x: xScale(key),
121
- y: yScale(index),
122
- width,
123
- height,
124
- value,
125
- label,
126
- color: isNaN(value) ? nanColor : colorScale(value),
127
- opacity: cellOpacity,
128
- }
129
- cell.labelTextColor = getLabelTextColor(cell)
130
-
131
- cells.push(cell)
132
- })
133
- })
87
+ const scale = scaleLinear ( )
88
+ . domain ( size . values ? size . values : [ min , max ] )
89
+ . range ( size . sizes )
134
90
135
- return cells
136
- }
137
- */
91
+ return ( value : number | null ) => {
92
+ if ( value === null ) return 1
93
+ return scale ( value )
94
+ }
95
+ } , [ size , min , max ] )
138
96
139
97
export const useHeatMap = <
140
98
Datum extends HeatMapDatum = DefaultHeatMapDatum ,
@@ -149,7 +107,7 @@ export const useHeatMap = <
149
107
xInnerPadding = commonDefaultProps . xInnerPadding ,
150
108
yOuterPadding = commonDefaultProps . yOuterPadding ,
151
109
yInnerPadding = commonDefaultProps . yInnerPadding ,
152
- // sizeVariation,
110
+ sizeVariation = commonDefaultProps . sizeVariation ,
153
111
colors = commonDefaultProps . colors as HeatMapCommonProps < Datum > [ 'colors' ] ,
154
112
emptyColor = commonDefaultProps . emptyColor ,
155
113
opacity = commonDefaultProps . opacity ,
@@ -220,6 +178,7 @@ export const useHeatMap = <
220
178
} ,
221
179
[ colors , colorScale , emptyColor ]
222
180
)
181
+ const getSize = useSizeScale ( sizeVariation , minValue , maxValue )
223
182
const theme = useTheme ( )
224
183
const getBorderColor = useInheritedColor ( borderColor , theme )
225
184
const getLabelTextColor = useInheritedColor ( labelTextColor , theme )
@@ -234,8 +193,12 @@ export const useHeatMap = <
234
193
computedOpacity = activeIds . includes ( cell . id ) ? activeOpacity : inactiveOpacity
235
194
}
236
195
196
+ const sizeMultiplier = getSize ( cell . value )
197
+
237
198
const computedCell = {
238
199
...cell ,
200
+ width : cell . width * sizeMultiplier ,
201
+ height : cell . height * sizeMultiplier ,
239
202
formattedValue : cell . value !== null ? formatValue ( cell . value ) : null ,
240
203
opacity : computedOpacity ,
241
204
} as ComputedCell < Datum >
@@ -249,6 +212,7 @@ export const useHeatMap = <
249
212
} ) ,
250
213
[
251
214
cells ,
215
+ getSize ,
252
216
getColor ,
253
217
getBorderColor ,
254
218
getLabelTextColor ,
0 commit comments