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

Support Custom Rendering For Pie Charts #311

Merged
merged 2 commits into from
Jun 20, 2018
Merged
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
45 changes: 26 additions & 19 deletions packages/vx-shape/src/shapes/Pie.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default function Pie({
pieSort,
pieSortValues,
pieValue,
children,
...restProps
}) {
const path = d3Arc();
Expand All @@ -35,26 +36,32 @@ export default function Pie({
if (startAngle != null) pie.startAngle(startAngle);
if (endAngle != null) pie.endAngle(endAngle);
const arcs = pie(data);
const renderFunctionArg = {
arcs,
generatePathProps: (arc, index) => ({
className: cx('vx-pie-arc', className),
d: path(arc),
...additionalProps(restProps, {
...arc,
index,
centroid: centroid ? path.centroid(arc) : undefined
})
}),
generateCentroid: arc => centroid && centroid(path.centroid(arc), arc)
};
return (
<Group className="vx-pie-arcs-group" top={top} left={left}>
{arcs.map((arc, i) => {
let c;
if (centroid) c = path.centroid(arc);
return (
<g key={`pie-arc-${i}`}>
<path
className={cx('vx-pie-arc', className)}
d={path(arc)}
{...additionalProps(restProps, {
...arc,
index: i,
centroid: c
})}
/>
{centroid && centroid(c, arc)}
</g>
);
})}
<Group className='vx-pie-arcs-group' top={top} left={left}>
{children
? children(renderFunctionArg)
: arcs.map((arc, i) => {
const pathProps = renderFunctionArg.generatePathProps(arc, i);
return (
<g key={`pie-arc-${i}`}>
<path {...pathProps} />
{renderFunctionArg.generateCentroid(arc)}
</g>
);
})}
</Group>
);
}