Skip to content

Commit

Permalink
feat(pie): pass datum to pie legend data
Browse files Browse the repository at this point in the history
  • Loading branch information
plouc committed Nov 4, 2020
1 parent ea8332e commit 5292831
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
5 changes: 3 additions & 2 deletions packages/pie/src/Pie.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import PieRadialLabels from './PieRadialLabels'
import PieSlicesLabels from './PieSlicesLabels'
import PieLayout from './PieLayout'
import PieLegends from './PieLegends'
import { usePie } from './hooks'

export default function Pie(props) {
const {
Expand Down Expand Up @@ -66,7 +67,7 @@ export default function Pie(props) {
slicesLabelsTextColor,

// styling
theme: _theme,
theme: partialTheme,
defs,
fill,

Expand All @@ -82,7 +83,7 @@ export default function Pie(props) {
role,
} = props

const theme = usePartialTheme(_theme)
const theme = usePartialTheme(partialTheme)

const { outerWidth, outerHeight, margin, innerWidth, innerHeight } = useDimensions(
_width,
Expand Down
17 changes: 11 additions & 6 deletions packages/pie/src/PieLegends.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@ export default function PieLegends(props) {

const data = React.useMemo(
() =>
arcs.map(({ color, data: { id, label }, fill }) => ({
id: id,
label: label || id,
color,
fill,
})),
arcs.map(({ color, data, fill }) => {
const { id, label } = data

return {
id: id,
label: label || id,
color,
fill,
data,
}
}),
[arcs]
)

Expand Down
30 changes: 23 additions & 7 deletions website/src/pages/pie/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,34 @@ const Pie = () => {
generateData={generateData}
>
{(properties, data, theme, logAction) => {
const handleArcClick = (slice) => {
logAction({
type: 'click',
label: `[arc] ${slice.label}: ${slice.value}`,
color: slice.color,
data: slice,
})
}

const handleLegendClick = (legendItem) => {
logAction({
type: 'click',
label: `[legend] ${legendItem.label}: ${legendItem.data.value}`,
color: legendItem.color,
data: legendItem,
})
}

return (
<ResponsivePie
data={data}
{...properties}
theme={theme}
onClick={slice => {
logAction({
type: 'click',
label: `[arc] ${slice.label}: ${slice.value}`,
data: slice,
})
}}
onClick={handleArcClick}
legends={properties.legends.map(legend => ({
...legend,
onClick: handleLegendClick
}))}
/>
)
}}
Expand Down

0 comments on commit 5292831

Please sign in to comment.