Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
12 changes: 12 additions & 0 deletions docs/data/migration/migration-charts-v8/migration-charts-v8.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,3 +343,15 @@ If you're using these classes manually in your styles, update them accordingly:
-`.MuiBar-root`
+`.MuiBarChart-root`
```

## Typescript

### Remove default generic of `SeriesItemIdentifier`

In v9 the argument of `SeriesItemIdentifier` is now required.

It accept an union of series types.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
It accept an union of series types.
It accepts an union of series types.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oupsi, I'm adding this change on the follow up PR 👍

770a4cc

For example:

- `SeriesItemIdentifier<'bar'>` for a BarChart.
- `SeriesItemIdentifier<'bar' | 'line'>` if you compose bar and line series.
4 changes: 2 additions & 2 deletions docs/pages/x/api/charts/chart-container.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"hiddenItems": {
"type": {
"name": "arrayOf",
"description": "Array&lt;{ dataIndex?: any, seriesId?: object, type: object }&gt;"
"description": "Array&lt;{ dataIndex?: number, seriesId?: string, type: object }&gt;"
}
},
"highlightedAxis": {
Expand All @@ -37,7 +37,7 @@
"initialHiddenItems": {
"type": {
"name": "arrayOf",
"description": "Array&lt;{ dataIndex?: any, seriesId?: object, type: object }&gt;"
"description": "Array&lt;{ dataIndex?: number, seriesId?: string, type: object }&gt;"
}
},
"localeText": { "type": { "name": "object" } },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import {
type ChartSeriesType,
type UseChartContainerPropsReturnValue,
} from '@mui/x-charts/internals';
import { type ChartDataProviderProps } from '@mui/x-charts/ChartDataProvider';
import { useChartContainerProProps } from '@mui/x-charts-pro/internals';
import { DEFAULT_PLUGINS, type AllPluginSignatures } from '../internals/plugins/allPlugins';
import type { ChartContainerPremiumProps } from './ChartContainerPremium';
import type { ChartDataProviderPremiumProps } from '../ChartDataProviderPremium';

export type UseChartContainerPremiumPropsReturnValue<
TSeries extends ChartSeriesType,
Expand All @@ -17,7 +17,7 @@ export type UseChartContainerPremiumPropsReturnValue<
UseChartContainerPropsReturnValue<TSeries, TSignatures>,
'chartsSurfaceProps' | 'children'
> & {
chartDataProviderPremiumProps: ChartDataProviderProps<TSeries, TSignatures>;
chartDataProviderPremiumProps: ChartDataProviderPremiumProps<TSeries, TSignatures>;
};

export function useChartContainerPremiumProps<
Expand All @@ -43,7 +43,7 @@ export function useChartContainerPremiumProps<
const chartDataProviderPremiumProps = {
...chartDataProviderProProps,
plugins: plugins ?? DEFAULT_PLUGINS,
} as unknown as ChartDataProviderProps<TSeries, TSignatures>;
} as unknown as ChartDataProviderPremiumProps<TSeries, TSignatures>;

return {
chartDataProviderPremiumProps,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
export type AllPluginSignatures<TSeries extends ChartSeriesType = ChartSeriesType> = [
UseChartZAxisSignature,
UseChartBrushSignature,
UseChartTooltipSignature,
UseChartTooltipSignature<TSeries>,
UseChartInteractionSignature,
UseChartCartesianAxisSignature<TSeries>,
UseChartPolarAxisSignature<TSeries>,
Expand Down
8 changes: 4 additions & 4 deletions packages/x-charts/src/ChartContainer/ChartContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ ChartContainer.propTypes = {
*/
hiddenItems: PropTypes.arrayOf(
PropTypes.shape({
dataIndex: PropTypes.any,
seriesId: PropTypes.object,
dataIndex: PropTypes.number,
seriesId: PropTypes.string,
type: PropTypes.object.isRequired,
}),
),
Expand Down Expand Up @@ -193,8 +193,8 @@ ChartContainer.propTypes = {
*/
initialHiddenItems: PropTypes.arrayOf(
PropTypes.shape({
dataIndex: PropTypes.any,
seriesId: PropTypes.object,
dataIndex: PropTypes.number,
seriesId: PropTypes.string,
type: PropTypes.object.isRequired,
}),
),
Expand Down
3 changes: 2 additions & 1 deletion packages/x-charts/src/ChartsTooltip/useItemTooltip.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use client';
import { useSeries } from '../hooks/useSeries';
import { type ChartSeriesDefaultized, type ChartSeriesType } from '../models/seriesType/config';
import { type SeriesItemIdentifier } from '../models/seriesType';
import { selectorChartsTooltipItem } from '../internals/plugins/featurePlugins/useChartTooltip';
import { useStore } from '../internals/store/useStore';
import { useRotationAxes, useXAxes, useYAxes } from '../hooks/useAxis';
Expand All @@ -23,7 +24,7 @@ export function useInternalItemTooltip<T extends ChartSeriesType>():
| (T extends 'radar' ? ItemTooltipWithMultipleValues<T> : ItemTooltip<T>)
| null {
const store = useStore();
const identifier = store.use(selectorChartsTooltipItem);
const identifier = store.use(selectorChartsTooltipItem) as SeriesItemIdentifier<T>;
const seriesConfig = store.use(selectorChartSeriesConfig);

const series = useSeries();
Expand Down
4 changes: 2 additions & 2 deletions packages/x-charts/src/hooks/useInteractionItemProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ function onPointerDown(event: React.PointerEvent) {
}
}

export const useInteractionItemProps = (
data: SeriesItemIdentifierWithData,
export const useInteractionItemProps = <SeriesType extends ChartSeriesType>(
data: SeriesItemIdentifierWithData<SeriesType>,
skip?: boolean,
): {
onPointerEnter?: () => void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ import {
import { serializeIdentifier as serializeIdentifierFn } from './utils/serializeIdentifier';
import { cleanIdentifier as cleanIdentifierFn } from './utils/cleanIdentifier';
import type { ChartSeriesConfig } from './types';
import type { SeriesItemIdentifier } from '../../../../models/seriesType';
import type { ChartSeriesType } from '../../../../models/seriesType/config';

export const useChartSeriesConfig: ChartPlugin<UseChartSeriesConfigSignature> = ({ store }) => {
const serializeIdentifier: SerializeIdentifierFunction = useEventCallback((identifier) =>
serializeIdentifierFn(store.state.seriesConfig.config, identifier),
);

const cleanIdentifier: CleanIdentifierFunction = useEventCallback((identifier) =>
cleanIdentifierFn(store.state.seriesConfig.config, identifier),
const cleanIdentifier: CleanIdentifierFunction = useEventCallback(
<T extends { type: ChartSeriesType }>(identifier: T): SeriesItemIdentifier<T['type']> =>
cleanIdentifierFn<T['type'], T>(store.state.seriesConfig.config, identifier),
);

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import useEventCallback from '@mui/utils/useEventCallback';
import { useSvgRef } from '../../../../hooks';
import { type UseChartTooltipSignature } from '../../featurePlugins/useChartTooltip';
import { type SeriesItemIdentifier } from '../../../../models/seriesType';
import { type ChartSeriesType } from '../../../../models/seriesType/config';
import { type UseChartInteractionSignature } from '../useChartInteraction';
import { type UseChartHighlightSignature } from '../useChartHighlight';
import { type UseChartCartesianAxisSignature } from '../useChartCartesianAxis';
Expand All @@ -15,11 +16,11 @@ import { type ChartState } from '../../models';
/**
* Hook to get pointer interaction props for chart items.
*/
export function useRegisterPointerInteractions(
export function useRegisterPointerInteractions<SeriesType extends ChartSeriesType>(
getItemAtPosition: (
state: ChartState<[UseChartCartesianAxisSignature, UseChartHighlightSignature]>,
point: { x: number; y: number },
) => SeriesItemIdentifier | undefined,
) => SeriesItemIdentifier<SeriesType> | undefined,
onItemEnter?: () => void,
onItemLeave?: () => void,
) {
Expand All @@ -30,7 +31,7 @@ export function useRegisterPointerInteractions(
const svgRef = useSvgRef();
const store = useStore<[UseChartCartesianAxisSignature, UseChartHighlightSignature]>();
const interactionActive = React.useRef(false);
const lastItemRef = React.useRef<SeriesItemIdentifier | undefined>(undefined);
const lastItemRef = React.useRef<SeriesItemIdentifier<SeriesType> | undefined>(undefined);

const onItemEnterRef = useEventCallback(() => onItemEnter?.());
const onItemLeaveRef = useEventCallback(() => onItemLeave?.());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const useChartTooltip: ChartPlugin<UseChartTooltipSignature<any>> = <
});

const setTooltipItem = useEventCallback(function setTooltipItem(
newItem: SeriesItemIdentifier<ChartSeriesType>,
newItem: SeriesItemIdentifier<SeriesType>,
) {
if (!fastObjectShallowCompare(store.state.tooltip.item, newItem)) {
params.onTooltipItemChange?.(newItem);
Expand Down
10 changes: 6 additions & 4 deletions packages/x-charts/src/models/seriesType/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ type DefaultizedSeriesType<T extends ChartSeriesType = ChartSeriesType> =

// item identifier

export type SeriesItemIdentifier<T extends ChartSeriesType = ChartSeriesType> =
ChartsSeriesConfig[T]['itemIdentifier'];
export type SeriesItemIdentifier<T extends ChartSeriesType> = T extends ChartSeriesType
? ChartsSeriesConfig[T]['itemIdentifier']
: never;

export type SeriesItemIdentifierWithData<T extends ChartSeriesType = ChartSeriesType> =
ChartsSeriesConfig[T]['itemIdentifierWithData'];
export type SeriesItemIdentifierWithData<T extends ChartSeriesType> = T extends ChartSeriesType
? ChartsSeriesConfig[T]['itemIdentifierWithData']
: never;

export type FocusedItemIdentifier<T extends ChartSeriesType = ChartSeriesType> = T extends
| 'line'
Expand Down
Loading