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

Pie: Add the ability to programmatically control the activeId #2465

Merged
merged 6 commits into from
Nov 20, 2023
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feat(pie): add the ability to programmatically control the activeId f…
…or the canvas implementation
  • Loading branch information
plouc committed Nov 19, 2023
commit 7c1f94736769cd46a1a3603d6c2bf05244d4975a
2 changes: 2 additions & 0 deletions packages/pie/src/Pie.tsx
Original file line number Diff line number Diff line change
@@ -76,6 +76,7 @@ const InnerPie = <RawDatum extends MayHaveLabel>({
tooltip = defaultProps.tooltip,
activeId: activeIdFromProps,
onActiveIdChange,
defaultActiveId,

transitionMode = defaultProps.transitionMode,

@@ -121,6 +122,7 @@ const InnerPie = <RawDatum extends MayHaveLabel>({
activeOuterRadiusOffset,
activeId: activeIdFromProps,
onActiveIdChange,
defaultActiveId,
})

const boundDefs = bindDefs(defs, dataWithArc, fill)
8 changes: 7 additions & 1 deletion packages/pie/src/PieCanvas.tsx
Original file line number Diff line number Diff line change
@@ -35,7 +35,7 @@ const InnerPieCanvas = <RawDatum extends MayHaveLabel>({
width,
height,
margin: partialMargin,
pixelRatio = 1,
pixelRatio = defaultProps.pixelRatio,

colors = defaultProps.colors,

@@ -67,6 +67,9 @@ const InnerPieCanvas = <RawDatum extends MayHaveLabel>({
onClick,
onMouseMove,
tooltip = defaultProps.tooltip,
activeId: activeIdFromProps,
onActiveIdChange,
defaultActiveId,

legends = defaultProps.legends,
}: PieCanvasProps<RawDatum>) => {
@@ -101,6 +104,9 @@ const InnerPieCanvas = <RawDatum extends MayHaveLabel>({
cornerRadius,
activeInnerRadiusOffset,
activeOuterRadiusOffset,
activeId: activeIdFromProps,
onActiveIdChange,
defaultActiveId,
})

const getBorderColor = useInheritedColor<ComputedDatum<RawDatum>>(borderColor, theme)
163 changes: 163 additions & 0 deletions storybook/stories/pie/PieCanvas.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
import { useState } from 'react'
import type { Meta, StoryObj } from '@storybook/react'
import { generateProgrammingLanguageStats } from '@nivo/generators'
import { PieCanvas } from '@nivo/pie'
import { nivoTheme } from '../nivo-theme'

const meta: Meta<typeof PieCanvas> = {
title: 'PieCanvas',
component: PieCanvas,
tags: ['autodocs'],
argTypes: {
legends: {
control: 'boolean',
},
},
args: {
legends: false,
},
}

export default meta
type Story = StoryObj<typeof PieCanvas>

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

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

export const Basic: Story = {
render: args => <PieCanvas {...commonProperties} legends={args.legends ? legends : []} />,
}

export const Donut: Story = {
render: () => <PieCanvas {...commonProperties} innerRadius={0.6} />,
}

/**
* It is possible to use colors coming from the provided dataset instead of using
* a color scheme, to do so, you should pass:
*
* ```
* colors={{ datum: 'data.color' }}
* ```
*
* given that each data point you pass has a `color` property.
*
* It's also possible to pass a function if you want to handle more advanced computation:
*
* ```
* colors={(datum) => datum.color}
* ```
*/
export const UsingColorsFromData: Story = {
render: () => <PieCanvas {...commonProperties} colors={{ datum: 'data.color' }} />,
}

export const FormattedValues: Story = {
render: () => (
<PieCanvas
{...commonProperties}
arcLabelsRadiusOffset={0.7}
valueFormat={value =>
`${Number(value).toLocaleString('ru-RU', {
minimumFractionDigits: 2,
})} ₽`
}
/>
),
}

export const CustomTooltip: Story = {
render: () => (
<PieCanvas
{...commonProperties}
tooltip={({ datum: { id, value, color } }) => (
<div
style={{
padding: 12,
color,
background: '#222222',
}}
>
<span>Look, I'm custom :)</span>
<br />
<strong>
{id}: {value}
</strong>
</div>
)}
theme={{
tooltip: {
container: {
background: '#333',
},
},
}}
/>
),
}

const controlledPieProps = {
...commonProperties,
width: 400,
height: 400,
margin: { top: 60, right: 80, bottom: 60, left: 80 },
innerRadius: 0.4,
padAngle: 0.3,
cornerRadius: 3,
activeOuterRadiusOffset: 12,
activeInnerRadiusOffset: 12,
arcLinkLabelsDiagonalLength: 10,
arcLinkLabelsStraightLength: 10,
}

const ControlledPies = () => {
const [activeId, setActiveId] = useState<string>(commonProperties.data[1].id)

return (
<div
style={{
width: '800px',
display: 'grid',
gridTemplateColumns: 'repeat(2, 1fr)',
}}
>
<PieCanvas {...controlledPieProps} activeId={activeId} onActiveIdChange={setActiveId} />
<PieCanvas {...controlledPieProps} activeId={activeId} onActiveIdChange={setActiveId} />
</div>
)
}

export const ControlledActiveId: Story = {
render: () => <ControlledPies />,
}
6 changes: 5 additions & 1 deletion website/src/data/components/pie/meta.yml
Original file line number Diff line number Diff line change
@@ -41,7 +41,11 @@ PieCanvas:
tags:
- radial
- canvas
stories: []
stories:
- label: Using colors from data
link: piecanvas--using-colors-from-data
- label: Sync activeId between two pies
link: piecanvas--controlled-active-id
description: |
A variation around the [Pie](self:/pie) component. Well suited for
large data sets as it does not impact DOM tree depth, however you'll
6 changes: 3 additions & 3 deletions website/src/data/components/pie/props.ts
Original file line number Diff line number Diff line change
@@ -510,7 +510,7 @@ const props: ChartProperty[] = [
},
{
key: 'activeId',
flavors: ['svg'],
flavors: ['svg', 'canvas'],
help: `Programmatically control the \`activeId\`.`,
description: `
This property should be used with \`onActiveIdChange\`,
@@ -531,7 +531,7 @@ const props: ChartProperty[] = [
},
{
key: 'onActiveIdChange',
flavors: ['svg'],
flavors: ['svg', 'canvas'],
help: `Programmatically control the \`activeId\`.`,
description: `
This property should be used with \`activeId\`,
@@ -552,7 +552,7 @@ const props: ChartProperty[] = [
},
{
key: 'defaultActiveId',
flavors: ['svg'],
flavors: ['svg', 'canvas'],
help: `Default \`activeId\`.`,
description: `
You can use this property in case you want to define a default \`activeId\`,
Loading