-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[charts] Add a
PolarProvider
to manage polar axes (#14642)
- Loading branch information
1 parent
edc76e0
commit 2889da1
Showing
18 changed files
with
356 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,4 @@ | ||
import { computeValue } from './computeValue'; | ||
|
||
export * from './CartesianProvider'; | ||
export * from './CartesianContext'; | ||
export * from './useCartesianContext'; | ||
export * from './Cartesian.types'; | ||
|
||
const cartesianProviderUtils = { | ||
computeValue, | ||
}; | ||
|
||
export { cartesianProviderUtils }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
packages/x-charts/src/context/PluginProvider/useRadiusExtremumGetter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
'use client'; | ||
import * as React from 'react'; | ||
import { ChartSeriesType } from '../../models/seriesType/config'; | ||
import { PluginContext } from './PluginContext'; | ||
import { ExtremumGettersConfig } from './ExtremumGetter.types'; | ||
|
||
export function useRadiusExtremumGetter<T extends ChartSeriesType>( | ||
seriesType: T, | ||
): ExtremumGettersConfig<T>[T]; | ||
export function useRadiusExtremumGetter(): ExtremumGettersConfig<ChartSeriesType>; | ||
export function useRadiusExtremumGetter(seriesType?: ChartSeriesType) { | ||
const { isInitialized, data } = React.useContext(PluginContext); | ||
|
||
if (!isInitialized) { | ||
throw new Error( | ||
[ | ||
'MUI X: Could not find the plugin context.', | ||
'It looks like you rendered your component outside of a ChartsContainer parent component.', | ||
].join('\n'), | ||
); | ||
} | ||
|
||
if (!seriesType) { | ||
return data.radiusExtremumGetters; | ||
} | ||
|
||
return data.radiusExtremumGetters[seriesType]; | ||
} |
28 changes: 28 additions & 0 deletions
28
packages/x-charts/src/context/PluginProvider/useRotationExtremumGetter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
'use client'; | ||
import * as React from 'react'; | ||
import { ChartSeriesType } from '../../models/seriesType/config'; | ||
import { PluginContext } from './PluginContext'; | ||
import { ExtremumGettersConfig } from './ExtremumGetter.types'; | ||
|
||
export function useRotationExtremumGetter<T extends ChartSeriesType>( | ||
seriesType: T, | ||
): ExtremumGettersConfig<T>[T]; | ||
export function useRotationExtremumGetter(): ExtremumGettersConfig<ChartSeriesType>; | ||
export function useRotationExtremumGetter(seriesType?: ChartSeriesType) { | ||
const { isInitialized, data } = React.useContext(PluginContext); | ||
|
||
if (!isInitialized) { | ||
throw new Error( | ||
[ | ||
'MUI X: Could not find the plugin context.', | ||
'It looks like you rendered your component outside of a ChartsContainer parent component.', | ||
].join('\n'), | ||
); | ||
} | ||
|
||
if (!seriesType) { | ||
return data.rotationExtremumGetters; | ||
} | ||
|
||
return data.rotationExtremumGetters[seriesType]; | ||
} |
52 changes: 52 additions & 0 deletions
52
packages/x-charts/src/context/PolarProvider/Polar.types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { DatasetType } from '../../models/seriesType/config'; | ||
import { | ||
AxisDefaultized, | ||
ScaleName, | ||
AxisId, | ||
AxisConfig, | ||
ChartsRotationAxisProps, | ||
ChartsRadiusAxisProps, | ||
} from '../../models/axis'; | ||
|
||
export type PolarProviderProps = { | ||
/** | ||
* The configuration of the rotation-axes. | ||
* If not provided, a default axis config is used. | ||
* An array of [[AxisConfig]] objects. | ||
*/ | ||
rotationAxis: AxisConfig<ScaleName, any, ChartsRotationAxisProps>[]; | ||
/** | ||
* The configuration of the radial-axes. | ||
* If not provided, a default axis config is used. | ||
* An array of [[AxisConfig]] objects. | ||
*/ | ||
radiusAxis: AxisConfig<'linear', any, ChartsRadiusAxisProps>[]; | ||
/** | ||
* An array of objects that can be used to populate series and axes data using their `dataKey` property. | ||
*/ | ||
dataset?: DatasetType; | ||
children: React.ReactNode; | ||
}; | ||
|
||
export type DefaultizedAxisConfig<Axis> = { | ||
[axisId: AxisId]: AxisDefaultized<ScaleName, any, Axis>; | ||
}; | ||
|
||
export type PolarContextState = { | ||
/** | ||
* Mapping from rotation-axis key to scaling configuration. | ||
*/ | ||
rotationAxis: DefaultizedAxisConfig<ChartsRotationAxisProps>; | ||
/** | ||
* Mapping from radius-axis key to scaling configuration. | ||
*/ | ||
radiusAxis: DefaultizedAxisConfig<ChartsRadiusAxisProps>; | ||
/** | ||
* The rotation-axes IDs sorted by order they got provided. | ||
*/ | ||
rotationAxisIds: AxisId[]; | ||
/** | ||
* The radius-axes IDs sorted by order they got provided. | ||
*/ | ||
radiusAxisIds: AxisId[]; | ||
}; |
18 changes: 18 additions & 0 deletions
18
packages/x-charts/src/context/PolarProvider/PolarContext.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import * as React from 'react'; | ||
|
||
import { Initializable } from '../context.types'; | ||
import { PolarContextState } from './Polar.types'; | ||
|
||
export const PolarContext = React.createContext<Initializable<PolarContextState>>({ | ||
isInitialized: false, | ||
data: { | ||
rotationAxis: {}, | ||
radiusAxis: {}, | ||
rotationAxisIds: [], | ||
radiusAxisIds: [], | ||
}, | ||
}); | ||
|
||
if (process.env.NODE_ENV !== 'production') { | ||
PolarContext.displayName = 'PolarContext'; | ||
} |
59 changes: 59 additions & 0 deletions
59
packages/x-charts/src/context/PolarProvider/PolarProvider.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
'use client'; | ||
import * as React from 'react'; | ||
import { computeAxisValue } from '../../internals/computeAxisValue'; | ||
import { useDrawingArea } from '../../hooks/useDrawingArea'; | ||
import { useSeries } from '../../hooks/useSeries'; | ||
import { PolarContext } from './PolarContext'; | ||
import { useRadiusExtremumGetter } from '../PluginProvider/useRadiusExtremumGetter'; | ||
import { useRotationExtremumGetter } from '../PluginProvider/useRotationExtremumGetter'; | ||
import { PolarProviderProps } from './Polar.types'; | ||
|
||
function PolarProvider(props: PolarProviderProps) { | ||
const { rotationAxis, radiusAxis, children } = props; | ||
|
||
const formattedSeries = useSeries(); | ||
const drawingArea = useDrawingArea(); | ||
const rotationExtremumGetters = useRotationExtremumGetter(); | ||
const radiusExtremumGetters = useRadiusExtremumGetter(); | ||
|
||
const rotationValues = React.useMemo( | ||
() => | ||
computeAxisValue({ | ||
drawingArea, | ||
formattedSeries, | ||
axis: rotationAxis, | ||
extremumGetters: rotationExtremumGetters, | ||
axisDirection: 'rotation', | ||
}), | ||
[drawingArea, formattedSeries, rotationAxis, rotationExtremumGetters], | ||
); | ||
|
||
const radiusValues = React.useMemo( | ||
() => | ||
computeAxisValue({ | ||
drawingArea, | ||
formattedSeries, | ||
axis: radiusAxis, | ||
extremumGetters: radiusExtremumGetters, | ||
axisDirection: 'radius', | ||
}), | ||
[drawingArea, formattedSeries, radiusAxis, radiusExtremumGetters], | ||
); | ||
|
||
const value = React.useMemo( | ||
() => ({ | ||
isInitialized: true, | ||
data: { | ||
rotationAxis: rotationValues.axis, | ||
radiusAxis: radiusValues.axis, | ||
rotationAxisIds: rotationValues.axisIds, | ||
radiusAxisIds: radiusValues.axisIds, | ||
}, | ||
}), | ||
[rotationValues, radiusValues], | ||
); | ||
|
||
return <PolarContext.Provider value={value}>{children}</PolarContext.Provider>; | ||
} | ||
|
||
export { PolarProvider }; |
48 changes: 48 additions & 0 deletions
48
packages/x-charts/src/context/PolarProvider/getAxisExtremum.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { AxisConfig } from '../../models'; | ||
import { CartesianChartSeriesType } from '../../models/seriesType/config'; | ||
import { FormattedSeries } from '../SeriesProvider'; | ||
import { ExtremumGettersConfig, ExtremumGetterResult } from '../PluginProvider'; | ||
|
||
const axisExtremumCallback = <T extends CartesianChartSeriesType>( | ||
acc: ExtremumGetterResult, | ||
chartType: T, | ||
axis: AxisConfig, | ||
getters: ExtremumGettersConfig<T>, | ||
axisIndex: number, | ||
formattedSeries: FormattedSeries, | ||
): ExtremumGetterResult => { | ||
const getter = getters[chartType]; | ||
const series = formattedSeries[chartType]?.series ?? {}; | ||
|
||
const [minChartTypeData, maxChartTypeData] = getter?.({ | ||
series, | ||
axis, | ||
axisIndex, | ||
isDefaultAxis: axisIndex === 0, | ||
}) ?? [Infinity, -Infinity]; | ||
|
||
const [minData, maxData] = acc; | ||
|
||
return [Math.min(minChartTypeData, minData), Math.max(maxChartTypeData, maxData)]; | ||
}; | ||
|
||
export const getAxisExtremum = ( | ||
axis: AxisConfig, | ||
getters: ExtremumGettersConfig, | ||
axisIndex: number, | ||
formattedSeries: FormattedSeries, | ||
) => { | ||
const charTypes = Object.keys(getters) as CartesianChartSeriesType[]; | ||
|
||
const extremums = charTypes.reduce<ExtremumGetterResult>( | ||
(acc, charType) => | ||
axisExtremumCallback(acc, charType, axis, getters, axisIndex, formattedSeries), | ||
[Infinity, -Infinity], | ||
); | ||
|
||
if (Number.isNaN(extremums[0]) || Number.isNaN(extremums[1])) { | ||
return [Infinity, -Infinity]; | ||
} | ||
|
||
return extremums; | ||
}; |
Empty file.
9 changes: 9 additions & 0 deletions
9
packages/x-charts/src/context/PolarProvider/usePolarContext.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
'use client'; | ||
import * as React from 'react'; | ||
import { PolarContext } from './PolarContext'; | ||
import { PolarContextState } from './Polar.types'; | ||
|
||
export const usePolarContext = (): PolarContextState => { | ||
const { data } = React.useContext(PolarContext); | ||
return data; | ||
}; |
Oops, something went wrong.