Skip to content

Commit

Permalink
fix(pie): address some issues with data label prop (#967)
Browse files Browse the repository at this point in the history
- Fix proptype stating label is required for PieSlice
- Fix PieLegend to use label if available, otherwise id
- Add knob to storybook to display legend
  • Loading branch information
wyze committed May 22, 2020
1 parent a293a64 commit d4714b6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
10 changes: 5 additions & 5 deletions packages/pie/src/PieLegends.js
Expand Up @@ -45,11 +45,11 @@ class PieLegends extends Component {
export const enhance = Component =>
compose(
withPropsOnChange(['arcs'], ({ arcs }) => ({
data: arcs.map(arc => ({
id: arc.data.id,
label: arc.data.id,
color: arc.color,
fill: arc.fill,
data: arcs.map(({ color, data: { id, label }, fill }) => ({
id: id,
label: label || id,
color,
fill,
})),
})),
pure
Expand Down
4 changes: 2 additions & 2 deletions packages/pie/src/PieSlice.js
Expand Up @@ -35,7 +35,7 @@ const PieSlice = ({
const handleTooltip = e =>
showTooltip(
<BasicTooltip
id={data.label}
id={data.label || data.id}
value={data.value}
enableChip={true}
color={color}
Expand Down Expand Up @@ -74,7 +74,7 @@ const PieSlice = ({
PieSlice.propTypes = {
data: PropTypes.shape({
id: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
label: PropTypes.string.isRequired,
label: PropTypes.string,
value: PropTypes.number.isRequired,
}).isRequired,

Expand Down
31 changes: 27 additions & 4 deletions packages/pie/stories/pie.stories.js
@@ -1,25 +1,48 @@
import React from 'react'
import { storiesOf } from '@storybook/react'
import { withKnobs } from '@storybook/addon-knobs'
import { withKnobs, boolean } from '@storybook/addon-knobs'
import { generateProgrammingLanguageStats } from '@nivo/generators'
import { Pie } from '../src'

const commonProperties = {
width: 900,
height: 500,
margin: { top: 80, right: 120, bottom: 80, left: 120 },
data: generateProgrammingLanguageStats(true, 9).map(d => ({
id: d.label,
data: generateProgrammingLanguageStats(true, 9).map(({ label, ...d }) => ({
id: label,
...d,
})),
animate: true,
}

const legends = [
{
anchor: 'bottom',
direction: 'row',
translateY: 56,
itemWidth: 100,
itemHeight: 18,
itemTextColor: '#999',
symbolSize: 18,
symbolShape: 'circle',
effects: [
{
on: 'hover',
style: {
itemTextColor: '#000',
},
},
],
},
]

const stories = storiesOf('Pie', module)

stories.addDecorator(withKnobs)

stories.add('default', () => <Pie {...commonProperties} />)
stories.add('default', () => (
<Pie {...commonProperties} legends={boolean('legends', false) ? legends : []} />
))

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

Expand Down

0 comments on commit d4714b6

Please sign in to comment.