Skip to content

Commit 892bd75

Browse files
authored
chore: Fix size behaviour improvements (#47)
* chore: Fix size behaviour improvements * Update chart-core-fit-size.test.tsx * fix regressions * use default height
1 parent 08360d2 commit 892bd75

File tree

5 files changed

+28
-12
lines changed

5 files changed

+28
-12
lines changed

pages/common/styles.module.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
border: 1px solid cs.$color-border-status-info;
1616
border-radius: 4px;
1717
position: relative;
18+
overflow: hidden;
1819
}
1920

2021
.chart-frame-annotation {

pages/common/templates.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ const ChartFrame = forwardRef(
264264
ref: React.Ref<HTMLDivElement>,
265265
) => {
266266
return (
267-
<div ref={ref} className={styles["chart-frame"]} style={{ height, width, overflow: "hidden" }}>
267+
<div ref={ref} className={styles["chart-frame"]} style={{ height, width }}>
268268
{children}
269269
<div className={styles["chart-frame-annotation"]}>{annotation}</div>
270270
</div>

src/core/__tests__/chart-core-fit-size.test.tsx

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ describe("CoreChart: fit-size", () => {
4545
test("uses explicit chart height", () => {
4646
const { rerender } = renderChart({ highcharts });
4747

48-
expect(HighchartsReact).toHaveBeenCalledWith(chartOptionsWithHeight(undefined), expect.anything());
48+
// Default height is used when explicit height is not set.
49+
expect(HighchartsReact).toHaveBeenCalledWith(chartOptionsWithHeight(400), expect.anything());
4950

5051
rerender({ highcharts, options: { chart: { height: "20rem" } } });
5152

@@ -110,20 +111,27 @@ describe("CoreChart: fit-size", () => {
110111
const offset = verticalAxisTitlePlacement === "top" ? 28 : 0;
111112
const { rerender } = renderChart({ highcharts, fitHeight: true, verticalAxisTitlePlacement });
112113

114+
// Uses default min height of 200px.
113115
await waitFor(() => {
114-
expect(HighchartsReact).toHaveBeenCalledWith(chartOptionsWithHeight(80 - offset), expect.anything());
116+
expect(HighchartsReact).toHaveBeenCalledWith(chartOptionsWithHeight(200 - offset), expect.anything());
115117
});
116118

117-
rerender({ highcharts, fitHeight: true, chartHeight: 300, verticalAxisTitlePlacement });
118-
119+
// Uses measured height when it is bigger than explicitly provided min height.
120+
rerender({ highcharts, fitHeight: true, chartMinHeight: 50, verticalAxisTitlePlacement });
119121
await waitFor(() => {
120122
expect(HighchartsReact).toHaveBeenCalledWith(chartOptionsWithHeight(80 - offset), expect.anything());
121123
});
122124

123-
rerender({ highcharts, fitHeight: true, chartHeight: 300, chartMinHeight: 200, verticalAxisTitlePlacement });
125+
// Chart height has no effect on charts with fit-height.
126+
rerender({ highcharts, fitHeight: true, chartMinHeight: 50, chartHeight: 300, verticalAxisTitlePlacement });
127+
await waitFor(() => {
128+
expect(HighchartsReact).toHaveBeenCalledWith(chartOptionsWithHeight(80 - offset), expect.anything());
129+
});
124130

131+
// Taking min height which is above the default min height.
132+
rerender({ highcharts, fitHeight: true, chartHeight: 300, chartMinHeight: 250, verticalAxisTitlePlacement });
125133
await waitFor(() => {
126-
expect(HighchartsReact).toHaveBeenCalledWith(chartOptionsWithHeight(200 - offset), expect.anything());
134+
expect(HighchartsReact).toHaveBeenCalledWith(chartOptionsWithHeight(250 - offset), expect.anything());
127135
});
128136
},
129137
);

src/core/chart-container.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ import testClasses from "./test-classes/styles.css.js";
1515
// Chart container implements the layout for top-level components, including chart plot, legend, and more.
1616
// It also implements the height- and width overflow behaviors.
1717

18+
const DEFAULT_CHART_HEIGHT = 400;
19+
const DEFAULT_CHART_MIN_HEIGHT = 200;
20+
1821
interface ChartContainerProps {
1922
// The header, footer, vertical axis title, and legend are rendered as is, and we measure the height of these components
2023
// to compute the available height for the chart plot when fitHeight=true. When there is not enough vertical space, the
@@ -51,17 +54,16 @@ export function ChartContainer({
5154
chartMinHeight,
5255
chartMinWidth,
5356
}: ChartContainerProps) {
57+
const { refs, measures } = useContainerQueries();
58+
5459
// The vertical axis title is rendered above the chart, and is technically not a part of the chart plot.
5560
// However, we want to include it to the chart's height computations as it does belong to the chart logically.
5661
// We do so by taking the title's constant height into account, when "top" axis placement is chosen.
5762
const verticalTitleOffset = Styles.verticalAxisTitleBlockSize + Styles.verticalAxisTitleMargin;
5863
const heightOffset = verticalAxisTitlePlacement === "top" ? verticalTitleOffset : 0;
59-
const withMinHeight = (height?: number) =>
60-
height === undefined ? chartMinHeight : Math.max(chartMinHeight ?? 0, height) - heightOffset;
61-
62-
const { refs, measures } = useContainerQueries();
64+
const withMinHeight = (height: number) => Math.max(chartMinHeight ?? DEFAULT_CHART_MIN_HEIGHT, height) - heightOffset;
6365
const measuredChartHeight = withMinHeight(measures.chart - measures.header - measures.footer);
64-
const effectiveChartHeight = fitHeight ? measuredChartHeight : withMinHeight(chartHeight);
66+
const effectiveChartHeight = fitHeight ? measuredChartHeight : withMinHeight(chartHeight ?? DEFAULT_CHART_HEIGHT);
6567
return (
6668
<div
6769
ref={refs.chart}

src/core/styles.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,12 @@ $side-legend-min-inline-size: max(20%, 150px);
8989
gap: cs.$space-static-xxs;
9090
}
9191

92+
.chart-container-root {
93+
block-size: 100%;
94+
}
95+
9296
.chart-container-fit-height {
97+
overflow-x: auto;
9398
position: absolute;
9499
inset: 0;
95100
}

0 commit comments

Comments
 (0)