diff --git a/packages/heatmap/src/HeatMap.tsx b/packages/heatmap/src/HeatMap.tsx index 736875594e..32b1764dc5 100644 --- a/packages/heatmap/src/HeatMap.tsx +++ b/packages/heatmap/src/HeatMap.tsx @@ -32,7 +32,7 @@ const InnerHeatMap = ({ xOuterPadding = svgDefaultProps.xOuterPadding, yInnerPadding = svgDefaultProps.yInnerPadding, yOuterPadding = svgDefaultProps.yOuterPadding, - // sizeVariation = svgDefaultProps.sizeVariation, + sizeVariation = svgDefaultProps.sizeVariation, cellComponent = svgDefaultProps.cellComponent as NonNullable< HeatMapSvgProps['cellComponent'] >, @@ -85,6 +85,7 @@ const InnerHeatMap = ({ xOuterPadding, yInnerPadding, yOuterPadding, + sizeVariation, colors, emptyColor, opacity, diff --git a/packages/heatmap/src/HeatMapCanvas.tsx b/packages/heatmap/src/HeatMapCanvas.tsx index 7f86bd1902..631afa5ade 100644 --- a/packages/heatmap/src/HeatMapCanvas.tsx +++ b/packages/heatmap/src/HeatMapCanvas.tsx @@ -34,7 +34,7 @@ const InnerHeatMapCanvas = { - return column * cellWidth + cellWidth * 0.5 + padding * column + padding -} -const computeY = (row: number, cellHeight: number, padding: number) => { - return row * cellHeight + cellHeight * 0.5 + padding * row + padding -} -*/ - const isHoverTargetByType = { cell: ( cell: Omit< @@ -83,58 +76,23 @@ const isHoverTargetByType = { ) => cell.serieId === current.serieId || cell.data.x === current.data.x, } -/* -const computeCells = ({ - data, - keys, - getIndex, - xScale, - yScale, - sizeScale, - cellOpacity, - cellWidth, - cellHeight, - colorScale, - nanColor, - getLabel, - getLabelTextColor, -}: { - data: HeatMapDataProps['data'] - keys: HeatMapCommonProps['keys'] - getIndex: (datum: Datum) => string | number -}) => { - const cells = [] - data.forEach(datum => { - keys.forEach(key => { - const value = datum[key] - const label = getLabel(datum, key) - const index = getIndex(datum) - const sizeMultiplier = sizeScale ? sizeScale(value) : 1 - const width = sizeMultiplier * cellWidth - const height = sizeMultiplier * cellHeight +const useSizeScale = ( + size: false | SizeVariationConfig, + min: number, + max: number +): ((value: number | null) => number) => + useMemo(() => { + if (!size) return () => 1 - const cell = { - id: `${key}.${index}`, - xKey: key, - yKey: index, - x: xScale(key), - y: yScale(index), - width, - height, - value, - label, - color: isNaN(value) ? nanColor : colorScale(value), - opacity: cellOpacity, - } - cell.labelTextColor = getLabelTextColor(cell) - - cells.push(cell) - }) - }) + const scale = scaleLinear() + .domain(size.values ? size.values : [min, max]) + .range(size.sizes) - return cells -} -*/ + return (value: number | null) => { + if (value === null) return 1 + return scale(value) + } + }, [size, min, max]) export const useHeatMap = < Datum extends HeatMapDatum = DefaultHeatMapDatum, @@ -149,7 +107,7 @@ export const useHeatMap = < xInnerPadding = commonDefaultProps.xInnerPadding, yOuterPadding = commonDefaultProps.yOuterPadding, yInnerPadding = commonDefaultProps.yInnerPadding, - // sizeVariation, + sizeVariation = commonDefaultProps.sizeVariation, colors = commonDefaultProps.colors as HeatMapCommonProps['colors'], emptyColor = commonDefaultProps.emptyColor, opacity = commonDefaultProps.opacity, @@ -220,6 +178,7 @@ export const useHeatMap = < }, [colors, colorScale, emptyColor] ) + const getSize = useSizeScale(sizeVariation, minValue, maxValue) const theme = useTheme() const getBorderColor = useInheritedColor(borderColor, theme) const getLabelTextColor = useInheritedColor(labelTextColor, theme) @@ -234,8 +193,12 @@ export const useHeatMap = < computedOpacity = activeIds.includes(cell.id) ? activeOpacity : inactiveOpacity } + const sizeMultiplier = getSize(cell.value) + const computedCell = { ...cell, + width: cell.width * sizeMultiplier, + height: cell.height * sizeMultiplier, formattedValue: cell.value !== null ? formatValue(cell.value) : null, opacity: computedOpacity, } as ComputedCell @@ -249,6 +212,7 @@ export const useHeatMap = < }), [ cells, + getSize, getColor, getBorderColor, getLabelTextColor, diff --git a/packages/heatmap/src/types.ts b/packages/heatmap/src/types.ts index fd29894556..4bf4481a82 100644 --- a/packages/heatmap/src/types.ts +++ b/packages/heatmap/src/types.ts @@ -63,7 +63,9 @@ export interface HeatMapDataProps ({ help: 'Define style for common elements such as labels, axes…', flavors, description: ` - Please have a look at [the dedicated guide](self:/guides/theming) + Please have a look at [the dedicated guide](self:/guides/theming/) on how to define a theme for your charts. `, })