Skip to content

The Open Standard for Generative UI

Fix: dynamically set width of y axis based on ticks generated by Recharts (#175)

abhithesysb02fa07509f7
diff --git a/js/packages/react-ui/src/components/Charts/AreaChart/AreaChart.tsx b/js/packages/react-ui/src/components/Charts/AreaChart/AreaChart.tsx
index eebe10c69..3d80733a1 100644
--- a/js/packages/react-ui/src/components/Charts/AreaChart/AreaChart.tsx
+++ b/js/packages/react-ui/src/components/Charts/AreaChart/AreaChart.tsx
@@ -81,7 +81,7 @@ const AreaChartComponent = <T extends AreaChartData>({
return getDataKeys(data, categoryKey as string);
}, [data, categoryKey]);
- const yAxisWidth = useYAxisLabelWidth(data, dataKeys);
+ const { yAxisWidth, setLabelWidth } = useYAxisLabelWidth(data, dataKeys);
const widthOfGroup = useMemo(() => {
return getWidthOfGroup(data);
@@ -262,7 +262,12 @@ const AreaChartComponent = <T extends AreaChartData>({
right: 0,
}}
>
- <YAxis width={yAxisWidth} tickLine={false} axisLine={false} tick={<YAxisTick />} />
+ <YAxis
+ width={yAxisWidth}
+ tickLine={false}
+ axisLine={false}
+ tick={<YAxisTick setLabelWidth={setLabelWidth} />}
+ />
{/* Invisible area to maintain scale synchronization */}
{dataKeys.map((key) => {
return (
diff --git a/js/packages/react-ui/src/components/Charts/BarChart/BarChart.tsx b/js/packages/react-ui/src/components/Charts/BarChart/BarChart.tsx
index 3875adb3e..f0083c490 100644
--- a/js/packages/react-ui/src/components/Charts/BarChart/BarChart.tsx
+++ b/js/packages/react-ui/src/components/Charts/BarChart/BarChart.tsx
@@ -100,7 +100,7 @@ const BarChartComponent = <T extends BarChartData>({
return getDataKeys(data, categoryKey as string);
}, [data, categoryKey]);
- const yAxisWidth = useYAxisLabelWidth(data, dataKeys);
+ const { yAxisWidth, setLabelWidth } = useYAxisLabelWidth(data, dataKeys);
const transformedKeys = useTransformedKeys(dataKeys);
@@ -271,7 +271,12 @@ const BarChartComponent = <T extends BarChartData>({
right: 0,
}}
>
- <YAxis width={yAxisWidth} tickLine={false} axisLine={false} tick={<YAxisTick />} />
+ <YAxis
+ width={yAxisWidth}
+ tickLine={false}
+ axisLine={false}
+ tick={<YAxisTick setLabelWidth={setLabelWidth} />}
+ />
{/* Invisible bars to maintain scale synchronization */}
{dataKeys.map((key) => {
return (
diff --git a/js/packages/react-ui/src/components/Charts/HorizontalBarChart/HorizontalBarChart.tsx b/js/packages/react-ui/src/components/Charts/HorizontalBarChart/HorizontalBarChart.tsx
index 1d5c075e3..e2c086b54 100644
--- a/js/packages/react-ui/src/components/Charts/HorizontalBarChart/HorizontalBarChart.tsx
+++ b/js/packages/react-ui/src/components/Charts/HorizontalBarChart/HorizontalBarChart.tsx
@@ -332,6 +332,8 @@ const HorizontalBarChartComponent = <T extends HorizontalBarChartData>({
[dataKeys, colors],
);
+ const setLabelWidth = useCallback(() => {}, []);
+
return (
<LabelTooltipProvider>
<SideBarTooltipProvider
@@ -380,7 +382,7 @@ const HorizontalBarChartComponent = <T extends HorizontalBarChartData>({
tickLine={false}
axisLine={false}
width={maxCategoryLabelWidth}
- tick={<YAxisTick />}
+ tick={<YAxisTick setLabelWidth={setLabelWidth} />}
interval={0}
// gives the padding on the 2 sides see the function for reference
padding={padding}
diff --git a/js/packages/react-ui/src/components/Charts/LineChart/LineChart.tsx b/js/packages/react-ui/src/components/Charts/LineChart/LineChart.tsx
index 7ea55ee2b..d55b1088d 100644
--- a/js/packages/react-ui/src/components/Charts/LineChart/LineChart.tsx
+++ b/js/packages/react-ui/src/components/Charts/LineChart/LineChart.tsx
@@ -81,7 +81,7 @@ export const LineChart = <T extends LineChartData>({
return getDataKeys(data, categoryKey as string);
}, [data, categoryKey]);
- const yAxisWidth = useYAxisLabelWidth(data, dataKeys);
+ const { yAxisWidth, setLabelWidth } = useYAxisLabelWidth(data, dataKeys);
const widthOfGroup = useMemo(() => {
return getWidthOfGroup(data);
@@ -260,7 +260,12 @@ export const LineChart = <T extends LineChartData>({
}}
onClick={onLineClick}
>
- <YAxis width={yAxisWidth} tickLine={false} axisLine={false} tick={<YAxisTick />} />
+ <YAxis
+ width={yAxisWidth}
+ tickLine={false}
+ axisLine={false}
+ tick={<YAxisTick setLabelWidth={setLabelWidth} />}
+ />
{/* Invisible lines to maintain scale synchronization */}
{dataKeys.map((key) => {
return (
diff --git a/js/packages/react-ui/src/components/Charts/RadarChart/RadarChart.tsx b/js/packages/react-ui/src/components/Charts/RadarChart/RadarChart.tsx
index 108fdf099..77f33d369 100644
--- a/js/packages/react-ui/src/components/Charts/RadarChart/RadarChart.tsx
+++ b/js/packages/react-ui/src/components/Charts/RadarChart/RadarChart.tsx
@@ -93,7 +93,7 @@ const RadarChartComponent = <T extends RadarChartData>({
}, [wrapperRect]);
const chartSizeStyle = useMemo(() => ({ width: chartSize, height: chartSize }), [chartSize]);
- const rechartsProps = useMemo(() => ({ width: chartSize, height: chartSize }), [chartSize]);
+ const rechartsProps = useMemo(() => ({ width: "100%", height: "100%" }), [chartSize]);
const radars = useMemo(() => {
return dataKeys.map((key) => {
diff --git a/js/packages/react-ui/src/components/Charts/hooks/useCanvasContextForLabelSize.ts b/js/packages/react-ui/src/components/Charts/hooks/useCanvasContextForLabelSize.ts
index 4c5d75bea..6e053ddd8 100644
--- a/js/packages/react-ui/src/components/Charts/hooks/useCanvasContextForLabelSize.ts
+++ b/js/packages/react-ui/src/components/Charts/hooks/useCanvasContextForLabelSize.ts
@@ -17,16 +17,12 @@ export const useCanvasContextForLabelSize = () => {
const { theme: userTheme } = useTheme();
return useMemo(() => {
- if (typeof window === "undefined" || typeof document === "undefined") {
- return null;
- }
const canvas = document.createElement("canvas");
- const context = canvas.getContext("2d");
- if (context) {
- // Should match the chart's actual font for accuracy.
- const font = userTheme.fontLabelExtraSmall ?? "400 10px/12px Inter";
- context.font = font;
- }
+ const context = canvas.getContext("2d")!;
+
+ // Should match the chart's actual font for accuracy.
+ const font = userTheme.fontLabelExtraSmall ?? "400 10px/12px Inter";
+ context.font = font;
return context;
}, [userTheme.fontLabelExtraSmall]);
};
diff --git a/js/packages/react-ui/src/components/Charts/hooks/useYAxisLabelWidth.tsx b/js/packages/react-ui/src/components/Charts/hooks/useYAxisLabelWidth.tsx
index d8941877e..ce9140f6b 100644
--- a/js/packages/react-ui/src/components/Charts/hooks/useYAxisLabelWidth.tsx
+++ b/js/packages/react-ui/src/components/Charts/hooks/useYAxisLabelWidth.tsx
@@ -1,4 +1,4 @@
-import { useMemo } from "react";
+import { useCallback, useMemo, useRef, useState } from "react";
import { AreaChartData } from "../AreaChart";
import { BarChartData } from "../BarChart";
@@ -9,12 +9,14 @@ import { useCanvasContextForLabelSize } from "./useCanvasContextForLabelSize";
const DEFAULT_Y_AXIS_WIDTH = 40;
const MIN_Y_AXIS_WIDTH = 20;
const MAX_Y_AXIS_WIDTH = 200;
+const LABEL_PADDING = 10;
export const useYAxisLabelWidth = (
data: AreaChartData | LineChartData | BarChartData,
dataKeys: string[],
) => {
const context = useCanvasContextForLabelSize();
+ const [maxLabelWidthRecieved, setMaxLabelWidthRecieved] = useState(0);
const maxLabelWidth = useMemo(() => {
if (typeof window === "undefined" || !data || data.length === 0 || !dataKeys.length) {
@@ -42,11 +44,25 @@ export const useYAxisLabelWidth = (
});
// Add padding for better visual appearance
- const totalWidth = Math.ceil(maxWidth) + 10; // 5px padding on each side
+ const totalWidth = Math.ceil(maxWidth) + LABEL_PADDING; // 5px padding on each side
// Clamp the width between MIN and MAX values
return Math.max(MIN_Y_AXIS_WIDTH, Math.min(MAX_Y_AXIS_WIDTH, totalWidth));
}, [data, dataKeys, context]);
- return maxLabelWidth;
+ const maxLabelWidthRef = useRef(maxLabelWidth);
+ maxLabelWidthRef.current = Math.max(maxLabelWidthRecieved, maxLabelWidth);
+
+ const setLabelWidth = useCallback(
+ (displayValue: string) => {
+ const textWidth = context.measureText(displayValue).width + LABEL_PADDING;
+
+ if (textWidth > maxLabelWidthRef.current) {
+ setMaxLabelWidthRecieved(textWidth);
+ }
+ },
+ [context],
+ );
+
+ return { yAxisWidth: maxLabelWidthRef.current, setLabelWidth };
};
diff --git a/js/packages/react-ui/src/components/Charts/shared/YAxisTick/YAxisTick.tsx b/js/packages/react-ui/src/components/Charts/shared/YAxisTick/YAxisTick.tsx
index 9ba948cf4..ef2a5f86f 100644
--- a/js/packages/react-ui/src/components/Charts/shared/YAxisTick/YAxisTick.tsx
+++ b/js/packages/react-ui/src/components/Charts/shared/YAxisTick/YAxisTick.tsx
@@ -1,3 +1,4 @@
+import { useLayoutEffect } from "react";
import { numberTickFormatter } from "../../utils";
interface YAxisTickProps {
@@ -22,16 +23,21 @@ interface YAxisTickProps {
tickFormatter?: (value: any) => string;
index?: number;
visibleTicksCount?: number;
+ setLabelWidth: (label: string) => void;
}
const YAxisTick: React.FC<YAxisTickProps> = (props) => {
- const { x, y, payload, textAnchor, verticalAnchor, className } = props;
+ const { x, y, payload, textAnchor, verticalAnchor, className, setLabelWidth } = props;
const displayValue =
typeof payload?.value === "number"
? numberTickFormatter(payload?.value)
: String(payload?.value);
+ useLayoutEffect(() => {
+ setLabelWidth(displayValue);
+ }, [displayValue, setLabelWidth]);
+
return (
<g transform={`translate(${x},${y})`} className={className}>
<text