@mui/x-charts 7.0.0-beta.0 → 7.0.0-beta.1

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 (185) hide show
  1. package/BarChart/BarChart.d.ts +2 -1
  2. package/BarChart/BarChart.js +23 -4
  3. package/BarChart/BarElement.d.ts +1 -1
  4. package/BarChart/BarElement.js +6 -3
  5. package/BarChart/BarPlot.d.ts +8 -1
  6. package/BarChart/BarPlot.js +19 -5
  7. package/BarChart/formatter.js +1 -1
  8. package/CHANGELOG.md +247 -0
  9. package/ChartsOnAxisClickHandler/ChartsOnAxisClickHandler.d.ts +20 -0
  10. package/ChartsOnAxisClickHandler/ChartsOnAxisClickHandler.js +83 -0
  11. package/ChartsOnAxisClickHandler/index.d.ts +1 -0
  12. package/ChartsOnAxisClickHandler/index.js +16 -0
  13. package/ChartsOnAxisClickHandler/package.json +6 -0
  14. package/ChartsReferenceLine/ChartsXReferenceLine.js +1 -1
  15. package/ChartsReferenceLine/ChartsYReferenceLine.js +1 -1
  16. package/ChartsTooltip/ChartsAxisTooltipContent.js +6 -5
  17. package/ChartsTooltip/DefaultChartsAxisTooltipContent.js +6 -4
  18. package/ChartsTooltip/utils.d.ts +3 -1
  19. package/ChartsTooltip/utils.js +8 -0
  20. package/ChartsVoronoiHandler/ChartsVoronoiHandler.d.ts +7 -0
  21. package/ChartsVoronoiHandler/ChartsVoronoiHandler.js +92 -44
  22. package/LineChart/AnimatedArea.d.ts +1361 -0
  23. package/LineChart/AnimatedArea.js +111 -0
  24. package/LineChart/AnimatedLine.d.ts +1361 -0
  25. package/LineChart/AnimatedLine.js +113 -0
  26. package/LineChart/AreaElement.d.ts +17 -17
  27. package/LineChart/AreaElement.js +17 -34
  28. package/LineChart/AreaPlot.d.ts +12 -7
  29. package/LineChart/AreaPlot.js +91 -58
  30. package/LineChart/LineChart.d.ts +13 -4
  31. package/LineChart/LineChart.js +35 -6
  32. package/LineChart/LineElement.d.ts +17 -17
  33. package/LineChart/LineElement.js +16 -36
  34. package/LineChart/LineHighlightElement.js +3 -2
  35. package/LineChart/LinePlot.d.ts +12 -7
  36. package/LineChart/LinePlot.js +89 -56
  37. package/LineChart/MarkElement.d.ts +5 -2
  38. package/LineChart/MarkElement.js +26 -13
  39. package/LineChart/MarkPlot.d.ts +8 -1
  40. package/LineChart/MarkPlot.js +80 -51
  41. package/LineChart/formatter.js +1 -1
  42. package/LineChart/index.d.ts +2 -0
  43. package/LineChart/index.js +22 -0
  44. package/PieChart/PieArcLabelPlot.d.ts +1 -1
  45. package/PieChart/PieArcLabelPlot.js +1 -1
  46. package/PieChart/PieArcPlot.d.ts +2 -2
  47. package/PieChart/PieArcPlot.js +6 -6
  48. package/PieChart/PieChart.d.ts +1 -1
  49. package/PieChart/PieChart.js +4 -4
  50. package/PieChart/PiePlot.d.ts +1 -1
  51. package/PieChart/PiePlot.js +4 -4
  52. package/ScatterChart/Scatter.d.ts +7 -1
  53. package/ScatterChart/Scatter.js +17 -3
  54. package/ScatterChart/ScatterChart.d.ts +8 -2
  55. package/ScatterChart/ScatterChart.js +11 -2
  56. package/ScatterChart/ScatterPlot.d.ts +1 -1
  57. package/ScatterChart/ScatterPlot.js +10 -2
  58. package/SparkLineChart/SparkLineChart.js +2 -0
  59. package/context/DrawingProvider.d.ts +6 -1
  60. package/context/DrawingProvider.js +9 -2
  61. package/context/InteractionProvider.d.ts +3 -3
  62. package/esm/BarChart/BarChart.js +23 -4
  63. package/esm/BarChart/BarElement.js +6 -3
  64. package/esm/BarChart/BarPlot.js +19 -5
  65. package/esm/BarChart/formatter.js +1 -1
  66. package/esm/ChartsOnAxisClickHandler/ChartsOnAxisClickHandler.js +77 -0
  67. package/esm/ChartsOnAxisClickHandler/index.js +1 -0
  68. package/esm/ChartsReferenceLine/ChartsXReferenceLine.js +1 -1
  69. package/esm/ChartsReferenceLine/ChartsYReferenceLine.js +1 -1
  70. package/esm/ChartsTooltip/ChartsAxisTooltipContent.js +6 -5
  71. package/esm/ChartsTooltip/DefaultChartsAxisTooltipContent.js +7 -4
  72. package/esm/ChartsTooltip/utils.js +6 -0
  73. package/esm/ChartsVoronoiHandler/ChartsVoronoiHandler.js +92 -44
  74. package/esm/LineChart/AnimatedArea.js +103 -0
  75. package/esm/LineChart/AnimatedLine.js +105 -0
  76. package/esm/LineChart/AreaElement.js +16 -33
  77. package/esm/LineChart/AreaPlot.js +92 -59
  78. package/esm/LineChart/LineChart.js +35 -6
  79. package/esm/LineChart/LineElement.js +16 -35
  80. package/esm/LineChart/LineHighlightElement.js +3 -2
  81. package/esm/LineChart/LinePlot.js +90 -57
  82. package/esm/LineChart/MarkElement.js +26 -13
  83. package/esm/LineChart/MarkPlot.js +80 -51
  84. package/esm/LineChart/formatter.js +1 -1
  85. package/esm/LineChart/index.js +2 -0
  86. package/esm/PieChart/PieArcLabelPlot.js +1 -1
  87. package/esm/PieChart/PieArcPlot.js +6 -6
  88. package/esm/PieChart/PieChart.js +4 -4
  89. package/esm/PieChart/PiePlot.js +4 -4
  90. package/esm/ScatterChart/Scatter.js +17 -3
  91. package/esm/ScatterChart/ScatterChart.js +11 -2
  92. package/esm/ScatterChart/ScatterPlot.js +10 -2
  93. package/esm/SparkLineChart/SparkLineChart.js +2 -0
  94. package/esm/context/DrawingProvider.js +9 -2
  95. package/esm/hooks/useDrawingArea.js +7 -3
  96. package/esm/index.js +1 -0
  97. package/esm/internals/geometry.js +1 -1
  98. package/esm/internals/useAnimatedPath.js +29 -0
  99. package/esm/internals/utils.js +7 -0
  100. package/hooks/useDrawingArea.d.ts +2 -0
  101. package/hooks/useDrawingArea.js +7 -3
  102. package/index.d.ts +1 -0
  103. package/index.js +12 -1
  104. package/internals/geometry.js +1 -1
  105. package/internals/useAnimatedPath.d.ts +1 -0
  106. package/internals/useAnimatedPath.js +38 -0
  107. package/internals/utils.d.ts +4 -0
  108. package/internals/utils.js +8 -0
  109. package/legacy/BarChart/BarChart.js +23 -4
  110. package/legacy/BarChart/BarElement.js +5 -2
  111. package/legacy/BarChart/BarPlot.js +18 -4
  112. package/legacy/BarChart/formatter.js +1 -1
  113. package/legacy/ChartsOnAxisClickHandler/ChartsOnAxisClickHandler.js +75 -0
  114. package/legacy/ChartsOnAxisClickHandler/index.js +1 -0
  115. package/legacy/ChartsReferenceLine/ChartsXReferenceLine.js +1 -1
  116. package/legacy/ChartsReferenceLine/ChartsYReferenceLine.js +1 -1
  117. package/legacy/ChartsTooltip/ChartsAxisTooltipContent.js +6 -7
  118. package/legacy/ChartsTooltip/DefaultChartsAxisTooltipContent.js +7 -4
  119. package/legacy/ChartsTooltip/utils.js +6 -0
  120. package/legacy/ChartsVoronoiHandler/ChartsVoronoiHandler.js +88 -44
  121. package/legacy/LineChart/AnimatedArea.js +102 -0
  122. package/legacy/LineChart/AnimatedLine.js +104 -0
  123. package/legacy/LineChart/AreaElement.js +15 -35
  124. package/legacy/LineChart/AreaPlot.js +102 -66
  125. package/legacy/LineChart/LineChart.js +35 -6
  126. package/legacy/LineChart/LineElement.js +15 -37
  127. package/legacy/LineChart/LineHighlightElement.js +3 -2
  128. package/legacy/LineChart/LinePlot.js +97 -63
  129. package/legacy/LineChart/MarkElement.js +29 -12
  130. package/legacy/LineChart/MarkPlot.js +83 -53
  131. package/legacy/LineChart/formatter.js +1 -1
  132. package/legacy/LineChart/index.js +2 -0
  133. package/legacy/PieChart/PieArcLabelPlot.js +1 -1
  134. package/legacy/PieChart/PieArcPlot.js +6 -6
  135. package/legacy/PieChart/PieChart.js +4 -4
  136. package/legacy/PieChart/PiePlot.js +4 -4
  137. package/legacy/ScatterChart/Scatter.js +19 -3
  138. package/legacy/ScatterChart/ScatterChart.js +11 -2
  139. package/legacy/ScatterChart/ScatterPlot.js +10 -2
  140. package/legacy/SparkLineChart/SparkLineChart.js +2 -0
  141. package/legacy/context/DrawingProvider.js +11 -2
  142. package/legacy/hooks/useDrawingArea.js +7 -3
  143. package/legacy/index.js +2 -1
  144. package/legacy/internals/geometry.js +1 -1
  145. package/legacy/internals/useAnimatedPath.js +32 -0
  146. package/legacy/internals/utils.js +7 -0
  147. package/modern/BarChart/BarChart.js +23 -4
  148. package/modern/BarChart/BarElement.js +6 -3
  149. package/modern/BarChart/BarPlot.js +19 -5
  150. package/modern/BarChart/formatter.js +1 -1
  151. package/modern/ChartsOnAxisClickHandler/ChartsOnAxisClickHandler.js +75 -0
  152. package/modern/ChartsOnAxisClickHandler/index.js +1 -0
  153. package/modern/ChartsReferenceLine/ChartsXReferenceLine.js +1 -1
  154. package/modern/ChartsReferenceLine/ChartsYReferenceLine.js +1 -1
  155. package/modern/ChartsTooltip/ChartsAxisTooltipContent.js +6 -5
  156. package/modern/ChartsTooltip/DefaultChartsAxisTooltipContent.js +6 -4
  157. package/modern/ChartsTooltip/utils.js +6 -0
  158. package/modern/ChartsVoronoiHandler/ChartsVoronoiHandler.js +92 -44
  159. package/modern/LineChart/AnimatedArea.js +103 -0
  160. package/modern/LineChart/AnimatedLine.js +105 -0
  161. package/modern/LineChart/AreaElement.js +16 -33
  162. package/modern/LineChart/AreaPlot.js +91 -58
  163. package/modern/LineChart/LineChart.js +35 -6
  164. package/modern/LineChart/LineElement.js +16 -35
  165. package/modern/LineChart/LineHighlightElement.js +3 -2
  166. package/modern/LineChart/LinePlot.js +89 -56
  167. package/modern/LineChart/MarkElement.js +26 -13
  168. package/modern/LineChart/MarkPlot.js +80 -51
  169. package/modern/LineChart/formatter.js +1 -1
  170. package/modern/LineChart/index.js +2 -0
  171. package/modern/PieChart/PieArcLabelPlot.js +1 -1
  172. package/modern/PieChart/PieArcPlot.js +6 -6
  173. package/modern/PieChart/PieChart.js +4 -4
  174. package/modern/PieChart/PiePlot.js +4 -4
  175. package/modern/ScatterChart/Scatter.js +17 -3
  176. package/modern/ScatterChart/ScatterChart.js +11 -2
  177. package/modern/ScatterChart/ScatterPlot.js +10 -2
  178. package/modern/SparkLineChart/SparkLineChart.js +2 -0
  179. package/modern/context/DrawingProvider.js +9 -2
  180. package/modern/hooks/useDrawingArea.js +7 -3
  181. package/modern/index.js +2 -1
  182. package/modern/internals/geometry.js +1 -1
  183. package/modern/internals/useAnimatedPath.js +29 -0
  184. package/modern/internals/utils.js +7 -0
  185. package/package.json +6 -5
@@ -6,11 +6,11 @@ export interface InteractionProviderProps {
6
6
  export type ItemInteractionData<T extends ChartSeriesType> = ChartItemIdentifier<T>;
7
7
  export type AxisInteractionData = {
8
8
  x: null | {
9
- value: number | Date;
9
+ value: number | Date | string;
10
10
  index?: number;
11
11
  };
12
12
  y: null | {
13
- value: number | Date;
13
+ value: number | Date | string;
14
14
  index?: number;
15
15
  };
16
16
  };
@@ -19,7 +19,7 @@ type InteractionActions<T extends ChartSeriesType = ChartSeriesType> = {
19
19
  data: ItemInteractionData<T>;
20
20
  } | {
21
21
  type: 'leaveItem';
22
- data: ItemInteractionData<T>;
22
+ data: Partial<ItemInteractionData<T>>;
23
23
  } | {
24
24
  type: 'exitChart';
25
25
  } | {
@@ -10,6 +10,7 @@ import { ChartsTooltip } from '../ChartsTooltip';
10
10
  import { ChartsLegend } from '../ChartsLegend';
11
11
  import { ChartsAxisHighlight } from '../ChartsAxisHighlight';
12
12
  import { ChartsClipPath } from '../ChartsClipPath';
13
+ import { ChartsOnAxisClickHandler } from '../ChartsOnAxisClickHandler';
13
14
  import { jsx as _jsx } from "react/jsx-runtime";
14
15
  import { jsxs as _jsxs } from "react/jsx-runtime";
15
16
  /**
@@ -43,6 +44,8 @@ const BarChart = /*#__PURE__*/React.forwardRef(function BarChart(props, ref) {
43
44
  rightAxis,
44
45
  bottomAxis,
45
46
  skipAnimation,
47
+ onItemClick,
48
+ onAxisClick,
46
49
  children,
47
50
  slots,
48
51
  slotProps
@@ -83,13 +86,16 @@ const BarChart = /*#__PURE__*/React.forwardRef(function BarChart(props, ref) {
83
86
  colors: colors,
84
87
  dataset: dataset,
85
88
  sx: sx,
86
- disableAxisListener: (tooltip == null ? void 0 : tooltip.trigger) !== 'axis' && (axisHighlight == null ? void 0 : axisHighlight.x) === 'none' && (axisHighlight == null ? void 0 : axisHighlight.y) === 'none',
87
- children: [/*#__PURE__*/_jsx("g", {
89
+ disableAxisListener: (tooltip == null ? void 0 : tooltip.trigger) !== 'axis' && (axisHighlight == null ? void 0 : axisHighlight.x) === 'none' && (axisHighlight == null ? void 0 : axisHighlight.y) === 'none' && !onAxisClick,
90
+ children: [onAxisClick && /*#__PURE__*/_jsx(ChartsOnAxisClickHandler, {
91
+ onAxisClick: onAxisClick
92
+ }), /*#__PURE__*/_jsx("g", {
88
93
  clipPath: `url(#${clipPathId})`,
89
94
  children: /*#__PURE__*/_jsx(BarPlot, {
90
95
  slots: slots,
91
96
  slotProps: slotProps,
92
- skipAnimation: skipAnimation
97
+ skipAnimation: skipAnimation,
98
+ onItemClick: onItemClick
93
99
  })
94
100
  }), /*#__PURE__*/_jsx(ChartsAxis, {
95
101
  topAxis: topAxis,
@@ -229,6 +235,19 @@ process.env.NODE_ENV !== "production" ? BarChart.propTypes = {
229
235
  right: PropTypes.number,
230
236
  top: PropTypes.number
231
237
  }),
238
+ /**
239
+ * The function called for onClick events.
240
+ * The second argument contains information about all line/bar elements at the current mouse position.
241
+ * @param {MouseEvent} event The mouse event recorded on the `<svg/>` element.
242
+ * @param {null | AxisData} data The data about the clicked axis and items associated with it.
243
+ */
244
+ onAxisClick: PropTypes.func,
245
+ /**
246
+ * Callback fired when a bar item is clicked.
247
+ * @param {React.MouseEvent<SVGElement, MouseEvent>} event The event source of the callback.
248
+ * @param {BarItemIdentifier} barItemIdentifier The bar item identifier.
249
+ */
250
+ onItemClick: PropTypes.func,
232
251
  /**
233
252
  * Indicate which axis to display the right of the charts.
234
253
  * Can be a string (the id of the axis) or an object `ChartsYAxisProps`.
@@ -258,7 +277,7 @@ process.env.NODE_ENV !== "production" ? BarChart.propTypes = {
258
277
  }), PropTypes.string]),
259
278
  series: PropTypes.arrayOf(PropTypes.object).isRequired,
260
279
  /**
261
- * If `true`, animations are skiped.
280
+ * If `true`, animations are skipped.
262
281
  * @default false
263
282
  */
264
283
  skipAnimation: PropTypes.bool,
@@ -1,6 +1,6 @@
1
1
  import _extends from "@babel/runtime/helpers/esm/extends";
2
2
  import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
3
- const _excluded = ["id", "dataIndex", "classes", "color", "highlightScope", "slots", "slotProps", "style"];
3
+ const _excluded = ["id", "dataIndex", "classes", "color", "highlightScope", "slots", "slotProps", "style", "onClick"];
4
4
  import * as React from 'react';
5
5
  import PropTypes from 'prop-types';
6
6
  import composeClasses from '@mui/utils/composeClasses';
@@ -50,7 +50,8 @@ function BarElement(props) {
50
50
  highlightScope,
51
51
  slots,
52
52
  slotProps,
53
- style
53
+ style,
54
+ onClick
54
55
  } = props,
55
56
  other = _objectWithoutPropertiesLoose(props, _excluded);
56
57
  const getInteractionItemProps = useInteractionItemProps(highlightScope);
@@ -86,7 +87,9 @@ function BarElement(props) {
86
87
  dataIndex
87
88
  }), {
88
89
  style,
89
- className: classes.root
90
+ className: classes.root,
91
+ onClick,
92
+ cursor: onClick ? 'pointer' : 'unset'
90
93
  }),
91
94
  ownerState
92
95
  });
@@ -1,6 +1,6 @@
1
1
  import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
2
2
  import _extends from "@babel/runtime/helpers/esm/extends";
3
- const _excluded = ["skipAnimation"];
3
+ const _excluded = ["skipAnimation", "onItemClick"];
4
4
  import * as React from 'react';
5
5
  import PropTypes from 'prop-types';
6
6
  import { useTransition } from '@react-spring/web';
@@ -38,7 +38,7 @@ function getBandSize({
38
38
  offset
39
39
  };
40
40
  }
41
- const useCompletedData = () => {
41
+ const useAggregatedData = () => {
42
42
  var _React$useContext$bar;
43
43
  const seriesData = (_React$useContext$bar = React.useContext(SeriesContext).bar) != null ? _React$useContext$bar : {
44
44
  series: {},
@@ -169,9 +169,10 @@ const getInStyle = ({
169
169
  * - [BarPlot API](https://mui.com/x/api/charts/bar-plot/)
170
170
  */
171
171
  function BarPlot(props) {
172
- const completedData = useCompletedData();
172
+ const completedData = useAggregatedData();
173
173
  const {
174
- skipAnimation
174
+ skipAnimation,
175
+ onItemClick
175
176
  } = props,
176
177
  other = _objectWithoutPropertiesLoose(props, _excluded);
177
178
  const transition = useTransition(completedData, {
@@ -194,6 +195,13 @@ function BarPlot(props) {
194
195
  highlightScope: highlightScope,
195
196
  color: color
196
197
  }, other, {
198
+ onClick: onItemClick && (event => {
199
+ onItemClick(event, {
200
+ type: 'bar',
201
+ seriesId,
202
+ dataIndex
203
+ });
204
+ }),
197
205
  style: style
198
206
  })))
199
207
  });
@@ -204,7 +212,13 @@ process.env.NODE_ENV !== "production" ? BarPlot.propTypes = {
204
212
  // | To update them edit the TypeScript types and run "yarn proptypes" |
205
213
  // ----------------------------------------------------------------------
206
214
  /**
207
- * If `true`, animations are skiped.
215
+ * Callback fired when a bar item is clicked.
216
+ * @param {React.MouseEvent<SVGElement, MouseEvent>} event The event source of the callback.
217
+ * @param {BarItemIdentifier} barItemIdentifier The bar item identifier.
218
+ */
219
+ onItemClick: PropTypes.func,
220
+ /**
221
+ * If `true`, animations are skipped.
208
222
  * @default false
209
223
  */
210
224
  skipAnimation: PropTypes.bool,
@@ -69,7 +69,7 @@ const formatter = (params, dataset) => {
69
69
  return {
70
70
  seriesOrder,
71
71
  stackingGroups,
72
- series: defaultizeValueFormatter(completedSeries, v => v === null ? '' : v.toLocaleString())
72
+ series: defaultizeValueFormatter(completedSeries, v => v == null ? '' : v.toLocaleString())
73
73
  };
74
74
  };
75
75
  export default formatter;
@@ -0,0 +1,77 @@
1
+ import * as React from 'react';
2
+ import PropTypes from 'prop-types';
3
+ import { SVGContext } from '../context/DrawingProvider';
4
+ import { InteractionContext } from '../context/InteractionProvider';
5
+ import { CartesianContext } from '../context/CartesianContextProvider';
6
+ import { SeriesContext } from '../context/SeriesContextProvider';
7
+ import { jsx as _jsx } from "react/jsx-runtime";
8
+ function ChartsOnAxisClickHandler(props) {
9
+ const {
10
+ onAxisClick
11
+ } = props;
12
+ const svgRef = React.useContext(SVGContext);
13
+ const series = React.useContext(SeriesContext);
14
+ const {
15
+ axis
16
+ } = React.useContext(InteractionContext);
17
+ const {
18
+ xAxisIds,
19
+ xAxis,
20
+ yAxisIds,
21
+ yAxis
22
+ } = React.useContext(CartesianContext);
23
+ React.useEffect(() => {
24
+ const element = svgRef.current;
25
+ if (element === null || !onAxisClick) {
26
+ return () => {};
27
+ }
28
+ const handleMouseClick = event => {
29
+ var _USED_AXIS_ID$data;
30
+ event.preventDefault();
31
+ const isXaxis = (axis.x && axis.x.index) !== undefined;
32
+ const USED_AXIS_ID = isXaxis ? xAxisIds[0] : yAxisIds[0];
33
+ const dataIndex = isXaxis ? axis.x && axis.x.index : axis.y && axis.y.index;
34
+ if (dataIndex == null) {
35
+ return;
36
+ }
37
+ const seriesValues = {};
38
+ Object.keys(series).filter(seriesType => ['bar', 'line'].includes(seriesType)).forEach(seriesType => {
39
+ var _series$seriesType;
40
+ (_series$seriesType = series[seriesType]) == null || _series$seriesType.seriesOrder.forEach(seriesId => {
41
+ const seriesItem = series[seriesType].series[seriesId];
42
+ const axisKey = isXaxis ? seriesItem.xAxisKey : seriesItem.yAxisKey;
43
+ if (axisKey === undefined || axisKey === USED_AXIS_ID) {
44
+ seriesValues[seriesId] = seriesItem.data[dataIndex];
45
+ }
46
+ });
47
+ });
48
+ const axisValue = (_USED_AXIS_ID$data = (isXaxis ? xAxis : yAxis)[USED_AXIS_ID].data) == null ? void 0 : _USED_AXIS_ID$data[dataIndex];
49
+ onAxisClick(event, {
50
+ dataIndex,
51
+ axisValue,
52
+ seriesValues
53
+ });
54
+ };
55
+ element.addEventListener('click', handleMouseClick);
56
+ return () => {
57
+ element.removeEventListener('click', handleMouseClick);
58
+ };
59
+ }, [axis.x, axis.y, onAxisClick, series, svgRef, xAxis, xAxisIds, yAxis, yAxisIds]);
60
+
61
+ // eslint-disable-next-line react/jsx-no-useless-fragment
62
+ return /*#__PURE__*/_jsx(React.Fragment, {});
63
+ }
64
+ process.env.NODE_ENV !== "production" ? ChartsOnAxisClickHandler.propTypes = {
65
+ // ----------------------------- Warning --------------------------------
66
+ // | These PropTypes are generated from the TypeScript type definitions |
67
+ // | To update them edit the TypeScript types and run "yarn proptypes" |
68
+ // ----------------------------------------------------------------------
69
+ /**
70
+ * The function called for onClick events.
71
+ * The second argument contains information about all line/bar elements at the current mouse position.
72
+ * @param {MouseEvent} event The mouse event recorded on the `<svg/>` element.
73
+ * @param {null | AxisData} data The data about the clicked axis and items associated with it.
74
+ */
75
+ onAxisClick: PropTypes.func
76
+ } : void 0;
77
+ export { ChartsOnAxisClickHandler };
@@ -0,0 +1 @@
1
+ export * from './ChartsOnAxisClickHandler';
@@ -70,7 +70,7 @@ function ChartsXReferenceLine(props) {
70
70
  if (process.env.NODE_ENV !== 'production') {
71
71
  if (!warnedOnce) {
72
72
  warnedOnce = true;
73
- console.error(`MUI X: the value ${x} does not exist in the data of x axis with id ${axisId}.`);
73
+ console.error(`MUI X Charts: the value ${x} does not exist in the data of x axis with id ${axisId}.`);
74
74
  }
75
75
  }
76
76
  return null;
@@ -70,7 +70,7 @@ function ChartsYReferenceLine(props) {
70
70
  if (process.env.NODE_ENV !== 'production') {
71
71
  if (!warnedOnce) {
72
72
  warnedOnce = true;
73
- console.error(`MUI X: the value ${y} does not exist in the data of y axis with id ${axisId}.`);
73
+ console.error(`MUI X Charts: the value ${y} does not exist in the data of y axis with id ${axisId}.`);
74
74
  }
75
75
  }
76
76
  return null;
@@ -5,6 +5,7 @@ import { useSlotProps } from '@mui/base/utils';
5
5
  import { SeriesContext } from '../context/SeriesContextProvider';
6
6
  import { CartesianContext } from '../context/CartesianContextProvider';
7
7
  import { DefaultChartsAxisTooltipContent } from './DefaultChartsAxisTooltipContent';
8
+ import { isCartesianSeriesType } from './utils';
8
9
  import { jsx as _jsx } from "react/jsx-runtime";
9
10
  function ChartsAxisTooltipContent(props) {
10
11
  const {
@@ -27,7 +28,7 @@ function ChartsAxisTooltipContent(props) {
27
28
  const USED_AXIS_ID = isXaxis ? xAxisIds[0] : yAxisIds[0];
28
29
  const relevantSeries = React.useMemo(() => {
29
30
  const rep = [];
30
- Object.keys(series).filter(seriesType => ['bar', 'line', 'scatter'].includes(seriesType)).forEach(seriesType => {
31
+ Object.keys(series).filter(isCartesianSeriesType).forEach(seriesType => {
31
32
  series[seriesType].seriesOrder.forEach(seriesId => {
32
33
  const item = series[seriesType].series[seriesId];
33
34
  const axisKey = isXaxis ? item.xAxisKey : item.yAxisKey;
@@ -66,11 +67,11 @@ process.env.NODE_ENV !== "production" ? ChartsAxisTooltipContent.propTypes = {
66
67
  axisData: PropTypes.shape({
67
68
  x: PropTypes.shape({
68
69
  index: PropTypes.number,
69
- value: PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number]).isRequired
70
+ value: PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number, PropTypes.string]).isRequired
70
71
  }),
71
72
  y: PropTypes.shape({
72
73
  index: PropTypes.number,
73
- value: PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number]).isRequired
74
+ value: PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number, PropTypes.string]).isRequired
74
75
  })
75
76
  }).isRequired,
76
77
  classes: PropTypes.object.isRequired,
@@ -80,11 +81,11 @@ process.env.NODE_ENV !== "production" ? ChartsAxisTooltipContent.propTypes = {
80
81
  axisData: PropTypes.shape({
81
82
  x: PropTypes.shape({
82
83
  index: PropTypes.number,
83
- value: PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number]).isRequired
84
+ value: PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number, PropTypes.string]).isRequired
84
85
  }),
85
86
  y: PropTypes.shape({
86
87
  index: PropTypes.number,
87
- value: PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number]).isRequired
88
+ value: PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number, PropTypes.string]).isRequired
88
89
  })
89
90
  }),
90
91
  axisValue: PropTypes.any,
@@ -3,6 +3,7 @@ import PropTypes from 'prop-types';
3
3
  import clsx from 'clsx';
4
4
  import Typography from '@mui/material/Typography';
5
5
  import { ChartsTooltipCell, ChartsTooltipPaper, ChartsTooltipTable, ChartsTooltipMark, ChartsTooltipRow } from './ChartsTooltipTable';
6
+ import { isCartesianSeries } from './utils';
6
7
  import { jsx as _jsx } from "react/jsx-runtime";
7
8
  import { jsxs as _jsxs } from "react/jsx-runtime";
8
9
  function DefaultChartsAxisTooltipContent(props) {
@@ -34,14 +35,16 @@ function DefaultChartsAxisTooltipContent(props) {
34
35
  })
35
36
  })
36
37
  }), /*#__PURE__*/_jsx("tbody", {
37
- children: series.map(({
38
+ children: series.filter(isCartesianSeries).map(({
38
39
  color,
39
40
  id,
40
41
  label,
41
42
  valueFormatter,
42
43
  data
43
44
  }) => {
44
- const formattedValue = valueFormatter(data[dataIndex]);
45
+ var _data$dataIndex;
46
+ // @ts-ignore
47
+ const formattedValue = valueFormatter((_data$dataIndex = data[dataIndex]) != null ? _data$dataIndex : null);
45
48
  if (formattedValue == null) {
46
49
  return null;
47
50
  }
@@ -88,11 +91,11 @@ process.env.NODE_ENV !== "production" ? DefaultChartsAxisTooltipContent.propType
88
91
  axisData: PropTypes.shape({
89
92
  x: PropTypes.shape({
90
93
  index: PropTypes.number,
91
- value: PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number]).isRequired
94
+ value: PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number, PropTypes.string]).isRequired
92
95
  }),
93
96
  y: PropTypes.shape({
94
97
  index: PropTypes.number,
95
- value: PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number]).isRequired
98
+ value: PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number, PropTypes.string]).isRequired
96
99
  })
97
100
  }).isRequired,
98
101
  /**
@@ -78,4 +78,10 @@ export function getTooltipHasData(trigger, displayedData) {
78
78
  const hasAxisXData = displayedData.x !== null;
79
79
  const hasAxisYData = displayedData.y !== null;
80
80
  return hasAxisXData || hasAxisYData;
81
+ }
82
+ export function isCartesianSeriesType(seriesType) {
83
+ return ['bar', 'line', 'scatter'].includes(seriesType);
84
+ }
85
+ export function isCartesianSeries(series) {
86
+ return isCartesianSeriesType(series.type);
81
87
  }
@@ -12,7 +12,8 @@ import { jsx as _jsx } from "react/jsx-runtime";
12
12
  function ChartsVoronoiHandler(props) {
13
13
  var _React$useContext$sca;
14
14
  const {
15
- voronoiMaxRadius
15
+ voronoiMaxRadius,
16
+ onItemClick
16
17
  } = props;
17
18
  const svgRef = React.useContext(SVGContext);
18
19
  const {
@@ -50,6 +51,8 @@ function ChartsVoronoiHandler(props) {
50
51
  };
51
52
  }, [dispatch]);
52
53
  useEnhancedEffect(() => {
54
+ // This effect generate and store the Delaunay object that's used to map coordinate to closest point.
55
+
53
56
  if (seriesOrder === undefined || series === undefined) {
54
57
  // If there is no scatter chart series
55
58
  return;
@@ -83,81 +86,126 @@ function ChartsVoronoiHandler(props) {
83
86
  if (element === null) {
84
87
  return undefined;
85
88
  }
86
- const handleMouseOut = () => {
87
- dispatch({
88
- type: 'exitChart'
89
- });
90
- };
91
89
 
92
90
  // TODO: A perf optimisation of voronoi could be to use the last point as the intial point for the next search.
93
- const handleMouseMove = event => {
91
+ function getClosestPoint(event) {
94
92
  var _voronoiRef$current$d;
95
93
  // Get mouse coordinate in global SVG space
96
94
  const svgPoint = getSVGPoint(svgRef.current, event);
97
95
  const outsideX = svgPoint.x < left || svgPoint.x > left + width;
98
96
  const outsideY = svgPoint.y < top || svgPoint.y > top + height;
99
97
  if (outsideX || outsideY) {
100
- dispatch({
101
- type: 'exitChart'
102
- });
103
- return;
98
+ return 'outside-chart';
104
99
  }
105
100
  if (!voronoiRef.current.delauney) {
106
- return;
101
+ return 'no-point-found';
107
102
  }
108
103
  const closestPointIndex = (_voronoiRef$current$d = voronoiRef.current.delauney) == null ? void 0 : _voronoiRef$current$d.find(svgPoint.x, svgPoint.y);
109
- if (closestPointIndex !== undefined) {
110
- const seriesId = Object.keys(voronoiRef.current).find(id => {
111
- if (id === 'delauney') {
112
- return false;
113
- }
114
- return 2 * closestPointIndex >= voronoiRef.current[id].startIndex && 2 * closestPointIndex < voronoiRef.current[id].endIndex;
115
- });
116
- if (seriesId === undefined) {
117
- return;
104
+ if (closestPointIndex === undefined) {
105
+ return 'no-point-found';
106
+ }
107
+ const seriesId = Object.keys(voronoiRef.current).find(id => {
108
+ if (id === 'delauney') {
109
+ return false;
118
110
  }
119
- const dataIndex = (2 * closestPointIndex - voronoiRef.current[seriesId].startIndex) / 2;
120
- if (voronoiMaxRadius !== undefined) {
121
- const pointX = voronoiRef.current.delauney.points[2 * closestPointIndex];
122
- const pointY = voronoiRef.current.delauney.points[2 * closestPointIndex + 1];
123
- const dist2 = (pointX - svgPoint.x) ** 2 + (pointY - svgPoint.y) ** 2;
124
- if (dist2 > voronoiMaxRadius ** 2) {
125
- // The closest point is too far to be considered.
126
- dispatch({
127
- type: 'leaveItem',
128
- data: {
129
- type: 'scatter',
130
- seriesId,
131
- dataIndex
132
- }
133
- });
134
- return;
135
- }
111
+ return 2 * closestPointIndex >= voronoiRef.current[id].startIndex && 2 * closestPointIndex < voronoiRef.current[id].endIndex;
112
+ });
113
+ if (seriesId === undefined) {
114
+ return 'no-point-found';
115
+ }
116
+ const dataIndex = (2 * closestPointIndex - voronoiRef.current[seriesId].startIndex) / 2;
117
+ if (voronoiMaxRadius !== undefined) {
118
+ const pointX = voronoiRef.current.delauney.points[2 * closestPointIndex];
119
+ const pointY = voronoiRef.current.delauney.points[2 * closestPointIndex + 1];
120
+ const dist2 = (pointX - svgPoint.x) ** 2 + (pointY - svgPoint.y) ** 2;
121
+ if (dist2 > voronoiMaxRadius ** 2) {
122
+ // The closest point is too far to be considered.
123
+ return 'outside-voronoi-max-radius';
136
124
  }
125
+ }
126
+ return {
127
+ seriesId,
128
+ dataIndex
129
+ };
130
+ }
131
+ const handleMouseOut = () => {
132
+ dispatch({
133
+ type: 'exitChart'
134
+ });
135
+ };
136
+ const handleMouseMove = event => {
137
+ const closestPoint = getClosestPoint(event);
138
+ if (closestPoint === 'outside-chart') {
139
+ dispatch({
140
+ type: 'exitChart'
141
+ });
142
+ return;
143
+ }
144
+ if (closestPoint === 'outside-voronoi-max-radius' || closestPoint === 'no-point-found') {
137
145
  dispatch({
138
- type: 'enterItem',
146
+ type: 'leaveItem',
139
147
  data: {
140
- type: 'scatter',
141
- seriesId,
142
- dataIndex
148
+ type: 'scatter'
143
149
  }
144
150
  });
151
+ return;
145
152
  }
153
+ const {
154
+ seriesId,
155
+ dataIndex
156
+ } = closestPoint;
157
+ dispatch({
158
+ type: 'enterItem',
159
+ data: {
160
+ type: 'scatter',
161
+ seriesId,
162
+ dataIndex
163
+ }
164
+ });
165
+ };
166
+ const handleMouseClick = event => {
167
+ if (!onItemClick) {
168
+ return;
169
+ }
170
+ const closestPoint = getClosestPoint(event);
171
+ if (typeof closestPoint === 'string') {
172
+ // No point fond for any reason
173
+ return;
174
+ }
175
+ const {
176
+ seriesId,
177
+ dataIndex
178
+ } = closestPoint;
179
+ onItemClick(event, {
180
+ type: 'scatter',
181
+ seriesId,
182
+ dataIndex
183
+ });
146
184
  };
147
185
  element.addEventListener('mouseout', handleMouseOut);
148
186
  element.addEventListener('mousemove', handleMouseMove);
187
+ element.addEventListener('click', handleMouseClick);
149
188
  return () => {
150
189
  element.removeEventListener('mouseout', handleMouseOut);
151
190
  element.removeEventListener('mousemove', handleMouseMove);
191
+ element.removeEventListener('click', handleMouseClick);
152
192
  };
153
- }, [svgRef, dispatch, left, width, top, height, yAxis, xAxis, voronoiMaxRadius]);
154
- return /*#__PURE__*/_jsx("g", {}); // Workaround to fix docs scripts
193
+ }, [svgRef, dispatch, left, width, top, height, yAxis, xAxis, voronoiMaxRadius, onItemClick]);
194
+
195
+ // eslint-disable-next-line react/jsx-no-useless-fragment
196
+ return /*#__PURE__*/_jsx(React.Fragment, {});
155
197
  }
156
198
  process.env.NODE_ENV !== "production" ? ChartsVoronoiHandler.propTypes = {
157
199
  // ----------------------------- Warning --------------------------------
158
200
  // | These PropTypes are generated from the TypeScript type definitions |
159
201
  // | To update them edit the TypeScript types and run "yarn proptypes" |
160
202
  // ----------------------------------------------------------------------
203
+ /**
204
+ * Callback fired when clicking on a scatter item.
205
+ * @param {MouseEvent} event Mouse event catched at the svg level
206
+ * @param {ScatterItemIdentifier} scatterItemIdentifier Identify whihc item got clicked
207
+ */
208
+ onItemClick: PropTypes.func,
161
209
  /**
162
210
  * Defines the maximal distance between a scatter point and the pointer that triggers the interaction.
163
211
  * If `undefined`, the radius is assumed to be infinite.