Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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: 2 additions & 2 deletions docs/data/charts/accessibility/accessibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ When focused, the chart highlights a value item that can be modified with arrow
The focus highlight is done with a dedicated SVG element.
When using composition, you've to add this component to make the focus visible.

Each series type has its own component.
Exception for the pie chart that has the focus directly managed in the `<PieArcPlot />`.
Each series type has its own component:

```js
import { FocusedBar } from '@mui/x-charts/BarChart';
import { FocusedPieArc } from '@mui/x-charts/BarChart';
import { FocusedLineMark } from '@mui/x-charts/LineChart';
import { FocusedScatterMark } from '@mui/x-charts/ScatterChart';
```
66 changes: 66 additions & 0 deletions packages/x-charts/src/PieChart/FocusedPieArc.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
'use client';
import * as React from 'react';
import { useTheme } from '@mui/material/styles';
import { useFocusedItem } from '../hooks/useFocusedItem';
import { usePieSeriesContext, usePieSeriesLayout } from '../hooks/usePieSeries';
import { PieArc, pieArcClasses, type PieArcProps } from './PieArc';
import { useItemHighlighted } from '../hooks/useItemHighlighted';
import { getModifiedArcProperties } from './dataTransform/getModifiedArcProperties';

export function FocusedPieArc(
props: Partial<
Omit<
PieArcProps,
'startAngle' | 'endAngle' | 'id' | 'dataIndex' | 'isFaded' | 'isHighlighted' | 'isFocused'
>
>,
) {
const theme = useTheme();
const focusedItem = useFocusedItem();
const pieSeriesLayout = usePieSeriesLayout();

const { isHighlighted, isFaded } = useItemHighlighted(focusedItem);
const pieSeries = usePieSeriesContext();

if (focusedItem === null || focusedItem.seriesType !== 'pie' || !pieSeries) {
return null;
}

const series = pieSeries?.series[focusedItem.seriesId];
const { center, radius } = pieSeriesLayout[focusedItem.seriesId];

if (!series || !center || !radius) {
return null;
}

const item = series.data[focusedItem.dataIndex];

const arcSizes = getModifiedArcProperties(
series,
pieSeriesLayout[focusedItem.seriesId],
isHighlighted,
isFaded,
);

return (
<PieArc
transform={`translate(${pieSeriesLayout[series.id].center.x}, ${pieSeriesLayout[series.id].center.y})`}
startAngle={item.startAngle}
endAngle={item.endAngle}
color="transparent"
pointerEvents="none"
skipInteraction
skipAnimation
stroke={(theme.vars ?? theme).palette.text.primary}
id={series.id}
className={pieArcClasses.focusIndicator}
dataIndex={focusedItem.dataIndex}
isFaded={false}
isHighlighted={false}
isFocused={false}
strokeWidth={3}
{...arcSizes}
{...props}
/>
);
}
35 changes: 1 addition & 34 deletions packages/x-charts/src/PieChart/PieArcPlot.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
'use client';
import * as React from 'react';
import PropTypes from 'prop-types';
import { useTheme } from '@mui/material/styles';
import { useFocusedItem } from '../hooks/useFocusedItem';
import { PieArc, type PieArcProps, pieArcClasses } from './PieArc';
import { PieArc, type PieArcProps } from './PieArc';
import {
type ComputedPieRadius,
type DefaultizedPieSeriesType,
Expand Down Expand Up @@ -76,8 +74,6 @@ function PieArcPlot(props: PieArcPlotProps) {
...other
} = props;

const theme = useTheme();

const transformedData = useTransformData({
innerRadius,
outerRadius,
Expand All @@ -89,12 +85,6 @@ function PieArcPlot(props: PieArcPlotProps) {
data,
});

const { dataIndex, seriesId, seriesType } = useFocusedItem() ?? {};
const focusedItem =
dataIndex !== undefined && seriesId === id && seriesType === 'pie'
? transformedData[dataIndex]
: null;

if (data.length === 0) {
return null;
}
Expand Down Expand Up @@ -128,29 +118,6 @@ function PieArcPlot(props: PieArcPlotProps) {
{...slotProps?.pieArc}
/>
))}
{/* Render the focus indicator last, so it can align nicely over all arcs */}
{focusedItem && (
Copy link
Member

Choose a reason for hiding this comment

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

Isn't this technically a breaking change? Someone using PieArcPlot will now be missing the outline, right?

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 but it's a legal breaking change O:)

image

<Arc
startAngle={focusedItem.startAngle}
endAngle={focusedItem.endAngle}
paddingAngle={focusedItem.paddingAngle}
innerRadius={focusedItem.innerRadius}
color="transparent"
pointerEvents="none"
skipInteraction
outerRadius={focusedItem.outerRadius}
cornerRadius={focusedItem.cornerRadius}
skipAnimation
stroke={(theme.vars ?? theme).palette.text.primary}
id={id}
className={pieArcClasses.focusIndicator}
dataIndex={focusedItem.dataIndex}
isFaded={false}
isHighlighted={false}
isFocused={false}
strokeWidth={3}
/>
)}
</g>
);
}
Expand Down
2 changes: 2 additions & 0 deletions packages/x-charts/src/PieChart/PieChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { useChartContainerProps } from '../ChartContainer/useChartContainerProps
import { ChartsWrapper } from '../ChartsWrapper';
import { PIE_CHART_PLUGINS, type PieChartPluginSignatures } from './PieChart.plugins';
import { defaultizeMargin } from '../internals/defaultizeMargin';
import { FocusedPieArc } from './FocusedPieArc';

export interface PieChartSlots
extends PiePlotSlots,
Expand Down Expand Up @@ -161,6 +162,7 @@ const PieChart = React.forwardRef(function PieChart(
)}
<ChartsSurface {...chartsSurfaceProps}>
<PiePlot slots={slots} slotProps={slotProps} onItemClick={onItemClick} />
<FocusedPieArc />
<ChartsOverlay loading={loading} slots={slots} slotProps={slotProps} />
{children}
</ChartsSurface>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import type { DefaultizedPieSeriesType, PieSeriesLayout } from '../../models/seriesType/pie';
import { deg2rad } from '../../internals/angleConversion';

/**
* Function that returns arc properties after applying transformation from highlight/fade states.
*/
export function getModifiedArcProperties(
seriesDef: Pick<
DefaultizedPieSeriesType,
'cornerRadius' | 'paddingAngle' | 'id' | 'highlighted' | 'faded' | 'data'
>,
seriesLayout: Pick<PieSeriesLayout, 'radius'>,
Copy link
Member

Choose a reason for hiding this comment

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

If we're just picking the radius, why not simplify the type?

Suggested change
seriesLayout: Pick<PieSeriesLayout, 'radius'>,
layoutRadius: PieSeriesLayout['radius'],

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 think it's easier to have a split that match hooks.

  • seriesDef is a subset of the useSeries
  • seriesLayout is a subset of the useSeriesLayout

Copy link
Member

Choose a reason for hiding this comment

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

I usually prefer simpler types so that functions are easier to test. IMO, the called function doesn't need to know where the data comes from, that's the calling function's responsibility. Not a big deal, though, just style preference

isHighlighted: boolean,
isFaded: boolean,
) {
const {
faded,
highlighted,
paddingAngle: basePaddingAngle = 0,
cornerRadius: baseCornerRadius = 0,
} = seriesDef;

const {
radius: { inner: baseInnerRadius = 0, label: baseArcLabelRadius, outer: baseOuterRadius },
} = seriesLayout;

const attributesOverride = {
additionalRadius: 0,
...((isFaded && faded) || (isHighlighted && highlighted) || {}),
};
const paddingAngle = Math.max(0, deg2rad(attributesOverride.paddingAngle ?? basePaddingAngle));
const innerRadius = Math.max(0, attributesOverride.innerRadius ?? baseInnerRadius);

const outerRadius = Math.max(
0,
attributesOverride.outerRadius ?? baseOuterRadius + attributesOverride.additionalRadius,
);
const cornerRadius = attributesOverride.cornerRadius ?? baseCornerRadius;

const arcLabelRadius =
attributesOverride.arcLabelRadius ?? baseArcLabelRadius ?? (innerRadius + outerRadius) / 2;

return {
paddingAngle,
innerRadius,
outerRadius,
cornerRadius,
arcLabelRadius,
};
}
65 changes: 18 additions & 47 deletions packages/x-charts/src/PieChart/dataTransform/useTransformData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from '../../models/seriesType/pie';
import { useItemHighlightedGetter } from '../../hooks/useItemHighlightedGetter';
import { useIsItemFocusedGetter } from '../../hooks/useIsItemFocusedGetter';
import { deg2rad } from '../../internals/angleConversion';
import { getModifiedArcProperties } from './getModifiedArcProperties';

export interface AnimatedObject {
innerRadius: number;
Expand All @@ -33,17 +33,7 @@ export function useTransformData(
> &
ComputedPieRadius,
) {
const {
id: seriesId,
data,
faded,
highlighted,
paddingAngle: basePaddingAngle = 0,
innerRadius: baseInnerRadius = 0,
arcLabelRadius: baseArcLabelRadius,
outerRadius: baseOuterRadius,
cornerRadius: baseCornerRadius = 0,
} = series;
const { id: seriesId, data, faded, highlighted } = series;

const { isFaded: isItemFaded, isHighlighted: isItemHighlighted } = useItemHighlightedGetter();
const isItemFocused = useIsItemFocusedGetter();
Expand All @@ -59,26 +49,24 @@ export function useTransformData(
const isFaded = !isHighlighted && isItemFaded(currentItem);
const isFocused = isItemFocused({ seriesType: 'pie', seriesId, dataIndex: itemIndex });

// TODO v9: Replace the second argument with the result of useSeriesLayout
Copy link
Member

Choose a reason for hiding this comment

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

Why can't we use it now?

Copy link
Member Author

Choose a reason for hiding this comment

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

A simplified version to illustrate the issue

export fucntion PiePlot(){
  const innerRadius = usePieSeriesLayout()

  return <g>
    <PieArcPlot innerRadius={innerRadius} /> // This component calls useTransformData()
    <PieArcLabelPlot innerRadius={innerRadius} /> // This component calls useTransformData()
  </g>
}

The issue is that PiePlot, PieArcPlot, and PieArcLabelPlot are all exported.

If I use directly the result of usePieSeriesLayout() in the useTransformData() the innerRadius prop would have no effect which is a breaking change

Copy link
Member

Choose a reason for hiding this comment

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

I don't see how we would fix this in the future then 🤔

Like, shouldn't we default to the value of pieSeriesLayout to prevent inconsistency?

  const pieSeriesLayout = usePieSeriesLayout();
  const layout = pieSeriesLayout[seriesId]

  const arcSizes = applyArcModifiers(
    series,
    {
      radius: {
        inner: series.innerRadius ?? layout.radius.innerRadius
        ...
      }
    },
    isHighlighted,
    isFaded,
  );

Copy link
Member Author

@alexfauquette alexfauquette Dec 11, 2025

Choose a reason for hiding this comment

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

In the future, we could

  • remove the PiePlot
  • remove innerRadius props from PieArcPlot to only use the seriesLayout

Then we will be able to do

const pieSeriesLayout = usePieSeriesLayout();
const layout = pieSeriesLayout[seriesId]

const arcSizes = applyArcModifiers(
    series,
    layout,
    isHighlighted,
    isFaded,
  );

By the way series.innerRadius ?? layout.radius.innerRadius is nto correct, because :

  • series.innerRadius is the user config. It can be a percentage like "90%"
  • layout.radius.innerRadius is the equivalent in px of the series.innerRadius

The only thing that interest us from series is the series.faded.inneRadius and series.highlighted.inneRadius

const arcSizes = getModifiedArcProperties(
series,
{
radius: {
inner: series.innerRadius ?? 0,
outer: series.outerRadius,
label: series.arcLabelRadius ?? 0,
available: 0,
},
},
isHighlighted,
isFaded,
);
const attributesOverride = {
additionalRadius: 0,
...((isFaded && faded) || (isHighlighted && highlighted) || {}),
};
const paddingAngle = Math.max(
0,
deg2rad(attributesOverride.paddingAngle ?? basePaddingAngle),
);
const innerRadius = Math.max(0, attributesOverride.innerRadius ?? baseInnerRadius);

const outerRadius = Math.max(
0,
attributesOverride.outerRadius ?? baseOuterRadius + attributesOverride.additionalRadius,
);
const cornerRadius = attributesOverride.cornerRadius ?? baseCornerRadius;

const arcLabelRadius =
attributesOverride.arcLabelRadius ??
baseArcLabelRadius ??
(innerRadius + outerRadius) / 2;

return {
...item,
Expand All @@ -87,27 +75,10 @@ export function useTransformData(
isFaded,
isHighlighted,
isFocused,
paddingAngle,
innerRadius,
outerRadius,
cornerRadius,
arcLabelRadius,
...arcSizes,
};
}),
[
baseCornerRadius,
baseInnerRadius,
baseOuterRadius,
basePaddingAngle,
baseArcLabelRadius,
data,
faded,
highlighted,
isItemFaded,
isItemHighlighted,
isItemFocused,
seriesId,
],
[data, seriesId, isItemHighlighted, isItemFaded, isItemFocused, series, faded, highlighted],
);

return dataWithHighlight;
Expand Down
1 change: 1 addition & 0 deletions scripts/buildApiDocs/chartsSettings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export default chartsApiPages;
'x-charts/src/BarChart/AnimatedBarElement.tsx',
'x-charts/src/RadarChart/RadarDataProvider/RadarDataProvider.tsx',
'x-charts/src/LineChart/FocusedLineMark.tsx',
'x-charts/src/PieChart/FocusedPieArc.tsx',
Copy link
Member

Choose a reason for hiding this comment

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

Isn't this technically documented in the accessibility docs? Should we add it there instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

This stuff will create a cross link between docs pages and the component API page.

Those components API are empty. They are just here to be display, so I prefer avoiding adding noise

'x-charts/src/ScatterChart/BatchScatter.tsx',
'x-charts-premium/src/ChartsRenderer/ChartsRenderer.tsx',
'x-charts-premium/src/ChartsRenderer/components/PaletteOption.tsx',
Expand Down
Loading