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] Introduce plugins system #13367

Merged
merged 6 commits into from
Jun 6, 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
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 @@ -32,6 +32,12 @@
"describedArgs": ["highlightedItem"]
}
},
"plugins": {
"type": {
"name": "arrayOf",
"description": "Array&lt;{ colorProcessor: func, seriesFormatter: func, seriesType: 'bar', xExtremumGetter?: func, yExtremumGetter?: func }<br>&#124;&nbsp;{ colorProcessor: func, seriesFormatter: func, seriesType: 'line', xExtremumGetter?: func, yExtremumGetter?: func }<br>&#124;&nbsp;{ colorProcessor: func, seriesFormatter: func, seriesType: 'scatter', xExtremumGetter?: func, yExtremumGetter?: func }<br>&#124;&nbsp;{ colorProcessor: func, seriesFormatter: func, seriesType: 'pie', xExtremumGetter?: func, yExtremumGetter?: func }&gt;"
}
},
"xAxis": {
"type": {
"name": "arrayOf",
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 @@ -31,6 +31,12 @@
"describedArgs": ["highlightedItem"]
}
},
"plugins": {
"type": {
"name": "arrayOf",
"description": "Array&lt;{ colorProcessor: func, seriesFormatter: func, seriesType: 'bar', xExtremumGetter?: func, yExtremumGetter?: func }<br>&#124;&nbsp;{ colorProcessor: func, seriesFormatter: func, seriesType: 'line', xExtremumGetter?: func, yExtremumGetter?: func }<br>&#124;&nbsp;{ colorProcessor: func, seriesFormatter: func, seriesType: 'scatter', xExtremumGetter?: func, yExtremumGetter?: func }<br>&#124;&nbsp;{ colorProcessor: func, seriesFormatter: func, seriesType: 'pie', xExtremumGetter?: func, yExtremumGetter?: func }&gt;"
}
},
"width": { "type": { "name": "number" } },
"xAxis": {
"type": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
"description": "The callback fired when the highlighted item changes.",
"typeDescriptions": { "highlightedItem": "The newly highlighted item." }
},
"plugins": {
"description": "An array of plugins defining how to preprocess data. If not provided, the container supports line, bar, scatter and pie charts."
},
"series": {
"description": "The array of series to display. Each type of series has its own specificity. Please refer to the appropriate docs page to learn more about it."
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
"description": "The callback fired when the highlighted item changes.",
"typeDescriptions": { "highlightedItem": "The newly highlighted item." }
},
"plugins": {
"description": "An array of plugins defining how to preprocess data. If not provided, the container supports line, bar, scatter and pie charts."
},
"series": {
"description": "The array of series to display. Each type of series has its own specificity. Please refer to the appropriate docs page to learn more about it."
},
Expand Down
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'>,
extends Omit<ResponsiveChartContainerProps, 'series' | 'plugins'>,
Omit<ChartsAxisProps, 'slots' | 'slotProps'>,
Omit<BarPlotProps, 'slots' | 'slotProps'>,
Omit<ChartsOverlayProps, 'slots' | 'slotProps'>,
Expand Down
2 changes: 1 addition & 1 deletion packages/x-charts/src/BarChart/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
DatasetType,
Formatter,
} from '../models/seriesType/config';
import defaultizeValueFormatter from '../internals/defaultizeValueFormatter';
import { defaultizeValueFormatter } from '../internals/defaultizeValueFormatter';
import { DefaultizedProps } from '../models/helpers';
import { SeriesId } from '../models/seriesType/common';

Expand Down
12 changes: 6 additions & 6 deletions packages/x-charts/src/BarChart/getColor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { DefaultizedBarSeriesType } from '../models/seriesType/bar';

export default function getColor(
series: DefaultizedBarSeriesType,
xAxis: AxisDefaultized,
yAxis: AxisDefaultized,
xAxis?: AxisDefaultized,
yAxis?: AxisDefaultized,
) {
const verticalLayout = series.layout === 'vertical';

const bandColorScale = verticalLayout ? xAxis.colorScale : yAxis.colorScale;
const valueColorScale = verticalLayout ? yAxis.colorScale : xAxis.colorScale;
const bandValues = verticalLayout ? xAxis.data! : yAxis.data!;
const bandColorScale = verticalLayout ? xAxis?.colorScale : yAxis?.colorScale;
const valueColorScale = verticalLayout ? yAxis?.colorScale : xAxis?.colorScale;
const bandValues = verticalLayout ? xAxis?.data : yAxis?.data;

if (valueColorScale) {
return (dataIndex: number) => {
Expand All @@ -22,7 +22,7 @@ export default function getColor(
return color;
};
}
if (bandColorScale) {
if (bandColorScale && bandValues) {
return (dataIndex: number) => {
const value = bandValues[dataIndex];
const color = value === null ? series.color : bandColorScale(value);
Expand Down
12 changes: 12 additions & 0 deletions packages/x-charts/src/BarChart/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ChartsPluginType } from '../models/plugin';
import { getExtremumX, getExtremumY } from './extremums';
import formatter from './formatter';
import getColor from './getColor';

export const plugin: ChartsPluginType<'bar'> = {
seriesType: 'bar',
seriesFormatter: formatter,
colorProcessor: getColor,
xExtremumGetter: getExtremumX,
yExtremumGetter: getExtremumY,
};
59 changes: 55 additions & 4 deletions packages/x-charts/src/ChartContainer/ChartContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,24 @@ import {
} from '../context/CartesianContextProvider';
import { ChartsAxesGradients } from '../internals/components/ChartsAxesGradients';
import { HighlightedProvider, HighlightedProviderProps } from '../context';
import { ChartsPluginTypes } from '../models/plugin';
import { ChartSeriesType } from '../models/seriesType/config';
import { usePluginsMerge } from './usePluginsMerge';

export type ChartContainerProps = Omit<
export type ChartContainerProps<T extends ChartSeriesType = ChartSeriesType> = Omit<
ChartsSurfaceProps &
SeriesContextProviderProps &
Omit<SeriesContextProviderProps, 'seriesFormatters'> &
Omit<DrawingProviderProps, 'svgRef'> &
CartesianContextProviderProps &
Omit<CartesianContextProviderProps, 'xExtremumGetters' | 'yExtremumGetters'> &
HighlightedProviderProps,
'children'
> & {
children?: React.ReactNode;
/**
* An array of plugins defining how to preprocess data.
* If not provided, the container supports line, bar, scatter and pie charts.
*/
plugins?: ChartsPluginTypes<T>[];
};

const ChartContainer = React.forwardRef(function ChartContainer(props: ChartContainerProps, ref) {
Expand All @@ -43,16 +51,23 @@ const ChartContainer = React.forwardRef(function ChartContainer(props: ChartCont
disableAxisListener,
highlightedItem,
onHighlightChange,
plugins,
children,
} = props;
const svgRef = React.useRef<SVGSVGElement>(null);
const handleRef = useForkRef(ref, svgRef);

const { seriesFormatters } = usePluginsMerge(plugins);
useReducedMotion(); // a11y reduce motion (see: https://react-spring.dev/docs/utilities/use-reduced-motion)

return (
<DrawingProvider width={width} height={height} margin={margin} svgRef={svgRef}>
<SeriesContextProvider series={series} colors={colors} dataset={dataset}>
<SeriesContextProvider
series={series}
colors={colors}
dataset={dataset}
seriesFormatters={seriesFormatters}
>
<CartesianContextProvider xAxis={xAxis} yAxis={yAxis} dataset={dataset}>
<InteractionProvider>
<HighlightedProvider
Expand Down Expand Up @@ -131,6 +146,42 @@ ChartContainer.propTypes = {
* @param {HighlightItemData | null} highlightedItem The newly highlighted item.
*/
onHighlightChange: PropTypes.func,
/**
* An array of plugins defining how to preprocess data.
* If not provided, the container supports line, bar, scatter and pie charts.
*/
plugins: PropTypes.arrayOf(
PropTypes.oneOfType([
PropTypes.shape({
colorProcessor: PropTypes.func.isRequired,
seriesFormatter: PropTypes.func.isRequired,
seriesType: PropTypes.oneOf(['bar']).isRequired,
xExtremumGetter: PropTypes.func,
yExtremumGetter: PropTypes.func,
}),
PropTypes.shape({
colorProcessor: PropTypes.func.isRequired,
seriesFormatter: PropTypes.func.isRequired,
seriesType: PropTypes.oneOf(['line']).isRequired,
xExtremumGetter: PropTypes.func,
yExtremumGetter: PropTypes.func,
}),
PropTypes.shape({
colorProcessor: PropTypes.func.isRequired,
seriesFormatter: PropTypes.func.isRequired,
seriesType: PropTypes.oneOf(['scatter']).isRequired,
xExtremumGetter: PropTypes.func,
yExtremumGetter: PropTypes.func,
}),
PropTypes.shape({
colorProcessor: PropTypes.func.isRequired,
seriesFormatter: PropTypes.func.isRequired,
seriesType: PropTypes.oneOf(['pie']).isRequired,
xExtremumGetter: PropTypes.func,
yExtremumGetter: PropTypes.func,
}),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this proptype seem overly verbose 😅

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, could be removed :)

I think I did it in the main PR, but forgot to extract it 👍

]).isRequired,
),
/**
* The array of series to display.
* Each type of series has its own specificity.
Expand Down
6 changes: 6 additions & 0 deletions packages/x-charts/src/ChartContainer/defaultPlugins.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { plugin as barPlugin } from '../BarChart/plugin';
import { plugin as scatterPlugin } from '../ScatterChart/plugin';
import { plugin as linePlugin } from '../LineChart/plugin';
import { plugin as piePlugin } from '../PieChart/plugin';

export const defaultPlugins = [barPlugin, scatterPlugin, linePlugin, piePlugin];
41 changes: 41 additions & 0 deletions packages/x-charts/src/ChartContainer/usePluginsMerge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import * as React from 'react';
import { ChartsPluginTypes, ColorProcessorsConfig } from '../models';
import { ChartSeriesType } from '../models/seriesType/config';
import { ExtremumGettersConfig } from '../context/CartesianContextProvider';
import { SeriesFormatterConfig } from '../context/SeriesContextProvider';
import { defaultPlugins } from './defaultPlugins';

export function usePluginsMerge<T extends ChartSeriesType>(plugins?: ChartsPluginTypes<T>[]) {
const defaultizedPlugins = plugins ?? defaultPlugins;
JCQuintas marked this conversation as resolved.
Show resolved Hide resolved

return React.useMemo(() => {
const seriesFormatters: SeriesFormatterConfig<ChartSeriesType> = {};
const colorProcessors: ColorProcessorsConfig<ChartSeriesType> = {};
const xExtremumGetters: ExtremumGettersConfig<ChartSeriesType> = {};
const yExtremumGetters: ExtremumGettersConfig<ChartSeriesType> = {};

for (let i = 0; i < defaultizedPlugins.length; i += 1) {
const plugin = defaultizedPlugins[i];

// To remove those any we will need to solve this union discrimination issue:
// https://www.typescriptlang.org/play/?#code/FDAuE8AcFMAIDkCuBbARtATgYQPYDsAzASwHNYBeWAb2FlgGsi8ATALlgHI8V0MOBuWrBwwMAQ1A4M7ABQAPdtzSYAlBQB8sJb0EBfEBBiwAyqAxMSuQqQrUhjFuw4BnMxYFCRmCVNkLYruZ4JGrkmoEWeiAAxviuWqhWxCTsSMrY+Mm2VAxMbLAARNqYBQA0wqI+0rByGrAATLAAVDWw+rF48YFJpOymQZaZNpQ5DvkFEcFlFd6S1bVhsAAG9S0AJFRyukttMXGgsB3JzrYA2niJQyTl3VcAugZQcADylXPOALJikJAW2ULFDAAflSPEwPRIpw4XnEcw4d1KQkmJBBJjcwQhUJhVXhiN0gmAHXi2LmXx+FnYr1mUk+31+wWy+JABCksBkABtoAcjjYcARDldnGoaCA6AB6MWwADqUnoJxw9FgRH5AHc4L9ooroGJogALQ5iZxwPJEABuRGYiDE7PASJVRFAerZPJIADoxsKhHRooa4FwwXxWF66DNYVIyfTIS73Xk7rZoySpIIQyHUBhtfRkyGfUbOMiOEGU3RExgIxZTtGxnHKAm3kng8xoAQxIh2aBC0W0xms-pvftqLkWOUS2141chBLYABJDimuB4HBKxtiWBiVA4RAHXU4FWwSSwTkHAAqxlgiBYmFcYhYAusbrGq5vtepGFX6YPTHo0GYnjrpbp5ZVrYJZ6EAA
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

furthest I could get quickly is this, it solves the assignment type issue operatorMapping[kind] = operator, but doesn't solve accessing operatorMapping[kind]() typing issue :(

type OperatorsMapping<T extends NumberConfig | StringConfig= NumberConfig | StringConfig> = {
  [k in T['kind']]?: T['operator']
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure it's feasible, but does not matter a lot. As long as the type constraint in the input/output of the hook are ok, it should not cause any issue

seriesFormatters[plugin.seriesType] = plugin.seriesFormatter as any;

colorProcessors[plugin.seriesType] = plugin.colorProcessor as any;

if (plugin.xExtremumGetter) {
xExtremumGetters[plugin.seriesType] = plugin.xExtremumGetter as any;
}

if (plugin.yExtremumGetter) {
yExtremumGetters[plugin.seriesType] = plugin.yExtremumGetter as any;
}
}
return {
seriesFormatters,
colorProcessors,
xExtremumGetters,
yExtremumGetters,
};
}, [defaultizedPlugins]);
}
2 changes: 1 addition & 1 deletion packages/x-charts/src/LineChart/LineChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export interface LineChartSlotProps
ChartsOverlaySlotProps {}

export interface LineChartProps
extends Omit<ResponsiveChartContainerProps, 'series'>,
extends Omit<ResponsiveChartContainerProps, 'series' | 'plugins'>,
Omit<ChartsAxisProps, 'slots' | 'slotProps'>,
Omit<ChartsOverlayProps, 'slots' | 'slotProps'>,
ChartsOnAxisClickHandlerProps {
Expand Down
2 changes: 1 addition & 1 deletion packages/x-charts/src/LineChart/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
DatasetType,
Formatter,
} from '../models/seriesType/config';
import defaultizeValueFormatter from '../internals/defaultizeValueFormatter';
import { defaultizeValueFormatter } from '../internals/defaultizeValueFormatter';
import { DefaultizedProps } from '../models/helpers';
import { SeriesId } from '../models/seriesType/common';

Expand Down
8 changes: 4 additions & 4 deletions packages/x-charts/src/LineChart/getColor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { DefaultizedLineSeriesType } from '../models/seriesType/line';

export default function getColor(
series: DefaultizedLineSeriesType,
xAxis: AxisDefaultized,
yAxis: AxisDefaultized,
xAxis?: AxisDefaultized,
yAxis?: AxisDefaultized,
) {
const yColorScale = yAxis.colorScale;
const xColorScale = xAxis.colorScale;
const yColorScale = yAxis?.colorScale;
const xColorScale = xAxis?.colorScale;

if (yColorScale) {
return (dataIndex: number) => {
Expand Down
12 changes: 12 additions & 0 deletions packages/x-charts/src/LineChart/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ChartsPluginType } from '../models/plugin';
import { getExtremumX, getExtremumY } from './extremums';
import formatter from './formatter';
import getColor from './getColor';

export const plugin: ChartsPluginType<'line'> = {
seriesType: 'line',
colorProcessor: getColor,
seriesFormatter: formatter,
xExtremumGetter: getExtremumX,
yExtremumGetter: getExtremumY,
};
2 changes: 1 addition & 1 deletion packages/x-charts/src/PieChart/PieChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export interface PieChartSlotProps
ChartsOverlaySlotProps {}

export interface PieChartProps
extends Omit<ResponsiveChartContainerProps, 'series' | 'leftAxis' | 'bottomAxis'>,
extends Omit<ResponsiveChartContainerProps, 'series' | 'leftAxis' | 'bottomAxis' | 'plugins'>,
Omit<ChartsAxisProps, 'slots' | 'slotProps'>,
Omit<ChartsOverlayProps, 'slots' | 'slotProps'>,
Pick<PiePlotProps, 'skipAnimation'> {
Expand Down
9 changes: 9 additions & 0 deletions packages/x-charts/src/PieChart/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ChartsPluginType } from '../models/plugin';
import formatter from './formatter';
import getColor from './getColor';

export const plugin: ChartsPluginType<'pie'> = {
seriesType: 'pie',
colorProcessor: getColor,
seriesFormatter: formatter,
};
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,42 @@ ResponsiveChartContainer.propTypes = {
* @param {HighlightItemData | null} highlightedItem The newly highlighted item.
*/
onHighlightChange: PropTypes.func,
/**
* An array of plugins defining how to preprocess data.
* If not provided, the container supports line, bar, scatter and pie charts.
*/
plugins: PropTypes.arrayOf(
PropTypes.oneOfType([
PropTypes.shape({
colorProcessor: PropTypes.func.isRequired,
seriesFormatter: PropTypes.func.isRequired,
seriesType: PropTypes.oneOf(['bar']).isRequired,
xExtremumGetter: PropTypes.func,
yExtremumGetter: PropTypes.func,
}),
PropTypes.shape({
colorProcessor: PropTypes.func.isRequired,
seriesFormatter: PropTypes.func.isRequired,
seriesType: PropTypes.oneOf(['line']).isRequired,
xExtremumGetter: PropTypes.func,
yExtremumGetter: PropTypes.func,
}),
PropTypes.shape({
colorProcessor: PropTypes.func.isRequired,
seriesFormatter: PropTypes.func.isRequired,
seriesType: PropTypes.oneOf(['scatter']).isRequired,
xExtremumGetter: PropTypes.func,
yExtremumGetter: PropTypes.func,
}),
PropTypes.shape({
colorProcessor: PropTypes.func.isRequired,
seriesFormatter: PropTypes.func.isRequired,
seriesType: PropTypes.oneOf(['pie']).isRequired,
xExtremumGetter: PropTypes.func,
yExtremumGetter: PropTypes.func,
}),
]).isRequired,
),
/**
* The array of series to display.
* Each type of series has its own specificity.
Expand Down
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 @@ -54,7 +54,7 @@ export interface ScatterChartSlotProps
ChartsOverlaySlotProps {}

export interface ScatterChartProps
extends Omit<ResponsiveChartContainerProps, 'series'>,
extends Omit<ResponsiveChartContainerProps, 'series' | 'plugins'>,
Omit<ZAxisContextProviderProps, 'children' | 'dataset'>,
Omit<ChartsAxisProps, 'slots' | 'slotProps'>,
Omit<ChartsOverlayProps, 'slots' | 'slotProps'>,
Expand Down
2 changes: 1 addition & 1 deletion packages/x-charts/src/ScatterChart/formatter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import defaultizeValueFormatter from '../internals/defaultizeValueFormatter';
import { defaultizeValueFormatter } from '../internals/defaultizeValueFormatter';
import { Formatter } from '../models/seriesType/config';

const formatter: Formatter<'scatter'> = ({ series, seriesOrder }) => {
Expand Down
8 changes: 4 additions & 4 deletions packages/x-charts/src/ScatterChart/getColor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { DefaultizedScatterSeriesType } from '../models/seriesType/scatter';

export default function getColor(
series: DefaultizedScatterSeriesType,
xAxis: AxisDefaultized,
yAxis: AxisDefaultized,
xAxis?: AxisDefaultized,
yAxis?: AxisDefaultized,
zAxis?: ZAxisDefaultized,
) {
const zColorScale = zAxis?.colorScale;
const yColorScale = yAxis.colorScale;
const xColorScale = xAxis.colorScale;
const yColorScale = yAxis?.colorScale;
const xColorScale = xAxis?.colorScale;

if (zColorScale) {
return (dataIndex: number) => {
Expand Down
Loading