From dbab51b28540cc724bb3a427f56dd9148720370b Mon Sep 17 00:00:00 2001 From: plouc Date: Tue, 3 Nov 2020 09:03:52 +0900 Subject: [PATCH] feat(pie): memoize radial labels computation --- packages/pie/src/hooks.js | 91 +++++++++++++++++++++++---------------- 1 file changed, 53 insertions(+), 38 deletions(-) diff --git a/packages/pie/src/hooks.js b/packages/pie/src/hooks.js index b8fcf2edf..ae67932ae 100644 --- a/packages/pie/src/hooks.js +++ b/packages/pie/src/hooks.js @@ -288,46 +288,61 @@ export const usePieRadialLabels = ({ const getTextColor = useInheritedColor(textColor, theme) const getLinkColor = useInheritedColor(linkColor, theme) - return dataWithArc - .filter(datum => skipAngle === 0 || datum.arc.angleDeg > skipAngle) - .map(datum => { - const angle = absoluteAngleRadians(midAngle(datum.arc) - Math.PI / 2) - const positionA = positionFromAngle(angle, radius + linkOffset) - const positionB = positionFromAngle(angle, radius + linkOffset + linkDiagonalLength) - - let positionC - let labelPosition - let textAlign - - if ( - absoluteAngleDegrees(radiansToDegrees(angle)) < 90 || - absoluteAngleDegrees(radiansToDegrees(angle)) >= 270 - ) { - positionC = { x: positionB.x + linkHorizontalLength, y: positionB.y } - labelPosition = { - x: positionB.x + linkHorizontalLength + textXOffset, - y: positionB.y, - } - textAlign = 'left' - } else { - positionC = { x: positionB.x - linkHorizontalLength, y: positionB.y } - labelPosition = { - x: positionB.x - linkHorizontalLength - textXOffset, - y: positionB.y, + return useMemo(() => { + if (!enable) return [] + + return dataWithArc + .filter(datum => skipAngle === 0 || datum.arc.angleDeg > skipAngle) + .map(datum => { + const angle = absoluteAngleRadians(midAngle(datum.arc) - Math.PI / 2) + const positionA = positionFromAngle(angle, radius + linkOffset) + const positionB = positionFromAngle(angle, radius + linkOffset + linkDiagonalLength) + + let positionC + let labelPosition + let textAlign + + if ( + absoluteAngleDegrees(radiansToDegrees(angle)) < 90 || + absoluteAngleDegrees(radiansToDegrees(angle)) >= 270 + ) { + positionC = { x: positionB.x + linkHorizontalLength, y: positionB.y } + labelPosition = { + x: positionB.x + linkHorizontalLength + textXOffset, + y: positionB.y, + } + textAlign = 'left' + } else { + positionC = { x: positionB.x - linkHorizontalLength, y: positionB.y } + labelPosition = { + x: positionB.x - linkHorizontalLength - textXOffset, + y: positionB.y, + } + textAlign = 'right' } - textAlign = 'right' - } - return { - text: getLabel(datum), - textColor: getTextColor(datum), - position: labelPosition, - align: textAlign, - line: [positionA, positionB, positionC], - linkColor: getLinkColor(datum), - datum, - } - }) + return { + text: getLabel(datum), + textColor: getTextColor(datum), + position: labelPosition, + align: textAlign, + line: [positionA, positionB, positionC], + linkColor: getLinkColor(datum), + datum, + } + }) + }, [ + dataWithArc, + skipAngle, + radius, + linkOffset, + linkDiagonalLength, + linkHorizontalLength, + textXOffset, + getLabel, + getTextColor, + getLinkColor, + ]) } /**