Skip to content

Commit

Permalink
change font size method
Browse files Browse the repository at this point in the history
  • Loading branch information
vincerubinetti committed Feb 11, 2025
1 parent 4fd3979 commit 7e9ab6a
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions frontend/src/components/Sunburst.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from "react-icons/fa6";
import clsx from "clsx";
import { clamp, startCase, sumBy, truncate } from "lodash";
import { useDebounce, useElementSize } from "@reactuses/core";
import Button from "@/components/Button";
import Flex from "@/components/Flex";
import Popover from "@/components/Popover";
Expand All @@ -27,6 +28,8 @@ import { flatMap } from "@/util/object";
import { formatNumber } from "@/util/string";
import classes from "./Sunburst.module.css";

const docFontSize = parseFloat(window.getComputedStyle(document.body).fontSize);

export type Item = {
/** human-readable label */
label?: string;
Expand Down Expand Up @@ -66,26 +69,26 @@ const ringSize = 20;
const startLevel = 3;
/** gap between rings */
const gapSize = 2;
/** font size */
const fontSize = 10;

const Sunburst = ({ title, data }: Props) => {
const container = useRef<HTMLDivElement>(null);
const svg = useRef<SVGSVGElement>(null);

const [maxHeight, setMaxHeight] = useState(99999);
/** font size, in svg units */
const [fontSize, setFontSize] = useState(16);

/** height of svg, in client units */
const clientHeight = useDebounce(useElementSize(svg)[1], 300);

useEffect(() => {
if (!svg.current) return;

/** fit viewBox to contents */
const { height } = fitViewbox(svg.current, 0.01);
const viewbox = fitViewbox(svg.current, 0.01);

/** set max dimensions so svg text isn't bigger than page text */
const pageFontSize = parseFloat(
window.getComputedStyle(svg.current).fontSize,
);
setMaxHeight((height / fontSize) * pageFontSize);
}, []);
/** scale svg font size to match document font size */
setFontSize(viewbox.height * (docFontSize / clientHeight));
}, [clientHeight]);

/** "trail" of breadcrumbs through tree of items */
const [breadcrumbs, setBreadcrumbs] = useState<ItemDerived[]>([]);
Expand Down Expand Up @@ -215,8 +218,12 @@ const Sunburst = ({ title, data }: Props) => {
</div>

{/* chart container */}
<svg ref={svg} className={classes.chart} style={{ maxHeight }}>
<Segment children={derived} selectItem={selectItem} />
<svg ref={svg} className={classes.chart}>
<Segment
children={derived}
selectItem={selectItem}
fontSize={fontSize}
/>
</svg>

{/* breadcrumbs */}
Expand Down Expand Up @@ -300,6 +307,7 @@ const Sunburst = ({ title, data }: Props) => {
export default Sunburst;

type SegmentProps = Partial<ItemDerived> & {
fontSize: number;
level?: number;
startAngle?: number;
endAngle?: number;
Expand All @@ -308,6 +316,7 @@ type SegmentProps = Partial<ItemDerived> & {

/** single arc segment */
const Segment = ({
fontSize,
level = 0,
startAngle = 0,
endAngle = 1,
Expand Down Expand Up @@ -388,7 +397,7 @@ const Segment = ({
className={classes.label}
textAnchor="middle"
// dominantBaseline="central"
dy="0.5ex"
dy="0.55ex"
fontSize={fontSize}
fill={theme["--black"]}
>
Expand All @@ -406,6 +415,7 @@ const Segment = ({
<Segment
key={index}
{...item}
fontSize={fontSize}
level={level + 1}
startAngle={offsetAngle}
endAngle={(offsetAngle = offsetAngle + item.percent)}
Expand Down

0 comments on commit 7e9ab6a

Please sign in to comment.