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
40 changes: 40 additions & 0 deletions docs/data/charts/funnel/FunnelDataAttributes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import * as React from 'react';
import Box from '@mui/material/Box';
import {
Unstable_FunnelChart as FunnelChart,
funnelSectionClasses,
} from '@mui/x-charts-pro/FunnelChart';

export default function FunnelDataAttributes() {
return (
<Box sx={{ width: '100%', maxWidth: 400 }}>
<FunnelChart
{...funnelProps}
sx={{
[`[data-series="main"] .${funnelSectionClasses.root}:nth-of-type(1)`]: {
fill: 'url(#pattern)',
},
}}
>
<defs>
<pattern id="pattern" width="50" height="50" patternUnits="userSpaceOnUse">
<rect width="100%" height="100%" fill="hotpink" />
<circle fill="slateblue" cx="15" cy="15" r="10" />
<circle fill="slateblue" cx="40" cy="40" r="10" />
</pattern>
</defs>
</FunnelChart>
</Box>
);
}

const funnelProps = {
height: 300,
hideLegend: true,
series: [
{
id: 'main',
data: [{ value: 200 }, { value: 180 }, { value: 90 }, { value: 50 }],
},
],
};
40 changes: 40 additions & 0 deletions docs/data/charts/funnel/FunnelDataAttributes.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import * as React from 'react';
import Box from '@mui/material/Box';
import {
Unstable_FunnelChart as FunnelChart,
funnelSectionClasses,
} from '@mui/x-charts-pro/FunnelChart';

export default function FunnelDataAttributes() {
return (
<Box sx={{ width: '100%', maxWidth: 400 }}>
<FunnelChart
{...funnelProps}
sx={{
[`[data-series="main"] .${funnelSectionClasses.root}:nth-of-type(1)`]: {
fill: 'url(#pattern)',
},
}}
>
<defs>
<pattern id="pattern" width="50" height="50" patternUnits="userSpaceOnUse">
<rect width="100%" height="100%" fill="hotpink" />
<circle fill="slateblue" cx="15" cy="15" r="10" />
<circle fill="slateblue" cx="40" cy="40" r="10" />
</pattern>
</defs>
</FunnelChart>
</Box>
);
}

const funnelProps = {
height: 300,
hideLegend: true,
series: [
{
id: 'main',
data: [{ value: 200 }, { value: 180 }, { value: 90 }, { value: 50 }],
},
],
} as const;
16 changes: 16 additions & 0 deletions docs/data/charts/funnel/FunnelDataAttributes.tsx.preview
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<FunnelChart
{...funnelProps}
sx={{
[`[data-series="main"] .${funnelSectionClasses.root}:nth-of-type(1)`]: {
fill: 'url(#pattern)',
},
}}
>
<defs>
<pattern id="pattern" width="50" height="50" patternUnits="userSpaceOnUse">
<rect width="100%" height="100%" fill="hotpink" />
<circle fill="slateblue" cx="15" cy="15" r="10" />
<circle fill="slateblue" cx="40" cy="40" r="10" />
</pattern>
</defs>
</FunnelChart>
10 changes: 10 additions & 0 deletions docs/data/charts/funnel/funnel.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,16 @@ The funnel colors can be customized in two ways.

{{"demo": "FunnelColor.js"}}

### CSS

The funnel chart can be styled using CSS.

Each section group has a `data-series` attribute that can be used to target specific series sections.

In order to target specific sections, you can use the `:nth-child` or `:nth-child-of-type` selectors as shown in the example below.

{{"demo": "FunnelDataAttributes.js"}}

## Multiple funnels

By default, multiple series are displayed on top of each other.
Expand Down
74 changes: 47 additions & 27 deletions packages/x-charts-pro/src/FunnelChart/FunnelPlot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ const useAggregatedData = () => {
});
});

return result.flat();
return result;
}, [seriesData, xAxis, xAxisIds, yAxis, yAxisIds, gap]);

return allData;
Expand All @@ -222,36 +222,56 @@ function FunnelPlot(props: FunnelPlotProps) {

return (
<React.Fragment>
{data.map(({ d, color, id, seriesId, dataIndex, variant }) => (
<FunnelSection
{...other}
d={d}
color={color}
key={id}
dataIndex={dataIndex}
seriesId={seriesId}
variant={variant}
onClick={
onItemClick &&
((event) => {
onItemClick(event, { type: 'funnel', seriesId, dataIndex });
})
}
/>
))}
{data.map(({ id, label, seriesId, dataIndex }) => {
if (!label || !label.value) {
{data.map((series) => {
if (series.length === 0) {
return null;
}

return (
<FunnelSectionLabel
key={id}
label={label}
dataIndex={dataIndex}
seriesId={seriesId}
{...other}
/>
<g data-series={series[0].seriesId} key={series[0].seriesId}>
{series.map(({ d, color, id, seriesId, dataIndex, variant }) => (
<FunnelSection
{...other}
d={d}
color={color}
key={id}
dataIndex={dataIndex}
seriesId={seriesId}
variant={variant}
onClick={
onItemClick &&
((event) => {
onItemClick(event, { type: 'funnel', seriesId, dataIndex });
})
}
/>
))}
</g>
);
})}
{data.map((series) => {
if (series.length === 0) {
return null;
}

return (
<g data-series={series[0].seriesId} key={series[0].seriesId}>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Won't this break if series is empty?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

yep 😅

{series.map(({ id, label, seriesId, dataIndex }) => {
if (!label || !label.value) {
return null;
}

return (
<FunnelSectionLabel
key={id}
label={label}
dataIndex={dataIndex}
seriesId={seriesId}
{...other}
/>
);
})}
</g>
);
})}
</React.Fragment>
Expand Down
2 changes: 2 additions & 0 deletions packages/x-charts-pro/src/FunnelChart/FunnelSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ const FunnelSection = consumeSlots(
strokeWidth={isOutlined ? 1.5 : 0}
cursor={onClick ? 'pointer' : 'unset'}
onClick={onClick}
data-highlighted={isHighlighted || undefined}
data-faded={isFaded || undefined}
className={clsx(
classes?.root,
isHighlighted && classes?.highlighted,
Expand Down