diff --git a/packages/osd-charts/integration/tests/__image_snapshots__/all-test-ts-baseline-visual-tests-for-all-stories-bar-chart-tooltip-boundary-visually-looks-correct-1-snap.png b/packages/osd-charts/integration/tests/__image_snapshots__/all-test-ts-baseline-visual-tests-for-all-stories-bar-chart-tooltip-boundary-visually-looks-correct-1-snap.png new file mode 100644 index 000000000000..fe9e824d8978 Binary files /dev/null and b/packages/osd-charts/integration/tests/__image_snapshots__/all-test-ts-baseline-visual-tests-for-all-stories-bar-chart-tooltip-boundary-visually-looks-correct-1-snap.png differ diff --git a/packages/osd-charts/integration/tests/__image_snapshots__/interactions-test-ts-interactions-tooltips-boundary-chart-should-contain-tooltip-inside-boundary-near-bottom-1-snap.png b/packages/osd-charts/integration/tests/__image_snapshots__/interactions-test-ts-interactions-tooltips-boundary-chart-should-contain-tooltip-inside-boundary-near-bottom-1-snap.png new file mode 100644 index 000000000000..d009829f3bc7 Binary files /dev/null and b/packages/osd-charts/integration/tests/__image_snapshots__/interactions-test-ts-interactions-tooltips-boundary-chart-should-contain-tooltip-inside-boundary-near-bottom-1-snap.png differ diff --git a/packages/osd-charts/integration/tests/__image_snapshots__/interactions-test-ts-interactions-tooltips-boundary-chart-should-contain-tooltip-inside-boundary-near-top-1-snap.png b/packages/osd-charts/integration/tests/__image_snapshots__/interactions-test-ts-interactions-tooltips-boundary-chart-should-contain-tooltip-inside-boundary-near-top-1-snap.png new file mode 100644 index 000000000000..3a9a49d1d8d3 Binary files /dev/null and b/packages/osd-charts/integration/tests/__image_snapshots__/interactions-test-ts-interactions-tooltips-boundary-chart-should-contain-tooltip-inside-boundary-near-top-1-snap.png differ diff --git a/packages/osd-charts/integration/tests/interactions.test.ts b/packages/osd-charts/integration/tests/interactions.test.ts index caf44747bf12..08ea8f59c500 100644 --- a/packages/osd-charts/integration/tests/interactions.test.ts +++ b/packages/osd-charts/integration/tests/interactions.test.ts @@ -169,6 +169,30 @@ describe('Interactions', () => { { left: 120, bottom: 80 }, ); }); + + describe.each([ + // TODO: find why these vrt don't position tooltip wrt boundary + // ['Root', 'root', 7], + // ['Red', 'red', 6], + // ['White', 'white', 5], + // ['Blue', 'blue', 3], + ['Chart', 'chart', 2], + ])('Boundary - %s', (_, boundary, groups) => { + it('should contain tooltip inside boundary near top', async () => { + await common.expectChartWithMouseAtUrlToMatchScreenshot( + `http://localhost:9001/?path=/story/bar-chart--tooltip-boundary&knob-Boundary Element=${boundary}&knob-Groups=${groups}&knob-Show axes=false`, + { left: 100, top: 20 }, + { screenshotSelector: 'body' }, + ); + }); + it('should contain tooltip inside boundary near bottom', async () => { + await common.expectChartWithMouseAtUrlToMatchScreenshot( + `http://localhost:9001/?path=/story/bar-chart--tooltip-boundary&knob-Boundary Element=${boundary}&knob-Groups=${groups}&knob-Show axes=false`, + { left: 100, bottom: 20 }, + { screenshotSelector: 'body' }, + ); + }); + }); }); describe('brushing', () => { diff --git a/packages/osd-charts/src/chart_types/xy_chart/renderer/dom/annotations/annotation_tooltip.tsx b/packages/osd-charts/src/chart_types/xy_chart/renderer/dom/annotations/annotation_tooltip.tsx index cceb482e43d2..d48982e3bbab 100644 --- a/packages/osd-charts/src/chart_types/xy_chart/renderer/dom/annotations/annotation_tooltip.tsx +++ b/packages/osd-charts/src/chart_types/xy_chart/renderer/dom/annotations/annotation_tooltip.tsx @@ -66,7 +66,7 @@ export const AnnotationTooltip = ({ state, chartRef, chartId, onScroll, zIndex } return { ...rest, placement: placement ?? Placement.Right, - boundary: boundary === 'chart' && chartRef.current ? chartRef.current : undefined, + boundary: boundary === 'chart' ? chartRef.current ?? undefined : boundary, }; }, [state?.tooltipSettings, chartRef]); diff --git a/packages/osd-charts/src/components/tooltip/tooltip.tsx b/packages/osd-charts/src/components/tooltip/tooltip.tsx index abc01b573de7..79c10a304e64 100644 --- a/packages/osd-charts/src/components/tooltip/tooltip.tsx +++ b/packages/osd-charts/src/components/tooltip/tooltip.tsx @@ -200,7 +200,7 @@ const TooltipComponent = ({ (rotation === 0 || rotation === 180 ? [Placement.Right, Placement.Left, Placement.Top, Placement.Bottom] : [Placement.Top, Placement.Bottom, Placement.Right, Placement.Left]), - boundary: boundary === 'chart' && chartRef.current ? chartRef.current : undefined, + boundary: boundary === 'chart' ? chartRef.current ?? undefined : boundary, }; }, [settings, chartRef, rotation]); diff --git a/packages/osd-charts/stories/bar/55_tooltip_boundary.tsx b/packages/osd-charts/stories/bar/55_tooltip_boundary.tsx new file mode 100644 index 000000000000..db9f8a428e88 --- /dev/null +++ b/packages/osd-charts/stories/bar/55_tooltip_boundary.tsx @@ -0,0 +1,101 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { boolean, number, select } from '@storybook/addon-knobs'; +import React, { useRef } from 'react'; + +import { Axis, BarSeries, Chart, Position, ScaleType, Settings, TooltipProps } from '../../src'; +import { getRandomNumberGenerator, SeededDataGenerator } from '../../src/mocks/utils'; +import { SB_KNOBS_PANEL } from '../utils/storybook'; + +const dg = new SeededDataGenerator(); +const rng = getRandomNumberGenerator(); + +export const Example = () => { + const showAxes = boolean('Show axes', false); + const groups = number('Groups', 5, { min: 2, max: 20, step: 1 }); + const data = dg.generateGroupedSeries(4, groups).map((d) => { + return { + ...d, + y1: rng(0, 20), + y2: rng(0, 20), + g1: `dog ${d.g}`, + g2: `cat ${d.g}`, + }; + }); + const red = useRef(null); + const white = useRef(null); + const blue = useRef(null); + const boundaryMap: Record = { + default: undefined, + red: red.current, + white: white.current, + blue: blue.current, + chart: 'chart', + root: document.getElementById('story-root'), + }; + const boundarySting = select( + 'Boundary Element', + { + Default: 'default', + 'Root (blanchedalmond)': 'root', + Red: 'red', + White: 'white', + Blue: 'blue', + Chart: 'chart', + }, + 'default', + ); + const boundary = boundaryMap[boundarySting] ?? undefined; + + return ( +
+
+
+ + + + Number(d).toFixed(2)} + /> + + +
+
+
+ ); +}; + +Example.story = { + parameters: { + options: { selectedPanel: SB_KNOBS_PANEL }, + }, +}; diff --git a/packages/osd-charts/stories/bar/bars.stories.tsx b/packages/osd-charts/stories/bar/bars.stories.tsx index 871a1c5a0347..c492126d2d82 100644 --- a/packages/osd-charts/stories/bar/bars.stories.tsx +++ b/packages/osd-charts/stories/bar/bars.stories.tsx @@ -83,4 +83,5 @@ export { Example as testDiscover } from './43_test_discover'; export { Example as testSingleHistogramBarChart } from './44_test_single_histogram'; export { Example as testMinHeightPositiveAndNegativeValues } from './46_test_min_height'; export { Example as testTooltipAndRotation } from './48_test_tooltip'; +export { Example as tooltipBoundary } from './55_tooltip_boundary'; export { Example as testDualYAxis } from './49_test_dual_axis'; diff --git a/packages/osd-charts/stories/utils/knobs.ts b/packages/osd-charts/stories/utils/knobs.ts index cb4e25ed13b1..f823b3bdf843 100644 --- a/packages/osd-charts/stories/utils/knobs.ts +++ b/packages/osd-charts/stories/utils/knobs.ts @@ -209,17 +209,24 @@ export const getFallbackPlacementsKnob = (): Placement[] | undefined => { return knob; }; -export const getBoundaryKnob = () => - // @ts-ignore - select( - 'Boundary Element', - { - Chart: 'chart', - 'Document Body': document.body, - Default: undefined, - }, - undefined, - ); +const boundaryMap: Record = { + default: undefined, + chart: 'chart', +}; + +export const getBoundaryKnob = () => { + const boundaryString = + select( + 'Boundary Element', + { + Default: 'default', + Chart: 'chart', + }, + 'default', + ) ?? ''; + + return boundaryMap[boundaryString] ?? undefined; +}; export const getVerticalTextAlignmentKnob = (group?: string) => select(