Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[charts] Add a PolarProvider to manage polar axes #14642

Merged
merged 6 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 { useDrawingArea } from '../../hooks/useDrawingArea';
import { useSeries } from '../../hooks/useSeries';
import { PolarContext } from './PolarContext';
import { computeValue } from './computeValue';
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(
() =>
computeValue({
drawingArea,
formattedSeries,
axis: rotationAxis,
extremumGetters: rotationExtremumGetters,
axisDirection: 'rotation',
}),
[drawingArea, formattedSeries, rotationAxis, rotationExtremumGetters],
);

const radiusValues = React.useMemo(
() =>
computeValue({
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 };
Loading