Skip to content

Commit

Permalink
Merge pull request #293 from adobe/widthHeightIntegers
Browse files Browse the repository at this point in the history
Width height integers
  • Loading branch information
marshallpete committed May 8, 2024
2 parents 4e08c33 + 7c495a1 commit 1b2340c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/hooks/useChartHeight.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ export default function useChartHeight(containerHeight: number, maxHeight: numbe
return useMemo(() => {
let targetHeight = minHeight;
if (typeof height === 'number') {
targetHeight = height;
// integers only, decimal values can cause performance issues with vega.
return Math.round(height);
} else if (/^\d+%$/.exec(height)) {
targetHeight = (containerHeight * Number(height.slice(0, -1))) / 100;
} else {
console.error(
`height of ${height} is not a valid height. Please provide a valid number or percentage ex. 75%`
);
}
return targetHeight === 0 ? 0 : Math.min(maxHeight, Math.max(minHeight, targetHeight));
// integers only, decimal values can cause performance issues with vega.
return targetHeight === 0 ? 0 : Math.round(Math.min(maxHeight, Math.max(minHeight, targetHeight)));
}, [containerHeight, maxHeight, minHeight, height]);
}
6 changes: 4 additions & 2 deletions src/hooks/useChartWidth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export default function useChartWidth(
return useMemo(() => {
let targetWidth = minWidth;
if (typeof width === 'number') {
targetWidth = width;
// integers only, decimal values can cause performance issues with vega.
return Math.round(width);
} else if (width === 'auto') {
targetWidth = containerWidth;
} else if (width.match(/^\d+%$/)) {
Expand All @@ -30,6 +31,7 @@ export default function useChartWidth(
`width of ${width} is not a valid width. Please provide a valid number, 'auto' or percentage ex. 75%`
);
}
return targetWidth === 0 ? 0 : Math.min(maxWidth, Math.max(minWidth, targetWidth));
// integers only, decimal values can cause performance issues with vega.
return targetWidth === 0 ? 0 : Math.round(Math.min(maxWidth, Math.max(minWidth, targetWidth)));
}, [containerWidth, maxWidth, minWidth, width]);
}

0 comments on commit 1b2340c

Please sign in to comment.