From bb6d82dc8d7945d3a564170122d6fa1ad164b414 Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Sat, 19 Oct 2024 04:16:46 +1100 Subject: [PATCH] [8.x] [ML] Data Drift: Update brush positions on window resize fix (#196830) (#196914) # Backport This will backport the following commits from `main` to `8.x`: - [[ML] Data Drift: Update brush positions on window resize fix (#196830)](https://github.com/elastic/kibana/pull/196830) ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) Co-authored-by: Robert Jaszczurek <92210485+rbrtj@users.noreply.github.com> --- .../single_brush.tsx | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/x-pack/plugins/data_visualizer/public/application/data_drift/document_count_chart_single_brush/single_brush.tsx b/x-pack/plugins/data_visualizer/public/application/data_drift/document_count_chart_single_brush/single_brush.tsx index b6b3a32f628f1..4e0af369a8124 100644 --- a/x-pack/plugins/data_visualizer/public/application/data_drift/document_count_chart_single_brush/single_brush.tsx +++ b/x-pack/plugins/data_visualizer/public/application/data_drift/document_count_chart_single_brush/single_brush.tsx @@ -5,6 +5,7 @@ * 2.0. */ +import { isEqual } from 'lodash'; import React, { useEffect, useRef, type FC } from 'react'; import * as d3Brush from 'd3-brush'; @@ -310,9 +311,42 @@ export const SingleBrush: FC = (props) => { mlBrushSelection.exit().remove(); } + function updateBrush() { + const mlBrushSelection = gBrushes + .selectAll('.brush') + .data(brushes.current, (d) => (d as SingleBrush).id); + + mlBrushSelection.each(function (brushObject, i, n) { + const x = d3 + .scaleLinear() + .domain([minRef.current, maxRef.current]) + .rangeRound([0, widthRef.current]); + brushObject.brush.extent([ + [0, BRUSH_MARGIN], + [widthRef.current, BRUSH_HEIGHT - BRUSH_MARGIN], + ]); + + brushObject.brush(d3.select(n[i] as SVGGElement)); + const xStart = x(brushObject.start) ?? 0; + const xEnd = x(brushObject.end) ?? 0; + brushObject.brush.move(d3.select(n[i] as SVGGElement), [xStart, xEnd]); + }); + } + if (brushes.current.length !== 1) { widthRef.current = width; newBrush(`${brushId}`, baselineMin, baselineMax); + } else if ( + widthRef.current !== width || + minRef.current !== min || + maxRef.current !== max || + !isEqual(snapTimestampsRef.current, snapTimestamps) + ) { + widthRef.current = width; + minRef.current = min; + maxRef.current = max; + snapTimestampsRef.current = snapTimestamps; + updateBrush(); } drawBrushes();