Skip to content

[charts] Move Voronoi handler in a dedicated plugin #16470

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/data/charts/scatter-demo/scatter-demo.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: Charts - Scatter demonstration
productId: x-charts
components: Scatter, ScatterChart, ScatterPlot, ChartsVoronoiHandler
components: Scatter, ScatterChart, ScatterPlot
---

# Charts - Scatter demonstration
Expand Down
8 changes: 1 addition & 7 deletions docs/data/charts/scatter/scatter.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: React Scatter chart
productId: x-charts
components: ScatterChart, ScatterChartPro, ScatterPlot, ChartsVoronoiHandler, ChartsGrid
components: ScatterChart, ScatterChartPro, ScatterPlot, ChartsGrid
---

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

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

To use this feature with composition, add the `ChartsVoronoiHandler`.

```jsx
<ChartsVoronoiHandler voronoiMaxRadius={50} />
```

## Click event

Scatter Chart provides an `onItemClick` handler for handling clicks on specific scatter items.
Expand Down
4 changes: 0 additions & 4 deletions docs/data/chartsApiPages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,6 @@ const chartsApiPages: MuiPage[] = [
pathname: '/x/api/charts/charts-tooltip-container',
title: 'ChartsTooltipContainer',
},
{
pathname: '/x/api/charts/charts-voronoi-handler',
title: 'ChartsVoronoiHandler',
},
{
pathname: '/x/api/charts/charts-x-axis',
title: 'ChartsXAxis',
Expand Down
9 changes: 9 additions & 0 deletions docs/pages/x/api/charts/chart-container.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
},
"dataset": { "type": { "name": "arrayOf", "description": "Array&lt;object&gt;" } },
"disableAxisListener": { "type": { "name": "bool" }, "default": "false" },
"disableVoronoi": { "type": { "name": "bool" } },
"height": { "type": { "name": "number" } },
"highlightedItem": {
"type": {
Expand Down Expand Up @@ -34,8 +35,16 @@
"describedArgs": ["highlightedItem"]
}
},
"onItemClick": {
"type": { "name": "func" },
"signature": {
"type": "function(event: MouseEvent, scatterItemIdentifier: ScatterItemIdentifier) => void",
"describedArgs": ["event", "scatterItemIdentifier"]
}
},
"series": { "type": { "name": "arrayOf", "description": "Array&lt;object&gt;" } },
"skipAnimation": { "type": { "name": "bool" } },
"voronoiMaxRadius": { "type": { "name": "number" } },
"width": { "type": { "name": "number" } },
"xAxis": {
"type": {
Expand Down
23 changes: 0 additions & 23 deletions docs/pages/x/api/charts/charts-voronoi-handler.js

This file was deleted.

24 changes: 0 additions & 24 deletions docs/pages/x/api/charts/charts-voronoi-handler.json

This file was deleted.

9 changes: 9 additions & 0 deletions docs/pages/x/api/charts/spark-line-chart.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
},
"dataset": { "type": { "name": "arrayOf", "description": "Array&lt;object&gt;" } },
"disableAxisListener": { "type": { "name": "bool" }, "default": "false" },
"disableVoronoi": { "type": { "name": "bool" } },
"height": { "type": { "name": "number" } },
"highlightedItem": {
"type": {
Expand Down Expand Up @@ -46,6 +47,13 @@
"describedArgs": ["highlightedItem"]
}
},
"onItemClick": {
"type": { "name": "func" },
"signature": {
"type": "function(event: MouseEvent, scatterItemIdentifier: ScatterItemIdentifier) => void",
"describedArgs": ["event", "scatterItemIdentifier"]
}
},
"plotType": {
"type": { "name": "enum", "description": "'bar'<br>&#124;&nbsp;'line'" },
"default": "'line'"
Expand All @@ -68,6 +76,7 @@
"returned": "string"
}
},
"voronoiMaxRadius": { "type": { "name": "number" } },
"width": { "type": { "name": "number" } },
"xAxis": {
"type": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"disableAxisListener": {
"description": "If <code>true</code>, the charts will not listen to the mouse move event. It might break interactive features, but will improve performance."
},
"disableVoronoi": { "description": "If true, the voronoi interaction are ignored." },
"height": {
"description": "The height of the chart in px. If not defined, it takes the height of the parent element."
},
Expand All @@ -31,12 +32,22 @@
"description": "The callback fired when the highlighted item changes.",
"typeDescriptions": { "highlightedItem": "The newly highlighted item." }
},
"onItemClick": {
"description": "Callback fired when clicking close to an item. This is only available for scatter plot for now.",
"typeDescriptions": {
"event": "Mouse event caught at the svg level",
"scatterItemIdentifier": "Identify which item got clicked"
}
},
"series": {
"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."
},
"skipAnimation": {
"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."
},
"voronoiMaxRadius": {
"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."
},
"width": {
"description": "The width of the chart in px. If not defined, it takes the width of the parent element."
},
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"disableAxisListener": {
"description": "If <code>true</code>, the charts will not listen to the mouse move event. It might break interactive features, but will improve performance."
},
"disableVoronoi": { "description": "If true, the voronoi interaction are ignored." },
"height": {
"description": "The height of the chart in px. If not defined, it takes the height of the parent element."
},
Expand All @@ -36,6 +37,13 @@
"description": "The callback fired when the highlighted item changes.",
"typeDescriptions": { "highlightedItem": "The newly highlighted item." }
},
"onItemClick": {
"description": "Callback fired when clicking close to an item. This is only available for scatter plot for now.",
"typeDescriptions": {
"event": "Mouse event caught at the svg level",
"scatterItemIdentifier": "Identify which item got clicked"
}
},
"plotType": { "description": "Type of plot used." },
"showHighlight": {
"description": "Set to <code>true</code> to highlight the value. With line, it shows a point. With bar, it shows a highlight band."
Expand All @@ -52,6 +60,9 @@
"description": "Formatter used by the tooltip.",
"typeDescriptions": { "value": "The value to format.", "string": "the formatted value." }
},
"voronoiMaxRadius": {
"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."
},
"width": {
"description": "The width of the chart in px. If not defined, it takes the width of the parent element."
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const useChartContainerProProps = <
const { initialZoom, onZoomChange, plugins, apiRef, ...baseProps } =
props as ChartContainerProProps<TSeries, AllPluginSignatures>;

const { chartDataProviderProps, chartsSurfaceProps, children } = useChartContainerProps<TSeries>(
const { chartDataProviderProps, chartsSurfaceProps, children } = useChartContainerProps(
baseProps,
ref,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
UseChartInteractionSignature,
useChartHighlight,
UseChartHighlightSignature,
useChartVoronoi,
UseChartVoronoiSignature,
ConvertSignaturesIntoPlugins,
} from '@mui/x-charts/internals';
import { useChartProZoom, UseChartProZoomSignature } from '../internals/plugins/useChartProZoom';
Expand All @@ -16,8 +18,16 @@ export type ScatterChartProPluginsSignatures = [
UseChartCartesianAxisSignature<'scatter'>,
UseChartInteractionSignature,
UseChartHighlightSignature,
UseChartVoronoiSignature,
UseChartProZoomSignature,
];

export const SCATTER_CHART_PRO_PLUGINS: ConvertSignaturesIntoPlugins<ScatterChartProPluginsSignatures> =
[useChartZAxis, useChartCartesianAxis, useChartInteraction, useChartHighlight, useChartProZoom];
[
useChartZAxis,
useChartCartesianAxis,
useChartInteraction,
useChartHighlight,
useChartVoronoi,
useChartProZoom,
];
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import PropTypes from 'prop-types';
import { useThemeProps } from '@mui/material/styles';
import { ChartsOverlay } from '@mui/x-charts/ChartsOverlay';
import { ScatterChartProps, ScatterPlot } from '@mui/x-charts/ScatterChart';
import { ChartsVoronoiHandler } from '@mui/x-charts/ChartsVoronoiHandler';
import { ChartsAxis } from '@mui/x-charts/ChartsAxis';
import { ChartsGrid } from '@mui/x-charts/ChartsGrid';
import { ChartsLegend } from '@mui/x-charts/ChartsLegend';
Expand All @@ -22,7 +21,10 @@ import {

export interface ScatterChartProProps
extends Omit<ScatterChartProps, 'apiRef'>,
Omit<ChartContainerProProps<'scatter'>, 'series' | 'plugins' | 'seriesConfig'> {}
Omit<
ChartContainerProProps<'scatter', ScatterChartProPluginsSignatures>,
'series' | 'plugins' | 'seriesConfig' | 'onItemClick'
> {}

/**
* Demos:
Expand All @@ -43,7 +45,6 @@ const ScatterChartPro = React.forwardRef(function ScatterChartPro(
const {
chartsWrapperProps,
chartContainerProps,
voronoiHandlerProps,
chartsAxisProps,
gridProps,
scatterPlotProps,
Expand Down Expand Up @@ -73,7 +74,6 @@ const ScatterChartPro = React.forwardRef(function ScatterChartPro(
<ChartsWrapper {...chartsWrapperProps}>
{!props.hideLegend && <ChartsLegend {...legendProps} />}
<ChartsSurface {...chartsSurfaceProps}>
{!props.disableVoronoi && <ChartsVoronoiHandler {...voronoiHandlerProps} />}
<ChartsAxis {...chartsAxisProps} />
<ChartsGrid {...gridProps} />
<g data-drawing-container>
Expand Down
1 change: 0 additions & 1 deletion packages/x-charts-pro/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export * from '@mui/x-charts/ChartsText';
export * from '@mui/x-charts/ChartsTooltip';
export * from '@mui/x-charts/ChartsLegend';
export * from '@mui/x-charts/ChartsAxisHighlight';
export * from '@mui/x-charts/ChartsVoronoiHandler';
export * from '@mui/x-charts/BarChart';
export * from '@mui/x-charts/LineChart';
export * from '@mui/x-charts/PieChart';
Expand Down
8 changes: 6 additions & 2 deletions packages/x-charts/src/BarChart/BarChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { ChartDataProvider } from '../ChartDataProvider';
import { ChartsSurface } from '../ChartsSurface';
import { useChartContainerProps } from '../ChartContainer/useChartContainerProps';
import { ChartsWrapper } from '../internals/components/ChartsWrapper';
import { BarChartPluginsSignatures } from './BarChart.plugins';

export interface BarChartSlots
extends ChartsAxisSlots,
Expand All @@ -40,7 +41,10 @@ export interface BarChartSlotProps
ChartsTooltipSlotProps {}

export interface BarChartProps
extends Omit<ChartContainerProps<'bar'>, 'series' | 'plugins' | 'zAxis'>,
extends Omit<
ChartContainerProps<'bar', BarChartPluginsSignatures>,
'series' | 'plugins' | 'zAxis'
>,
Omit<ChartsAxisProps, 'slots' | 'slotProps'>,
Omit<BarPlotProps, 'slots' | 'slotProps'>,
Omit<ChartsOverlayProps, 'slots' | 'slotProps'> {
Expand Down Expand Up @@ -119,7 +123,7 @@ const BarChart = React.forwardRef(function BarChart(
const Tooltip = props.slots?.tooltip ?? ChartsTooltip;

return (
<ChartDataProvider {...chartDataProviderProps}>
<ChartDataProvider<'bar', BarChartPluginsSignatures> {...chartDataProviderProps}>
<ChartsWrapper {...chartsWrapperProps}>
{!props.hideLegend && <ChartsLegend {...legendProps} />}
<ChartsSurface {...chartsSurfaceProps}>
Expand Down
16 changes: 16 additions & 0 deletions packages/x-charts/src/ChartContainer/ChartContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ ChartContainer.propTypes = {
* @default false
*/
disableAxisListener: PropTypes.bool,
/**
* If true, the voronoi interaction are ignored.
*/
disableVoronoi: PropTypes.bool,
/**
* The height of the chart in px. If not defined, it takes the height of the parent element.
*/
Expand Down Expand Up @@ -124,6 +128,13 @@ ChartContainer.propTypes = {
* @param {HighlightItemData | null} highlightedItem The newly highlighted item.
*/
onHighlightChange: PropTypes.func,
/**
* Callback fired when clicking close to an item.
* This is only available for scatter plot for now.
* @param {MouseEvent} event Mouse event caught at the svg level
* @param {ScatterItemIdentifier} scatterItemIdentifier Identify which item got clicked
*/
onItemClick: PropTypes.func,
/**
* The array of series to display.
* Each type of series has its own specificity.
Expand All @@ -142,6 +153,11 @@ ChartContainer.propTypes = {
]),
theme: PropTypes.oneOf(['dark', 'light']),
title: PropTypes.string,
/**
* Defines the maximal distance between a scatter point and the pointer that triggers the interaction.
* If `undefined`, the radius is assumed to be infinite.
*/
voronoiMaxRadius: PropTypes.number,
/**
* The width of the chart in px. If not defined, it takes the width of the parent element.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export const useChartContainerProps = <
dataset,
desc,
onAxisClick,
voronoiMaxRadius,
onItemClick,
disableAxisListener,
highlightedItem,
onHighlightChange,
Expand Down Expand Up @@ -63,6 +65,8 @@ export const useChartContainerProps = <
highlightedItem,
onHighlightChange,
onAxisClick,
voronoiMaxRadius,
onItemClick,
xAxis,
yAxis,
zAxis,
Expand Down
Loading