Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
@import "src/core/index.module";

.visualization {
width: fit-content;
// Fill the flex/grid slot so the chart inside can resize down on
// narrow viewports.
width: 100%;
position: relative;
background-color: #fff;
color: $headings-color;
Expand All @@ -26,8 +28,7 @@
display: flex;
justify-content: center;
align-items: center;
// These are the dimensions of the linegraph.
min-width: 947px;
// Reserve vertical space for the spinner so layout doesn't shift on load.
min-height: 300px;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,26 @@ export function InternalLineGraph({
};
}, []);

// Resize uPlot when its container changes size. The !!data dependency
// attaches the observer once data arrives and Visualization renders
// children (until then, the el ref is unattached).
useEffect(() => {
const container = el.current;
if (!container || typeof ResizeObserver === "undefined") {
return;
}
const observer = new ResizeObserver(entries => {
const u = uRef.current;
if (!u) return;
const width = entries[0]?.contentRect.width;
if (width && width > 0) {
u.setSize({ width, height: 300 });
}
});
observer.observe(container);
return () => observer.disconnect();
}, [!!data]);

// Update chart when data, time info, or display dependencies change.
// prevDataRef is still needed because the effect uses previous data to
// compute prior series keys (deciding whether to setData() on the
Expand Down Expand Up @@ -341,13 +361,15 @@ export function InternalLineGraph({
// Updates existing plot with new points.
uRef.current.setData(uPlotData);
} else {
const initialWidth = el.current?.clientWidth || 947;
const options = configureUPlotLineChart(
metricElements,
axisElement,
data,
setNewTimeRange,
() => xAxisDomainRef.current,
() => yAxisDomainRef.current,
initialWidth,
);

if (uRef.current) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ export function configureUPlotLineChart(
setMetricsFixedWindow: (startMillis: number, endMillis: number) => void,
getLatestXAxisDomain: () => AxisDomain,
getLatestYAxisDomain: () => AxisDomain,
// Pass the container's current width; subsequent resizes should be
// driven via uPlot.setSize().
initialWidth?: number,
): uPlot.Options {
const formattedRaw = formatMetricData(metrics, data);
// Copy palette over since we mutate it in the `series` function
Expand All @@ -224,7 +227,7 @@ export function configureUPlotLineChart(
// Please see https://github.com/leeoniya/uPlot/tree/master/docs for
// information on how to construct this object.
return {
width: 947,
width: initialWidth ?? 947,
height: 300,
cursor: {
lock: true,
Expand Down
Loading