diff --git a/src/components/vis-elements/BarList/BarList.tsx b/src/components/vis-elements/BarList/BarList.tsx index 2c6681a3a..ee567f72c 100644 --- a/src/components/vis-elements/BarList/BarList.tsx +++ b/src/components/vis-elements/BarList/BarList.tsx @@ -111,8 +111,9 @@ function BarListInner(props: BarListProps, ref: React.ForwardedRef + value ? (value / maxValue) * 100 : 0; + const BarLabels = ({ values }: { values: number[] }) => { - const sumValues = sumNumericArray(values); + const sumValues = useMemo(() => sumNumericArray(values), [values]); let prefixSum = 0; - let sumConsecutveHiddenLabels = 0; + let sumConsecutiveHiddenLabels = 0; return (
{ {values.slice(0, values.length).map((widthPercentage, idx) => { prefixSum += widthPercentage; const showLabel = - (widthPercentage >= 0.1 * sumValues || sumConsecutveHiddenLabels >= 0.09 * sumValues) && + (widthPercentage >= 0.1 * sumValues || sumConsecutiveHiddenLabels >= 0.09 * sumValues) && sumValues - prefixSum >= 0.15 * sumValues && prefixSum >= 0.1 * sumValues; - sumConsecutveHiddenLabels = showLabel ? 0 : (sumConsecutveHiddenLabels += widthPercentage); + sumConsecutiveHiddenLabels = showLabel + ? 0 + : (sumConsecutiveHiddenLabels += widthPercentage); + + const widthPositionLeft = getPositionLeft(widthPercentage, sumValues); return (
((props, r ...other } = props; - const markerBgColor = getMarkerBgColor(markerValue, values, colors); + const markerBgColor = useMemo( + () => getMarkerBgColor(markerValue, values, colors), + [markerValue, values, colors], + ); const { tooltipProps, getReferenceProps } = useTooltip(); + const maxValue = useMemo(() => sumNumericArray(values), [values]); + + const markerPositionLeft: number = useMemo( + () => getPositionLeft(markerValue, maxValue), + [markerValue, maxValue], + ); + return ( <> @@ -126,6 +143,7 @@ const CategoryBar = React.forwardRef((props, r > {values.map((value, idx) => { const baseColor = colors[idx] ?? "gray"; + const percentage = (value / maxValue) * 100; return (
((props, r "h-full", getColorClassNames(baseColor, colorPalette.background).bgColor, )} - style={{ width: `${value}%` }} + style={{ width: `${percentage}%` }} /> ); })} @@ -147,7 +165,7 @@ const CategoryBar = React.forwardRef((props, r "absolute right-1/2 -translate-x-1/2 w-5", )} style={{ - left: `${markerValue}%`, + left: `${markerPositionLeft}%`, transition: showAnimation ? "all 1s" : "", }} {...getReferenceProps} diff --git a/src/stories/vis-elements/CategoryBar.stories.tsx b/src/stories/vis-elements/CategoryBar.stories.tsx index 8a20cf6e2..8555319eb 100644 --- a/src/stories/vis-elements/CategoryBar.stories.tsx +++ b/src/stories/vis-elements/CategoryBar.stories.tsx @@ -73,6 +73,26 @@ export const WithZeroValues: Story = { }, }; +export const WithValuesMoreThan100: Story = { + args: { + values: [400, 400, 800], + colors: ["emerald", "yellow", "orange", "rose"], + markerValue: 1400, + tooltip: "90%", + className: "mt-5", + }, +}; + +export const WithValuesLessThan100: Story = { + args: { + values: [8, 7, 9, 8], + colors: ["emerald", "yellow", "orange", "rose"], + markerValue: 20, + tooltip: "90%", + className: "mt-5", + }, +}; + export const WithConsecutiveSmallValues: Story = { args: { values: [10, 5, 5, 5, 5, 5, 50, 15, 0], diff --git a/src/tests/vis-elements/CategoryBar.test.tsx b/src/tests/vis-elements/CategoryBar.test.tsx index 5799f7962..bebcc884a 100644 --- a/src/tests/vis-elements/CategoryBar.test.tsx +++ b/src/tests/vis-elements/CategoryBar.test.tsx @@ -8,4 +8,10 @@ describe("CategoryBar", () => { test("renders the CategoryBar component with default props", () => { render(); }); + test("renders the CategoryBar component with values more than 100", () => { + render(); + }); + test("renders the CategoryBar component with values less than 100", () => { + render(); + }); });