Skip to content

Commit 5eeb07f

Browse files
[charts] Move Voronoi handler in a dedicated plugin (#16470)
1 parent 2372cf8 commit 5eeb07f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+400
-276
lines changed

docs/data/charts/scatter-demo/scatter-demo.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Charts - Scatter demonstration
33
productId: x-charts
4-
components: Scatter, ScatterChart, ScatterPlot, ChartsVoronoiHandler
4+
components: Scatter, ScatterChart, ScatterPlot
55
---
66

77
# Charts - Scatter demonstration

docs/data/charts/scatter/scatter.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: React Scatter chart
33
productId: x-charts
4-
components: ScatterChart, ScatterChartPro, ScatterPlot, ChartsVoronoiHandler, ChartsGrid
4+
components: ScatterChart, ScatterChartPro, ScatterPlot, ChartsGrid
55
---
66

77
# Charts - Scatter
@@ -40,12 +40,6 @@ Or set the `disableVoronoi` prop to `true` to trigger interactions only when hov
4040

4141
{{"demo": "VoronoiInteraction.js"}}
4242

43-
To use this feature with composition, add the `ChartsVoronoiHandler`.
44-
45-
```jsx
46-
<ChartsVoronoiHandler voronoiMaxRadius={50} />
47-
```
48-
4943
## Click event
5044

5145
Scatter Chart provides an `onItemClick` handler for handling clicks on specific scatter items.

docs/data/chartsApiPages.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,6 @@ const chartsApiPages: MuiPage[] = [
104104
pathname: '/x/api/charts/charts-tooltip-container',
105105
title: 'ChartsTooltipContainer',
106106
},
107-
{
108-
pathname: '/x/api/charts/charts-voronoi-handler',
109-
title: 'ChartsVoronoiHandler',
110-
},
111107
{
112108
pathname: '/x/api/charts/charts-x-axis',
113109
title: 'ChartsXAxis',

docs/pages/x/api/charts/chart-container.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
},
77
"dataset": { "type": { "name": "arrayOf", "description": "Array&lt;object&gt;" } },
88
"disableAxisListener": { "type": { "name": "bool" }, "default": "false" },
9+
"disableVoronoi": { "type": { "name": "bool" } },
910
"height": { "type": { "name": "number" } },
1011
"highlightedItem": {
1112
"type": {
@@ -34,8 +35,16 @@
3435
"describedArgs": ["highlightedItem"]
3536
}
3637
},
38+
"onItemClick": {
39+
"type": { "name": "func" },
40+
"signature": {
41+
"type": "function(event: MouseEvent, scatterItemIdentifier: ScatterItemIdentifier) => void",
42+
"describedArgs": ["event", "scatterItemIdentifier"]
43+
}
44+
},
3745
"series": { "type": { "name": "arrayOf", "description": "Array&lt;object&gt;" } },
3846
"skipAnimation": { "type": { "name": "bool" } },
47+
"voronoiMaxRadius": { "type": { "name": "number" } },
3948
"width": { "type": { "name": "number" } },
4049
"xAxis": {
4150
"type": {

docs/pages/x/api/charts/charts-voronoi-handler.js

Lines changed: 0 additions & 23 deletions
This file was deleted.

docs/pages/x/api/charts/charts-voronoi-handler.json

Lines changed: 0 additions & 24 deletions
This file was deleted.

docs/pages/x/api/charts/spark-line-chart.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
},
1818
"dataset": { "type": { "name": "arrayOf", "description": "Array&lt;object&gt;" } },
1919
"disableAxisListener": { "type": { "name": "bool" }, "default": "false" },
20+
"disableVoronoi": { "type": { "name": "bool" } },
2021
"height": { "type": { "name": "number" } },
2122
"highlightedItem": {
2223
"type": {
@@ -46,6 +47,13 @@
4647
"describedArgs": ["highlightedItem"]
4748
}
4849
},
50+
"onItemClick": {
51+
"type": { "name": "func" },
52+
"signature": {
53+
"type": "function(event: MouseEvent, scatterItemIdentifier: ScatterItemIdentifier) => void",
54+
"describedArgs": ["event", "scatterItemIdentifier"]
55+
}
56+
},
4957
"plotType": {
5058
"type": { "name": "enum", "description": "'bar'<br>&#124;&nbsp;'line'" },
5159
"default": "'line'"
@@ -68,6 +76,7 @@
6876
"returned": "string"
6977
}
7078
},
79+
"voronoiMaxRadius": { "type": { "name": "number" } },
7180
"width": { "type": { "name": "number" } },
7281
"xAxis": {
7382
"type": {

docs/translations/api-docs/charts/chart-container/chart-container.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"disableAxisListener": {
99
"description": "If <code>true</code>, the charts will not listen to the mouse move event. It might break interactive features, but will improve performance."
1010
},
11+
"disableVoronoi": { "description": "If true, the voronoi interaction are ignored." },
1112
"height": {
1213
"description": "The height of the chart in px. If not defined, it takes the height of the parent element."
1314
},
@@ -31,12 +32,22 @@
3132
"description": "The callback fired when the highlighted item changes.",
3233
"typeDescriptions": { "highlightedItem": "The newly highlighted item." }
3334
},
35+
"onItemClick": {
36+
"description": "Callback fired when clicking close to an item. This is only available for scatter plot for now.",
37+
"typeDescriptions": {
38+
"event": "Mouse event caught at the svg level",
39+
"scatterItemIdentifier": "Identify which item got clicked"
40+
}
41+
},
3442
"series": {
3543
"description": "The array of series to display. Each type of series has its own specificity. Please refer to the appropriate docs page to learn more about it."
3644
},
3745
"skipAnimation": {
3846
"description": "If <code>true</code>, animations are skipped. If unset or <code>false</code>, the animations respects the user&#39;s <code>prefers-reduced-motion</code> setting."
3947
},
48+
"voronoiMaxRadius": {
49+
"description": "Defines the maximal distance between a scatter point and the pointer that triggers the interaction. If <code>undefined</code>, the radius is assumed to be infinite."
50+
},
4051
"width": {
4152
"description": "The width of the chart in px. If not defined, it takes the width of the parent element."
4253
},

docs/translations/api-docs/charts/charts-voronoi-handler/charts-voronoi-handler.json

Lines changed: 0 additions & 16 deletions
This file was deleted.

docs/translations/api-docs/charts/spark-line-chart/spark-line-chart.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"disableAxisListener": {
1414
"description": "If <code>true</code>, the charts will not listen to the mouse move event. It might break interactive features, but will improve performance."
1515
},
16+
"disableVoronoi": { "description": "If true, the voronoi interaction are ignored." },
1617
"height": {
1718
"description": "The height of the chart in px. If not defined, it takes the height of the parent element."
1819
},
@@ -36,6 +37,13 @@
3637
"description": "The callback fired when the highlighted item changes.",
3738
"typeDescriptions": { "highlightedItem": "The newly highlighted item." }
3839
},
40+
"onItemClick": {
41+
"description": "Callback fired when clicking close to an item. This is only available for scatter plot for now.",
42+
"typeDescriptions": {
43+
"event": "Mouse event caught at the svg level",
44+
"scatterItemIdentifier": "Identify which item got clicked"
45+
}
46+
},
3947
"plotType": { "description": "Type of plot used." },
4048
"showHighlight": {
4149
"description": "Set to <code>true</code> to highlight the value. With line, it shows a point. With bar, it shows a highlight band."
@@ -52,6 +60,9 @@
5260
"description": "Formatter used by the tooltip.",
5361
"typeDescriptions": { "value": "The value to format.", "string": "the formatted value." }
5462
},
63+
"voronoiMaxRadius": {
64+
"description": "Defines the maximal distance between a scatter point and the pointer that triggers the interaction. If <code>undefined</code>, the radius is assumed to be infinite."
65+
},
5566
"width": {
5667
"description": "The width of the chart in px. If not defined, it takes the width of the parent element."
5768
},

packages/x-charts-pro/src/ChartContainerPro/useChartContainerProProps.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export const useChartContainerProProps = <
3030
const { initialZoom, onZoomChange, plugins, apiRef, ...baseProps } =
3131
props as ChartContainerProProps<TSeries, AllPluginSignatures>;
3232

33-
const { chartDataProviderProps, chartsSurfaceProps, children } = useChartContainerProps<TSeries>(
33+
const { chartDataProviderProps, chartsSurfaceProps, children } = useChartContainerProps(
3434
baseProps,
3535
ref,
3636
);

packages/x-charts-pro/src/ScatterChartPro/ScatterChartPro.plugins.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import {
77
UseChartInteractionSignature,
88
useChartHighlight,
99
UseChartHighlightSignature,
10+
useChartVoronoi,
11+
UseChartVoronoiSignature,
1012
ConvertSignaturesIntoPlugins,
1113
} from '@mui/x-charts/internals';
1214
import { useChartProZoom, UseChartProZoomSignature } from '../internals/plugins/useChartProZoom';
@@ -16,8 +18,16 @@ export type ScatterChartProPluginsSignatures = [
1618
UseChartCartesianAxisSignature<'scatter'>,
1719
UseChartInteractionSignature,
1820
UseChartHighlightSignature,
21+
UseChartVoronoiSignature,
1922
UseChartProZoomSignature,
2023
];
2124

2225
export const SCATTER_CHART_PRO_PLUGINS: ConvertSignaturesIntoPlugins<ScatterChartProPluginsSignatures> =
23-
[useChartZAxis, useChartCartesianAxis, useChartInteraction, useChartHighlight, useChartProZoom];
26+
[
27+
useChartZAxis,
28+
useChartCartesianAxis,
29+
useChartInteraction,
30+
useChartHighlight,
31+
useChartVoronoi,
32+
useChartProZoom,
33+
];

packages/x-charts-pro/src/ScatterChartPro/ScatterChartPro.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import PropTypes from 'prop-types';
44
import { useThemeProps } from '@mui/material/styles';
55
import { ChartsOverlay } from '@mui/x-charts/ChartsOverlay';
66
import { ScatterChartProps, ScatterPlot } from '@mui/x-charts/ScatterChart';
7-
import { ChartsVoronoiHandler } from '@mui/x-charts/ChartsVoronoiHandler';
87
import { ChartsAxis } from '@mui/x-charts/ChartsAxis';
98
import { ChartsGrid } from '@mui/x-charts/ChartsGrid';
109
import { ChartsLegend } from '@mui/x-charts/ChartsLegend';
@@ -22,7 +21,10 @@ import {
2221

2322
export interface ScatterChartProProps
2423
extends Omit<ScatterChartProps, 'apiRef'>,
25-
Omit<ChartContainerProProps<'scatter'>, 'series' | 'plugins' | 'seriesConfig'> {}
24+
Omit<
25+
ChartContainerProProps<'scatter', ScatterChartProPluginsSignatures>,
26+
'series' | 'plugins' | 'seriesConfig' | 'onItemClick'
27+
> {}
2628

2729
/**
2830
* Demos:
@@ -43,7 +45,6 @@ const ScatterChartPro = React.forwardRef(function ScatterChartPro(
4345
const {
4446
chartsWrapperProps,
4547
chartContainerProps,
46-
voronoiHandlerProps,
4748
chartsAxisProps,
4849
gridProps,
4950
scatterPlotProps,
@@ -73,7 +74,6 @@ const ScatterChartPro = React.forwardRef(function ScatterChartPro(
7374
<ChartsWrapper {...chartsWrapperProps}>
7475
{!props.hideLegend && <ChartsLegend {...legendProps} />}
7576
<ChartsSurface {...chartsSurfaceProps}>
76-
{!props.disableVoronoi && <ChartsVoronoiHandler {...voronoiHandlerProps} />}
7777
<ChartsAxis {...chartsAxisProps} />
7878
<ChartsGrid {...gridProps} />
7979
<g data-drawing-container>

packages/x-charts-pro/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ export * from '@mui/x-charts/ChartsText';
1616
export * from '@mui/x-charts/ChartsTooltip';
1717
export * from '@mui/x-charts/ChartsLegend';
1818
export * from '@mui/x-charts/ChartsAxisHighlight';
19-
export * from '@mui/x-charts/ChartsVoronoiHandler';
2019
export * from '@mui/x-charts/BarChart';
2120
export * from '@mui/x-charts/LineChart';
2221
export * from '@mui/x-charts/PieChart';

packages/x-charts/src/BarChart/BarChart.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { ChartDataProvider } from '../ChartDataProvider';
2525
import { ChartsSurface } from '../ChartsSurface';
2626
import { useChartContainerProps } from '../ChartContainer/useChartContainerProps';
2727
import { ChartsWrapper } from '../internals/components/ChartsWrapper';
28+
import { BarChartPluginsSignatures } from './BarChart.plugins';
2829

2930
export interface BarChartSlots
3031
extends ChartsAxisSlots,
@@ -40,7 +41,10 @@ export interface BarChartSlotProps
4041
ChartsTooltipSlotProps {}
4142

4243
export interface BarChartProps
43-
extends Omit<ChartContainerProps<'bar'>, 'series' | 'plugins' | 'zAxis'>,
44+
extends Omit<
45+
ChartContainerProps<'bar', BarChartPluginsSignatures>,
46+
'series' | 'plugins' | 'zAxis'
47+
>,
4448
Omit<ChartsAxisProps, 'slots' | 'slotProps'>,
4549
Omit<BarPlotProps, 'slots' | 'slotProps'>,
4650
Omit<ChartsOverlayProps, 'slots' | 'slotProps'> {
@@ -119,7 +123,7 @@ const BarChart = React.forwardRef(function BarChart(
119123
const Tooltip = props.slots?.tooltip ?? ChartsTooltip;
120124

121125
return (
122-
<ChartDataProvider {...chartDataProviderProps}>
126+
<ChartDataProvider<'bar', BarChartPluginsSignatures> {...chartDataProviderProps}>
123127
<ChartsWrapper {...chartsWrapperProps}>
124128
{!props.hideLegend && <ChartsLegend {...legendProps} />}
125129
<ChartsSurface {...chartsSurfaceProps}>

packages/x-charts/src/ChartContainer/ChartContainer.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ ChartContainer.propTypes = {
8383
* @default false
8484
*/
8585
disableAxisListener: PropTypes.bool,
86+
/**
87+
* If true, the voronoi interaction are ignored.
88+
*/
89+
disableVoronoi: PropTypes.bool,
8690
/**
8791
* The height of the chart in px. If not defined, it takes the height of the parent element.
8892
*/
@@ -124,6 +128,13 @@ ChartContainer.propTypes = {
124128
* @param {HighlightItemData | null} highlightedItem The newly highlighted item.
125129
*/
126130
onHighlightChange: PropTypes.func,
131+
/**
132+
* Callback fired when clicking close to an item.
133+
* This is only available for scatter plot for now.
134+
* @param {MouseEvent} event Mouse event caught at the svg level
135+
* @param {ScatterItemIdentifier} scatterItemIdentifier Identify which item got clicked
136+
*/
137+
onItemClick: PropTypes.func,
127138
/**
128139
* The array of series to display.
129140
* Each type of series has its own specificity.
@@ -142,6 +153,11 @@ ChartContainer.propTypes = {
142153
]),
143154
theme: PropTypes.oneOf(['dark', 'light']),
144155
title: PropTypes.string,
156+
/**
157+
* Defines the maximal distance between a scatter point and the pointer that triggers the interaction.
158+
* If `undefined`, the radius is assumed to be infinite.
159+
*/
160+
voronoiMaxRadius: PropTypes.number,
145161
/**
146162
* The width of the chart in px. If not defined, it takes the width of the parent element.
147163
*/

packages/x-charts/src/ChartContainer/useChartContainerProps.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ export const useChartContainerProps = <
3333
dataset,
3434
desc,
3535
onAxisClick,
36+
disableVoronoi,
37+
voronoiMaxRadius,
38+
onItemClick,
3639
disableAxisListener,
3740
highlightedItem,
3841
onHighlightChange,
@@ -63,6 +66,9 @@ export const useChartContainerProps = <
6366
highlightedItem,
6467
onHighlightChange,
6568
onAxisClick,
69+
disableVoronoi,
70+
voronoiMaxRadius,
71+
onItemClick,
6672
xAxis,
6773
yAxis,
6874
zAxis,

0 commit comments

Comments
 (0)