Skip to content

Commit

Permalink
[charts] Add a PolarProvider to manage polar axes (mui#14642)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfauquette authored and Arthur Balduini committed Sep 30, 2024
1 parent b232839 commit e8bc9a9
Show file tree
Hide file tree
Showing 18 changed files with 356 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@ import {
useSeries,
CartesianContext,
CartesianProviderProps,
cartesianProviderUtils,
computeAxisValue,
useXExtremumGetter,
useYExtremumGetter,
ZoomAxisFilters,
} from '@mui/x-charts/internals';
import { useZoom } from '../ZoomProvider/useZoom';
import { createAxisFilterMapper, createGetAxisFilters } from './createAxisFilterMapper';

const { computeValue } = cartesianProviderUtils;

export interface CartesianProviderProProps extends CartesianProviderProps {}

function CartesianProviderPro(props: CartesianProviderProProps) {
Expand Down Expand Up @@ -66,7 +64,7 @@ function CartesianProviderPro(props: CartesianProviderProProps) {

const xValues = React.useMemo(
() =>
computeValue({
computeAxisValue({
drawingArea,
formattedSeries,
axis: xAxis,
Expand All @@ -81,7 +79,7 @@ function CartesianProviderPro(props: CartesianProviderProProps) {

const yValues = React.useMemo(
() =>
computeValue({
computeAxisValue({
drawingArea,
formattedSeries,
axis: yAxis,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use client';
import * as React from 'react';
import { computeAxisValue } from '../../internals/computeAxisValue';
import { useDrawingArea } from '../../hooks/useDrawingArea';
import { useSeries } from '../../hooks/useSeries';
import { CartesianContext } from './CartesianContext';
import { computeValue } from './computeValue';
import { useXExtremumGetter } from '../PluginProvider/useXExtremumGetter';
import { useYExtremumGetter } from '../PluginProvider';
import { CartesianProviderProps } from './Cartesian.types';
Expand All @@ -18,7 +18,7 @@ function CartesianProvider(props: CartesianProviderProps) {

const xValues = React.useMemo(
() =>
computeValue({
computeAxisValue({
drawingArea,
formattedSeries,
axis: xAxis,
Expand All @@ -30,7 +30,7 @@ function CartesianProvider(props: CartesianProviderProps) {

const yValues = React.useMemo(
() =>
computeValue({
computeAxisValue({
drawingArea,
formattedSeries,
axis: yAxis,
Expand Down
8 changes: 0 additions & 8 deletions packages/x-charts/src/context/CartesianProvider/index.ts
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 };
4 changes: 4 additions & 0 deletions packages/x-charts/src/context/PluginProvider/Plugin.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export type PluginContextState = {
colorProcessors: ColorProcessorsConfig<ChartSeriesType>;
xExtremumGetters: ExtremumGettersConfig<ChartSeriesType>;
yExtremumGetters: ExtremumGettersConfig<ChartSeriesType>;
rotationExtremumGetters: ExtremumGettersConfig<ChartSeriesType>;
radiusExtremumGetters: ExtremumGettersConfig<ChartSeriesType>;
};

export type ChartsPlugin<T> = T extends ChartSeriesType
Expand All @@ -26,5 +28,7 @@ export type ChartsPlugin<T> = T extends ChartSeriesType
colorProcessor: ColorProcessor<T>;
xExtremumGetter?: ExtremumGetter<T>;
yExtremumGetter?: ExtremumGetter<T>;
rotationExtremumGetter?: ExtremumGetter<T>;
radiusExtremumGetter?: ExtremumGetter<T>;
}
: never;
2 changes: 2 additions & 0 deletions packages/x-charts/src/context/PluginProvider/PluginContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export const PluginContext = React.createContext<Initializable<PluginContextStat
seriesFormatters: {},
xExtremumGetters: {},
yExtremumGetters: {},
rotationExtremumGetters: {},
radiusExtremumGetters: {},
},
});

Expand Down
2 changes: 2 additions & 0 deletions packages/x-charts/src/context/PluginProvider/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ export * from './useColorProcessor';
export * from './useSeriesFormatter';
export * from './useXExtremumGetter';
export * from './useYExtremumGetter';
export * from './useRadiusExtremumGetter';
export * from './useRotationExtremumGetter';
15 changes: 15 additions & 0 deletions packages/x-charts/src/context/PluginProvider/mergePlugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export function mergePlugins(plugins?: ChartsPlugin<ChartSeriesType>[]) {
const colorProcessors: ColorProcessorsConfig<ChartSeriesType> = {};
const xExtremumGetters: ExtremumGettersConfig<ChartSeriesType> = {};
const yExtremumGetters: ExtremumGettersConfig<ChartSeriesType> = {};
const rotationExtremumGetters: ExtremumGettersConfig<ChartSeriesType> = {};
const radiusExtremumGetters: ExtremumGettersConfig<ChartSeriesType> = {};

for (let i = 0; i < defaultizedPlugins.length; i += 1) {
const plugin = defaultizedPlugins[i];
Expand All @@ -33,12 +35,25 @@ export function mergePlugins(plugins?: ChartsPlugin<ChartSeriesType>[]) {
if (plugin.yExtremumGetter) {
yExtremumGetters[seriesType] = plugin.yExtremumGetter as ExtremumGetter<typeof seriesType>;
}

if (plugin.rotationExtremumGetter) {
rotationExtremumGetters[seriesType] = plugin.rotationExtremumGetter as ExtremumGetter<
typeof seriesType
>;
}
if (plugin.radiusExtremumGetter) {
radiusExtremumGetters[seriesType] = plugin.radiusExtremumGetter as ExtremumGetter<
typeof seriesType
>;
}
}

return {
seriesFormatters,
colorProcessors,
xExtremumGetters,
yExtremumGetters,
rotationExtremumGetters,
radiusExtremumGetters,
};
}
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];
}
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 packages/x-charts/src/context/PolarProvider/Polar.types.ts
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 packages/x-charts/src/context/PolarProvider/PolarContext.ts
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 packages/x-charts/src/context/PolarProvider/PolarProvider.tsx
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 packages/x-charts/src/context/PolarProvider/getAxisExtremum.ts
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.
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;
};
Loading

0 comments on commit e8bc9a9

Please sign in to comment.