Skip to content

Commit

Permalink
[charts] Move z-axis to plugin (mui#16130)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfauquette authored Jan 13, 2025
1 parent 2c9ddc9 commit aa515ec
Show file tree
Hide file tree
Showing 35 changed files with 316 additions and 322 deletions.
4 changes: 1 addition & 3 deletions docs/pages/x/api/charts/chart-data-provider.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
{
"props": {
"colors": { "type": { "name": "any" }, "default": "blueberryTwilightPalette" },
"dataset": { "type": { "name": "any" } },
"height": { "type": { "name": "any" } },
"highlightedItem": { "type": { "name": "any" } },
"id": { "type": { "name": "any" } },
"margin": { "type": { "name": "any" } },
"onHighlightChange": { "type": { "name": "any" } },
"series": { "type": { "name": "any" } },
"skipAnimation": { "type": { "name": "any" } },
"width": { "type": { "name": "any" } },
"zAxis": { "type": { "name": "any" } }
"width": { "type": { "name": "any" } }
},
"name": "ChartDataProvider",
"imports": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
"componentDescription": "Orchestrates the data providers for the chart components and hooks.\n\nUse this component if you have custom HTML components that need to access the chart data.",
"propDescriptions": {
"colors": { "description": "Color palette used to colorize multiple series." },
"dataset": {
"description": "An array of objects that can be used to populate series and axes data using their <code>dataKey</code> property."
},
"height": {
"description": "The height of the chart in px. If not defined, it takes the height of the parent element."
},
Expand All @@ -26,8 +23,7 @@
},
"width": {
"description": "The width of the chart in px. If not defined, it takes the width of the parent element."
},
"zAxis": { "description": "The configuration of the z-axes." }
}
},
"classDescriptions": {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@ import { Watermark } from '@mui/x-license/Watermark';
import { useLicenseVerifier } from '@mui/x-license/useLicenseVerifier';
import { ChartsSurface, ChartsSurfaceProps } from '@mui/x-charts/ChartsSurface';
import { ChartDataProvider, ChartDataProviderProps } from '@mui/x-charts/context';
import { ChartSeriesType, UseChartCartesianAxisSignature } from '@mui/x-charts/internals';
import { ChartSeriesType } from '@mui/x-charts/internals';
import { getReleaseInfo } from '../internals/utils/releaseInfo';
import { useChartContainerProProps } from './useChartContainerProProps';
import { UseChartProZoomSignature } from '../internals/plugins/useChartProZoom/useChartProZoom.types';
import { AllPluginSignatures } from '../internals/plugins/allPlugins';

export interface ChartContainerProProps<TSeries extends ChartSeriesType = ChartSeriesType>
extends ChartDataProviderProps<
[UseChartCartesianAxisSignature<TSeries>, UseChartProZoomSignature],
TSeries
>,
extends ChartDataProviderProps<TSeries, AllPluginSignatures<TSeries>>,
ChartsSurfaceProps {}

const releaseInfo = getReleaseInfo();
Expand Down Expand Up @@ -53,9 +50,7 @@ const ChartContainerPro = React.forwardRef(function ChartContainerProInner<
useLicenseVerifier('x-charts-pro', releaseInfo);

return (
<ChartDataProvider<TSeries, [UseChartCartesianAxisSignature<TSeries>, UseChartProZoomSignature]>
{...chartDataProviderProProps}
>
<ChartDataProvider<TSeries, AllPluginSignatures<TSeries>> {...chartDataProviderProProps}>
<ChartsSurface {...chartsSurfaceProps}>{children}</ChartsSurface>
<Watermark packageName="x-charts-pro" releaseInfo={releaseInfo} />
</ChartDataProvider>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
'use client';
import {
ChartDataProviderProps,
ChartPlugin,
ChartSeriesType,
useChartCartesianAxis,
UseChartCartesianAxisSignature,
useChartContainerProps,
UseChartContainerPropsReturnValue,
} from '@mui/x-charts/internals';
import * as React from 'react';
import type { ChartContainerProProps } from './ChartContainerPro';
import { useChartProZoom } from '../internals/plugins/useChartProZoom';
import { UseChartProZoomSignature } from '../internals/plugins/useChartProZoom/useChartProZoom.types';
import { ALL_PLUGINS, AllPluginsType, AllPluginSignatures } from '../internals/plugins/allPlugins';

export type UseChartContainerProPropsReturnValue<TSeries extends ChartSeriesType> = Pick<
UseChartContainerPropsReturnValue<TSeries>,
'chartsSurfaceProps' | 'children'
> & {
chartDataProviderProProps: ChartDataProviderProps<
[UseChartCartesianAxisSignature<TSeries>, UseChartProZoomSignature],
TSeries
>;
chartDataProviderProProps: ChartDataProviderProps<TSeries, AllPluginSignatures<TSeries>>;
};

export const useChartContainerProProps = <TSeries extends ChartSeriesType = ChartSeriesType>(
Expand All @@ -30,10 +23,7 @@ export const useChartContainerProProps = <TSeries extends ChartSeriesType = Char
const { initialZoom, onZoomChange, plugins, apiRef, ...baseProps } = props;

const chartDataProviderProProps: Pick<
ChartDataProviderProps<
[UseChartCartesianAxisSignature<TSeries>, UseChartProZoomSignature],
TSeries
>,
ChartDataProviderProps<TSeries, AllPluginSignatures<TSeries>>,
'initialZoom' | 'onZoomChange'
> = {
initialZoom,
Expand All @@ -50,12 +40,7 @@ export const useChartContainerProProps = <TSeries extends ChartSeriesType = Char
...chartDataProviderProps,
...chartDataProviderProProps,
apiRef,
plugins: plugins ?? [
// eslint-disable-next-line react-compiler/react-compiler
useChartCartesianAxis as unknown as ChartPlugin<UseChartCartesianAxisSignature<TSeries>>,
// eslint-disable-next-line react-compiler/react-compiler
useChartProZoom,
],
plugins: plugins ?? (ALL_PLUGINS as unknown as AllPluginsType<TSeries>),
},
chartsSurfaceProps,
children,
Expand Down
27 changes: 12 additions & 15 deletions packages/x-charts-pro/src/ScatterChartPro/ScatterChartPro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import PropTypes from 'prop-types';
import { useThemeProps } from '@mui/material/styles';
import { ChartsOverlay } from '@mui/x-charts/ChartsOverlay';
import { ScatterChartProps, ScatterPlot } from '@mui/x-charts/ScatterChart';
import { ChartDataProvider, ZAxisContextProvider } from '@mui/x-charts/context';
import { ChartDataProvider } from '@mui/x-charts/context';
import { ChartsVoronoiHandler } from '@mui/x-charts/ChartsVoronoiHandler';
import { ChartsAxis } from '@mui/x-charts/ChartsAxis';
import { ChartsGrid } from '@mui/x-charts/ChartsGrid';
Expand Down Expand Up @@ -39,7 +39,6 @@ const ScatterChartPro = React.forwardRef(function ScatterChartPro(
const {
chartsWrapperProps,
chartContainerProps,
zAxisProps,
voronoiHandlerProps,
chartsAxisProps,
gridProps,
Expand All @@ -66,19 +65,17 @@ const ScatterChartPro = React.forwardRef(function ScatterChartPro(
<ChartsWrapper {...chartsWrapperProps}>
{!props.hideLegend && <ChartsLegend {...legendProps} />}
<ChartsSurface {...chartsSurfaceProps}>
<ZAxisContextProvider {...zAxisProps}>
{!props.disableVoronoi && <ChartsVoronoiHandler {...voronoiHandlerProps} />}
<ChartsAxis {...chartsAxisProps} />
<ChartsGrid {...gridProps} />
<g data-drawing-container>
{/* The `data-drawing-container` indicates that children are part of the drawing area. Ref: https://github.com/mui/mui-x/issues/13659 */}
<ScatterPlot {...scatterPlotProps} />
</g>
<ChartsOverlay {...overlayProps} />
<ChartsAxisHighlight {...axisHighlightProps} />
{!props.loading && <Tooltip {...props?.slotProps?.tooltip} trigger="item" />}
{children}
</ZAxisContextProvider>
{!props.disableVoronoi && <ChartsVoronoiHandler {...voronoiHandlerProps} />}
<ChartsAxis {...chartsAxisProps} />
<ChartsGrid {...gridProps} />
<g data-drawing-container>
{/* The `data-drawing-container` indicates that children are part of the drawing area. Ref: https://github.com/mui/mui-x/issues/13659 */}
<ScatterPlot {...scatterPlotProps} />
</g>
<ChartsOverlay {...overlayProps} />
<ChartsAxisHighlight {...axisHighlightProps} />
{!props.loading && <Tooltip {...props?.slotProps?.tooltip} trigger="item" />}
{children}
</ChartsSurface>
</ChartsWrapper>
</ChartDataProvider>
Expand Down
30 changes: 30 additions & 0 deletions packages/x-charts-pro/src/internals/plugins/allPlugins.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// This file should be removed after creating all plugins in favor of a file per chart type.

import {
ChartSeriesType,
ConvertSignaturesIntoPlugins,
useChartCartesianAxis,
UseChartCartesianAxisSignature,
useChartInteraction,
UseChartInteractionSignature,
useChartZAxis,
UseChartZAxisSignature,
} from '@mui/x-charts/internals';
import { useChartProZoom, UseChartProZoomSignature } from './useChartProZoom';

export type AllPluginSignatures<TSeries extends ChartSeriesType = ChartSeriesType> = [
UseChartZAxisSignature,
UseChartCartesianAxisSignature<TSeries>,
UseChartInteractionSignature,
UseChartProZoomSignature,
];

export type AllPluginsType<TSeries extends ChartSeriesType = ChartSeriesType> =
ConvertSignaturesIntoPlugins<AllPluginSignatures<TSeries>>;

export const ALL_PLUGINS = [
useChartZAxis,
useChartCartesianAxis,
useChartInteraction,
useChartProZoom,
];
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './useChartProZoom.selectors';
export * from './useChartProZoom';
export * from './useChartProZoom.types';
15 changes: 3 additions & 12 deletions packages/x-charts/src/ChartContainer/ChartContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@ import { ChartSeriesType } from '../models/seriesType/config';
import { ChartDataProvider, ChartDataProviderProps } from '../context/ChartDataProvider';
import { useChartContainerProps } from './useChartContainerProps';
import { ChartsSurface, ChartsSurfaceProps } from '../ChartsSurface';
import { UseChartCartesianAxisSignature } from '../internals/plugins/featurePlugins/useChartCartesianAxis';
import { UseChartInteractionSignature } from '../internals/plugins/featurePlugins/useChartInteraction';
import { AllPluginSignatures } from '../internals/plugins/allPlugins';

export interface ChartContainerProps<SeriesType extends ChartSeriesType = ChartSeriesType>
extends Omit<
ChartDataProviderProps<[UseChartCartesianAxisSignature<SeriesType>], SeriesType>,
'children'
>,
extends Omit<ChartDataProviderProps<SeriesType, AllPluginSignatures<SeriesType>>, 'children'>,
ChartsSurfaceProps {}

/**
Expand Down Expand Up @@ -49,12 +45,7 @@ const ChartContainer = React.forwardRef(function ChartContainer<TSeries extends
);

return (
<ChartDataProvider<
TSeries,
[UseChartCartesianAxisSignature<TSeries>, UseChartInteractionSignature]
>
{...chartDataProviderProps}
>
<ChartDataProvider<TSeries, AllPluginSignatures<TSeries>> {...chartDataProviderProps}>
<ChartsSurface {...chartsSurfaceProps}>{children}</ChartsSurface>
</ChartDataProvider>
);
Expand Down
21 changes: 4 additions & 17 deletions packages/x-charts/src/ChartContainer/useChartContainerProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,11 @@ import * as React from 'react';
import { ChartsSurfaceProps } from '../ChartsSurface';
import { ChartDataProviderProps } from '../context/ChartDataProvider';
import type { ChartContainerProps } from './ChartContainer';
import {
useChartCartesianAxis,
UseChartCartesianAxisSignature,
} from '../internals/plugins/featurePlugins/useChartCartesianAxis';
import {
useChartInteraction,
UseChartInteractionSignature,
} from '../internals/plugins/featurePlugins/useChartInteraction';
import { ChartSeriesType } from '../models/seriesType/config';
import { ALL_PLUGINS, AllPluginSignatures, AllPluginsType } from '../internals/plugins/allPlugins';

export type UseChartContainerPropsReturnValue<TSeries extends ChartSeriesType> = {
chartDataProviderProps: ChartDataProviderProps<
[UseChartCartesianAxisSignature<TSeries>, UseChartInteractionSignature],
TSeries
>;
chartDataProviderProps: ChartDataProviderProps<TSeries, AllPluginSignatures<TSeries>>;
chartsSurfaceProps: ChartsSurfaceProps & { ref: React.Ref<SVGSVGElement> };
children: React.ReactNode;
};
Expand Down Expand Up @@ -58,10 +48,7 @@ export const useChartContainerProps = <TSeries extends ChartSeriesType = ChartSe
};

const chartDataProviderProps: Omit<
ChartDataProviderProps<
[UseChartCartesianAxisSignature<TSeries>, UseChartInteractionSignature],
TSeries
>,
ChartDataProviderProps<TSeries, AllPluginSignatures<TSeries>>,
'children'
> = {
margin,
Expand All @@ -77,7 +64,7 @@ export const useChartContainerProps = <TSeries extends ChartSeriesType = ChartSe
width,
height,
seriesConfig,
plugins: [useChartCartesianAxis as any, useChartInteraction],
plugins: ALL_PLUGINS as unknown as AllPluginsType<TSeries>,
};

return {
Expand Down
4 changes: 2 additions & 2 deletions packages/x-charts/src/ChartsLegend/useAxis.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use client';
import { AxisDefaultized } from '../models/axis';
import { ZAxisDefaultized } from '../models/z-axis';
import { useZAxis } from '../hooks/useZAxis';
import { useZAxes } from '../hooks/useZAxis';
import { useXAxes, useYAxes } from '../hooks/useAxis';
import { ColorLegendSelector } from './colorLegend.types';

Expand All @@ -14,7 +14,7 @@ export function useAxis({
}: ColorLegendSelector): ZAxisDefaultized | AxisDefaultized {
const { xAxis, xAxisIds } = useXAxes();
const { yAxis, yAxisIds } = useYAxes();
const { zAxis, zAxisIds } = useZAxis();
const { zAxis, zAxisIds } = useZAxes();

switch (axisDirection) {
case 'x': {
Expand Down
4 changes: 2 additions & 2 deletions packages/x-charts/src/ChartsTooltip/useAxisTooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { getLabel } from '../internals/getLabel';
import { isCartesianSeriesType } from '../internals/isCartesian';
import { utcFormatter } from './utils';
import { useXAxes, useXAxis, useYAxes, useYAxis } from '../hooks/useAxis';
import { useZAxis } from '../hooks/useZAxis';
import { useZAxes } from '../hooks/useZAxis';
import {
selectorChartsInteractionXAxis,
selectorChartsInteractionYAxis,
Expand Down Expand Up @@ -53,7 +53,7 @@ export function useAxisTooltip(): UseAxisTooltipReturnValue | null {
const { xAxis } = useXAxes();
const { yAxis } = useYAxes();

const { zAxis, zAxisIds } = useZAxis();
const { zAxis, zAxisIds } = useZAxes();
const colorProcessors = useColorProcessor();

if (axisData === null) {
Expand Down
6 changes: 3 additions & 3 deletions packages/x-charts/src/ChartsTooltip/useItemTooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { getLabel } from '../internals/getLabel';
import { selectorChartsInteractionItem } from '../internals/plugins/featurePlugins/useChartInteraction';
import { useSelector } from '../internals/store/useSelector';
import { useStore } from '../internals/store/useStore';
import { useXAxes, useYAxes } from '../hooks';
import { useZAxis } from '../hooks/useZAxis';
import { useXAxes, useYAxes } from '../hooks/useAxis';
import { useZAxes } from '../hooks/useZAxis';
import { ChartsLabelMarkProps } from '../ChartsLabel';

export interface UseItemTooltipReturnValue<T extends ChartSeriesType> {
Expand All @@ -32,7 +32,7 @@ export function useItemTooltip<T extends ChartSeriesType>(): null | UseItemToolt

const { xAxis, xAxisIds } = useXAxes();
const { yAxis, yAxisIds } = useYAxes();
const { zAxis, zAxisIds } = useZAxis();
const { zAxis, zAxisIds } = useZAxes();
const colorProcessors = useColorProcessor();

const xAxisId = (series as any).xAxisId ?? xAxisIds[0];
Expand Down
27 changes: 11 additions & 16 deletions packages/x-charts/src/ScatterChart/ScatterChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import {
ChartsVoronoiHandlerProps,
} from '../ChartsVoronoiHandler/ChartsVoronoiHandler';
import { ChartsGrid, ChartsGridProps } from '../ChartsGrid';
import { ZAxisContextProvider, ZAxisContextProviderProps } from '../context/ZAxisContextProvider';
import { useScatterChartProps } from './useScatterChartProps';
import { useChartContainerProps } from '../ChartContainer/useChartContainerProps';
import { ChartDataProvider } from '../context';
Expand All @@ -50,7 +49,6 @@ export interface ScatterChartSlotProps

export interface ScatterChartProps
extends Omit<ChartContainerProps, 'series' | 'plugins'>,
Omit<ZAxisContextProviderProps, 'children' | 'dataset'>,
Omit<ChartsAxisProps, 'slots' | 'slotProps'>,
Omit<ChartsOverlayProps, 'slots' | 'slotProps'>,
Omit<ChartsVoronoiHandlerProps, 'onItemClick'> {
Expand Down Expand Up @@ -114,7 +112,6 @@ const ScatterChart = React.forwardRef(function ScatterChart(
const {
chartsWrapperProps,
chartContainerProps,
zAxisProps,
voronoiHandlerProps,
chartsAxisProps,
gridProps,
Expand All @@ -136,19 +133,17 @@ const ScatterChart = React.forwardRef(function ScatterChart(
<ChartsWrapper {...chartsWrapperProps}>
{!props.hideLegend && <ChartsLegend {...legendProps} />}
<ChartsSurface {...chartsSurfaceProps}>
<ZAxisContextProvider {...zAxisProps}>
{!props.disableVoronoi && <ChartsVoronoiHandler {...voronoiHandlerProps} />}
<ChartsAxis {...chartsAxisProps} />
<ChartsGrid {...gridProps} />
<g data-drawing-container>
{/* The `data-drawing-container` indicates that children are part of the drawing area. Ref: https://github.com/mui/mui-x/issues/13659 */}
<ScatterPlot {...scatterPlotProps} />
</g>
<ChartsOverlay {...overlayProps} />
<ChartsAxisHighlight {...axisHighlightProps} />
{!props.loading && <Tooltip trigger="item" {...props.slotProps?.tooltip} />}
{children}
</ZAxisContextProvider>
{!props.disableVoronoi && <ChartsVoronoiHandler {...voronoiHandlerProps} />}
<ChartsAxis {...chartsAxisProps} />
<ChartsGrid {...gridProps} />
<g data-drawing-container>
{/* The `data-drawing-container` indicates that children are part of the drawing area. Ref: https://github.com/mui/mui-x/issues/13659 */}
<ScatterPlot {...scatterPlotProps} />
</g>
<ChartsOverlay {...overlayProps} />
<ChartsAxisHighlight {...axisHighlightProps} />
{!props.loading && <Tooltip trigger="item" {...props.slotProps?.tooltip} />}
{children}
</ChartsSurface>
</ChartsWrapper>
</ChartDataProvider>
Expand Down
Loading

0 comments on commit aa515ec

Please sign in to comment.