Skip to content

Commit d4714b6

Browse files
authored
fix(pie): address some issues with data label prop (#967)
- Fix proptype stating label is required for PieSlice - Fix PieLegend to use label if available, otherwise id - Add knob to storybook to display legend
1 parent a293a64 commit d4714b6

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

packages/pie/src/PieLegends.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ class PieLegends extends Component {
4545
export const enhance = Component =>
4646
compose(
4747
withPropsOnChange(['arcs'], ({ arcs }) => ({
48-
data: arcs.map(arc => ({
49-
id: arc.data.id,
50-
label: arc.data.id,
51-
color: arc.color,
52-
fill: arc.fill,
48+
data: arcs.map(({ color, data: { id, label }, fill }) => ({
49+
id: id,
50+
label: label || id,
51+
color,
52+
fill,
5353
})),
5454
})),
5555
pure

packages/pie/src/PieSlice.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const PieSlice = ({
3535
const handleTooltip = e =>
3636
showTooltip(
3737
<BasicTooltip
38-
id={data.label}
38+
id={data.label || data.id}
3939
value={data.value}
4040
enableChip={true}
4141
color={color}
@@ -74,7 +74,7 @@ const PieSlice = ({
7474
PieSlice.propTypes = {
7575
data: PropTypes.shape({
7676
id: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
77-
label: PropTypes.string.isRequired,
77+
label: PropTypes.string,
7878
value: PropTypes.number.isRequired,
7979
}).isRequired,
8080

packages/pie/stories/pie.stories.js

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,48 @@
11
import React from 'react'
22
import { storiesOf } from '@storybook/react'
3-
import { withKnobs } from '@storybook/addon-knobs'
3+
import { withKnobs, boolean } from '@storybook/addon-knobs'
44
import { generateProgrammingLanguageStats } from '@nivo/generators'
55
import { Pie } from '../src'
66

77
const commonProperties = {
88
width: 900,
99
height: 500,
1010
margin: { top: 80, right: 120, bottom: 80, left: 120 },
11-
data: generateProgrammingLanguageStats(true, 9).map(d => ({
12-
id: d.label,
11+
data: generateProgrammingLanguageStats(true, 9).map(({ label, ...d }) => ({
12+
id: label,
1313
...d,
1414
})),
1515
animate: true,
1616
}
1717

18+
const legends = [
19+
{
20+
anchor: 'bottom',
21+
direction: 'row',
22+
translateY: 56,
23+
itemWidth: 100,
24+
itemHeight: 18,
25+
itemTextColor: '#999',
26+
symbolSize: 18,
27+
symbolShape: 'circle',
28+
effects: [
29+
{
30+
on: 'hover',
31+
style: {
32+
itemTextColor: '#000',
33+
},
34+
},
35+
],
36+
},
37+
]
38+
1839
const stories = storiesOf('Pie', module)
1940

2041
stories.addDecorator(withKnobs)
2142

22-
stories.add('default', () => <Pie {...commonProperties} />)
43+
stories.add('default', () => (
44+
<Pie {...commonProperties} legends={boolean('legends', false) ? legends : []} />
45+
))
2346

2447
stories.add('donut', () => <Pie {...commonProperties} innerRadius={0.6} />)
2548

0 commit comments

Comments
 (0)