Skip to content

Commit

Permalink
[charts] Move the ZAxisContextProvider by default in the `ChartCont…
Browse files Browse the repository at this point in the history
…ainer` (mui#13465)
  • Loading branch information
alexfauquette authored and thomasmoon committed Sep 6, 2024
1 parent 7a3a288 commit a5a8b65
Show file tree
Hide file tree
Showing 14 changed files with 139 additions and 31 deletions.
2 changes: 1 addition & 1 deletion docs/data/charts/scatter/scatter.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ The scatter charts use by priority:

:::info
The z-axis is a third axis that allows to customize scatter points independently from their position.
It can be provided with `zAxis` props, or with `ZAxisContextProvider` when using composition.
It can be provided with `zAxis` props.

The value to map can either come from the `z` property of series data, or from the zAxis data.
Here are three ways to set z value to 5.
Expand Down
6 changes: 6 additions & 0 deletions docs/pages/x/api/charts/chart-container.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@
"name": "arrayOf",
"description": "Array&lt;{ axisId?: number<br>&#124;&nbsp;string, classes?: object, colorMap?: { colors: Array&lt;string&gt;, type: 'ordinal', unknownColor?: string, values?: Array&lt;Date<br>&#124;&nbsp;number<br>&#124;&nbsp;string&gt; }<br>&#124;&nbsp;{ color: Array&lt;string&gt;<br>&#124;&nbsp;func, max?: Date<br>&#124;&nbsp;number, min?: Date<br>&#124;&nbsp;number, type: 'continuous' }<br>&#124;&nbsp;{ colors: Array&lt;string&gt;, thresholds: Array&lt;Date<br>&#124;&nbsp;number&gt;, type: 'piecewise' }, data?: array, dataKey?: string, disableLine?: bool, disableTicks?: bool, fill?: string, hideTooltip?: bool, id?: number<br>&#124;&nbsp;string, label?: string, labelFontSize?: number, labelStyle?: object, max?: Date<br>&#124;&nbsp;number, min?: Date<br>&#124;&nbsp;number, position?: 'left'<br>&#124;&nbsp;'right', reverse?: bool, scaleType?: 'band'<br>&#124;&nbsp;'linear'<br>&#124;&nbsp;'log'<br>&#124;&nbsp;'point'<br>&#124;&nbsp;'pow'<br>&#124;&nbsp;'sqrt'<br>&#124;&nbsp;'time'<br>&#124;&nbsp;'utc', slotProps?: object, slots?: object, stroke?: string, tickFontSize?: number, tickInterval?: 'auto'<br>&#124;&nbsp;array<br>&#124;&nbsp;func, tickLabelInterval?: 'auto'<br>&#124;&nbsp;func, tickLabelPlacement?: 'middle'<br>&#124;&nbsp;'tick', tickLabelStyle?: object, tickMaxStep?: number, tickMinStep?: number, tickNumber?: number, tickPlacement?: 'end'<br>&#124;&nbsp;'extremities'<br>&#124;&nbsp;'middle'<br>&#124;&nbsp;'start', tickSize?: number, valueFormatter?: func }&gt;"
}
},
"zAxis": {
"type": {
"name": "arrayOf",
"description": "Array&lt;{ colorMap?: { colors: Array&lt;string&gt;, type: 'ordinal', unknownColor?: string, values?: Array&lt;Date<br>&#124;&nbsp;number<br>&#124;&nbsp;string&gt; }<br>&#124;&nbsp;{ color: Array&lt;string&gt;<br>&#124;&nbsp;func, max?: Date<br>&#124;&nbsp;number, min?: Date<br>&#124;&nbsp;number, type: 'continuous' }<br>&#124;&nbsp;{ colors: Array&lt;string&gt;, thresholds: Array&lt;Date<br>&#124;&nbsp;number&gt;, type: 'piecewise' }, data?: array, dataKey?: string, id?: string }&gt;"
}
}
},
"name": "ChartContainer",
Expand Down
6 changes: 6 additions & 0 deletions docs/pages/x/api/charts/responsive-chart-container.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@
"name": "arrayOf",
"description": "Array&lt;{ axisId?: number<br>&#124;&nbsp;string, classes?: object, colorMap?: { colors: Array&lt;string&gt;, type: 'ordinal', unknownColor?: string, values?: Array&lt;Date<br>&#124;&nbsp;number<br>&#124;&nbsp;string&gt; }<br>&#124;&nbsp;{ color: Array&lt;string&gt;<br>&#124;&nbsp;func, max?: Date<br>&#124;&nbsp;number, min?: Date<br>&#124;&nbsp;number, type: 'continuous' }<br>&#124;&nbsp;{ colors: Array&lt;string&gt;, thresholds: Array&lt;Date<br>&#124;&nbsp;number&gt;, type: 'piecewise' }, data?: array, dataKey?: string, disableLine?: bool, disableTicks?: bool, fill?: string, hideTooltip?: bool, id?: number<br>&#124;&nbsp;string, label?: string, labelFontSize?: number, labelStyle?: object, max?: Date<br>&#124;&nbsp;number, min?: Date<br>&#124;&nbsp;number, position?: 'left'<br>&#124;&nbsp;'right', reverse?: bool, scaleType?: 'band'<br>&#124;&nbsp;'linear'<br>&#124;&nbsp;'log'<br>&#124;&nbsp;'point'<br>&#124;&nbsp;'pow'<br>&#124;&nbsp;'sqrt'<br>&#124;&nbsp;'time'<br>&#124;&nbsp;'utc', slotProps?: object, slots?: object, stroke?: string, tickFontSize?: number, tickInterval?: 'auto'<br>&#124;&nbsp;array<br>&#124;&nbsp;func, tickLabelInterval?: 'auto'<br>&#124;&nbsp;func, tickLabelPlacement?: 'middle'<br>&#124;&nbsp;'tick', tickLabelStyle?: object, tickMaxStep?: number, tickMinStep?: number, tickNumber?: number, tickPlacement?: 'end'<br>&#124;&nbsp;'extremities'<br>&#124;&nbsp;'middle'<br>&#124;&nbsp;'start', tickSize?: number, valueFormatter?: func }&gt;"
}
},
"zAxis": {
"type": {
"name": "arrayOf",
"description": "Array&lt;{ colorMap?: { colors: Array&lt;string&gt;, type: 'ordinal', unknownColor?: string, values?: Array&lt;Date<br>&#124;&nbsp;number<br>&#124;&nbsp;string&gt; }<br>&#124;&nbsp;{ color: Array&lt;string&gt;<br>&#124;&nbsp;func, max?: Date<br>&#124;&nbsp;number, min?: Date<br>&#124;&nbsp;number, type: 'continuous' }<br>&#124;&nbsp;{ colors: Array&lt;string&gt;, thresholds: Array&lt;Date<br>&#124;&nbsp;number&gt;, type: 'piecewise' }, data?: array, dataKey?: string, id?: string }&gt;"
}
}
},
"name": "ResponsiveChartContainer",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
},
"yAxis": {
"description": "The configuration of the y-axes. If not provided, a default axis config is used. An array of <a href='/x/api/charts/axis-config/'>AxisConfig</a> objects."
}
},
"zAxis": { "description": "The configuration of the z-axes." }
},
"classDescriptions": {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
},
"yAxis": {
"description": "The configuration of the y-axes. If not provided, a default axis config is used. An array of <a href='/x/api/charts/axis-config/'>AxisConfig</a> objects."
}
},
"zAxis": { "description": "The configuration of the z-axes." }
},
"classDescriptions": {}
}
2 changes: 1 addition & 1 deletion packages/x-charts/src/BarChart/BarChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export interface BarChartSlotProps
ChartsOverlaySlotProps {}

export interface BarChartProps
extends Omit<ResponsiveChartContainerProps, 'series' | 'plugins'>,
extends Omit<ResponsiveChartContainerProps, 'series' | 'plugins' | 'zAxis'>,
Omit<ChartsAxisProps, 'slots' | 'slotProps'>,
Omit<BarPlotProps, 'slots' | 'slotProps'>,
Omit<ChartsOverlayProps, 'slots' | 'slotProps'>,
Expand Down
84 changes: 65 additions & 19 deletions packages/x-charts/src/ChartContainer/ChartContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ import {
CartesianContextProviderProps,
} from '../context/CartesianContextProvider';
import { ChartsAxesGradients } from '../internals/components/ChartsAxesGradients';
import { HighlightedProvider, HighlightedProviderProps } from '../context';
import {
HighlightedProvider,
HighlightedProviderProps,
ZAxisContextProvider,
ZAxisContextProviderProps,
} from '../context';
import { ChartsPluginType } from '../models/plugin';
import { ChartSeriesType } from '../models/seriesType/config';
import { usePluginsMerge } from './usePluginsMerge';
Expand All @@ -25,6 +30,7 @@ export type ChartContainerProps = Omit<
Omit<SeriesContextProviderProps, 'seriesFormatters'> &
Omit<DrawingProviderProps, 'svgRef'> &
Omit<CartesianContextProviderProps, 'xExtremumGetters' | 'yExtremumGetters'> &
ZAxisContextProviderProps &
HighlightedProviderProps,
'children'
> & {
Expand All @@ -44,6 +50,7 @@ const ChartContainer = React.forwardRef(function ChartContainer(props: ChartCont
margin,
xAxis,
yAxis,
zAxis,
colors,
dataset,
sx,
Expand Down Expand Up @@ -78,25 +85,27 @@ const ChartContainer = React.forwardRef(function ChartContainer(props: ChartCont
xExtremumGetters={xExtremumGetters}
yExtremumGetters={yExtremumGetters}
>
<InteractionProvider>
<HighlightedProvider
highlightedItem={highlightedItem}
onHighlightChange={onHighlightChange}
>
<ChartsSurface
width={width}
height={height}
ref={handleRef}
sx={sx}
title={title}
desc={desc}
disableAxisListener={disableAxisListener}
<ZAxisContextProvider zAxis={zAxis} dataset={dataset}>
<InteractionProvider>
<HighlightedProvider
highlightedItem={highlightedItem}
onHighlightChange={onHighlightChange}
>
<ChartsAxesGradients />
{children}
</ChartsSurface>
</HighlightedProvider>
</InteractionProvider>
<ChartsSurface
width={width}
height={height}
ref={handleRef}
sx={sx}
title={title}
desc={desc}
disableAxisListener={disableAxisListener}
>
<ChartsAxesGradients />
{children}
</ChartsSurface>
</HighlightedProvider>
</InteractionProvider>
</ZAxisContextProvider>
</CartesianContextProvider>
</SeriesContextProvider>
</ColorProvider>
Expand Down Expand Up @@ -325,6 +334,43 @@ ChartContainer.propTypes = {
valueFormatter: PropTypes.func,
}),
),
/**
* The configuration of the z-axes.
*/
zAxis: PropTypes.arrayOf(
PropTypes.shape({
colorMap: PropTypes.oneOfType([
PropTypes.shape({
colors: PropTypes.arrayOf(PropTypes.string).isRequired,
type: PropTypes.oneOf(['ordinal']).isRequired,
unknownColor: PropTypes.string,
values: PropTypes.arrayOf(
PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number, PropTypes.string])
.isRequired,
),
}),
PropTypes.shape({
color: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.string.isRequired),
PropTypes.func,
]).isRequired,
max: PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number]),
min: PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number]),
type: PropTypes.oneOf(['continuous']).isRequired,
}),
PropTypes.shape({
colors: PropTypes.arrayOf(PropTypes.string).isRequired,
thresholds: PropTypes.arrayOf(
PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number]).isRequired,
).isRequired,
type: PropTypes.oneOf(['piecewise']).isRequired,
}),
]),
data: PropTypes.array,
dataKey: PropTypes.string,
id: PropTypes.string,
}),
),
} as any;

export { ChartContainer };
5 changes: 5 additions & 0 deletions packages/x-charts/src/ChartsOverlay/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
export { ChartsOverlay } from './ChartsOverlay';
export type {
ChartsOverlayProps,
ChartsOverlaySlotProps,
ChartsOverlaySlots,
} from './ChartsOverlay';
export { ChartsLoadingOverlay } from './ChartsLoadingOverlay';
export { ChartsNoDataOverlay } from './ChartsNoDataOverlay';
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import { ChartSeriesDefaultized, ChartSeriesType } from '../models/seriesType/co
import { AxisDefaultized } from '../models/axis';
import { ChartsTooltipClasses } from './chartsTooltipClasses';
import { DefaultChartsAxisTooltipContent } from './DefaultChartsAxisTooltipContent';
import { isCartesianSeriesType } from '../internals/isCartesian';
import { useColorProcessor } from '../hooks/useColor';
import { ZAxisContext } from '../context/ZAxisContextProvider';
import { useColorProcessor } from '../hooks/useColor';
import { isCartesianSeriesType } from '../internals/isCartesian';
import { useSeries } from '../hooks/useSeries';

type ChartSeriesDefaultizedWithColorGetter = ChartSeriesDefaultized<ChartSeriesType> & {
Expand Down
4 changes: 2 additions & 2 deletions packages/x-charts/src/LineChart/LineChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import {
ChartsOverlayProps,
ChartsOverlaySlotProps,
ChartsOverlaySlots,
} from '../ChartsOverlay/ChartsOverlay';
} from '../ChartsOverlay';

export interface LineChartSlots
extends ChartsAxisSlots,
Expand All @@ -64,7 +64,7 @@ export interface LineChartSlotProps
ChartsOverlaySlotProps {}

export interface LineChartProps
extends Omit<ResponsiveChartContainerProps, 'series' | 'plugins'>,
extends Omit<ResponsiveChartContainerProps, 'series' | 'plugins' | 'zAxis'>,
Omit<ChartsAxisProps, 'slots' | 'slotProps'>,
Omit<ChartsOverlayProps, 'slots' | 'slotProps'>,
ChartsOnAxisClickHandlerProps {
Expand Down
7 changes: 5 additions & 2 deletions packages/x-charts/src/PieChart/PieChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
ChartsOverlayProps,
ChartsOverlaySlotProps,
ChartsOverlaySlots,
} from '../ChartsOverlay/ChartsOverlay';
} from '../ChartsOverlay';

export interface PieChartSlots
extends ChartsAxisSlots,
Expand All @@ -52,7 +52,10 @@ export interface PieChartSlotProps
ChartsOverlaySlotProps {}

export interface PieChartProps
extends Omit<ResponsiveChartContainerProps, 'series' | 'leftAxis' | 'bottomAxis' | 'plugins'>,
extends Omit<
ResponsiveChartContainerProps,
'series' | 'leftAxis' | 'bottomAxis' | 'plugins' | 'zAxis'
>,
Omit<ChartsAxisProps, 'slots' | 'slotProps'>,
Omit<ChartsOverlayProps, 'slots' | 'slotProps'>,
Pick<PiePlotProps, 'skipAnimation'> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,43 @@ ResponsiveChartContainer.propTypes = {
valueFormatter: PropTypes.func,
}),
),
/**
* The configuration of the z-axes.
*/
zAxis: PropTypes.arrayOf(
PropTypes.shape({
colorMap: PropTypes.oneOfType([
PropTypes.shape({
colors: PropTypes.arrayOf(PropTypes.string).isRequired,
type: PropTypes.oneOf(['ordinal']).isRequired,
unknownColor: PropTypes.string,
values: PropTypes.arrayOf(
PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number, PropTypes.string])
.isRequired,
),
}),
PropTypes.shape({
color: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.string.isRequired),
PropTypes.func,
]).isRequired,
max: PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number]),
min: PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number]),
type: PropTypes.oneOf(['continuous']).isRequired,
}),
PropTypes.shape({
colors: PropTypes.arrayOf(PropTypes.string).isRequired,
thresholds: PropTypes.arrayOf(
PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number]).isRequired,
).isRequired,
type: PropTypes.oneOf(['piecewise']).isRequired,
}),
]),
data: PropTypes.array,
dataKey: PropTypes.string,
id: PropTypes.string,
}),
),
} as any;

export { ResponsiveChartContainer };
2 changes: 1 addition & 1 deletion packages/x-charts/src/ScatterChart/ScatterChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
ChartsOverlayProps,
ChartsOverlaySlotProps,
ChartsOverlaySlots,
} from '../ChartsOverlay/ChartsOverlay';
} from '../ChartsOverlay';
import { ChartsAxisHighlight, ChartsAxisHighlightProps } from '../ChartsAxisHighlight';
import { ChartsAxisSlots, ChartsAxisSlotProps } from '../models/axis';
import {
Expand Down
5 changes: 4 additions & 1 deletion packages/x-charts/src/SparkLineChart/SparkLineChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ export interface SparkLineChartSlotProps
ChartsTooltipSlotProps {}

export interface SparkLineChartProps
extends Omit<ResponsiveChartContainerProps, 'series' | 'xAxis' | 'yAxis' | 'margin' | 'plugins'> {
extends Omit<
ResponsiveChartContainerProps,
'series' | 'xAxis' | 'yAxis' | 'zAxis' | 'margin' | 'plugins'
> {
/**
* The xAxis configuration.
* Notice it is a single [[AxisConfig]] object, not an array of configuration.
Expand Down

0 comments on commit a5a8b65

Please sign in to comment.