|
| 1 | +import { useMemo, useCallback } from 'react' |
| 2 | +import { |
| 3 | + useTheme, |
| 4 | + usePropertyAccessor, |
| 5 | + useValueFormatter, |
| 6 | + // @ts-ignore |
| 7 | + getLabelGenerator, |
| 8 | +} from '@nivo/core' |
| 9 | +import { useInheritedColor, useContinuousColorScale } from '@nivo/colors' |
| 10 | +import { |
| 11 | + ComputedCell, |
| 12 | + DefaultHeatMapDatum, |
| 13 | + HeatMapCommonProps, |
| 14 | + HeatMapDataProps, |
| 15 | + HeatMapDatum, |
| 16 | +} from './types' |
| 17 | +import { commonDefaultProps } from './defaults' |
| 18 | +import { computeCells } from './compute' |
| 19 | + |
| 20 | +export const useComputeCells = <Datum extends HeatMapDatum, ExtraProps extends object>({ |
| 21 | + data, |
| 22 | + width, |
| 23 | + height, |
| 24 | + xInnerPadding, |
| 25 | + xOuterPadding, |
| 26 | + yInnerPadding, |
| 27 | + yOuterPadding, |
| 28 | +}: { |
| 29 | + data: HeatMapDataProps<Datum, ExtraProps>['data'] |
| 30 | + width: number |
| 31 | + height: number |
| 32 | + xInnerPadding: HeatMapCommonProps<Datum>['xInnerPadding'] |
| 33 | + xOuterPadding: HeatMapCommonProps<Datum>['xOuterPadding'] |
| 34 | + yInnerPadding: HeatMapCommonProps<Datum>['yInnerPadding'] |
| 35 | + yOuterPadding: HeatMapCommonProps<Datum>['yOuterPadding'] |
| 36 | +}) => |
| 37 | + useMemo( |
| 38 | + () => |
| 39 | + computeCells<Datum, ExtraProps>({ |
| 40 | + data, |
| 41 | + width, |
| 42 | + height, |
| 43 | + xInnerPadding, |
| 44 | + xOuterPadding, |
| 45 | + yInnerPadding, |
| 46 | + yOuterPadding, |
| 47 | + }), |
| 48 | + [data, width, height, xInnerPadding, xOuterPadding, yInnerPadding, yOuterPadding] |
| 49 | + ) |
| 50 | + |
| 51 | +const computeX = (column: number, cellWidth: number, padding: number) => { |
| 52 | + return column * cellWidth + cellWidth * 0.5 + padding * column + padding |
| 53 | +} |
| 54 | +const computeY = (row: number, cellHeight: number, padding: number) => { |
| 55 | + return row * cellHeight + cellHeight * 0.5 + padding * row + padding |
| 56 | +} |
| 57 | + |
| 58 | +const isHoverTargetByType = { |
| 59 | + cell: (cell: ComputedCell<object>, current: ComputedCell<object>) => |
| 60 | + cell.xKey === current.xKey && cell.yKey === current.yKey, |
| 61 | + row: (cell: ComputedCell<object>, current: ComputedCell<object>) => cell.yKey === current.yKey, |
| 62 | + column: (cell: ComputedCell<object>, current: ComputedCell<object>) => |
| 63 | + cell.xKey === current.xKey, |
| 64 | + rowColumn: (cell: ComputedCell<object>, current: ComputedCell<object>) => |
| 65 | + cell.xKey === current.xKey || cell.yKey === current.yKey, |
| 66 | +} |
| 67 | + |
| 68 | +/* |
| 69 | +const computeCells = <Datum extends object>({ |
| 70 | + data, |
| 71 | + keys, |
| 72 | + getIndex, |
| 73 | + xScale, |
| 74 | + yScale, |
| 75 | + sizeScale, |
| 76 | + cellOpacity, |
| 77 | + cellWidth, |
| 78 | + cellHeight, |
| 79 | + colorScale, |
| 80 | + nanColor, |
| 81 | + getLabel, |
| 82 | + getLabelTextColor, |
| 83 | +}: { |
| 84 | + data: HeatMapDataProps<Datum>['data'] |
| 85 | + keys: HeatMapCommonProps<Datum>['keys'] |
| 86 | + getIndex: (datum: Datum) => string | number |
| 87 | +}) => { |
| 88 | + const cells = [] |
| 89 | + data.forEach(datum => { |
| 90 | + keys.forEach(key => { |
| 91 | + const value = datum[key] |
| 92 | + const label = getLabel(datum, key) |
| 93 | + const index = getIndex(datum) |
| 94 | + const sizeMultiplier = sizeScale ? sizeScale(value) : 1 |
| 95 | + const width = sizeMultiplier * cellWidth |
| 96 | + const height = sizeMultiplier * cellHeight |
| 97 | +
|
| 98 | + const cell = { |
| 99 | + id: `${key}.${index}`, |
| 100 | + xKey: key, |
| 101 | + yKey: index, |
| 102 | + x: xScale(key), |
| 103 | + y: yScale(index), |
| 104 | + width, |
| 105 | + height, |
| 106 | + value, |
| 107 | + label, |
| 108 | + color: isNaN(value) ? nanColor : colorScale(value), |
| 109 | + opacity: cellOpacity, |
| 110 | + } |
| 111 | + cell.labelTextColor = getLabelTextColor(cell) |
| 112 | +
|
| 113 | + cells.push(cell) |
| 114 | + }) |
| 115 | + }) |
| 116 | +
|
| 117 | + return cells |
| 118 | +} |
| 119 | +*/ |
| 120 | + |
| 121 | +export const useHeatMap = < |
| 122 | + Datum extends HeatMapDatum = DefaultHeatMapDatum, |
| 123 | + ExtraProps extends object = Record<string, never> |
| 124 | +>({ |
| 125 | + data, |
| 126 | + minValue: _minValue = commonDefaultProps.minValue, |
| 127 | + maxValue: _maxValue = commonDefaultProps.maxValue, |
| 128 | + valueFormat, |
| 129 | + width, |
| 130 | + height, |
| 131 | + forceSquare = commonDefaultProps.forceSquare, |
| 132 | + xOuterPadding = commonDefaultProps.xOuterPadding, |
| 133 | + xInnerPadding = commonDefaultProps.xInnerPadding, |
| 134 | + yOuterPadding = commonDefaultProps.yOuterPadding, |
| 135 | + yInnerPadding = commonDefaultProps.yInnerPadding, |
| 136 | + sizeVariation, |
| 137 | + colors = commonDefaultProps.colors as HeatMapCommonProps<Datum>['colors'], |
| 138 | + nanColor, |
| 139 | + opacity, |
| 140 | + activeOpacity, |
| 141 | + inactiveOpacity, |
| 142 | + borderColor = commonDefaultProps.borderColor as HeatMapCommonProps<Datum>['borderColor'], |
| 143 | + label = commonDefaultProps.label as HeatMapCommonProps<Datum>['label'], |
| 144 | + labelTextColor = commonDefaultProps.labelTextColor as HeatMapCommonProps<Datum>['labelTextColor'], |
| 145 | + hoverTarget, |
| 146 | +}: { |
| 147 | + data: HeatMapDataProps<Datum, ExtraProps>['data'] |
| 148 | + minValue?: HeatMapCommonProps<Datum>['minValue'] |
| 149 | + maxValue?: HeatMapCommonProps<Datum>['maxValue'] |
| 150 | + valueFormat?: HeatMapCommonProps<Datum>['valueFormat'] |
| 151 | + width: number |
| 152 | + height: number |
| 153 | + forceSquare?: HeatMapCommonProps<Datum>['forceSquare'] |
| 154 | + xOuterPadding?: HeatMapCommonProps<Datum>['xOuterPadding'] |
| 155 | + xInnerPadding?: HeatMapCommonProps<Datum>['xInnerPadding'] |
| 156 | + yOuterPadding?: HeatMapCommonProps<Datum>['yOuterPadding'] |
| 157 | + yInnerPadding?: HeatMapCommonProps<Datum>['yInnerPadding'] |
| 158 | + sizeVariation?: HeatMapCommonProps<Datum>['sizeVariation'] |
| 159 | + colors?: HeatMapCommonProps<Datum>['colors'] |
| 160 | + nanColor?: HeatMapCommonProps<Datum>['nanColor'] |
| 161 | + opacity?: HeatMapCommonProps<Datum>['opacity'] |
| 162 | + activeOpacity?: HeatMapCommonProps<Datum>['activeOpacity'] |
| 163 | + inactiveOpacity?: HeatMapCommonProps<Datum>['inactiveOpacity'] |
| 164 | + borderColor?: HeatMapCommonProps<Datum>['borderColor'] |
| 165 | + label?: HeatMapCommonProps<Datum>['label'] |
| 166 | + labelTextColor?: HeatMapCommonProps<Datum>['labelTextColor'] |
| 167 | + hoverTarget?: HeatMapCommonProps<Datum>['hoverTarget'] |
| 168 | +}) => { |
| 169 | + const { cells, xScale, yScale, minValue, maxValue } = useComputeCells<Datum, ExtraProps>({ |
| 170 | + data, |
| 171 | + width, |
| 172 | + height, |
| 173 | + xOuterPadding, |
| 174 | + xInnerPadding, |
| 175 | + yOuterPadding, |
| 176 | + yInnerPadding, |
| 177 | + }) |
| 178 | + |
| 179 | + const colorScale = useContinuousColorScale(colors, { |
| 180 | + min: minValue, |
| 181 | + max: maxValue, |
| 182 | + }) |
| 183 | + |
| 184 | + const getColor = useCallback( |
| 185 | + (cell: Omit<ComputedCell<Datum>, 'color' | 'opacity' | 'borderColor'>) => |
| 186 | + colorScale(cell.value), |
| 187 | + [colorScale] |
| 188 | + ) |
| 189 | + const theme = useTheme() |
| 190 | + const getBorderColor = useInheritedColor(borderColor, theme) |
| 191 | + const getLabelTextColor = useInheritedColor(labelTextColor, theme) |
| 192 | + const formatValue = useValueFormatter(valueFormat) |
| 193 | + const getLabel = usePropertyAccessor(label) |
| 194 | + |
| 195 | + const computedCells = cells.map(cell => { |
| 196 | + const computedCell = { |
| 197 | + ...cell, |
| 198 | + formattedValue: formatValue(cell.value), |
| 199 | + opacity: 1, |
| 200 | + } as ComputedCell<Datum> |
| 201 | + |
| 202 | + computedCell.label = getLabel(computedCell) |
| 203 | + computedCell.color = getColor(computedCell) |
| 204 | + computedCell.borderColor = getBorderColor(computedCell) |
| 205 | + computedCell.labelTextColor = getLabelTextColor(computedCell) |
| 206 | + |
| 207 | + return computedCell |
| 208 | + }) |
| 209 | + |
| 210 | + return { |
| 211 | + cells: computedCells, |
| 212 | + xScale, |
| 213 | + yScale, |
| 214 | + colorScale, |
| 215 | + } |
| 216 | + |
| 217 | + /* |
| 218 | + const [currentCellId, setCurrentCellId] = useState(null) |
| 219 | +
|
| 220 | + const layoutConfig = useMemo(() => { |
| 221 | + const columns = keys.length |
| 222 | + const rows = data.length |
| 223 | +
|
| 224 | + let cellWidth = Math.max((width - padding * (columns + 1)) / columns, 0) |
| 225 | + let cellHeight = Math.max((height - padding * (rows + 1)) / rows, 0) |
| 226 | +
|
| 227 | + let offsetX = 0 |
| 228 | + let offsetY = 0 |
| 229 | + if (forceSquare === true) { |
| 230 | + const cellSize = Math.min(cellWidth, cellHeight) |
| 231 | + cellWidth = cellSize |
| 232 | + cellHeight = cellSize |
| 233 | +
|
| 234 | + offsetX = (width - ((cellWidth + padding) * columns + padding)) / 2 |
| 235 | + offsetY = (height - ((cellHeight + padding) * rows + padding)) / 2 |
| 236 | + } |
| 237 | +
|
| 238 | + return { |
| 239 | + cellWidth, |
| 240 | + cellHeight, |
| 241 | + offsetX, |
| 242 | + offsetY, |
| 243 | + } |
| 244 | + }, [data, keys, width, height, padding, forceSquare]) |
| 245 | +
|
| 246 | + const sizeScale = useMemo(() => { |
| 247 | + if (sizeVariation > 0) { |
| 248 | + return scaleLinear() |
| 249 | + .range([1 - sizeVariation, 1]) |
| 250 | + .domain([values.min, values.max]) |
| 251 | + } |
| 252 | + }, [sizeVariation, values]) |
| 253 | +
|
| 254 | + const getCellBorderColor = useInheritedColor(cellBorderColor, theme) |
| 255 | + const getLabelTextColor = useInheritedColor(labelTextColor, theme) |
| 256 | +
|
| 257 | + const cells = useMemo( |
| 258 | + () => |
| 259 | + computeCells<Datum>({ |
| 260 | + data, |
| 261 | + keys, |
| 262 | + getIndex, |
| 263 | + xScale: scales.x, |
| 264 | + yScale: scales.y, |
| 265 | + sizeScale, |
| 266 | + cellOpacity, |
| 267 | + cellWidth: layoutConfig.cellWidth, |
| 268 | + cellHeight: layoutConfig.cellHeight, |
| 269 | + colorScale, |
| 270 | + nanColor, |
| 271 | + getLabel, |
| 272 | + getLabelTextColor, |
| 273 | + }), |
| 274 | + [ |
| 275 | + data, |
| 276 | + keys, |
| 277 | + getIndex, |
| 278 | + scales, |
| 279 | + sizeScale, |
| 280 | + cellOpacity, |
| 281 | + layoutConfig, |
| 282 | + colorScale, |
| 283 | + nanColor, |
| 284 | + getLabel, |
| 285 | + getLabelTextColor, |
| 286 | + ] |
| 287 | + ) |
| 288 | +
|
| 289 | + const cellsWithCurrent = useMemo(() => { |
| 290 | + if (currentCellId === null) return cells |
| 291 | +
|
| 292 | + const isHoverTarget = isHoverTargetByType[hoverTarget] |
| 293 | + const currentCell = cells.find(cell => cell.id === currentCellId) |
| 294 | +
|
| 295 | + return cells.map(cell => { |
| 296 | + const opacity = isHoverTarget(cell, currentCell) |
| 297 | + ? cellHoverOpacity |
| 298 | + : cellHoverOthersOpacity |
| 299 | +
|
| 300 | + if (opacity === cell.opacity) return cell |
| 301 | +
|
| 302 | + return { |
| 303 | + ...cell, |
| 304 | + opacity, |
| 305 | + } |
| 306 | + }) |
| 307 | + }, [cells, currentCellId, hoverTarget, cellHoverOpacity, cellHoverOthersOpacity]) |
| 308 | +
|
| 309 | + return { |
| 310 | + cells: cellsWithCurrent, |
| 311 | + getIndex, |
| 312 | + xScale: scales.x, |
| 313 | + yScale: scales.y, |
| 314 | + ...layoutConfig, |
| 315 | + sizeScale, |
| 316 | + currentCellId, |
| 317 | + setCurrentCellId, |
| 318 | + colorScale, |
| 319 | + getCellBorderColor, |
| 320 | + getLabelTextColor, |
| 321 | + } |
| 322 | + */ |
| 323 | +} |
0 commit comments