Skip to content

Commit

Permalink
sunburst style tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
vincerubinetti committed Feb 11, 2025
1 parent 88a53be commit 4fd3979
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
6 changes: 5 additions & 1 deletion frontend/src/components/Sunburst.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
height: 20px;
}

.chart {
justify-self: center;
}

.segment {
cursor: pointer;
transition: opacity var(--fast);
Expand Down Expand Up @@ -47,7 +51,7 @@
padding: 5px;
}

@media (max-width: 600px) {
@media (max-width: 800px) {
.container {
grid-template-columns: 1fr;
}
Expand Down
22 changes: 16 additions & 6 deletions frontend/src/components/Sunburst.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,19 @@ const Sunburst = ({ title, data }: Props) => {
const container = useRef<HTMLDivElement>(null);
const svg = useRef<SVGSVGElement>(null);

/** fit viewBox after any change */
const [maxHeight, setMaxHeight] = useState(99999);

useEffect(() => {
if (svg.current) fitViewbox(svg.current, 0.01);
});
if (!svg.current) return;
/** fit viewBox to contents */
const { height } = 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);
}, []);

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

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

Expand All @@ -217,7 +226,7 @@ const Sunburst = ({ title, data }: Props) => {
gap="sm"
gapRatio={1}
direction="column"
hAlign="stretch"
hAlign="right"
vAlign="top"
>
{breadcrumbs.map((item, index) => (
Expand Down Expand Up @@ -378,7 +387,8 @@ const Segment = ({
<text
className={classes.label}
textAnchor="middle"
dominantBaseline="middle"
// dominantBaseline="central"
dy="0.5ex"
fontSize={fontSize}
fill={theme["--black"]}
>
Expand Down
19 changes: 8 additions & 11 deletions frontend/src/util/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,19 +161,16 @@ export const isCovering = (

/** fit view box to contents of svg */
export const fitViewbox = (svg?: SVGSVGElement, paddingPercent = 0) => {
if (!svg) return;
const { x, y, width, height } = svg.getBBox();
if (!svg) return { x: 0, y: 0, width: 100, height: 100 };
let { x, y, width, height } = svg.getBBox();
const padding = Math.min(width, height) * paddingPercent;
const viewBox = [
x - padding,
y - padding,
width + padding * 2,
height + padding * 2,
]
.map((v) => Math.round(v))
.join(" ");
x -= padding;
y -= padding;
width += padding * 2;
height += padding * 2;
const viewBox = [x, y, width, height].map((v) => Math.round(v)).join(" ");
svg.setAttribute("viewBox", viewBox);
return viewBox;
return { x, y, width, height };
};

/** open browser print dialog with just one element shown */
Expand Down

0 comments on commit 4fd3979

Please sign in to comment.