@mui/x-charts 7.2.0 → 7.3.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 (188) hide show
  1. package/BarChart/BarChart.js +32 -2
  2. package/BarChart/BarPlot.js +12 -5
  3. package/BarChart/getColor.d.ts +3 -0
  4. package/BarChart/getColor.js +33 -0
  5. package/CHANGELOG.md +125 -0
  6. package/ChartContainer/ChartContainer.js +35 -4
  7. package/ChartsAxis/ChartsAxis.js +4 -4
  8. package/ChartsClipPath/ChartsClipPath.js +2 -2
  9. package/ChartsLegend/ChartsLegend.js +2 -2
  10. package/ChartsTooltip/ChartsAxisTooltipContent.d.ts +4 -1
  11. package/ChartsTooltip/ChartsAxisTooltipContent.js +7 -2
  12. package/ChartsTooltip/ChartsItemTooltipContent.d.ts +6 -0
  13. package/ChartsTooltip/ChartsItemTooltipContent.js +15 -1
  14. package/ChartsTooltip/DefaultChartsAxisTooltipContent.js +6 -3
  15. package/ChartsTooltip/DefaultChartsItemTooltipContent.js +13 -4
  16. package/ChartsTooltip/utils.d.ts +5 -1
  17. package/ChartsVoronoiHandler/ChartsVoronoiHandler.js +6 -6
  18. package/ChartsXAxis/ChartsXAxis.js +2 -2
  19. package/ChartsYAxis/ChartsYAxis.js +2 -2
  20. package/Gauge/GaugeProvider.js +5 -5
  21. package/LineChart/AnimatedArea.js +6 -5
  22. package/LineChart/AnimatedLine.js +7 -5
  23. package/LineChart/AreaElement.d.ts +1 -0
  24. package/LineChart/AreaElement.js +4 -1
  25. package/LineChart/AreaPlot.js +7 -1
  26. package/LineChart/LineChart.js +32 -2
  27. package/LineChart/LineElement.d.ts +1 -0
  28. package/LineChart/LineElement.js +4 -1
  29. package/LineChart/LineHighlightPlot.js +4 -1
  30. package/LineChart/LinePlot.js +7 -1
  31. package/LineChart/MarkPlot.js +5 -5
  32. package/LineChart/getColor.d.ts +3 -0
  33. package/LineChart/getColor.js +31 -0
  34. package/PieChart/PieChart.js +32 -2
  35. package/PieChart/PiePlot.js +26 -6
  36. package/PieChart/formatter.js +4 -2
  37. package/PieChart/getColor.d.ts +2 -0
  38. package/PieChart/getColor.js +11 -0
  39. package/PieChart/getPieCoordinates.d.ts +7 -0
  40. package/PieChart/getPieCoordinates.js +25 -0
  41. package/PieChart/index.d.ts +1 -0
  42. package/PieChart/index.js +11 -0
  43. package/ResponsiveChartContainer/ResponsiveChartContainer.js +32 -2
  44. package/ScatterChart/Scatter.d.ts +1 -0
  45. package/ScatterChart/Scatter.js +6 -3
  46. package/ScatterChart/ScatterChart.js +32 -2
  47. package/ScatterChart/ScatterPlot.js +3 -0
  48. package/ScatterChart/getColor.d.ts +3 -0
  49. package/ScatterChart/getColor.js +31 -0
  50. package/SparkLineChart/SparkLineChart.js +15 -0
  51. package/context/CartesianContextProvider.d.ts +2 -2
  52. package/context/CartesianContextProvider.js +23 -8
  53. package/esm/BarChart/BarChart.js +32 -2
  54. package/esm/BarChart/BarPlot.js +13 -5
  55. package/esm/BarChart/getColor.js +27 -0
  56. package/esm/ChartContainer/ChartContainer.js +36 -4
  57. package/esm/ChartsAxis/ChartsAxis.js +4 -4
  58. package/esm/ChartsClipPath/ChartsClipPath.js +2 -2
  59. package/esm/ChartsLegend/ChartsLegend.js +2 -2
  60. package/esm/ChartsTooltip/ChartsAxisTooltipContent.js +7 -2
  61. package/esm/ChartsTooltip/ChartsItemTooltipContent.js +15 -1
  62. package/esm/ChartsTooltip/DefaultChartsAxisTooltipContent.js +6 -3
  63. package/esm/ChartsTooltip/DefaultChartsItemTooltipContent.js +13 -4
  64. package/esm/ChartsVoronoiHandler/ChartsVoronoiHandler.js +6 -6
  65. package/esm/ChartsXAxis/ChartsXAxis.js +2 -2
  66. package/esm/ChartsYAxis/ChartsYAxis.js +2 -2
  67. package/esm/Gauge/GaugeProvider.js +5 -5
  68. package/esm/LineChart/AnimatedArea.js +6 -5
  69. package/esm/LineChart/AnimatedLine.js +7 -5
  70. package/esm/LineChart/AreaElement.js +4 -1
  71. package/esm/LineChart/AreaPlot.js +7 -1
  72. package/esm/LineChart/LineChart.js +32 -2
  73. package/esm/LineChart/LineElement.js +4 -1
  74. package/esm/LineChart/LineHighlightPlot.js +4 -1
  75. package/esm/LineChart/LinePlot.js +7 -1
  76. package/esm/LineChart/MarkPlot.js +5 -5
  77. package/esm/LineChart/getColor.js +25 -0
  78. package/esm/PieChart/PieChart.js +32 -2
  79. package/esm/PieChart/PiePlot.js +26 -6
  80. package/esm/PieChart/formatter.js +4 -2
  81. package/esm/PieChart/getColor.js +5 -0
  82. package/esm/PieChart/getPieCoordinates.js +19 -0
  83. package/esm/PieChart/index.js +2 -1
  84. package/esm/ResponsiveChartContainer/ResponsiveChartContainer.js +32 -2
  85. package/esm/ScatterChart/Scatter.js +6 -3
  86. package/esm/ScatterChart/ScatterChart.js +32 -2
  87. package/esm/ScatterChart/ScatterPlot.js +3 -0
  88. package/esm/ScatterChart/getColor.js +25 -0
  89. package/esm/SparkLineChart/SparkLineChart.js +15 -0
  90. package/esm/context/CartesianContextProvider.js +23 -8
  91. package/esm/hooks/index.js +4 -1
  92. package/esm/hooks/useAxisEvents.js +7 -6
  93. package/esm/hooks/useChartId.js +8 -0
  94. package/esm/hooks/useSeries.js +64 -0
  95. package/esm/hooks/useSvgRef.js +9 -0
  96. package/esm/internals/colorGetter.js +22 -0
  97. package/esm/internals/colorScale.js +16 -0
  98. package/esm/internals/components/ChartsAxesGradients/ChartsAxesGradients.js +96 -0
  99. package/esm/internals/components/ChartsAxesGradients/ChartsContinuousGradient.js +55 -0
  100. package/esm/internals/components/ChartsAxesGradients/ChartsPiecewiseGradient.js +41 -0
  101. package/esm/internals/components/ChartsAxesGradients/index.js +1 -0
  102. package/esm/models/colorMapping.js +1 -0
  103. package/esm/models/seriesType/index.js +6 -1
  104. package/hooks/index.d.ts +3 -0
  105. package/hooks/index.js +65 -1
  106. package/hooks/useAxisEvents.js +7 -6
  107. package/hooks/useChartId.d.ts +1 -0
  108. package/hooks/useChartId.js +16 -0
  109. package/hooks/useSeries.d.ts +45 -0
  110. package/hooks/useSeries.js +75 -0
  111. package/hooks/useSvgRef.d.ts +2 -0
  112. package/hooks/useSvgRef.js +17 -0
  113. package/index.js +1 -1
  114. package/internals/colorGetter.d.ts +5 -0
  115. package/internals/colorGetter.js +29 -0
  116. package/internals/colorScale.d.ts +5 -0
  117. package/internals/colorScale.js +24 -0
  118. package/internals/components/ChartsAxesGradients/ChartsAxesGradients.d.ts +3 -0
  119. package/internals/components/ChartsAxesGradients/ChartsAxesGradients.js +105 -0
  120. package/internals/components/ChartsAxesGradients/ChartsContinuousGradient.d.ts +13 -0
  121. package/internals/components/ChartsAxesGradients/ChartsContinuousGradient.js +63 -0
  122. package/internals/components/ChartsAxesGradients/ChartsPiecewiseGradient.d.ts +12 -0
  123. package/internals/components/ChartsAxesGradients/ChartsPiecewiseGradient.js +48 -0
  124. package/internals/components/ChartsAxesGradients/index.d.ts +1 -0
  125. package/internals/components/ChartsAxesGradients/index.js +16 -0
  126. package/internals/defaultizeColor.d.ts +4 -4
  127. package/internals/defaultizeValueFormatter.d.ts +5 -5
  128. package/internals/getScale.d.ts +2 -2
  129. package/models/axis.d.ts +44 -4
  130. package/models/colorMapping.d.ts +45 -0
  131. package/models/colorMapping.js +5 -0
  132. package/models/index.d.ts +1 -1
  133. package/models/seriesType/common.d.ts +10 -2
  134. package/models/seriesType/index.d.ts +2 -0
  135. package/models/seriesType/index.js +17 -1
  136. package/modern/BarChart/BarChart.js +32 -2
  137. package/modern/BarChart/BarPlot.js +13 -5
  138. package/modern/BarChart/getColor.js +27 -0
  139. package/modern/ChartContainer/ChartContainer.js +36 -4
  140. package/modern/ChartsAxis/ChartsAxis.js +4 -4
  141. package/modern/ChartsClipPath/ChartsClipPath.js +2 -2
  142. package/modern/ChartsLegend/ChartsLegend.js +2 -2
  143. package/modern/ChartsTooltip/ChartsAxisTooltipContent.js +7 -2
  144. package/modern/ChartsTooltip/ChartsItemTooltipContent.js +15 -1
  145. package/modern/ChartsTooltip/DefaultChartsAxisTooltipContent.js +6 -3
  146. package/modern/ChartsTooltip/DefaultChartsItemTooltipContent.js +13 -4
  147. package/modern/ChartsVoronoiHandler/ChartsVoronoiHandler.js +6 -6
  148. package/modern/ChartsXAxis/ChartsXAxis.js +2 -2
  149. package/modern/ChartsYAxis/ChartsYAxis.js +2 -2
  150. package/modern/Gauge/GaugeProvider.js +5 -5
  151. package/modern/LineChart/AnimatedArea.js +6 -5
  152. package/modern/LineChart/AnimatedLine.js +7 -5
  153. package/modern/LineChart/AreaElement.js +4 -1
  154. package/modern/LineChart/AreaPlot.js +7 -1
  155. package/modern/LineChart/LineChart.js +32 -2
  156. package/modern/LineChart/LineElement.js +4 -1
  157. package/modern/LineChart/LineHighlightPlot.js +4 -1
  158. package/modern/LineChart/LinePlot.js +7 -1
  159. package/modern/LineChart/MarkPlot.js +5 -5
  160. package/modern/LineChart/getColor.js +25 -0
  161. package/modern/PieChart/PieChart.js +32 -2
  162. package/modern/PieChart/PiePlot.js +26 -6
  163. package/modern/PieChart/formatter.js +4 -2
  164. package/modern/PieChart/getColor.js +5 -0
  165. package/modern/PieChart/getPieCoordinates.js +19 -0
  166. package/modern/PieChart/index.js +2 -1
  167. package/modern/ResponsiveChartContainer/ResponsiveChartContainer.js +32 -2
  168. package/modern/ScatterChart/Scatter.js +6 -3
  169. package/modern/ScatterChart/ScatterChart.js +32 -2
  170. package/modern/ScatterChart/ScatterPlot.js +3 -0
  171. package/modern/ScatterChart/getColor.js +25 -0
  172. package/modern/SparkLineChart/SparkLineChart.js +15 -0
  173. package/modern/context/CartesianContextProvider.js +23 -8
  174. package/modern/hooks/index.js +4 -1
  175. package/modern/hooks/useAxisEvents.js +7 -6
  176. package/modern/hooks/useChartId.js +8 -0
  177. package/modern/hooks/useSeries.js +64 -0
  178. package/modern/hooks/useSvgRef.js +9 -0
  179. package/modern/index.js +1 -1
  180. package/modern/internals/colorGetter.js +22 -0
  181. package/modern/internals/colorScale.js +16 -0
  182. package/modern/internals/components/ChartsAxesGradients/ChartsAxesGradients.js +96 -0
  183. package/modern/internals/components/ChartsAxesGradients/ChartsContinuousGradient.js +55 -0
  184. package/modern/internals/components/ChartsAxesGradients/ChartsPiecewiseGradient.js +41 -0
  185. package/modern/internals/components/ChartsAxesGradients/index.js +1 -0
  186. package/modern/models/colorMapping.js +1 -0
  187. package/modern/models/seriesType/index.js +6 -1
  188. package/package.json +1 -1
@@ -0,0 +1,27 @@
1
+ export default function getColor(series, xAxis, yAxis) {
2
+ const verticalLayout = series.layout === 'vertical';
3
+ const bandColorScale = verticalLayout ? xAxis.colorScale : yAxis.colorScale;
4
+ const valueColorScale = verticalLayout ? yAxis.colorScale : xAxis.colorScale;
5
+ const bandValues = verticalLayout ? xAxis.data : yAxis.data;
6
+ if (valueColorScale) {
7
+ return dataIndex => {
8
+ const value = series.data[dataIndex];
9
+ const color = value === null ? series.color : valueColorScale(value);
10
+ if (color === null) {
11
+ return series.color;
12
+ }
13
+ return color;
14
+ };
15
+ }
16
+ if (bandColorScale) {
17
+ return dataIndex => {
18
+ const value = bandValues[dataIndex];
19
+ const color = value === null ? series.color : bandColorScale(value);
20
+ if (color === null) {
21
+ return series.color;
22
+ }
23
+ return color;
24
+ };
25
+ }
26
+ return () => series.color;
27
+ }
@@ -8,7 +8,9 @@ import { useReducedMotion } from '../hooks/useReducedMotion';
8
8
  import { ChartsSurface } from '../ChartsSurface';
9
9
  import { CartesianContextProvider } from '../context/CartesianContextProvider';
10
10
  import { HighlightProvider } from '../context/HighlightProvider';
11
+ import { ChartsAxesGradients } from '../internals/components/ChartsAxesGradients';
11
12
  import { jsx as _jsx } from "react/jsx-runtime";
13
+ import { jsxs as _jsxs } from "react/jsx-runtime";
12
14
  const ChartContainer = /*#__PURE__*/React.forwardRef(function ChartContainer(props, ref) {
13
15
  const {
14
16
  width,
@@ -44,7 +46,7 @@ const ChartContainer = /*#__PURE__*/React.forwardRef(function ChartContainer(pro
44
46
  dataset: dataset,
45
47
  children: /*#__PURE__*/_jsx(InteractionProvider, {
46
48
  children: /*#__PURE__*/_jsx(HighlightProvider, {
47
- children: /*#__PURE__*/_jsx(ChartsSurface, {
49
+ children: /*#__PURE__*/_jsxs(ChartsSurface, {
48
50
  width: width,
49
51
  height: height,
50
52
  ref: handleRef,
@@ -52,7 +54,7 @@ const ChartContainer = /*#__PURE__*/React.forwardRef(function ChartContainer(pro
52
54
  title: title,
53
55
  desc: desc,
54
56
  disableAxisListener: disableAxisListener,
55
- children: children
57
+ children: [/*#__PURE__*/_jsx(ChartsAxesGradients, {}), children]
56
58
  })
57
59
  })
58
60
  })
@@ -119,11 +121,26 @@ process.env.NODE_ENV !== "production" ? ChartContainer.propTypes = {
119
121
  width: PropTypes.number.isRequired,
120
122
  /**
121
123
  * The configuration of the x-axes.
122
- * If not provided, a default axis config is used with id set to `DEFAULT_X_AXIS_KEY`.
124
+ * If not provided, a default axis config is used.
123
125
  */
124
126
  xAxis: PropTypes.arrayOf(PropTypes.shape({
125
127
  axisId: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
126
128
  classes: PropTypes.object,
129
+ colorMap: PropTypes.oneOfType([PropTypes.shape({
130
+ color: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.string.isRequired), PropTypes.func]).isRequired,
131
+ max: PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number]),
132
+ min: PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number]),
133
+ type: PropTypes.oneOf(['continuous']).isRequired
134
+ }), PropTypes.shape({
135
+ colors: PropTypes.arrayOf(PropTypes.string).isRequired,
136
+ thresholds: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number]).isRequired).isRequired,
137
+ type: PropTypes.oneOf(['piecewise']).isRequired
138
+ }), PropTypes.shape({
139
+ colors: PropTypes.arrayOf(PropTypes.string).isRequired,
140
+ type: PropTypes.oneOf(['ordinal']).isRequired,
141
+ unknownColor: PropTypes.string,
142
+ values: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number, PropTypes.string]).isRequired)
143
+ })]),
127
144
  data: PropTypes.array,
128
145
  dataKey: PropTypes.string,
129
146
  disableLine: PropTypes.bool,
@@ -156,11 +173,26 @@ process.env.NODE_ENV !== "production" ? ChartContainer.propTypes = {
156
173
  })),
157
174
  /**
158
175
  * The configuration of the y-axes.
159
- * If not provided, a default axis config is used with id set to `DEFAULT_Y_AXIS_KEY`.
176
+ * If not provided, a default axis config is used.
160
177
  */
161
178
  yAxis: PropTypes.arrayOf(PropTypes.shape({
162
179
  axisId: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
163
180
  classes: PropTypes.object,
181
+ colorMap: PropTypes.oneOfType([PropTypes.shape({
182
+ color: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.string.isRequired), PropTypes.func]).isRequired,
183
+ max: PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number]),
184
+ min: PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number]),
185
+ type: PropTypes.oneOf(['continuous']).isRequired
186
+ }), PropTypes.shape({
187
+ colors: PropTypes.arrayOf(PropTypes.string).isRequired,
188
+ thresholds: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number]).isRequired).isRequired,
189
+ type: PropTypes.oneOf(['piecewise']).isRequired
190
+ }), PropTypes.shape({
191
+ colors: PropTypes.arrayOf(PropTypes.string).isRequired,
192
+ type: PropTypes.oneOf(['ordinal']).isRequired,
193
+ unknownColor: PropTypes.string,
194
+ values: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number, PropTypes.string]).isRequired)
195
+ })]),
164
196
  data: PropTypes.array,
165
197
  dataKey: PropTypes.string,
166
198
  disableLine: PropTypes.bool,
@@ -6,12 +6,12 @@ import { ChartsXAxis } from '../ChartsXAxis';
6
6
  import { ChartsYAxis } from '../ChartsYAxis';
7
7
  import { jsx as _jsx } from "react/jsx-runtime";
8
8
  import { jsxs as _jsxs } from "react/jsx-runtime";
9
- const getAxisId = propsValue => {
9
+ const getAxisId = (propsValue, defaultAxisId) => {
10
10
  if (propsValue == null) {
11
11
  return null;
12
12
  }
13
13
  if (typeof propsValue === 'object') {
14
- return propsValue.axisId ?? null;
14
+ return propsValue.axisId ?? defaultAxisId ?? null;
15
15
  }
16
16
  return propsValue;
17
17
  };
@@ -55,8 +55,8 @@ function ChartsAxis(props) {
55
55
 
56
56
  const leftId = getAxisId(leftAxis === undefined ? yAxisIds[0] : leftAxis);
57
57
  const bottomId = getAxisId(bottomAxis === undefined ? xAxisIds[0] : bottomAxis);
58
- const topId = getAxisId(topAxis);
59
- const rightId = getAxisId(rightAxis);
58
+ const topId = getAxisId(topAxis, xAxisIds[0]);
59
+ const rightId = getAxisId(rightAxis, yAxisIds[0]);
60
60
  if (topId !== null && !xAxis[topId]) {
61
61
  throw Error([`MUI X Charts: id used for top axis "${topId}" is not defined.`, `Available ids are: ${xAxisIds.join(', ')}.`].join('\n'));
62
62
  }
@@ -1,7 +1,7 @@
1
1
  import _extends from "@babel/runtime/helpers/esm/extends";
2
2
  import * as React from 'react';
3
3
  import PropTypes from 'prop-types';
4
- import { DrawingContext } from '../context/DrawingProvider';
4
+ import { useDrawingArea } from '../hooks/useDrawingArea';
5
5
  import { jsx as _jsx } from "react/jsx-runtime";
6
6
  /**
7
7
  * API:
@@ -18,7 +18,7 @@ function ChartsClipPath(props) {
18
18
  top,
19
19
  width,
20
20
  height
21
- } = React.useContext(DrawingContext);
21
+ } = useDrawingArea();
22
22
  const offset = _extends({
23
23
  top: 0,
24
24
  right: 0,
@@ -4,11 +4,11 @@ import PropTypes from 'prop-types';
4
4
  import { useSlotProps } from '@mui/base/utils';
5
5
  import { unstable_composeClasses as composeClasses } from '@mui/utils';
6
6
  import { useThemeProps, useTheme } from '@mui/material/styles';
7
- import { DrawingContext } from '../context/DrawingProvider';
8
7
  import { getSeriesToDisplay } from './utils';
9
8
  import { SeriesContext } from '../context/SeriesContextProvider';
10
9
  import { getLegendUtilityClass } from './chartsLegendClasses';
11
10
  import { DefaultChartsLegend } from './DefaultChartsLegend';
11
+ import { useDrawingArea } from '../hooks';
12
12
  import { jsx as _jsx } from "react/jsx-runtime";
13
13
  const useUtilityClasses = ownerState => {
14
14
  const {
@@ -46,7 +46,7 @@ function ChartsLegend(inProps) {
46
46
  const classes = useUtilityClasses(_extends({}, props, {
47
47
  theme
48
48
  }));
49
- const drawingArea = React.useContext(DrawingContext);
49
+ const drawingArea = useDrawingArea();
50
50
  const series = React.useContext(SeriesContext);
51
51
  const seriesToDisplay = getSeriesToDisplay(series);
52
52
  const ChartLegendRender = slots?.legend ?? DefaultChartsLegend;
@@ -6,6 +6,7 @@ import { SeriesContext } from '../context/SeriesContextProvider';
6
6
  import { CartesianContext } from '../context/CartesianContextProvider';
7
7
  import { DefaultChartsAxisTooltipContent } from './DefaultChartsAxisTooltipContent';
8
8
  import { isCartesianSeriesType } from './utils';
9
+ import colorGetter from '../internals/colorGetter';
9
10
  import { jsx as _jsx } from "react/jsx-runtime";
10
11
  function ChartsAxisTooltipContent(props) {
11
12
  const {
@@ -33,12 +34,16 @@ function ChartsAxisTooltipContent(props) {
33
34
  const item = series[seriesType].series[seriesId];
34
35
  const axisKey = isXaxis ? item.xAxisKey : item.yAxisKey;
35
36
  if (axisKey === undefined || axisKey === USED_AXIS_ID) {
36
- rep.push(series[seriesType].series[seriesId]);
37
+ const seriesToAdd = series[seriesType].series[seriesId];
38
+ const color = colorGetter(seriesToAdd, xAxis[seriesToAdd.xAxisKey ?? xAxisIds[0]], yAxis[seriesToAdd.yAxisKey ?? yAxisIds[0]]);
39
+ rep.push(_extends({}, seriesToAdd, {
40
+ getColor: color
41
+ }));
37
42
  }
38
43
  });
39
44
  });
40
45
  return rep;
41
- }, [USED_AXIS_ID, isXaxis, series]);
46
+ }, [USED_AXIS_ID, isXaxis, series, xAxis, xAxisIds, yAxis, yAxisIds]);
42
47
  const relevantAxis = React.useMemo(() => {
43
48
  return isXaxis ? xAxis[USED_AXIS_ID] : yAxis[USED_AXIS_ID];
44
49
  }, [USED_AXIS_ID, isXaxis, xAxis, yAxis]);
@@ -4,6 +4,8 @@ import PropTypes from 'prop-types';
4
4
  import { useSlotProps } from '@mui/base/utils';
5
5
  import { SeriesContext } from '../context/SeriesContextProvider';
6
6
  import { DefaultChartsItemTooltipContent } from './DefaultChartsItemTooltipContent';
7
+ import { CartesianContext } from '../context/CartesianContextProvider';
8
+ import colorGetter from '../internals/colorGetter';
7
9
  import { jsx as _jsx } from "react/jsx-runtime";
8
10
  function ChartsItemTooltipContent(props) {
9
11
  const {
@@ -14,6 +16,16 @@ function ChartsItemTooltipContent(props) {
14
16
  contentProps
15
17
  } = props;
16
18
  const series = React.useContext(SeriesContext)[itemData.type].series[itemData.seriesId];
19
+ const axisData = React.useContext(CartesianContext);
20
+ const {
21
+ xAxis,
22
+ yAxis,
23
+ xAxisIds,
24
+ yAxisIds
25
+ } = axisData;
26
+ const defaultXAxisId = xAxisIds[0];
27
+ const defaultYAxisId = yAxisIds[0];
28
+ const getColor = series.type === 'pie' ? colorGetter(series) : colorGetter(series, xAxis[series.xAxisKey ?? defaultXAxisId], yAxis[series.yAxisKey ?? defaultYAxisId]);
17
29
  const Content = content ?? DefaultChartsItemTooltipContent;
18
30
  const chartTooltipContentProps = useSlotProps({
19
31
  elementType: Content,
@@ -22,7 +34,8 @@ function ChartsItemTooltipContent(props) {
22
34
  itemData,
23
35
  series,
24
36
  sx,
25
- classes
37
+ classes,
38
+ getColor
26
39
  },
27
40
  ownerState: {}
28
41
  });
@@ -37,6 +50,7 @@ process.env.NODE_ENV !== "production" ? ChartsItemTooltipContent.propTypes = {
37
50
  content: PropTypes.elementType,
38
51
  contentProps: PropTypes.shape({
39
52
  classes: PropTypes.object,
53
+ getColor: PropTypes.func,
40
54
  itemData: PropTypes.shape({
41
55
  dataIndex: PropTypes.number,
42
56
  seriesId: PropTypes.oneOfType([PropTypes.number, PropTypes.string]).isRequired,
@@ -41,10 +41,13 @@ function DefaultChartsAxisTooltipContent(props) {
41
41
  id,
42
42
  label,
43
43
  valueFormatter,
44
- data
44
+ data,
45
+ getColor
45
46
  }) => {
46
47
  // @ts-ignore
47
- const formattedValue = valueFormatter(data[dataIndex] ?? null);
48
+ const formattedValue = valueFormatter(data[dataIndex] ?? null, {
49
+ dataIndex
50
+ });
48
51
  if (formattedValue == null) {
49
52
  return null;
50
53
  }
@@ -54,7 +57,7 @@ function DefaultChartsAxisTooltipContent(props) {
54
57
  className: clsx(classes.markCell, classes.cell),
55
58
  children: /*#__PURE__*/_jsx(ChartsTooltipMark, {
56
59
  ownerState: {
57
- color
60
+ color: getColor(dataIndex) ?? color
58
61
  },
59
62
  boxShadow: 1,
60
63
  className: classes.mark
@@ -9,7 +9,8 @@ function DefaultChartsItemTooltipContent(props) {
9
9
  series,
10
10
  itemData,
11
11
  sx,
12
- classes
12
+ classes,
13
+ getColor
13
14
  } = props;
14
15
  if (itemData.dataIndex === undefined || !series.data[itemData.dataIndex]) {
15
16
  return null;
@@ -18,14 +19,16 @@ function DefaultChartsItemTooltipContent(props) {
18
19
  displayedLabel,
19
20
  color
20
21
  } = series.type === 'pie' ? {
21
- color: series.data[itemData.dataIndex].color,
22
+ color: getColor(itemData.dataIndex),
22
23
  displayedLabel: series.data[itemData.dataIndex].label
23
24
  } : {
24
- color: series.color,
25
+ color: getColor(itemData.dataIndex) ?? series.color,
25
26
  displayedLabel: series.label
26
27
  };
27
28
  const value = series.data[itemData.dataIndex];
28
- const formattedValue = series.valueFormatter?.(value);
29
+ const formattedValue = series.valueFormatter?.(value, {
30
+ dataIndex: itemData.dataIndex
31
+ });
29
32
  return /*#__PURE__*/_jsx(ChartsTooltipPaper, {
30
33
  sx: sx,
31
34
  className: classes.root,
@@ -63,6 +66,12 @@ process.env.NODE_ENV !== "production" ? DefaultChartsItemTooltipContent.propType
63
66
  * Override or extend the styles applied to the component.
64
67
  */
65
68
  classes: PropTypes.object.isRequired,
69
+ /**
70
+ * Get the color of the item with index `dataIndex`.
71
+ * @param {number} dataIndex The data index of the item.
72
+ * @returns {string} The color to display.
73
+ */
74
+ getColor: PropTypes.func.isRequired,
66
75
  /**
67
76
  * The data used to identify the triggered item.
68
77
  */
@@ -4,23 +4,23 @@ import { Delaunay } from 'd3-delaunay';
4
4
  import useEnhancedEffect from '@mui/utils/useEnhancedEffect';
5
5
  import { InteractionContext } from '../context/InteractionProvider';
6
6
  import { CartesianContext } from '../context/CartesianContextProvider';
7
- import { SvgContext, DrawingContext } from '../context/DrawingProvider';
8
7
  import { SeriesContext } from '../context/SeriesContextProvider';
9
8
  import { getValueToPositionMapper } from '../hooks/useScale';
10
9
  import { getSVGPoint } from '../internals/utils';
10
+ import { useDrawingArea, useSvgRef } from '../hooks';
11
11
  import { jsx as _jsx } from "react/jsx-runtime";
12
12
  function ChartsVoronoiHandler(props) {
13
13
  const {
14
14
  voronoiMaxRadius,
15
15
  onItemClick
16
16
  } = props;
17
- const svgRef = React.useContext(SvgContext);
17
+ const svgRef = useSvgRef();
18
18
  const {
19
- width,
20
- height,
19
+ left,
21
20
  top,
22
- left
23
- } = React.useContext(DrawingContext);
21
+ width,
22
+ height
23
+ } = useDrawingArea();
24
24
  const {
25
25
  xAxis,
26
26
  yAxis,
@@ -7,13 +7,13 @@ import { useSlotProps } from '@mui/base/utils';
7
7
  import { unstable_composeClasses as composeClasses } from '@mui/utils';
8
8
  import { useThemeProps, useTheme } from '@mui/material/styles';
9
9
  import { CartesianContext } from '../context/CartesianContextProvider';
10
- import { DrawingContext } from '../context/DrawingProvider';
11
10
  import { useTicks } from '../hooks/useTicks';
12
11
  import { getAxisUtilityClass } from '../ChartsAxis/axisClasses';
13
12
  import { AxisRoot } from '../internals/components/AxisSharedComponents';
14
13
  import { ChartsText } from '../ChartsText';
15
14
  import { getMinXTranslation } from '../internals/geometry';
16
15
  import { useMounted } from '../hooks/useMounted';
16
+ import { useDrawingArea } from '../hooks/useDrawingArea';
17
17
  import { getWordsByLines } from '../internals/getWordsByLines';
18
18
  import { jsx as _jsx } from "react/jsx-runtime";
19
19
  import { jsxs as _jsxs } from "react/jsx-runtime";
@@ -153,7 +153,7 @@ function ChartsXAxis(inProps) {
153
153
  top,
154
154
  width,
155
155
  height
156
- } = React.useContext(DrawingContext);
156
+ } = useDrawingArea();
157
157
  const tickSize = disableTicks ? 4 : tickSizeProp;
158
158
  const positionSign = position === 'bottom' ? 1 : -1;
159
159
  const Line = slots?.axisLine ?? 'line';
@@ -7,8 +7,8 @@ import { useSlotProps } from '@mui/base/utils';
7
7
  import { unstable_composeClasses as composeClasses } from '@mui/utils';
8
8
  import { useThemeProps, useTheme } from '@mui/material/styles';
9
9
  import { CartesianContext } from '../context/CartesianContextProvider';
10
- import { DrawingContext } from '../context/DrawingProvider';
11
10
  import { useTicks } from '../hooks/useTicks';
11
+ import { useDrawingArea } from '../hooks/useDrawingArea';
12
12
  import { AxisRoot } from '../internals/components/AxisSharedComponents';
13
13
  import { ChartsText } from '../ChartsText';
14
14
  import { getAxisUtilityClass } from '../ChartsAxis/axisClasses';
@@ -94,7 +94,7 @@ function ChartsYAxis(inProps) {
94
94
  top,
95
95
  width,
96
96
  height
97
- } = React.useContext(DrawingContext);
97
+ } = useDrawingArea();
98
98
  const tickSize = disableTicks ? 4 : tickSizeProp;
99
99
  const yTicks = useTicks({
100
100
  scale: yScale,
@@ -1,8 +1,8 @@
1
1
  // @ignore - do not document.
2
2
  import * as React from 'react';
3
- import { DrawingContext } from '../context/DrawingProvider';
4
3
  import { getPercentageValue } from '../internals/utils';
5
4
  import { getArcRatios, getAvailableRadius } from './utils';
5
+ import { useDrawingArea } from '../hooks/useDrawingArea';
6
6
  import { jsx as _jsx } from "react/jsx-runtime";
7
7
  export const GaugeContext = /*#__PURE__*/React.createContext({
8
8
  value: null,
@@ -36,11 +36,11 @@ export function GaugeProvider(props) {
36
36
  children
37
37
  } = props;
38
38
  const {
39
- width,
40
- height,
39
+ left,
41
40
  top,
42
- left
43
- } = React.useContext(DrawingContext);
41
+ width,
42
+ height
43
+ } = useDrawingArea();
44
44
  const ratios = getArcRatios(startAngle, endAngle);
45
45
  const innerCx = cxParam ? getPercentageValue(cxParam, width) : ratios.cx * width;
46
46
  const innerCy = cyParam ? getPercentageValue(cyParam, height) : ratios.cy * height;
@@ -7,8 +7,8 @@ import { styled } from '@mui/material/styles';
7
7
  import { color as d3Color } from 'd3-color';
8
8
  import { animated, useSpring } from '@react-spring/web';
9
9
  import { useAnimatedPath } from '../internals/useAnimatedPath';
10
- import { DrawingContext } from '../context/DrawingProvider';
11
10
  import { cleanId } from '../internals/utils';
11
+ import { useChartId, useDrawingArea } from '../hooks';
12
12
  import { jsx as _jsx } from "react/jsx-runtime";
13
13
  import { jsxs as _jsxs } from "react/jsx-runtime";
14
14
  export const AreaElementPath = styled(animated.path, {
@@ -19,7 +19,7 @@ export const AreaElementPath = styled(animated.path, {
19
19
  ownerState
20
20
  }) => ({
21
21
  stroke: 'none',
22
- fill: ownerState.isHighlighted ? d3Color(ownerState.color).brighter(1).formatHex() : d3Color(ownerState.color).brighter(0.5).formatHex(),
22
+ fill: ownerState.gradientId && `url(#${ownerState.gradientId})` || ownerState.isHighlighted && d3Color(ownerState.color).brighter(1).formatHex() || d3Color(ownerState.color).brighter(0.5).formatHex(),
23
23
  transition: 'opacity 0.2s ease-in, fill 0.2s ease-in',
24
24
  opacity: ownerState.isFaded ? 0.3 : 1
25
25
  }));
@@ -46,9 +46,9 @@ function AnimatedArea(props) {
46
46
  right,
47
47
  bottom,
48
48
  width,
49
- height,
50
- chartId
51
- } = React.useContext(DrawingContext);
49
+ height
50
+ } = useDrawingArea();
51
+ const chartId = useChartId();
52
52
  const path = useAnimatedPath(d, skipAnimation);
53
53
  const {
54
54
  animatedWidth
@@ -90,6 +90,7 @@ process.env.NODE_ENV !== "production" ? AnimatedArea.propTypes = {
90
90
  ownerState: PropTypes.shape({
91
91
  classes: PropTypes.object,
92
92
  color: PropTypes.string.isRequired,
93
+ gradientId: PropTypes.string,
93
94
  id: PropTypes.oneOfType([PropTypes.number, PropTypes.string]).isRequired,
94
95
  isFaded: PropTypes.bool.isRequired,
95
96
  isHighlighted: PropTypes.bool.isRequired
@@ -7,8 +7,9 @@ import { animated, useSpring } from '@react-spring/web';
7
7
  import { color as d3Color } from 'd3-color';
8
8
  import { styled } from '@mui/material/styles';
9
9
  import { useAnimatedPath } from '../internals/useAnimatedPath';
10
- import { DrawingContext } from '../context/DrawingProvider';
11
10
  import { cleanId } from '../internals/utils';
11
+ import { useChartId } from '../hooks/useChartId';
12
+ import { useDrawingArea } from '../hooks/useDrawingArea';
12
13
  import { jsx as _jsx } from "react/jsx-runtime";
13
14
  import { jsxs as _jsxs } from "react/jsx-runtime";
14
15
  export const LineElementPath = styled(animated.path, {
@@ -21,7 +22,7 @@ export const LineElementPath = styled(animated.path, {
21
22
  strokeWidth: 2,
22
23
  strokeLinejoin: 'round',
23
24
  fill: 'none',
24
- stroke: ownerState.isHighlighted ? d3Color(ownerState.color).brighter(0.5).formatHex() : ownerState.color,
25
+ stroke: ownerState.gradientId && `url(#${ownerState.gradientId})` || ownerState.isHighlighted && d3Color(ownerState.color).brighter(0.5).formatHex() || ownerState.color,
25
26
  transition: 'opacity 0.2s ease-in, stroke 0.2s ease-in',
26
27
  opacity: ownerState.isFaded ? 0.3 : 1
27
28
  }));
@@ -48,9 +49,9 @@ function AnimatedLine(props) {
48
49
  bottom,
49
50
  width,
50
51
  height,
51
- right,
52
- chartId
53
- } = React.useContext(DrawingContext);
52
+ right
53
+ } = useDrawingArea();
54
+ const chartId = useChartId();
54
55
  const path = useAnimatedPath(d, skipAnimation);
55
56
  const {
56
57
  animatedWidth
@@ -92,6 +93,7 @@ process.env.NODE_ENV !== "production" ? AnimatedLine.propTypes = {
92
93
  ownerState: PropTypes.shape({
93
94
  classes: PropTypes.object,
94
95
  color: PropTypes.string.isRequired,
96
+ gradientId: PropTypes.string,
95
97
  id: PropTypes.oneOfType([PropTypes.number, PropTypes.string]).isRequired,
96
98
  isFaded: PropTypes.bool.isRequired,
97
99
  isHighlighted: PropTypes.bool.isRequired
@@ -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", "classes", "color", "highlightScope", "slots", "slotProps", "onClick"];
3
+ const _excluded = ["id", "classes", "color", "gradientId", "highlightScope", "slots", "slotProps", "onClick"];
4
4
  import * as React from 'react';
5
5
  import PropTypes from 'prop-types';
6
6
  import composeClasses from '@mui/utils/composeClasses';
@@ -42,6 +42,7 @@ function AreaElement(props) {
42
42
  id,
43
43
  classes: innerClasses,
44
44
  color,
45
+ gradientId,
45
46
  highlightScope,
46
47
  slots,
47
48
  slotProps,
@@ -64,6 +65,7 @@ function AreaElement(props) {
64
65
  id,
65
66
  classes: innerClasses,
66
67
  color,
68
+ gradientId,
67
69
  isFaded,
68
70
  isHighlighted
69
71
  };
@@ -92,6 +94,7 @@ process.env.NODE_ENV !== "production" ? AreaElement.propTypes = {
92
94
  classes: PropTypes.object,
93
95
  color: PropTypes.string.isRequired,
94
96
  d: PropTypes.string.isRequired,
97
+ gradientId: PropTypes.string,
95
98
  highlightScope: PropTypes.shape({
96
99
  faded: PropTypes.oneOf(['global', 'none', 'series']),
97
100
  highlighted: PropTypes.oneOf(['item', 'none', 'series'])
@@ -10,6 +10,7 @@ import { AreaElement } from './AreaElement';
10
10
  import { getValueToPositionMapper } from '../hooks/useScale';
11
11
  import getCurveFactory from '../internals/getCurve';
12
12
  import { DEFAULT_X_AXIS_KEY } from '../constants';
13
+ import { useChartGradient } from '../internals/components/ChartsAxesGradients';
13
14
  import { jsx as _jsx } from "react/jsx-runtime";
14
15
  const useAggregatedData = () => {
15
16
  const seriesData = React.useContext(SeriesContext).line;
@@ -43,6 +44,7 @@ const useAggregatedData = () => {
43
44
  const xScale = getValueToPositionMapper(xAxis[xAxisKey].scale);
44
45
  const yScale = yAxis[yAxisKey].scale;
45
46
  const xData = xAxis[xAxisKey].data;
47
+ const gradientUsed = yAxis[yAxisKey].colorScale && [yAxisKey, 'y'] || xAxis[xAxisKey].colorScale && [xAxisKey, 'x'] || undefined;
46
48
  if (process.env.NODE_ENV !== 'production') {
47
49
  if (xData === undefined) {
48
50
  throw new Error(`MUI X Charts: ${xAxisKey === DEFAULT_X_AXIS_KEY ? 'The first `xAxis`' : `The x-axis with id "${xAxisKey}"`} should have data property to be able to display a line plot.`);
@@ -60,6 +62,7 @@ const useAggregatedData = () => {
60
62
  const d3Data = connectNulls ? formattedData.filter((_, i) => data[i] != null) : formattedData;
61
63
  const d = areaPath.curve(curve)(d3Data) || '';
62
64
  return _extends({}, series[seriesId], {
65
+ gradientUsed,
63
66
  d,
64
67
  seriesId
65
68
  });
@@ -86,6 +89,7 @@ function AreaPlot(props) {
86
89
  skipAnimation
87
90
  } = props,
88
91
  other = _objectWithoutPropertiesLoose(props, _excluded);
92
+ const getGradientId = useChartGradient();
89
93
  const completedData = useAggregatedData();
90
94
  return /*#__PURE__*/_jsx("g", _extends({}, other, {
91
95
  children: completedData.reverse().map(({
@@ -93,11 +97,13 @@ function AreaPlot(props) {
93
97
  seriesId,
94
98
  color,
95
99
  highlightScope,
96
- area
100
+ area,
101
+ gradientUsed
97
102
  }) => !!area && /*#__PURE__*/_jsx(AreaElement, {
98
103
  id: seriesId,
99
104
  d: d,
100
105
  color: color,
106
+ gradientId: gradientUsed && getGradientId(...gradientUsed),
101
107
  highlightScope: highlightScope,
102
108
  slots: slots,
103
109
  slotProps: slotProps,
@@ -379,11 +379,26 @@ process.env.NODE_ENV !== "production" ? LineChart.propTypes = {
379
379
  width: PropTypes.number,
380
380
  /**
381
381
  * The configuration of the x-axes.
382
- * If not provided, a default axis config is used with id set to `DEFAULT_X_AXIS_KEY`.
382
+ * If not provided, a default axis config is used.
383
383
  */
384
384
  xAxis: PropTypes.arrayOf(PropTypes.shape({
385
385
  axisId: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
386
386
  classes: PropTypes.object,
387
+ colorMap: PropTypes.oneOfType([PropTypes.shape({
388
+ color: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.string.isRequired), PropTypes.func]).isRequired,
389
+ max: PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number]),
390
+ min: PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number]),
391
+ type: PropTypes.oneOf(['continuous']).isRequired
392
+ }), PropTypes.shape({
393
+ colors: PropTypes.arrayOf(PropTypes.string).isRequired,
394
+ thresholds: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number]).isRequired).isRequired,
395
+ type: PropTypes.oneOf(['piecewise']).isRequired
396
+ }), PropTypes.shape({
397
+ colors: PropTypes.arrayOf(PropTypes.string).isRequired,
398
+ type: PropTypes.oneOf(['ordinal']).isRequired,
399
+ unknownColor: PropTypes.string,
400
+ values: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number, PropTypes.string]).isRequired)
401
+ })]),
387
402
  data: PropTypes.array,
388
403
  dataKey: PropTypes.string,
389
404
  disableLine: PropTypes.bool,
@@ -416,11 +431,26 @@ process.env.NODE_ENV !== "production" ? LineChart.propTypes = {
416
431
  })),
417
432
  /**
418
433
  * The configuration of the y-axes.
419
- * If not provided, a default axis config is used with id set to `DEFAULT_Y_AXIS_KEY`.
434
+ * If not provided, a default axis config is used.
420
435
  */
421
436
  yAxis: PropTypes.arrayOf(PropTypes.shape({
422
437
  axisId: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
423
438
  classes: PropTypes.object,
439
+ colorMap: PropTypes.oneOfType([PropTypes.shape({
440
+ color: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.string.isRequired), PropTypes.func]).isRequired,
441
+ max: PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number]),
442
+ min: PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number]),
443
+ type: PropTypes.oneOf(['continuous']).isRequired
444
+ }), PropTypes.shape({
445
+ colors: PropTypes.arrayOf(PropTypes.string).isRequired,
446
+ thresholds: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number]).isRequired).isRequired,
447
+ type: PropTypes.oneOf(['piecewise']).isRequired
448
+ }), PropTypes.shape({
449
+ colors: PropTypes.arrayOf(PropTypes.string).isRequired,
450
+ type: PropTypes.oneOf(['ordinal']).isRequired,
451
+ unknownColor: PropTypes.string,
452
+ values: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number, PropTypes.string]).isRequired)
453
+ })]),
424
454
  data: PropTypes.array,
425
455
  dataKey: PropTypes.string,
426
456
  disableLine: PropTypes.bool,