@automattic/charts 3.1.1 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (116) hide show
  1. package/CHANGELOG.md +41 -0
  2. package/TOKENS.md +80 -46
  3. package/dist/index.cjs +1224 -662
  4. package/dist/index.cjs.map +1 -1
  5. package/dist/index.css +72 -23
  6. package/dist/index.d.cts +230 -162
  7. package/dist/index.d.ts +230 -162
  8. package/dist/index.js +1224 -663
  9. package/dist/index.js.map +1 -1
  10. package/package.json +10 -10
  11. package/src/charts/area-chart/area-chart.tsx +39 -25
  12. package/src/charts/area-chart/test/area-chart.test.tsx +4 -33
  13. package/src/charts/area-chart/test/tick-placement.test.tsx +55 -0
  14. package/src/charts/area-chart/test/tooltip-resolution-branches.test.tsx +86 -0
  15. package/src/charts/area-chart/test/tooltip-resolution.test.tsx +69 -0
  16. package/src/charts/area-chart/types.ts +4 -7
  17. package/src/charts/bar-chart/bar-chart.tsx +38 -11
  18. package/src/charts/bar-chart/private/test/use-bar-chart-options-formatting.test.tsx +69 -0
  19. package/src/charts/bar-chart/private/use-bar-chart-options.ts +27 -15
  20. package/src/charts/bar-chart/test/bar-chart-pointer-events.test.tsx +67 -0
  21. package/src/charts/bar-chart/test/bar-chart.test.tsx +79 -0
  22. package/src/charts/bar-list-chart/bar-list-chart.tsx +45 -2
  23. package/src/charts/bar-list-chart/test/bar-list-chart.test.tsx +38 -0
  24. package/src/charts/conversion-funnel-chart/conversion-funnel-chart.module.scss +33 -23
  25. package/src/charts/conversion-funnel-chart/conversion-funnel-chart.tsx +138 -182
  26. package/src/charts/conversion-funnel-chart/test/conversion-funnel-chart.test.tsx +46 -0
  27. package/src/charts/geo-chart/geo-chart.module.scss +1 -0
  28. package/src/charts/geo-chart/geo-chart.tsx +5 -10
  29. package/src/charts/geo-chart/test/geo-chart.test.tsx +2 -2
  30. package/src/charts/heatmap-chart/heatmap-chart.module.scss +21 -16
  31. package/src/charts/heatmap-chart/heatmap-chart.tsx +45 -28
  32. package/src/charts/heatmap-chart/private/build-calendar-data.ts +3 -0
  33. package/src/charts/heatmap-chart/private/heatmap-legend.tsx +11 -4
  34. package/src/charts/heatmap-chart/test/heatmap-chart.test.tsx +51 -12
  35. package/src/charts/heatmap-chart/types.ts +2 -2
  36. package/src/charts/leaderboard-chart/hooks/use-fitted-row-count.ts +2 -2
  37. package/src/charts/leaderboard-chart/hooks/use-leaderboard-legend-items.ts +3 -5
  38. package/src/charts/leaderboard-chart/leaderboard-chart.module.scss +31 -19
  39. package/src/charts/leaderboard-chart/leaderboard-chart.tsx +27 -17
  40. package/src/charts/leaderboard-chart/test/leaderboard-chart.test.tsx +14 -0
  41. package/src/charts/leaderboard-chart/test/use-leaderboard-legend-items.test.tsx +2 -9
  42. package/src/charts/line-chart/line-chart.module.scss +10 -18
  43. package/src/charts/line-chart/line-chart.tsx +109 -25
  44. package/src/charts/line-chart/private/line-chart-annotation.tsx +18 -19
  45. package/src/charts/line-chart/test/default-tooltip-formatting.test.tsx +74 -0
  46. package/src/charts/line-chart/test/line-chart.test.tsx +74 -3
  47. package/src/charts/line-chart/test/tick-placement.test.tsx +84 -0
  48. package/src/charts/line-chart/test/tooltip-resolution.test.tsx +140 -0
  49. package/src/charts/line-chart/types.ts +4 -1
  50. package/src/charts/pie-chart/pie-chart.module.scss +13 -1
  51. package/src/charts/pie-chart/pie-chart.tsx +30 -40
  52. package/src/charts/pie-chart/test/pie-chart.test.tsx +56 -0
  53. package/src/charts/pie-semi-circle-chart/pie-semi-circle-chart.module.scss +8 -3
  54. package/src/charts/pie-semi-circle-chart/pie-semi-circle-chart.tsx +23 -37
  55. package/src/charts/pie-semi-circle-chart/test/pie-semi-circle-chart.test.tsx +50 -0
  56. package/src/charts/private/chart-layout/chart-layout.module.scss +3 -2
  57. package/src/charts/private/svg-empty-state/svg-empty-state.module.scss +1 -1
  58. package/src/charts/private/test/axis-formatting.test.tsx +85 -0
  59. package/src/charts/private/test/time-axis-formatting.test.ts +109 -0
  60. package/src/charts/private/test/time-axis-options.test.ts +267 -0
  61. package/src/charts/private/test/time-axis-tick-invariants.test.ts +153 -0
  62. package/src/charts/private/test/time-axis-tick-values.test.ts +280 -0
  63. package/src/charts/private/test/time-axis.test.ts +9 -69
  64. package/src/charts/private/time-axis-options.ts +106 -0
  65. package/src/charts/private/time-axis.ts +318 -136
  66. package/src/charts/private/x-zoom/x-zoom.module.scss +3 -3
  67. package/src/charts/private/xy-plot/xy-plot.module.scss +8 -0
  68. package/src/charts/sparkline/sparkline.tsx +9 -3
  69. package/src/charts/sparkline/test/sparkline.test.tsx +29 -56
  70. package/src/charts/sparkline/types.ts +2 -2
  71. package/src/components/legend/private/base-legend.module.scss +9 -5
  72. package/src/components/tooltip/accessible-tooltip.tsx +38 -10
  73. package/src/components/tooltip/base-tooltip.module.scss +3 -3
  74. package/src/components/tooltip/private/bounded-tooltip.tsx +172 -0
  75. package/src/components/tooltip/test/accessible-tooltip.test.tsx +67 -0
  76. package/src/components/tooltip/test/bounded-tooltip.test.tsx +129 -0
  77. package/src/components/tooltip/test/xy-chart-tooltip.test.tsx +279 -0
  78. package/src/components/tooltip/xy-chart-tooltip.tsx +288 -0
  79. package/src/components/trend-indicator/trend-indicator.module.scss +2 -2
  80. package/src/hooks/test/use-xychart-theme-style-reads.test.tsx +17 -10
  81. package/src/hooks/test/use-xychart-theme.test.tsx +63 -5
  82. package/src/hooks/use-xychart-theme.ts +33 -23
  83. package/src/index.ts +4 -0
  84. package/src/providers/chart-context/global-charts-provider.tsx +64 -54
  85. package/src/providers/chart-context/hooks/use-chart-formatting.ts +22 -0
  86. package/src/providers/chart-context/index.ts +1 -0
  87. package/src/providers/chart-context/private/catalog-pointers.ts +42 -0
  88. package/src/providers/chart-context/private/series-palette.ts +35 -0
  89. package/src/providers/chart-context/test/catalog-pointers.test.ts +61 -0
  90. package/src/providers/chart-context/test/chart-context.test.tsx +177 -792
  91. package/src/providers/chart-context/test/use-global-charts-theme.test.tsx +7 -18
  92. package/src/providers/chart-context/themes.ts +7 -47
  93. package/src/providers/chart-context/types.ts +3 -0
  94. package/src/styles/chart-scope.scss +63 -58
  95. package/src/styles/test/chart-scope.test.ts +25 -40
  96. package/src/test-utils/series-fixtures.ts +31 -0
  97. package/src/types.ts +103 -73
  98. package/src/utils/bucket-info.ts +87 -0
  99. package/src/utils/color-utils.ts +0 -27
  100. package/src/utils/date-formatting.ts +174 -0
  101. package/src/utils/get-styles.ts +7 -1
  102. package/src/utils/index.ts +3 -0
  103. package/src/utils/test/bucket-info.test.ts +185 -0
  104. package/src/utils/test/color-utils.test.ts +1 -48
  105. package/src/utils/test/date-formatting.test.ts +89 -0
  106. package/src/utils/test/get-styles.test.ts +10 -7
  107. package/src/utils/test/merge-themes.test.ts +18 -46
  108. package/src/visx/types.ts +29 -2
  109. package/src/hooks/test/use-xychart-theme-provider-override.test.tsx +0 -28
  110. package/src/providers/chart-context/private/theme-override-vars.ts +0 -87
  111. package/src/providers/chart-context/private/with-catalog-pointers.ts +0 -50
  112. package/src/providers/chart-context/test/sparse-theme.test.ts +0 -35
  113. package/src/providers/chart-context/test/theme-override-vars-guard.test.tsx +0 -29
  114. package/src/providers/chart-context/test/theme-override-vars.test.ts +0 -118
  115. package/src/providers/chart-context/test/with-catalog-pointers.test.ts +0 -47
  116. package/src/styles/test/wpds-fallbacks.test.ts +0 -131
@@ -0,0 +1,129 @@
1
+ import { render, screen } from '@testing-library/react';
2
+ import { BoundedTooltip, getBoundedPosition } from '../private/bounded-tooltip';
3
+
4
+ const BOX = { width: 208, height: 36 };
5
+ const OFFSETS = { offsetLeft: 10, offsetTop: 10 };
6
+
7
+ describe( 'getBoundedPosition', () => {
8
+ test( 'sits below and to the right of the anchor when that fits', () => {
9
+ expect(
10
+ getBoundedPosition( {
11
+ left: 40,
12
+ top: 20,
13
+ ...OFFSETS,
14
+ box: BOX,
15
+ bounds: { left: 0, top: 0, right: 600, bottom: 300 },
16
+ } )
17
+ ).toEqual( { x: 50, y: 30 } );
18
+ } );
19
+
20
+ test( 'flips to the other side when the near side clips more', () => {
21
+ expect(
22
+ getBoundedPosition( {
23
+ left: 500,
24
+ top: 280,
25
+ ...OFFSETS,
26
+ box: BOX,
27
+ bounds: { left: 0, top: 0, right: 600, bottom: 300 },
28
+ } )
29
+ ).toEqual( { x: 282, y: 234 } );
30
+ } );
31
+
32
+ test( 'pins the box to the edge when neither side fits, instead of spilling', () => {
33
+ // A 208px box mid-way across a 346px plot: neither side fits, so the box
34
+ // keeps the near side and parks at the right edge instead of spilling.
35
+ expect(
36
+ getBoundedPosition( {
37
+ left: 173,
38
+ top: 50,
39
+ ...OFFSETS,
40
+ box: BOX,
41
+ bounds: { left: 0, top: 0, right: 346, bottom: 200 },
42
+ } )
43
+ ).toEqual( { x: 138, y: 60 } );
44
+ } );
45
+
46
+ test( 'may leave the wrapper as long as it stays inside the clipping ancestor', () => {
47
+ // The wrapper starts 40px inside its clipping card, so the box can reach
48
+ // 40px past the wrapper's left edge, and stops there.
49
+ expect(
50
+ getBoundedPosition( {
51
+ left: 173,
52
+ top: 50,
53
+ ...OFFSETS,
54
+ box: BOX,
55
+ bounds: { left: -40, top: -60, right: 360, bottom: 200 },
56
+ } )
57
+ ).toEqual( { x: -40, y: 60 } );
58
+ } );
59
+ } );
60
+
61
+ const rect = ( left: number, top: number, width: number, height: number ) =>
62
+ ( {
63
+ left,
64
+ top,
65
+ width,
66
+ height,
67
+ right: left + width,
68
+ bottom: top + height,
69
+ x: left,
70
+ y: top,
71
+ toJSON: () => ( {} ),
72
+ } ) as DOMRect;
73
+
74
+ describe( 'BoundedTooltip', () => {
75
+ afterEach( () => {
76
+ jest.restoreAllMocks();
77
+ } );
78
+
79
+ test( 'keeps the box inside an overflow-hidden ancestor that is wider than the wrapper', () => {
80
+ // Card at x=400 (400 wide, overflow hidden); plot wrapper at x=440, 346 wide.
81
+ jest.spyOn( Element.prototype, 'getBoundingClientRect' ).mockImplementation( function (
82
+ this: Element
83
+ ) {
84
+ if ( this.classList.contains( 'visx-tooltip' ) ) {
85
+ return rect( 0, 0, 208, 36 );
86
+ }
87
+ if ( this.getAttribute( 'data-testid' ) === 'card' ) {
88
+ return rect( 400, 100, 400, 400 );
89
+ }
90
+ return rect( 440, 160, 346, 200 );
91
+ } );
92
+
93
+ render(
94
+ <div data-testid="card" style={ { overflow: 'hidden' } }>
95
+ <div style={ { position: 'relative' } }>
96
+ <BoundedTooltip left={ 173 } top={ 50 } data-testid="box">
97
+ content
98
+ </BoundedTooltip>
99
+ </div>
100
+ </div>
101
+ );
102
+
103
+ // Neither side of the anchor fits inside the card, so the box stops at
104
+ // the card's left edge, 40px outside the wrapper.
105
+ expect( screen.getByTestId( 'box' ) ).toHaveStyle( { transform: 'translate(-40px, 60px)' } );
106
+ } );
107
+
108
+ test( 'falls back to the viewport when nothing clips', () => {
109
+ jest.spyOn( Element.prototype, 'getBoundingClientRect' ).mockImplementation( function (
110
+ this: Element
111
+ ) {
112
+ return this.classList.contains( 'visx-tooltip' )
113
+ ? rect( 0, 0, 208, 36 )
114
+ : rect( 900, 0, 346, 200 );
115
+ } );
116
+
117
+ render(
118
+ <div style={ { position: 'relative' } }>
119
+ <BoundedTooltip left={ 173 } top={ 50 } data-testid="box">
120
+ content
121
+ </BoundedTooltip>
122
+ </div>
123
+ );
124
+
125
+ // JSDOM's viewport is 1024px wide and the wrapper starts at 900: the box
126
+ // flips left of the anchor and stops at the viewport's right edge.
127
+ expect( screen.getByTestId( 'box' ) ).toHaveStyle( { transform: 'translate(-84px, 60px)' } );
128
+ } );
129
+ } );
@@ -0,0 +1,279 @@
1
+ import { render, screen, waitFor } from '@testing-library/react';
2
+ import { BarSeries, LineSeries, TooltipContext, XYChart } from '@visx/xychart';
3
+ import { useContext, useEffect } from 'react';
4
+ import { XyChartTooltip } from '../xy-chart-tooltip';
5
+ import type { XyChartTooltipProps } from '../../../visx/types';
6
+ import type { EventHandlerParams } from '@visx/xychart';
7
+
8
+ type Datum = { x: number; y: number };
9
+
10
+ const SERIES_A: Datum[] = [
11
+ { x: 0, y: 0 },
12
+ { x: 5, y: 50 },
13
+ { x: 10, y: 100 },
14
+ ];
15
+ const SERIES_B: Datum[] = [
16
+ { x: 0, y: 100 },
17
+ { x: 5, y: 25 },
18
+ { x: 10, y: 0 },
19
+ ];
20
+
21
+ const xAccessor = ( d: Datum ) => d.x;
22
+ const yAccessor = ( d: Datum ) => d.y;
23
+
24
+ type BandDatum = { x: string; y: number };
25
+ const BAND_DATA: BandDatum[] = [
26
+ { x: 'a', y: 50 },
27
+ { x: 'b', y: 80 },
28
+ ];
29
+ const bandXAccessor = ( d: BandDatum ) => d.x;
30
+ const bandYAccessor = ( d: BandDatum ) => d.y;
31
+ const renderBarTooltip = () => <span>bar</span>;
32
+ const BAND_PARAMS = [
33
+ { key: 'bars', index: 0, datum: BAND_DATA[ 0 ], svgPoint: { x: 20, y: 90 } },
34
+ ] as unknown as EventHandlerParams< Datum >[];
35
+
36
+ // The pointer sits on A's datum, 25px above B's, so A is the nearest datum.
37
+ const BOTH_SERIES: EventHandlerParams< Datum >[] = [
38
+ {
39
+ key: 'A',
40
+ index: 1,
41
+ datum: SERIES_A[ 1 ],
42
+ svgPoint: { x: 100, y: 50 },
43
+ distanceX: 0,
44
+ distanceY: 0,
45
+ },
46
+ {
47
+ key: 'B',
48
+ index: 1,
49
+ datum: SERIES_B[ 1 ],
50
+ svgPoint: { x: 100, y: 50 },
51
+ distanceX: 0,
52
+ distanceY: 25,
53
+ },
54
+ ];
55
+
56
+ // Opens the tooltip the way a pointer event would, once the series have registered.
57
+ const OpenTooltip = ( { params }: { params: EventHandlerParams< Datum >[] } ) => {
58
+ const tooltipContext = useContext( TooltipContext );
59
+ useEffect( () => {
60
+ params.forEach( p => tooltipContext?.showTooltip( p ) );
61
+ // The context object changes identity on every tooltip update; re-running would loop.
62
+ // eslint-disable-next-line react-hooks/exhaustive-deps
63
+ }, [] );
64
+ return null;
65
+ };
66
+
67
+ const renderTooltip: XyChartTooltipProps< Datum >[ 'renderTooltip' ] = ( { tooltipData } ) => (
68
+ <span>{ `nearest:${ tooltipData?.nearestDatum?.key ?? '' }` }</span>
69
+ );
70
+
71
+ const NO_MARGIN = { top: 0, right: 0, bottom: 0, left: 0 };
72
+
73
+ // 200x100 chart; with no margin x maps 0..10 to 0..200px and y maps 0..100 to 100..0px.
74
+ const renderChart = (
75
+ tooltipProps: Partial< XyChartTooltipProps< Datum > > = {},
76
+ params: EventHandlerParams< Datum >[] = [
77
+ { key: 'A', index: 1, datum: SERIES_A[ 1 ], svgPoint: { x: 123, y: 45 } },
78
+ ],
79
+ margin = NO_MARGIN
80
+ ) =>
81
+ render(
82
+ <div data-testid="wrapper" style={ { position: 'relative' } }>
83
+ <XYChart
84
+ width={ 200 }
85
+ height={ 100 }
86
+ margin={ margin }
87
+ xScale={ { type: 'linear', domain: [ 0, 10 ] } }
88
+ yScale={ { type: 'linear', domain: [ 0, 100 ] } }
89
+ >
90
+ <LineSeries dataKey="A" data={ SERIES_A } xAccessor={ xAccessor } yAccessor={ yAccessor } />
91
+ <LineSeries dataKey="B" data={ SERIES_B } xAccessor={ xAccessor } yAccessor={ yAccessor } />
92
+ <XyChartTooltip
93
+ renderTooltip={ renderTooltip }
94
+ data-testid="tooltip-box"
95
+ { ...tooltipProps }
96
+ />
97
+ <OpenTooltip params={ params } />
98
+ </XYChart>
99
+ </div>
100
+ );
101
+
102
+ describe( 'XyChartTooltip', () => {
103
+ test( 'renders the tooltip box inside the chart wrapper, not in a body-level portal', async () => {
104
+ const { container } = renderChart();
105
+
106
+ const box = await screen.findByTestId( 'tooltip-box' );
107
+
108
+ expect( box ).toHaveTextContent( 'nearest:A' );
109
+ expect( screen.getByTestId( 'wrapper' ) ).toContainElement( box );
110
+ // A body-level portal would sit outside the render root.
111
+ expect( container ).toContainElement( box );
112
+ } );
113
+
114
+ test( 'places the box at the pointer position when snapping is off', async () => {
115
+ renderChart();
116
+
117
+ // TooltipWithBounds moves the box through a transform from (0, 0), after its 10px offsets.
118
+ await expect( screen.findByTestId( 'tooltip-box' ) ).resolves.toHaveStyle( {
119
+ transform: 'translate(133px, 55px)',
120
+ } );
121
+ } );
122
+
123
+ test( 'snaps the box to the nearest datum', async () => {
124
+ renderChart( { snapTooltipToDatumX: true, snapTooltipToDatumY: true } );
125
+
126
+ // Datum (5, 50) sits at (100px, 50px); the default 10px offsets apply after snapping.
127
+ await waitFor( () =>
128
+ expect( screen.getByTestId( 'tooltip-box' ) ).toHaveStyle( {
129
+ transform: 'translate(110px, 60px)',
130
+ } )
131
+ );
132
+ } );
133
+
134
+ test( 'draws one glyph per series in the SVG at the datum position', async () => {
135
+ renderChart( { showSeriesGlyphs: true }, BOTH_SERIES );
136
+
137
+ const glyphA = await screen.findByTestId( 'xy-chart-tooltip-glyph-A' );
138
+ const glyphB = await screen.findByTestId( 'xy-chart-tooltip-glyph-B' );
139
+
140
+ // The group is placed at the datum; the glyph itself draws at the origin.
141
+ expect( screen.getByTestId( 'xy-chart-tooltip-glyph-group-A' ) ).toHaveAttribute(
142
+ 'transform',
143
+ 'translate(100, 50)'
144
+ );
145
+ expect( screen.getByTestId( 'xy-chart-tooltip-glyph-group-B' ) ).toHaveAttribute(
146
+ 'transform',
147
+ 'translate(100, 75)'
148
+ );
149
+ expect( glyphA ).toHaveAttribute( 'cx', '0' );
150
+ expect( glyphA ).toHaveAttribute( 'cy', '0' );
151
+ expect( glyphB ).toHaveAttribute( 'cx', '0' );
152
+ expect( glyphB ).toHaveAttribute( 'cy', '0' );
153
+ // Drawn into the chart SVG, not portaled out as HTML.
154
+ expect( glyphA ).toBeInstanceOf( SVGElement );
155
+ } );
156
+
157
+ test( 'hands the glyph renderer the origin, like visx, and marks the nearest datum', async () => {
158
+ const renderGlyph = jest.fn( () => null );
159
+ renderChart( { showSeriesGlyphs: true, renderGlyph }, BOTH_SERIES );
160
+
161
+ await waitFor( () => expect( renderGlyph ).toHaveBeenCalledTimes( 2 ) );
162
+
163
+ const calls = renderGlyph.mock.calls as unknown as Array<
164
+ [ { key: string; x: number; y: number; isNearestDatum: boolean; datum: Datum } ]
165
+ >;
166
+ const byKey = Object.fromEntries( calls.map( ( [ p ] ) => [ p.key, p ] ) );
167
+ // A renderer written against visx's Tooltip ignores x and y, so both stay 0.
168
+ expect( byKey.A ).toMatchObject( { x: 0, y: 0, datum: SERIES_A[ 1 ] } );
169
+ expect( byKey.B ).toMatchObject( { x: 0, y: 0, datum: SERIES_B[ 1 ] } );
170
+ expect( byKey.A.isNearestDatum ).toBe( true );
171
+ expect( byKey.B.isNearestDatum ).toBe( false );
172
+ } );
173
+
174
+ test( 'centres a band-scale datum in its band', async () => {
175
+ render(
176
+ <div style={ { position: 'relative' } }>
177
+ <XYChart
178
+ width={ 200 }
179
+ height={ 100 }
180
+ margin={ NO_MARGIN }
181
+ xScale={ { type: 'band', domain: [ 'a', 'b' ], paddingInner: 0, paddingOuter: 0 } }
182
+ yScale={ { type: 'linear', domain: [ 0, 100 ] } }
183
+ >
184
+ <BarSeries
185
+ dataKey="bars"
186
+ data={ BAND_DATA }
187
+ xAccessor={ bandXAccessor }
188
+ yAccessor={ bandYAccessor }
189
+ />
190
+ <XyChartTooltip
191
+ renderTooltip={ renderBarTooltip }
192
+ data-testid="tooltip-box"
193
+ snapTooltipToDatumX
194
+ snapTooltipToDatumY
195
+ showDatumGlyph
196
+ />
197
+ <OpenTooltip params={ BAND_PARAMS } />
198
+ </XYChart>
199
+ </div>
200
+ );
201
+
202
+ // Band 'a' spans 0..100px; the datum sits at its centre, 50px, not its start.
203
+ await waitFor( () =>
204
+ expect( screen.getByTestId( 'xy-chart-tooltip-glyph-group-bars' ) ).toHaveAttribute(
205
+ 'transform',
206
+ 'translate(50, 50)'
207
+ )
208
+ );
209
+ expect( screen.getByTestId( 'tooltip-box' ) ).toHaveStyle( {
210
+ transform: 'translate(60px, 60px)',
211
+ } );
212
+ } );
213
+
214
+ test( 'draws the crosshairs across the plot area, inside the margins, at the snapped position', async () => {
215
+ renderChart(
216
+ {
217
+ snapTooltipToDatumX: true,
218
+ snapTooltipToDatumY: true,
219
+ showVerticalCrosshair: true,
220
+ showHorizontalCrosshair: true,
221
+ },
222
+ undefined,
223
+ { top: 10, right: 20, bottom: 30, left: 40 }
224
+ );
225
+
226
+ const vertical = await screen.findByTestId( 'xy-chart-tooltip-crosshair-vertical' );
227
+ const horizontal = await screen.findByTestId( 'xy-chart-tooltip-crosshair-horizontal' );
228
+
229
+ // Plot area is 140x60 at (40, 10); datum (5, 50) sits at its centre, (110, 40).
230
+ expect( vertical ).toHaveAttribute( 'x1', '110' );
231
+ expect( vertical ).toHaveAttribute( 'y1', '10' );
232
+ expect( vertical ).toHaveAttribute( 'y2', '70' );
233
+ expect( horizontal ).toHaveAttribute( 'y1', '40' );
234
+ expect( horizontal ).toHaveAttribute( 'x1', '40' );
235
+ expect( horizontal ).toHaveAttribute( 'x2', '180' );
236
+ } );
237
+
238
+ test( 'stacks the box above the chart overlays, with zIndex as the override', async () => {
239
+ const { unmount } = renderChart();
240
+ await expect( screen.findByTestId( 'tooltip-box' ) ).resolves.toHaveStyle( { zIndex: '3' } );
241
+ unmount();
242
+
243
+ renderChart( { zIndex: 9 } );
244
+ await expect( screen.findByTestId( 'tooltip-box' ) ).resolves.toHaveStyle( { zIndex: '9' } );
245
+ } );
246
+
247
+ test( 'accepts the portal-era options without passing them to the box', async () => {
248
+ renderChart( { scroll: true, debounce: 50, resizeObserverPolyfill: undefined } );
249
+
250
+ const box = await screen.findByTestId( 'tooltip-box' );
251
+
252
+ expect( box ).not.toHaveAttribute( 'scroll' );
253
+ expect( box ).not.toHaveAttribute( 'debounce' );
254
+ } );
255
+
256
+ test( 'renders no box when there is no tooltip renderer', async () => {
257
+ renderChart( { renderTooltip: undefined } );
258
+
259
+ await expect( screen.findByTestId( 'xy-chart-tooltip-anchor' ) ).resolves.toBeInTheDocument();
260
+ expect( screen.queryByTestId( 'tooltip-box' ) ).not.toBeInTheDocument();
261
+ } );
262
+
263
+ test( 'renders nothing when the tooltip content is empty', async () => {
264
+ renderChart( { renderTooltip: () => null, showSeriesGlyphs: true }, BOTH_SERIES );
265
+
266
+ // The anchor is the only thing rendered while there is nothing to show.
267
+ await expect( screen.findByTestId( 'xy-chart-tooltip-anchor' ) ).resolves.toBeInTheDocument();
268
+ expect( screen.queryByTestId( 'tooltip-box' ) ).not.toBeInTheDocument();
269
+ expect( screen.queryByTestId( 'xy-chart-tooltip-glyph-A' ) ).not.toBeInTheDocument();
270
+ } );
271
+
272
+ test( 'renders nothing while the tooltip is closed', () => {
273
+ renderChart( { showSeriesGlyphs: true, showVerticalCrosshair: true }, [] );
274
+
275
+ expect( screen.queryByTestId( 'tooltip-box' ) ).not.toBeInTheDocument();
276
+ expect( screen.queryByTestId( 'xy-chart-tooltip-glyph-A' ) ).not.toBeInTheDocument();
277
+ expect( screen.queryByTestId( 'xy-chart-tooltip-crosshair-vertical' ) ).not.toBeInTheDocument();
278
+ } );
279
+ } );
@@ -0,0 +1,288 @@
1
+ import { Tooltip, defaultStyles } from '@visx/tooltip';
2
+ import { DataContext, TooltipContext } from '@visx/xychart';
3
+ import { useCallback, useContext, useState } from 'react';
4
+ import { createPortal } from 'react-dom';
5
+ import { BoundedTooltip } from './private/bounded-tooltip';
6
+ import type { RenderTooltipGlyphProps, XyChartTooltipProps } from '../../visx/types';
7
+ import type { TooltipContextType } from '@visx/xychart';
8
+ import type { CSSProperties, ReactNode } from 'react';
9
+
10
+ const CROSSHAIR_STROKE_WIDTH = 1.5;
11
+ const DEFAULT_GLYPH_RADIUS = 4;
12
+ const FALLBACK_COLOR = '#222';
13
+ // Above the chart's own overlays (the zoom reset button sits at 2). The chart
14
+ // wrapper isolates its stacking context, so this never competes with page chrome.
15
+ const DEFAULT_TOOLTIP_Z_INDEX = 3;
16
+ // Portal-era options, accepted for compatibility and dropped before the box
17
+ // renders: it sits in the chart wrapper and moves with it, and nothing
18
+ // measures it any more.
19
+ const PORTAL_OPTIONS = new Set( [ 'scroll', 'debounce', 'resizeObserverPolyfill' ] );
20
+
21
+ type ScaleFn = ( value: unknown ) => number | undefined;
22
+
23
+ const isValidNumber = ( value: unknown ): value is number =>
24
+ typeof value === 'number' && Number.isFinite( value );
25
+
26
+ // Band scales place a datum at the start of its band; snapping targets the band's centre.
27
+ // A d3 scale is a function that carries `bandwidth` as a property.
28
+ const scaleBandwidth = ( scale: unknown ): number => {
29
+ const bandwidth = ( scale as { bandwidth?: unknown } | null | undefined )?.bandwidth;
30
+ return typeof bandwidth === 'function' ? Number( bandwidth() ) : 0;
31
+ };
32
+
33
+ const DefaultGlyph = < Datum extends object >( {
34
+ x,
35
+ y,
36
+ size,
37
+ color,
38
+ glyphStyle,
39
+ seriesKey,
40
+ }: Omit< RenderTooltipGlyphProps< Datum >, 'key' > & { seriesKey: string } ) => {
41
+ const { theme } = useContext( DataContext ) || {};
42
+
43
+ return (
44
+ <circle
45
+ data-testid={ `xy-chart-tooltip-glyph-${ seriesKey }` }
46
+ cx={ x }
47
+ cy={ y }
48
+ r={ size }
49
+ fill={ color }
50
+ stroke={ theme?.backgroundColor }
51
+ strokeWidth={ CROSSHAIR_STROKE_WIDTH }
52
+ paintOrder="fill"
53
+ { ...glyphStyle }
54
+ />
55
+ );
56
+ };
57
+
58
+ const defaultRenderGlyph = < Datum extends object >( {
59
+ key,
60
+ ...props
61
+ }: RenderTooltipGlyphProps< Datum > ) => (
62
+ <DefaultGlyph key={ key } seriesKey={ key } { ...props } />
63
+ );
64
+
65
+ type XyChartTooltipContentProps< Datum extends object > = XyChartTooltipProps< Datum > & {
66
+ tooltipContext: TooltipContextType< Datum >;
67
+ /** The SVG's HTML parent once found. The box waits for it; the SVG overlay does not. */
68
+ container: HTMLElement | null;
69
+ };
70
+
71
+ const XyChartTooltipContent = < Datum extends object >( {
72
+ tooltipContext,
73
+ container,
74
+ renderTooltip,
75
+ renderGlyph = defaultRenderGlyph,
76
+ glyphStyle,
77
+ snapTooltipToDatumX = false,
78
+ snapTooltipToDatumY = false,
79
+ showVerticalCrosshair = false,
80
+ showHorizontalCrosshair = false,
81
+ showDatumGlyph = false,
82
+ showSeriesGlyphs = false,
83
+ verticalCrosshairStyle,
84
+ horizontalCrosshairStyle,
85
+ detectBounds = true,
86
+ zIndex = DEFAULT_TOOLTIP_Z_INDEX,
87
+ ...rest
88
+ }: XyChartTooltipContentProps< Datum > ) => {
89
+ const tooltipProps = Object.fromEntries(
90
+ Object.entries( rest ).filter( ( [ key ] ) => ! PORTAL_OPTIONS.has( key ) )
91
+ ) as Omit< typeof rest, 'scroll' | 'debounce' | 'resizeObserverPolyfill' >;
92
+ const {
93
+ colorScale,
94
+ theme,
95
+ innerHeight = 0,
96
+ innerWidth = 0,
97
+ margin,
98
+ xScale,
99
+ yScale,
100
+ dataRegistry,
101
+ } = useContext( DataContext ) || {};
102
+
103
+ const tooltipContent = renderTooltip ? renderTooltip( { ...tooltipContext, colorScale } ) : null;
104
+ if ( tooltipContent == null ) {
105
+ return null;
106
+ }
107
+
108
+ const scaleX = xScale as ScaleFn | undefined;
109
+ const scaleY = yScale as ScaleFn | undefined;
110
+
111
+ // Datum position in SVG coordinates, from the accessors the series registered.
112
+ const getDatumLeftTop = ( key: string, datum: Datum ) => {
113
+ const entry = dataRegistry?.get( key );
114
+ const left =
115
+ scaleX && entry?.xAccessor
116
+ ? Number( scaleX( entry.xAccessor( datum ) ) ) + scaleBandwidth( xScale ) / 2
117
+ : undefined;
118
+ const top =
119
+ scaleY && entry?.yAccessor
120
+ ? Number( scaleY( entry.yAccessor( datum ) ) ) + scaleBandwidth( yScale ) / 2
121
+ : undefined;
122
+ return { left, top };
123
+ };
124
+
125
+ const nearestDatum = tooltipContext.tooltipData?.nearestDatum;
126
+ let { tooltipLeft, tooltipTop } = tooltipContext;
127
+
128
+ if ( nearestDatum && ( snapTooltipToDatumX || snapTooltipToDatumY ) ) {
129
+ const { left, top } = getDatumLeftTop( nearestDatum.key, nearestDatum.datum );
130
+ if ( snapTooltipToDatumX && isValidNumber( left ) ) {
131
+ tooltipLeft = left;
132
+ }
133
+ if ( snapTooltipToDatumY && isValidNumber( top ) ) {
134
+ tooltipTop = top;
135
+ }
136
+ }
137
+
138
+ const glyphs: ReactNode[] = [];
139
+ if ( showDatumGlyph || showSeriesGlyphs ) {
140
+ const size = Number( glyphStyle?.radius ?? DEFAULT_GLYPH_RADIUS );
141
+ const labelColor = theme?.htmlLabel?.color ?? FALLBACK_COLOR;
142
+ // visx colours a lone nearest-datum glyph like the gridlines and series glyphs like labels.
143
+ const fallbackColor = showSeriesGlyphs ? labelColor : theme?.gridStyles?.stroke ?? labelColor;
144
+ let entries: Array< { key: string; datum: Datum; index: number } > = [];
145
+ if ( showSeriesGlyphs ) {
146
+ entries = Object.values( tooltipContext.tooltipData?.datumByKey ?? {} );
147
+ } else if ( nearestDatum ) {
148
+ entries = [ nearestDatum ];
149
+ }
150
+
151
+ for ( const { key, datum, index } of entries ) {
152
+ const { left, top } = getDatumLeftTop( key, datum );
153
+ if ( ! isValidNumber( left ) || ! isValidNumber( top ) ) {
154
+ continue;
155
+ }
156
+ // The group carries the position and the renderer draws at the origin,
157
+ // as with visx's portal: a renderer that ignores `x` and `y` still lands
158
+ // on its datum.
159
+ glyphs.push(
160
+ <g
161
+ key={ key }
162
+ className="visx-tooltip-glyph"
163
+ data-testid={ `xy-chart-tooltip-glyph-group-${ key }` }
164
+ transform={ `translate(${ left }, ${ top })` }
165
+ >
166
+ { renderGlyph( {
167
+ key,
168
+ color: colorScale?.( key ) ?? fallbackColor,
169
+ datum,
170
+ index,
171
+ size,
172
+ x: 0,
173
+ y: 0,
174
+ glyphStyle,
175
+ isNearestDatum: nearestDatum?.key === key,
176
+ } ) }
177
+ </g>
178
+ );
179
+ }
180
+ }
181
+
182
+ const crosshairStroke = theme?.gridStyles?.stroke ?? theme?.htmlLabel?.color ?? FALLBACK_COLOR;
183
+ const marginTop = margin?.top ?? 0;
184
+ const marginLeft = margin?.left ?? 0;
185
+
186
+ const TooltipComponent = detectBounds ? BoundedTooltip : Tooltip;
187
+ const boxStyle: CSSProperties = {
188
+ ...defaultStyles,
189
+ zIndex,
190
+ background: theme?.backgroundColor ?? 'white',
191
+ boxShadow: `0 1px 2px ${
192
+ theme?.htmlLabel?.color ? `${ theme.htmlLabel.color }55` : '#22222255'
193
+ }`,
194
+ ...theme?.htmlLabel,
195
+ };
196
+
197
+ return (
198
+ <>
199
+ <g className="visx-tooltip-overlay" pointerEvents="none">
200
+ { showVerticalCrosshair && isValidNumber( tooltipLeft ) && (
201
+ <line
202
+ className="visx-crosshair visx-crosshair-vertical"
203
+ data-testid="xy-chart-tooltip-crosshair-vertical"
204
+ x1={ tooltipLeft }
205
+ x2={ tooltipLeft }
206
+ y1={ marginTop }
207
+ y2={ marginTop + innerHeight }
208
+ stroke={ crosshairStroke }
209
+ strokeWidth={ CROSSHAIR_STROKE_WIDTH }
210
+ { ...verticalCrosshairStyle }
211
+ />
212
+ ) }
213
+ { showHorizontalCrosshair && isValidNumber( tooltipTop ) && (
214
+ <line
215
+ className="visx-crosshair visx-crosshair-horizontal"
216
+ data-testid="xy-chart-tooltip-crosshair-horizontal"
217
+ x1={ marginLeft }
218
+ x2={ marginLeft + innerWidth }
219
+ y1={ tooltipTop }
220
+ y2={ tooltipTop }
221
+ stroke={ crosshairStroke }
222
+ strokeWidth={ CROSSHAIR_STROKE_WIDTH }
223
+ { ...horizontalCrosshairStyle }
224
+ />
225
+ ) }
226
+ { glyphs }
227
+ </g>
228
+ { container &&
229
+ createPortal(
230
+ <TooltipComponent
231
+ left={ tooltipLeft }
232
+ top={ tooltipTop }
233
+ style={ boxStyle }
234
+ applyPositionStyle
235
+ { ...tooltipProps }
236
+ >
237
+ { tooltipContent }
238
+ </TooltipComponent>,
239
+ container
240
+ ) }
241
+ </>
242
+ );
243
+ };
244
+
245
+ /**
246
+ * In-tree replacement for `@visx/xychart`'s `Tooltip`.
247
+ *
248
+ * visx renders its tooltip box, glyphs and crosshairs through portals appended
249
+ * to `document.body`. That puts them in the page's root stacking context, where
250
+ * they paint above every sticky or fixed element that lives inside a nested
251
+ * stacking context, and no z-index on that element can change the order. This
252
+ * component draws the glyphs and crosshairs straight into the chart SVG and
253
+ * renders the tooltip box into the SVG's parent element, so all of them stack
254
+ * as ordinary descendants of the chart.
255
+ *
256
+ * Render it as a child of `XYChart`. The element wrapping that `XYChart` must
257
+ * be `position: relative` with the SVG at its origin, and `isolation: isolate`:
258
+ * the box is placed with the SVG-local coordinates visx reports, and the
259
+ * isolation keeps the box's `zIndex` from competing with page chrome. The box
260
+ * flips and clamps to stay inside the nearest ancestor that clips its overflow
261
+ * (or the viewport), so it may extend past the chart wrapper but is never cut
262
+ * off unless that ancestor is smaller than the box itself.
263
+ *
264
+ * @param props - visx's `Tooltip` options. `scroll`, `debounce` and `resizeObserverPolyfill` are accepted and ignored.
265
+ * @return An anchor in the SVG, plus the overlay and the tooltip box while the tooltip is open.
266
+ */
267
+ export const XyChartTooltip = < Datum extends object >( props: XyChartTooltipProps< Datum > ) => {
268
+ const tooltipContext = useContext( TooltipContext ) as TooltipContextType< Datum > | null;
269
+ const [ container, setContainer ] = useState< HTMLElement | null >( null );
270
+
271
+ // Children of XYChart render inside its SVG, and the box needs an HTML parent.
272
+ const anchorRef = useCallback( ( node: SVGGElement | null ) => {
273
+ setContainer( node?.ownerSVGElement?.parentElement ?? null );
274
+ }, [] );
275
+
276
+ return (
277
+ <>
278
+ <g ref={ anchorRef } data-testid="xy-chart-tooltip-anchor" />
279
+ { tooltipContext?.tooltipOpen && (
280
+ <XyChartTooltipContent
281
+ { ...props }
282
+ tooltipContext={ tooltipContext }
283
+ container={ container }
284
+ />
285
+ ) }
286
+ </>
287
+ );
288
+ };