This is an automated email from the ASF dual-hosted git repository. villebro pushed a commit to branch 7.0 in repository https://gitbox.apache.org/repos/asf/superset.git
commit fa67c7da8bcbea9e033e55ba1bd325829ea0ff78 Author: Joe Li <[email protected]> AuthorDate: Thu Nov 5 09:30:30 2026 -0800 fix(partition): keep the tooltip within the chart near the right edge (#45570) (cherry picked from commit a072f7e9efe77b4bc471c39067171e1b80ed3071) --- superset-frontend/jest.config.js | 2 +- .../plugin-chart-partition/src/Partition.ts | 18 ++- .../plugin-chart-partition/test/Partition.test.ts | 129 +++++++++++++++++++++ 3 files changed, 144 insertions(+), 5 deletions(-) diff --git a/superset-frontend/jest.config.js b/superset-frontend/jest.config.js index e62c3957e06..614f9eee84b 100644 --- a/superset-frontend/jest.config.js +++ b/superset-frontend/jest.config.js @@ -94,7 +94,7 @@ module.exports = { // react-markdown and the remark/rehype/vfile packages it pulls in are // ESM-only, so they are allowed through for the suites that opt out of the // react-markdown stub in spec/helpers/shim.tsx to render real Markdown. - 'node_modules/(?!@ant-design/(colors|fast-color)|@formatjs/.*|d3-(array|interpolate|color|time|scale|time-format|format|selection)|internmap|@mapbox/tiny-sdf|remark-gfm|(?!@ngrx|(?!deck.gl)|d3-scale)|markdown-table|micromark-*.|decode-named-character-reference|character-entities|mdast-util-*.|unist-util-*.|ccount|escape-string-regexp|nanoid|uuid|@rjsf/*.|@x0k/.*|echarts|zrender|fetch-mock|pretty-ms|parse-ms|ol|@babel/runtime|@emotion|cheerio|cheerio/lib|parse5|dom-serializer|entities [...] + 'node_modules/(?!@ant-design/(colors|fast-color)|@formatjs/.*|d3-(array|interpolate|color|time|scale|time-format|format|selection|hierarchy)|internmap|@mapbox/tiny-sdf|remark-gfm|(?!@ngrx|(?!deck.gl)|d3-scale)|markdown-table|micromark-*.|decode-named-character-reference|character-entities|mdast-util-*.|unist-util-*.|ccount|escape-string-regexp|nanoid|uuid|@rjsf/*.|@x0k/.*|echarts|zrender|fetch-mock|pretty-ms|parse-ms|ol|@babel/runtime|@emotion|cheerio|cheerio/lib|parse5|dom-serialize [...] ], preset: 'ts-jest', transform: { diff --git a/superset-frontend/plugins/plugin-chart-partition/src/Partition.ts b/superset-frontend/plugins/plugin-chart-partition/src/Partition.ts index 5e9d259519c..e1c25fb01c2 100644 --- a/superset-frontend/plugins/plugin-chart-partition/src/Partition.ts +++ b/superset-frontend/plugins/plugin-chart-partition/src/Partition.ts @@ -30,6 +30,9 @@ import { sanitizeHtml, } from '@superset-ui/core'; +// Horizontal gap, in px, between the cursor and the tooltip. +const TOOLTIP_OFFSET = 15; + interface PartitionDataNode { // A plain string for the metric row and the first grouping level; // an array of the full ancestor path (e.g. ["a", "a.1", "a.1.1"]) @@ -366,10 +369,17 @@ function Icicle(element: HTMLElement, props: IcicleProps): void { } t += '</tbody></table>'; const [tipX, tipY] = d3.mouse(element); - tip - .html(sanitizeHtml(t)) - .style('left', `${tipX + 15}px`) - .style('top', `${tipY}px`); + // Reset left before measuring: an absolutely positioned box shrinks to + // the space remaining right of `left`, which would under-report its width. + tip.html(sanitizeHtml(t)).style('left', '0px'); + // Flip to the left of the cursor when the tooltip would overflow the + // chart's right edge, where the chart container clips it. + const tipWidth = tip.node().offsetWidth; + const left = + tipX + TOOLTIP_OFFSET + tipWidth > width + ? Math.max(0, tipX - TOOLTIP_OFFSET - tipWidth) + : tipX + TOOLTIP_OFFSET; + tip.style('left', `${left}px`).style('top', `${tipY}px`); } const nodes = init(root); diff --git a/superset-frontend/plugins/plugin-chart-partition/test/Partition.test.ts b/superset-frontend/plugins/plugin-chart-partition/test/Partition.test.ts new file mode 100644 index 00000000000..cd7b6df7c03 --- /dev/null +++ b/superset-frontend/plugins/plugin-chart-partition/test/Partition.test.ts @@ -0,0 +1,129 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF 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 Partition from '../src/Partition'; + +const CHART_WIDTH = 400; +const CHART_HEIGHT = 300; +const DEFAULT_TOOLTIP_WIDTH = 120; +const TOOLTIP_OFFSET = 15; + +const props = { + width: CHART_WIDTH, + height: CHART_HEIGHT, + data: [ + { + name: 'sum__num', + val: 100, + children: [ + { name: 'boy', val: 60 }, + { name: 'girl', val: 40 }, + ], + }, + ], + colorScheme: 'supersetColors', + dateTimeFormat: '%Y-%m-%d', + equalDateSize: true, + levels: ['gender'], + metrics: ['sum__num'], + numberFormat: ',d', + partitionLimit: 0, + partitionThreshold: 0, + timeSeriesOption: 'not_time', + useLogScale: false, + useRichTooltip: false, + sliceId: 1, +}; + +let container: HTMLDivElement; +let tooltipWidth = DEFAULT_TOOLTIP_WIDTH; +let rectSpy: jest.SpyInstance; +let offsetWidthSpy: jest.SpyInstance; + +beforeEach(() => { + tooltipWidth = DEFAULT_TOOLTIP_WIDTH; + container = document.createElement('div'); + document.body.appendChild(container); + // jsdom performs no layout: give the container a fixed box so the + // pointer coordinates d3.mouse() derives from clientX are deterministic. + rectSpy = jest + .spyOn(HTMLElement.prototype, 'getBoundingClientRect') + .mockImplementation(function getRect(this: HTMLElement) { + const isTooltip = this.classList.contains('partition-tooltip'); + const width = isTooltip ? tooltipWidth : CHART_WIDTH; + + return { + x: 0, + y: 0, + top: 0, + left: 0, + right: width, + bottom: CHART_HEIGHT, + width, + height: CHART_HEIGHT, + toJSON: () => ({}), + } as DOMRect; + }); + offsetWidthSpy = jest + .spyOn(HTMLElement.prototype, 'offsetWidth', 'get') + .mockImplementation(function getOffsetWidth(this: HTMLElement) { + return this.classList.contains('partition-tooltip') + ? tooltipWidth + : CHART_WIDTH; + }); +}); + +afterEach(() => { + rectSpy.mockRestore(); + offsetWidthSpy.mockRestore(); + container.remove(); +}); + +function hoverAt(clientX: number, clientY: number): HTMLElement { + Partition(container, props); + const node = container.querySelector('svg g rect') as SVGRectElement; + node.dispatchEvent( + new MouseEvent('mousemove', { bubbles: true, clientX, clientY }), + ); + + return container.querySelector('.partition-tooltip') as HTMLElement; +} + +test('places the tooltip right of the cursor when there is room', () => { + const tooltip = hoverAt(50, 100); + + expect(tooltip.style.left).toBe(`${50 + TOOLTIP_OFFSET}px`); + expect(tooltip.style.top).toBe('100px'); +}); + +test('keeps the tooltip inside the chart when hovering near the right edge', () => { + const cursorX = CHART_WIDTH - 10; + const tooltip = hoverAt(cursorX, 100); + + const left = parseFloat(tooltip.style.left); + expect(left + tooltipWidth).toBeLessThanOrEqual(CHART_WIDTH); + // shifted to the left of the cursor rather than overlapping it + expect(left + tooltipWidth).toBeLessThanOrEqual(cursorX); +}); + +test('clamps the tooltip to the chart origin when it is wider than the chart', () => { + tooltipWidth = CHART_WIDTH + 100; + const tooltip = hoverAt(CHART_WIDTH / 2, 100); + + expect(parseFloat(tooltip.style.left)).toBe(0); +});
