diff --git a/packages/pie/src/Pie.tsx b/packages/pie/src/Pie.tsx index a6189c455..2f6eb159a 100644 --- a/packages/pie/src/Pie.tsx +++ b/packages/pie/src/Pie.tsx @@ -8,7 +8,7 @@ import { } from '@nivo/core' import { ArcLabelsLayer, ArcLinkLabelsLayer } from '@nivo/arcs' import { InheritedColorConfig } from '@nivo/colors' -import PieLegends from './PieLegends' +import { PieLegends } from './PieLegends' import { useNormalizedData, usePieFromBox, usePieLayerContext } from './hooks' import { ComputedDatum, PieLayer, PieSvgProps, PieLayerId, MayHaveLabel } from './types' import { defaultProps } from './props' @@ -74,10 +74,15 @@ const InnerPie = ({ onMouseMove, onMouseLeave, tooltip = defaultProps.tooltip, + activeId: activeIdFromProps, + onActiveIdChange, + defaultActiveId, transitionMode = defaultProps.transitionMode, legends = defaultProps.legends, + forwardLegendData, + role = defaultProps.role, }: PieSvgProps) => { const { outerWidth, outerHeight, margin, innerWidth, innerHeight } = useDimensions( @@ -117,6 +122,10 @@ const InnerPie = ({ cornerRadius, activeInnerRadiusOffset, activeOuterRadiusOffset, + activeId: activeIdFromProps, + onActiveIdChange, + defaultActiveId, + forwardLegendData, }) const boundDefs = bindDefs(defs, dataWithArc, fill) diff --git a/packages/pie/src/PieCanvas.tsx b/packages/pie/src/PieCanvas.tsx index a270588e1..2d59b4b22 100644 --- a/packages/pie/src/PieCanvas.tsx +++ b/packages/pie/src/PieCanvas.tsx @@ -35,7 +35,7 @@ const InnerPieCanvas = ({ width, height, margin: partialMargin, - pixelRatio = 1, + pixelRatio = defaultProps.pixelRatio, colors = defaultProps.colors, @@ -67,8 +67,12 @@ const InnerPieCanvas = ({ onClick, onMouseMove, tooltip = defaultProps.tooltip, + activeId: activeIdFromProps, + onActiveIdChange, + defaultActiveId, legends = defaultProps.legends, + forwardLegendData, }: PieCanvasProps) => { const canvasEl = useRef(null) const theme = useTheme() @@ -101,6 +105,10 @@ const InnerPieCanvas = ({ cornerRadius, activeInnerRadiusOffset, activeOuterRadiusOffset, + activeId: activeIdFromProps, + onActiveIdChange, + defaultActiveId, + forwardLegendData, }) const getBorderColor = useInheritedColor>(borderColor, theme) @@ -199,6 +207,7 @@ const InnerPieCanvas = ({ centerY, arcGenerator, dataWithArc, + borderWidth, getBorderColor, enableArcLabels, arcLabels, diff --git a/packages/pie/src/PieLegends.tsx b/packages/pie/src/PieLegends.tsx index 481ad9823..f461dffdc 100644 --- a/packages/pie/src/PieLegends.tsx +++ b/packages/pie/src/PieLegends.tsx @@ -1,15 +1,15 @@ import { BoxLegendSvg } from '@nivo/legends' -import { CompletePieSvgProps, ComputedDatum, DatumId } from './types' +import { CompletePieSvgProps, DatumId, LegendDatum } from './types' interface PieLegendsProps { width: number height: number legends: CompletePieSvgProps['legends'] - data: Omit, 'arc'>[] + data: LegendDatum[] toggleSerie: (id: DatumId) => void } -const PieLegends = ({ +export const PieLegends = ({ width, height, legends, @@ -31,5 +31,3 @@ const PieLegends = ({ ) } - -export default PieLegends diff --git a/packages/pie/src/hooks.ts b/packages/pie/src/hooks.ts index 83be94273..5fdb669cd 100644 --- a/packages/pie/src/hooks.ts +++ b/packages/pie/src/hooks.ts @@ -1,4 +1,4 @@ -import { useCallback, useMemo, useState } from 'react' +import { useCallback, useEffect, useMemo, useRef, useState } from 'react' import { pie as d3Pie } from 'd3-shape' import { useArcGenerator, computeArcBoundingBox } from '@nivo/arcs' import { @@ -16,6 +16,8 @@ import { DatumId, PieArc, PieCustomLayerProps, + LegendDatum, + CommonPieProps, } from './types' /** @@ -81,6 +83,7 @@ export const usePieArcs = ({ activeInnerRadiusOffset, activeOuterRadiusOffset, hiddenIds, + forwardLegendData, }: { data: Omit, 'arc' | 'fill'>[] // in degrees @@ -97,9 +100,10 @@ export const usePieArcs = ({ activeInnerRadiusOffset: number activeOuterRadiusOffset: number hiddenIds: DatumId[] + forwardLegendData?: CommonPieProps['forwardLegendData'] }): { dataWithArc: Omit, 'fill'>[] - legendData: Omit, 'arc' | 'fill'>[] + legendData: LegendDatum[] } => { const pie = useMemo(() => { const innerPie = d3Pie, 'arc' | 'fill'>>() @@ -115,7 +119,7 @@ export const usePieArcs = ({ return innerPie }, [startAngle, endAngle, padAngle, sortByValue]) - return useMemo(() => { + const result = useMemo(() => { const hiddenData = data.filter(item => !hiddenIds.includes(item.id)) const dataWithArc = pie(hiddenData).map( ( @@ -150,7 +154,13 @@ export const usePieArcs = ({ } } ) - const legendData = data.map(item => ({ ...item, hidden: hiddenIds.includes(item.id) })) + const legendData: LegendDatum[] = data.map(item => ({ + id: item.id, + label: item.label, + color: item.color, + hidden: hiddenIds.includes(item.id), + data: item, + })) return { dataWithArc, legendData } }, [ @@ -163,6 +173,55 @@ export const usePieArcs = ({ outerRadius, activeOuterRadiusOffset, ]) + + // Forward the legends data if `forwardLegendData` is defined. + const legendData = result.legendData + const forwardLegendDataRef = useRef(forwardLegendData) + useEffect(() => { + if (typeof forwardLegendDataRef.current !== 'function') return + forwardLegendDataRef.current(legendData) + }, [forwardLegendDataRef, legendData]) + + return result +} + +/** + * Encapsulate the logic for defining/reading the active arc ID, + * which can be either controlled (handled externally), or uncontrolled + * (handled internally), we can optionally define a default value when + * it's uncontrolled. + */ +const useActiveId = ({ + activeId: activeIdFromProps, + onActiveIdChange, + defaultActiveId = null, +}: { + activeId?: DatumId | null + onActiveIdChange?: (id: DatumId | null) => void + defaultActiveId?: DatumId | null +}) => { + const isControlled = typeof activeIdFromProps != 'undefined' + + const [internalActiveId, setInternalActiveId] = useState( + !isControlled ? defaultActiveId : null + ) + + const activeId = isControlled ? activeIdFromProps : internalActiveId + + const setActiveId = useCallback( + (id: DatumId | null) => { + if (onActiveIdChange) { + onActiveIdChange(id) + } + + if (!isControlled) { + setInternalActiveId(id) + } + }, + [isControlled, onActiveIdChange, setInternalActiveId] + ) + + return { activeId, setActiveId } } /** @@ -180,6 +239,10 @@ export const usePie = ({ cornerRadius = defaultProps.cornerRadius, activeInnerRadiusOffset = defaultProps.activeInnerRadiusOffset, activeOuterRadiusOffset = defaultProps.activeOuterRadiusOffset, + activeId: activeIdFromProps, + onActiveIdChange, + defaultActiveId, + forwardLegendData, }: Pick< Partial>, | 'startAngle' @@ -189,12 +252,21 @@ export const usePie = ({ | 'cornerRadius' | 'activeInnerRadiusOffset' | 'activeOuterRadiusOffset' + | 'activeId' + | 'onActiveIdChange' + | 'defaultActiveId' + | 'forwardLegendData' > & { data: Omit, 'arc'>[] radius: number innerRadius: number }) => { - const [activeId, setActiveId] = useState(null) + const { activeId, setActiveId } = useActiveId({ + activeId: activeIdFromProps, + onActiveIdChange, + defaultActiveId, + }) + const [hiddenIds, setHiddenIds] = useState([]) const pieArcs = usePieArcs({ data, @@ -208,6 +280,7 @@ export const usePie = ({ activeInnerRadiusOffset, activeOuterRadiusOffset, hiddenIds, + forwardLegendData, }) const toggleSerie = useCallback((id: DatumId) => { @@ -242,6 +315,10 @@ export const usePieFromBox = ({ fit = defaultProps.fit, activeInnerRadiusOffset = defaultProps.activeInnerRadiusOffset, activeOuterRadiusOffset = defaultProps.activeOuterRadiusOffset, + activeId: activeIdFromProps, + onActiveIdChange, + defaultActiveId, + forwardLegendData, }: Pick< CompletePieSvgProps, | 'width' @@ -255,10 +332,19 @@ export const usePieFromBox = ({ | 'fit' | 'activeInnerRadiusOffset' | 'activeOuterRadiusOffset' -> & { - data: Omit, 'arc'>[] -}) => { - const [activeId, setActiveId] = useState(null) +> & + Pick< + Partial>, + 'activeId' | 'onActiveIdChange' | 'defaultActiveId' | 'forwardLegendData' + > & { + data: Omit, 'arc'>[] + }) => { + const { activeId, setActiveId } = useActiveId({ + activeId: activeIdFromProps, + onActiveIdChange, + defaultActiveId, + }) + const [hiddenIds, setHiddenIds] = useState([]) const computedProps = useMemo(() => { let radius = Math.min(width, height) / 2 @@ -306,7 +392,7 @@ export const usePieFromBox = ({ innerRadius, debug: boundingBox, } - }, [width, height, innerRadiusRatio, startAngle, endAngle, fit, cornerRadius]) + }, [width, height, innerRadiusRatio, startAngle, endAngle, fit]) const pieArcs = usePieArcs({ data, @@ -320,6 +406,7 @@ export const usePieFromBox = ({ activeInnerRadiusOffset, activeOuterRadiusOffset, hiddenIds, + forwardLegendData, }) const toggleSerie = useCallback((id: DatumId) => { @@ -335,6 +422,7 @@ export const usePieFromBox = ({ return { arcGenerator, + activeId, setActiveId, toggleSerie, ...pieArcs, diff --git a/packages/pie/src/types.ts b/packages/pie/src/types.ts index 66f926e8b..6e0239a21 100644 --- a/packages/pie/src/types.ts +++ b/packages/pie/src/types.ts @@ -28,7 +28,7 @@ export interface DefaultRawDatum { value: number } -export interface MayHaveLabel { +export interface MayHaveLabel extends Object { label?: string | number } @@ -113,8 +113,12 @@ export type CommonPieProps = { // interactivity isInteractive: boolean tooltip: React.FC> + activeId: DatumId | null + onActiveIdChange: (id: DatumId | null) => void + defaultActiveId: DatumId | null legends: readonly LegendProps[] + forwardLegendData: (data: LegendDatum[]) => void role: string renderWrapper: boolean @@ -132,6 +136,14 @@ export type PieSvgCustomComponents = { arcLinkLabelComponent?: ArcLinkLabelsProps>['component'] } +export interface LegendDatum { + id: ComputedDatum['id'] + label: ComputedDatum['label'] + color: string + hidden: boolean + data: Omit, 'fill' | 'arc'> +} + export type PieSvgProps = DataProps & Dimensions & Partial> & diff --git a/packages/pie/tests/Pie.test.tsx b/packages/pie/tests/Pie.test.tsx index c3278a8b2..ba3213485 100644 --- a/packages/pie/tests/Pie.test.tsx +++ b/packages/pie/tests/Pie.test.tsx @@ -1,4 +1,5 @@ -import { mount } from 'enzyme' +import { create, act } from 'react-test-renderer' +import { Globals } from '@react-spring/web' import { linearGradientDef, radiansToDegrees } from '@nivo/core' import { ArcShape, @@ -11,7 +12,13 @@ import { LegendSvgItem, SymbolSquare } from '@nivo/legends' // @ts-ignore import { Pie } from '../src/index' -const sampleData = [ +interface SampleDatum { + id: string + value: number + color: string +} + +const sampleData: readonly SampleDatum[] = [ { id: 'A', value: 30, @@ -27,7 +34,7 @@ const sampleData = [ value: 50, color: '#99cc44', }, -] +] as const const sampleGradientData = [ linearGradientDef('gradientA', [ @@ -79,176 +86,174 @@ const sampleDataWithCustomProps = sampleData.map(datum => ({ })) describe('Pie', () => { + beforeAll(() => { + Globals.assign({ skipAnimation: true }) + }) + + afterAll(() => { + Globals.assign({ skipAnimation: false }) + }) + describe('data', () => { it('should use default id and value properties', () => { - const wrapper = mount( - - ) + const instance = create().root - const arcs = wrapper.find(ArcShape) + const arcs = instance.findAllByType(ArcShape) expect(arcs).toHaveLength(sampleData.length) - expect(arcs.at(0).prop('datum').id).toEqual('A') - expect(arcs.at(0).prop('datum').value).toEqual(30) - expect(arcs.at(0).prop('datum').formattedValue).toEqual('30') + expect(arcs[0].props.datum.id).toEqual('A') + expect(arcs[0].props.datum.value).toEqual(30) + expect(arcs[0].props.datum.formattedValue).toEqual('30') - expect(arcs.at(1).prop('datum').id).toEqual('B') - expect(arcs.at(1).prop('datum').value).toEqual(20) - expect(arcs.at(1).prop('datum').formattedValue).toEqual('20') + expect(arcs[1].props.datum.id).toEqual('B') + expect(arcs[1].props.datum.value).toEqual(20) + expect(arcs[1].props.datum.formattedValue).toEqual('20') - expect(arcs.at(2).prop('datum').id).toEqual('C') - expect(arcs.at(2).prop('datum').value).toEqual(50) - expect(arcs.at(2).prop('datum').formattedValue).toEqual('50') + expect(arcs[2].props.datum.id).toEqual('C') + expect(arcs[2].props.datum.value).toEqual(50) + expect(arcs[2].props.datum.formattedValue).toEqual('50') }) it('should use custom id and value accessors expressed as path', () => { - const wrapper = mount( + const instance = create( - ) + ).root - const arcs = wrapper.find(ArcShape) + const arcs = instance.findAllByType(ArcShape) expect(arcs).toHaveLength(sampleData.length) - expect(arcs.at(0).prop('datum').id).toEqual('A') - expect(arcs.at(0).prop('datum').value).toEqual(30) - expect(arcs.at(0).prop('datum').formattedValue).toEqual('30') + expect(arcs[0].props.datum.id).toEqual('A') + expect(arcs[0].props.datum.value).toEqual(30) + expect(arcs[0].props.datum.formattedValue).toEqual('30') - expect(arcs.at(1).prop('datum').id).toEqual('B') - expect(arcs.at(1).prop('datum').value).toEqual(20) - expect(arcs.at(1).prop('datum').formattedValue).toEqual('20') + expect(arcs[1].props.datum.id).toEqual('B') + expect(arcs[1].props.datum.value).toEqual(20) + expect(arcs[1].props.datum.formattedValue).toEqual('20') - expect(arcs.at(2).prop('datum').id).toEqual('C') - expect(arcs.at(2).prop('datum').value).toEqual(50) - expect(arcs.at(2).prop('datum').formattedValue).toEqual('50') + expect(arcs[2].props.datum.id).toEqual('C') + expect(arcs[2].props.datum.value).toEqual(50) + expect(arcs[2].props.datum.formattedValue).toEqual('50') }) it('should use custom id and value accessors expressed as functions', () => { - const wrapper = mount( + const instance = create( d.name} value={d => d.attributes.volume} - animate={false} /> - ) + ).root - const arcs = wrapper.find(ArcShape) + const arcs = instance.findAllByType(ArcShape) expect(arcs).toHaveLength(sampleData.length) - expect(arcs.at(0).prop('datum').id).toEqual('A') - expect(arcs.at(0).prop('datum').value).toEqual(30) - expect(arcs.at(0).prop('datum').formattedValue).toEqual('30') + expect(arcs[0].props.datum.id).toEqual('A') + expect(arcs[0].props.datum.value).toEqual(30) + expect(arcs[0].props.datum.formattedValue).toEqual('30') - expect(arcs.at(1).prop('datum').id).toEqual('B') - expect(arcs.at(1).prop('datum').value).toEqual(20) - expect(arcs.at(1).prop('datum').formattedValue).toEqual('20') + expect(arcs[1].props.datum.id).toEqual('B') + expect(arcs[1].props.datum.value).toEqual(20) + expect(arcs[1].props.datum.formattedValue).toEqual('20') - expect(arcs.at(2).prop('datum').id).toEqual('C') - expect(arcs.at(2).prop('datum').value).toEqual(50) - expect(arcs.at(2).prop('datum').formattedValue).toEqual('50') + expect(arcs[2].props.datum.id).toEqual('C') + expect(arcs[2].props.datum.value).toEqual(50) + expect(arcs[2].props.datum.formattedValue).toEqual('50') }) it('should support custom value formatting', () => { - const wrapper = mount( - - ) + const instance = create( + + ).root - const arcs = wrapper.find(ArcShape) + const arcs = instance.findAllByType(ArcShape) expect(arcs).toHaveLength(sampleData.length) - expect(arcs.at(0).prop('datum').id).toEqual('A') - expect(arcs.at(0).prop('datum').value).toEqual(30) - expect(arcs.at(0).prop('datum').formattedValue).toEqual('$30.00') + expect(arcs[0].props.datum.id).toEqual('A') + expect(arcs[0].props.datum.value).toEqual(30) + expect(arcs[0].props.datum.formattedValue).toEqual('$30.00') - expect(arcs.at(1).prop('datum').id).toEqual('B') - expect(arcs.at(1).prop('datum').value).toEqual(20) - expect(arcs.at(1).prop('datum').formattedValue).toEqual('$20.00') + expect(arcs[1].props.datum.id).toEqual('B') + expect(arcs[1].props.datum.value).toEqual(20) + expect(arcs[1].props.datum.formattedValue).toEqual('$20.00') - expect(arcs.at(2).prop('datum').id).toEqual('C') - expect(arcs.at(2).prop('datum').value).toEqual(50) - expect(arcs.at(2).prop('datum').formattedValue).toEqual('$50.00') + expect(arcs[2].props.datum.id).toEqual('C') + expect(arcs[2].props.datum.value).toEqual(50) + expect(arcs[2].props.datum.formattedValue).toEqual('$50.00') }) it('should support sorting data by value', () => { - const wrapper = mount( - - ) + const instance = create( + + ).root - const arcs = wrapper.find(ArcShape) + const arcs = instance.findAllByType(ArcShape) expect(arcs).toHaveLength(sampleData.length) - const arc30 = arcs.at(0) - const arc20 = arcs.at(1) - const arc50 = arcs.at(2) + const arc30 = arcs[0] + const arc20 = arcs[1] + const arc50 = arcs[2] - expect(arc50.prop('datum').arc.startAngle).toEqual(0) - expect(arc30.prop('datum').arc.startAngle).toBeGreaterThan(0) - expect(arc20.prop('datum').arc.startAngle).toBeGreaterThan( - arc30.prop('datum').arc.startAngle + expect(arc50.props.datum.arc.startAngle).toEqual(0) + expect(arc30.props.datum.arc.startAngle).toBeGreaterThan(0) + expect(arc20.props.datum.arc.startAngle).toBeGreaterThan( + arc30.props.datum.arc.startAngle ) }) }) describe('layout', () => { it('should support donut charts', () => { - const wrapper = mount( - - ) + const instance = create( + + ).root + + const arcs = instance.findAllByType(ArcShape) + expect(arcs).toHaveLength(sampleData.length) // we can use a slice to check computed radii - const arc = wrapper.find(ArcShape).at(0) - expect(arc.prop('datum').arc.innerRadius).toEqual(100) - expect(arc.prop('datum').arc.outerRadius).toEqual(200) + expect(arcs[0].props.datum.arc.innerRadius).toEqual(100) + expect(arcs[0].props.datum.arc.outerRadius).toEqual(200) }) it('should support padAngle', () => { - const wrapper = mount( - - ) + const instance = create( + + ).root - const arcs = wrapper.find(ArcShape) + const arcs = instance.findAllByType(ArcShape) expect(arcs).toHaveLength(sampleData.length) arcs.forEach(arc => { - expect(radiansToDegrees(arc.prop('datum').arc.padAngle)).toEqual(10) + expect(radiansToDegrees(arc.props.datum.arc.padAngle)).toEqual(10) }) }) it('should support cornerRadius', () => { // using a custom layer to inspect the `arcGenerator` const CustomLayer = () => null - const wrapper = mount( + const instance = create( - ) + ).root - const layer = wrapper.find(CustomLayer) - expect(layer.exists()).toBeTruthy() - expect(layer.prop('arcGenerator').cornerRadius()()).toEqual(3) + const layer = instance.findByType(CustomLayer) + expect(layer.props.arcGenerator.cornerRadius()()).toEqual(3) }) it('should support custom start and end angles', () => { - const wrapper = mount( + const instance = create( { innerRadius={0.5} startAngle={90} endAngle={180} - animate={false} /> - ) + ).root - const arcs = wrapper.find(ArcShape) + const arcs = instance.findAllByType(ArcShape) expect(arcs).toHaveLength(sampleData.length) - expect(radiansToDegrees(arcs.at(0).prop('datum').arc.startAngle)).toEqual(90) - expect(radiansToDegrees(arcs.at(2).prop('datum').arc.endAngle)).toEqual(180) + + expect(radiansToDegrees(arcs[0].props.datum.arc.startAngle)).toEqual(90) + expect(radiansToDegrees(arcs[2].props.datum.arc.endAngle)).toEqual(180) }) it('should support optimizing space usage via the fit property', () => { - const wrapper = mount( + const instance = create( { startAngle={-90} endAngle={90} fit - animate={false} /> - ) + ).root + + const arcs = instance.findAllByType(ArcShape) + expect(arcs).toHaveLength(sampleData.length) // we can use a slice to check computed radii - const arc = wrapper.find(ArcShape).at(0) - expect(arc.prop('datum').arc.innerRadius).toEqual(200) - expect(arc.prop('datum').arc.outerRadius).toEqual(400) + expect(arcs[0].props.datum.arc.innerRadius).toEqual(200) + expect(arcs[0].props.datum.arc.outerRadius).toEqual(400) }) }) describe('colors', () => { it('should use colors from scheme', () => { - const wrapper = mount( - - ) + const instance = create( + + ).root - const arcs = wrapper.find(ArcShape) + const arcs = instance.findAllByType(ArcShape) expect(arcs).toHaveLength(sampleData.length) - expect(arcs.at(0).prop('datum').id).toEqual('A') - expect(arcs.at(0).prop('datum').color).toEqual('#7fc97f') + expect(arcs[0].props.datum.id).toEqual('A') + expect(arcs[0].props.datum.color).toEqual('#7fc97f') - expect(arcs.at(1).prop('datum').id).toEqual('B') - expect(arcs.at(1).prop('datum').color).toEqual('#beaed4') + expect(arcs[1].props.datum.id).toEqual('B') + expect(arcs[1].props.datum.color).toEqual('#beaed4') - expect(arcs.at(2).prop('datum').id).toEqual('C') - expect(arcs.at(2).prop('datum').color).toEqual('#fdc086') + expect(arcs[2].props.datum.id).toEqual('C') + expect(arcs[2].props.datum.color).toEqual('#fdc086') }) it('should allow to use colors from data using a path', () => { - const wrapper = mount( + const instance = create( - ) + ).root - const arcs = wrapper.find(ArcShape) + const arcs = instance.findAllByType(ArcShape) expect(arcs).toHaveLength(sampleData.length) - expect(arcs.at(0).prop('datum').id).toEqual('A') - expect(arcs.at(0).prop('datum').color).toEqual('#ff5500') + expect(arcs[0].props.datum.id).toEqual('A') + expect(arcs[0].props.datum.color).toEqual('#ff5500') - expect(arcs.at(1).prop('datum').id).toEqual('B') - expect(arcs.at(1).prop('datum').color).toEqual('#ffdd00') + expect(arcs[1].props.datum.id).toEqual('B') + expect(arcs[1].props.datum.color).toEqual('#ffdd00') - expect(arcs.at(2).prop('datum').id).toEqual('C') - expect(arcs.at(2).prop('datum').color).toEqual('#99cc44') + expect(arcs[2].props.datum.id).toEqual('C') + expect(arcs[2].props.datum.color).toEqual('#99cc44') }) it('should allow to use colors from data using a function', () => { - const wrapper = mount( - d.data.color} - animate={false} - /> - ) + const instance = create( + d.data.color} /> + ).root - const slices = wrapper.find(ArcShape) - expect(slices).toHaveLength(sampleData.length) + const arcs = instance.findAllByType(ArcShape) + expect(arcs).toHaveLength(sampleData.length) - expect(slices.at(0).prop('datum').id).toEqual('A') - expect(slices.at(0).prop('datum').color).toEqual('#ff5500') + expect(arcs[0].props.datum.id).toEqual('A') + expect(arcs[0].props.datum.color).toEqual('#ff5500') - expect(slices.at(1).prop('datum').id).toEqual('B') - expect(slices.at(1).prop('datum').color).toEqual('#ffdd00') + expect(arcs[1].props.datum.id).toEqual('B') + expect(arcs[1].props.datum.color).toEqual('#ffdd00') - expect(slices.at(2).prop('datum').id).toEqual('C') - expect(slices.at(2).prop('datum').color).toEqual('#99cc44') + expect(arcs[2].props.datum.id).toEqual('C') + expect(arcs[2].props.datum.color).toEqual('#99cc44') }) }) @@ -367,7 +360,7 @@ describe('Pie', () => { }) it('should support gradients', () => { - const wrapper = mount( + const instance = create( { { match: { id: 'B' }, id: 'gradientB' }, { match: { id: 'C' }, id: 'gradientC' }, ]} - animate={false} /> - ) - const slices = wrapper.find(ArcShape) - expect(slices).toHaveLength(sampleData.length) + ).root + + const arcs = instance.findAllByType(ArcShape) + expect(arcs).toHaveLength(sampleData.length) - expect(slices.at(0).prop('datum').id).toEqual('A') - expect(slices.at(0).prop('datum').fill).toEqual('url(#gradientA)') + expect(arcs[0].props.datum.id).toEqual('A') + expect(arcs[0].props.datum.fill).toEqual('url(#gradientA)') - expect(slices.at(1).prop('datum').id).toEqual('B') - expect(slices.at(1).prop('datum').fill).toEqual('url(#gradientB)') + expect(arcs[1].props.datum.id).toEqual('B') + expect(arcs[1].props.datum.fill).toEqual('url(#gradientB)') - expect(slices.at(2).prop('datum').id).toEqual('C') - expect(slices.at(2).prop('datum').fill).toEqual('url(#gradientC)') + expect(arcs[2].props.datum.id).toEqual('C') + expect(arcs[2].props.datum.fill).toEqual('url(#gradientC)') }) }) describe('arc labels', () => { it('should render labels when enabled', () => { - const wrapper = mount( - - ) + const instance = create().root - expect(wrapper.find(ArcLabelsLayer).exists()).toBeTruthy() - const labels = wrapper.find(ArcLabel) + instance.findByType(ArcLabelsLayer) + + const labels = instance.findAllByType(ArcLabel) expect(labels).toHaveLength(sampleData.length) sampleData.forEach((datum, index) => { - expect(labels.at(index).find('text').text()).toEqual(`${datum.value}`) + expect(labels[index].findByType('text').props.children).toEqual(`${datum.value}`) }) }) it('should allow to disable labels', () => { - const wrapper = mount( - - ) - expect(wrapper.find(ArcLabelsLayer).exists()).toBeFalsy() + const instance = create( + + ).root + + expect(instance.findAllByType(ArcLabelsLayer)).toHaveLength(0) }) it('should use formattedValue', () => { - const wrapper = mount( - - ) + const instance = create( + + ).root - const labels = wrapper.find(ArcLabel) + const labels = instance.findAllByType(ArcLabel) expect(labels).toHaveLength(sampleData.length) sampleData.forEach((datum, index) => { - expect(labels.at(index).find('text').text()).toEqual(`$${datum.value}.00`) + expect(labels[index].findByType('text').children[0]).toEqual(`$${datum.value}.00`) }) }) it('should allow to change the label accessor using a path', () => { - const wrapper = mount( - - ) + const instance = create( + + ).root - const labels = wrapper.find(ArcLabel) + const labels = instance.findAllByType(ArcLabel) expect(labels).toHaveLength(sampleData.length) sampleData.forEach((datum, index) => { - expect(labels.at(index).find('text').text()).toEqual(datum.id) + expect(labels[index].findByType('text').children[0]).toEqual(datum.id) }) }) it('should allow to change the label accessor using a function', () => { - const wrapper = mount( + const instance = create( `${datum.id} - ${datum.value}`} - animate={false} /> - ) + ).root - const labels = wrapper.find(ArcLabel) + const labels = instance.findAllByType(ArcLabel) expect(labels).toHaveLength(sampleData.length) sampleData.forEach((datum, index) => { - expect(labels.at(index).find('text').text()).toEqual(`${datum.id} - ${datum.value}`) + expect(labels[index].findByType('text').children[0]).toEqual( + `${datum.id} - ${datum.value}` + ) }) }) it('should allow to customize the label component', () => { const CustomArcLabel = () => - const wrapper = mount( + const instance = create( - ) + ).root - const labels = wrapper.find(CustomArcLabel) + const labels = instance.findAllByType(CustomArcLabel) expect(labels).toHaveLength(sampleData.length) sampleData.forEach((datum, index) => { - expect(labels.at(index).prop('label')).toEqual(`${datum.value}`) + expect(labels[index].props.label).toEqual(`${datum.value}`) }) }) }) describe('arc link labels', () => { it('should render labels when enabled', () => { - const wrapper = mount( - - ) + const instance = create().root - expect(wrapper.find(ArcLinkLabelsLayer).exists()).toBeTruthy() - const labels = wrapper.find(ArcLinkLabel) + instance.findByType(ArcLinkLabelsLayer) + const labels = instance.findAllByType(ArcLinkLabel) expect(labels).toHaveLength(sampleData.length) sampleData.forEach((datum, index) => { - expect(labels.at(index).find('text').text()).toEqual(datum.id) + expect(labels[index].findByType('text').children[0]).toEqual(datum.id) }) }) it('should allow to disable labels', () => { - const wrapper = mount( - - ) - expect(wrapper.find(ArcLinkLabelsLayer).exists()).toBeFalsy() + const instance = create( + + ).root + + expect(instance.findAllByType(ArcLinkLabelsLayer)).toHaveLength(0) }) it('should allow to change the label accessor using a path', () => { - const wrapper = mount( - - ) + const instance = create( + + ).root - const labels = wrapper.find(ArcLinkLabel) + const labels = instance.findAllByType(ArcLinkLabel) expect(labels).toHaveLength(sampleData.length) sampleData.forEach((datum, index) => { - expect(labels.at(index).find('text').text()).toEqual(`${datum.value}`) + expect(labels[index].findByType('text').children[0]).toEqual(`${datum.value}`) }) }) it('should allow to change the label accessor using a function', () => { - const wrapper = mount( + const instance = create( `${datum.id} - ${datum.value}`} - animate={false} /> - ) + ).root - const labels = wrapper.find(ArcLinkLabel) + const labels = instance.findAllByType(ArcLinkLabel) expect(labels).toHaveLength(sampleData.length) sampleData.forEach((datum, index) => { - expect(labels.at(index).find('text').text()).toEqual(`${datum.id} - ${datum.value}`) + expect(labels[index].findByType('text').children[0]).toEqual( + `${datum.id} - ${datum.value}` + ) }) }) it('should allow to customize the label component', () => { const CustomArcLinkLabel = () => null - const wrapper = mount( + const instance = create( - ) + ).root - const labels = wrapper.find(CustomArcLinkLabel) + const labels = instance.findAllByType(CustomArcLinkLabel) expect(labels).toHaveLength(sampleData.length) sampleData.forEach((datum, index) => { - expect(labels.at(index).prop('label')).toEqual(datum.id) + expect(labels[index].props.label).toEqual(datum.id) }) }) }) describe('legends', () => { it('should render legends', () => { - const wrapper = mount( + const instance = create( { itemHeight: 20, }, ]} - animate={false} /> - ) + ).root - const legendItems = wrapper.find(LegendSvgItem) + const legendItems = instance.findAllByType(LegendSvgItem) expect(legendItems).toHaveLength(sampleData.length) sampleData.forEach((datum, index) => { - const legendItem = legendItems.at(index) - expect(legendItem.text()).toEqual(datum.id) - expect(legendItem.find(SymbolSquare).find('rect').prop('fill')).toEqual(datum.color) + const legendItem = legendItems[index] + expect(legendItem.findByType('text').children[0]).toEqual(datum.id) + expect(legendItem.findByType(SymbolSquare).findByType('rect').props.fill).toEqual( + datum.color + ) }) }) it('should use legend.data if provided', () => { - const wrapper = mount( + const instance = create( { itemHeight: 20, }, ]} - animate={false} /> - ) + ).root - const legendItems = wrapper.find(LegendSvgItem) + const legendItems = instance.findAllByType(LegendSvgItem) expect(legendItems).toHaveLength(sampleData.length) sampleData.forEach((datum, index) => { - const legendItem = legendItems.at(index) - expect(legendItem.text()).toEqual(`${datum.id}.${index}`) - expect(legendItem.find(SymbolSquare).find('rect').prop('fill')).toEqual(datum.color) + const legendItem = legendItems[index] + expect(legendItem.findByType('text').children[0]).toEqual(`${datum.id}.${index}`) + expect(legendItem.findByType(SymbolSquare).findByType('rect').props.fill).toEqual( + datum.color + ) }) }) - it('should toggle serie via legend', done => { - const wrapper = mount( + it('should toggle series via legend', async () => { + const instance = create( { itemHeight: 20, }, ]} - animate={false} /> - ) - - const legendItems = wrapper.find(LegendSvgItem) - const shapes = wrapper.find(ArcShape) - - expect(shapes.at(0).prop('style').opacity).toMatchInlineSnapshot(`1`) + ).root - legendItems.at(0).find('rect').at(0).simulate('click') + expect(instance.findAllByType(ArcShape)).toHaveLength(sampleData.length) - // TODO: Figure out why pie isn't respecting animate property - setTimeout(() => { - expect(shapes.at(0).prop('style').opacity).toMatchInlineSnapshot(`0`) + const legendItem = instance.findAllByType(LegendSvgItem)[0] + await act(() => { + legendItem.findAllByType('rect')[0].props.onClick() + }) - done() - }, 1000) + expect(instance.findAllByType(ArcShape)).toHaveLength(sampleData.length - 1) }) }) describe('interactivity', () => { - it('should support onClick handler', () => { + it('should support onClick handler', async () => { const onClick = jest.fn() - const wrapper = mount( - - ) + const instance = create( + + ).root - wrapper.find(ArcShape).at(0).simulate('click') + await act(() => { + instance.findAllByType(ArcShape)[0].findByType('path').props.onClick() + }) expect(onClick).toHaveBeenCalledTimes(1) const [datum] = onClick.mock.calls[0] expect(datum.id).toEqual('A') }) - it('should support onMouseEnter handler', () => { + // @todo: Fix this test due to the use of `getBoundingClientRect`. + xit('should support onMouseEnter handler', async () => { const onMouseEnter = jest.fn() - const wrapper = mount( - - ) + const instance = create( + + ).root - wrapper.find(ArcShape).at(1).simulate('mouseenter') + await act(() => { + instance.findAllByType(ArcShape)[0].findByType('path').props.onMouseEnter() + }) expect(onMouseEnter).toHaveBeenCalledTimes(1) const [datum] = onMouseEnter.mock.calls[0] expect(datum.id).toEqual('B') }) - it('should support onMouseMove handler', () => { + // @todo: Fix this test due to the use of `getBoundingClientRect`. + xit('should support onMouseMove handler', async () => { const onMouseMove = jest.fn() - const wrapper = mount( - - ) + const instance = create( + + ).root - wrapper.find(ArcShape).at(2).simulate('mousemove') + await act(() => { + instance.findAllByType(ArcShape)[0].findByType('path').props.onMouseMove() + }) expect(onMouseMove).toHaveBeenCalledTimes(1) const [datum] = onMouseMove.mock.calls[0] expect(datum.id).toEqual('C') }) - it('should support onMouseLeave handler', () => { + it('should support onMouseLeave handler', async () => { const onMouseLeave = jest.fn() - const wrapper = mount( - - ) + const instance = create( + + ).root - wrapper.find(ArcShape).at(0).simulate('mouseleave') + await act(() => { + instance.findAllByType(ArcShape)[0].findByType('path').props.onMouseLeave() + }) expect(onMouseLeave).toHaveBeenCalledTimes(1) const [datum] = onMouseLeave.mock.calls[0] expect(datum.id).toEqual('A') }) - it('should allow to completely disable interactivity', () => { - const onClick = jest.fn() - const onMouseEnter = jest.fn() - const onMouseMove = jest.fn() - const onMouseLeave = jest.fn() + it('should allow to completely disable interactivity', async () => { + const instance = create( + + ).root - const wrapper = mount( - - ) + instance.findAllByType(ArcShape).forEach(arc => { + const path = arc.findByType('path') + expect(path.props.onClick).toBeUndefined() + expect(path.props.onMouseEnter).toBeUndefined() + expect(path.props.onMouseMove).toBeUndefined() + expect(path.props.onMouseLeave).toBeUndefined() + }) + }) + + describe('activeId', () => { + it('should allow to define a default activeId', () => { + const instance = create( + + ).root + + const arcs = instance.findAllByType(ArcShape) + expect(arcs).toHaveLength(sampleData.length) + + expect(arcs[0].props.datum.id).toEqual(sampleData[0].id) + expect(arcs[0].props.datum.arc.outerRadius).toEqual(200) + + expect(arcs[1].props.datum.id).toEqual(sampleData[1].id) + expect(arcs[1].props.datum.arc.outerRadius).toEqual(210) - const slice = wrapper.find(ArcShape).at(0) - slice.simulate('click') - slice.simulate('mouseenter') - slice.simulate('mousemove') - slice.simulate('mouseleave') - - expect(onClick).not.toHaveBeenCalled() - expect(onMouseEnter).not.toHaveBeenCalled() - expect(onMouseMove).not.toHaveBeenCalled() - expect(onMouseLeave).not.toHaveBeenCalled() - - wrapper.find(ArcShape).forEach(slice => { - const shape = slice.find('path') - expect(shape.prop('onClick')).toBeUndefined() - expect(shape.prop('onMouseEnter')).toBeUndefined() - expect(shape.prop('onMouseMove')).toBeUndefined() - expect(shape.prop('onMouseLeave')).toBeUndefined() + expect(arcs[2].props.datum.id).toEqual(sampleData[2].id) + expect(arcs[2].props.datum.arc.outerRadius).toEqual(200) }) + + xit('should allow to control the activeId externally', () => {}) }) }) describe('tooltip', () => { - it('should render a tooltip when hovering a slice', () => { - const wrapper = mount( - - ) + // @todo: Fix this test due to the use of `getBoundingClientRect`. + xit('should render a tooltip when hovering a slice', async () => { + const instance = create().root - expect(wrapper.find('PieTooltip').exists()).toBeFalsy() + expect(instance.findAllByType('PieTooltip')).toHaveLength(0) - wrapper.find(ArcShape).at(1).simulate('mouseenter') + await act(() => { + instance.findAllByType(ArcShape)[1].findByType('path').props.onMouseEnter() + }) - const tooltip = wrapper.find('PieTooltip') - expect(tooltip.exists()).toBeTruthy() - expect(tooltip.text()).toEqual(`${sampleData[1].id}: ${sampleData[1].value}`) + expect(instance.findAllByType('PieTooltip')).toHaveLength(1) + const tooltip = instance.findAllByType('PieTooltip')[0] + expect(tooltip.children).toEqual(`${sampleData[1].id}: ${sampleData[1].value}`) }) - it('should allow to override the default tooltip', () => { + // @todo: Fix this test due to the use of `getBoundingClientRect`. + xit('should allow to override the default tooltip', async () => { const CustomTooltip = ({ datum }) => {datum.id} - const wrapper = mount( - - ) + const instance = create( + + ).root - wrapper.find(ArcShape).at(1).simulate('mouseenter') + await act(() => { + instance.findAllByType(ArcShape)[1].findByType('path').props.onMouseEnter() + }) - expect(wrapper.find(CustomTooltip).exists()).toBeTruthy() + expect(instance.findAllByType(CustomTooltip)).toHaveLength(1) }) }) describe('layers', () => { it('should support disabling a layer', () => { - const wrapper = mount( - - ) - expect(wrapper.find(ArcShape)).toHaveLength(3) + const instance = create( + + ).root - wrapper.setProps({ layers: ['arcLinkLabels', 'arcLabels', 'legends'] }) - expect(wrapper.find(ArcShape)).toHaveLength(0) + expect(instance.findAllByType(ArcShape)).toHaveLength(0) }) it('should support adding a custom layer', () => { const CustomLayer = () => null - const wrapper = mount( + const instance = create( - ) + ).root - const customLayer = wrapper.find(CustomLayer) + const customLayer = instance.findByType(CustomLayer) - expect(customLayer.prop('dataWithArc')).toHaveLength(3) - expect(customLayer.prop('centerX')).toEqual(200) - expect(customLayer.prop('centerY')).toEqual(200) - expect(customLayer.prop('arcGenerator')).toBeDefined() - expect(customLayer.prop('radius')).toEqual(200) - expect(customLayer.prop('innerRadius')).toEqual(100) + expect(customLayer.props.dataWithArc).toHaveLength(3) + expect(customLayer.props.centerX).toEqual(200) + expect(customLayer.props.centerY).toEqual(200) + expect(customLayer.props.arcGenerator).toBeDefined() + expect(customLayer.props.radius).toEqual(200) + expect(customLayer.props.innerRadius).toEqual(100) }) }) }) diff --git a/packages/waffle/src/hooks.ts b/packages/waffle/src/hooks.ts index 0c72b4607..933803752 100644 --- a/packages/waffle/src/hooks.ts +++ b/packages/waffle/src/hooks.ts @@ -240,7 +240,7 @@ export const useWaffle = ({ id: datum.id, label: datum.label, color: datum.color, - // fill: datum.fill,, + // fill: datum.fill, data: datum, })) diff --git a/storybook/stories/pie/Pie.stories.tsx b/storybook/stories/pie/Pie.stories.tsx index 38750b3d7..ee38ddc9e 100644 --- a/storybook/stories/pie/Pie.stories.tsx +++ b/storybook/stories/pie/Pie.stories.tsx @@ -1,7 +1,8 @@ +import { useCallback, useState } from 'react' import type { Meta, StoryObj } from '@storybook/react' import { animated } from '@react-spring/web' import { generateProgrammingLanguageStats } from '@nivo/generators' -import { Pie } from '@nivo/pie' +import { LegendDatum, Pie } from '@nivo/pie' import { nivoTheme } from '../nivo-theme' const meta: Meta = { @@ -245,3 +246,106 @@ export const CustomArcLabelComponent: Story = { /> ), } + +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(commonProperties.data[1].id) + + return ( +
+ + +
+ ) +} + +export const ControlledActiveId: Story = { + render: () => , +} + +const PieWithCustomLegend = () => { + const [customLegends, setCustomLegends] = useState[]>([]) + + const valueFormat = useCallback( + (value: number) => + `${Number(value).toLocaleString('ru-RU', { + minimumFractionDigits: 2, + })} ₽`, + [] + ) + + return ( +
+ +
+ + + + + + + + + + + + {customLegends.map(legend => { + return ( + + + + + + + + ) + })} + +
ColorIDValueFormatted ValueLabel
+ + + {legend.id} + + {legend.data.value} + {legend.data.formattedValue}{legend.label}
+
+
+ ) +} + +export const CustomLegend: Story = { + render: () => , +} diff --git a/storybook/stories/pie/PieCanvas.stories.tsx b/storybook/stories/pie/PieCanvas.stories.tsx new file mode 100644 index 000000000..61c075a1b --- /dev/null +++ b/storybook/stories/pie/PieCanvas.stories.tsx @@ -0,0 +1,231 @@ +import { useCallback, useState } from 'react' +import type { Meta, StoryObj } from '@storybook/react' +import { generateProgrammingLanguageStats } from '@nivo/generators' +import { LegendDatum, PieCanvas } from '@nivo/pie' +import { nivoTheme } from '../nivo-theme' + +const meta: Meta = { + title: 'PieCanvas', + component: PieCanvas, + tags: ['autodocs'], + argTypes: { + legends: { + control: 'boolean', + }, + }, + args: { + legends: false, + }, +} + +export default meta +type Story = StoryObj + +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 => , +} + +export const Donut: Story = { + render: () => , +} + +/** + * 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: () => , +} + +export const FormattedValues: Story = { + render: () => ( + + `${Number(value).toLocaleString('ru-RU', { + minimumFractionDigits: 2, + })} ₽` + } + /> + ), +} + +export const CustomTooltip: Story = { + render: () => ( + ( +
+ Look, I'm custom :) +
+ + {id}: {value} + +
+ )} + 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(commonProperties.data[1].id) + + return ( +
+ + +
+ ) +} + +export const ControlledActiveId: Story = { + render: () => , +} + +const PieWithCustomLegend = () => { + const [customLegends, setCustomLegends] = useState[]>([]) + + const valueFormat = useCallback( + (value: number) => + `${Number(value).toLocaleString('ru-RU', { + minimumFractionDigits: 2, + })} ₽`, + [] + ) + + return ( +
+ +
+ + + + + + + + + + + + {customLegends.map(legend => { + return ( + + + + + + + + ) + })} + +
ColorIDValueFormatted ValueLabel
+ + + {legend.id} + + {legend.data.value} + {legend.data.formattedValue}{legend.label}
+
+
+ ) +} + +export const CustomLegend: Story = { + render: () => , +} diff --git a/storybook/stories/waffle/Waffle.stories.tsx b/storybook/stories/waffle/Waffle.stories.tsx index caefcfab1..89c4b973a 100644 --- a/storybook/stories/waffle/Waffle.stories.tsx +++ b/storybook/stories/waffle/Waffle.stories.tsx @@ -70,7 +70,7 @@ export const CustomLegend: Story = { render: args => { const [legends, setLegends] = useState[]>([]) - const formatValue = useCallback((value: number) => `${value} peolpe`, []) + const formatValue = useCallback((value: number) => `${value} people`, []) return (
diff --git a/website/src/data/components/pie/meta.yml b/website/src/data/components/pie/meta.yml index c04f5f0b1..e0fb5b1cb 100644 --- a/website/src/data/components/pie/meta.yml +++ b/website/src/data/components/pie/meta.yml @@ -17,6 +17,10 @@ Pie: link: pie--adding-a-metric-in-the-center-using-a-custom-layer - label: Custom arc label component link: pie--custom-arc-label-component + - label: Sync activeId between two pies + link: pie--controlled-active-id + - label: Implementing a custom legend + link: pie--custom-legend description: | Generates a pie chart from an array of data, each datum must have an id and a value property. @@ -39,7 +43,13 @@ 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 + - label: Implementing a custom legend + link: piecanvas--custom-legend 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 diff --git a/website/src/data/components/pie/props.ts b/website/src/data/components/pie/props.ts index 909e4f0b7..79dc2a929 100644 --- a/website/src/data/components/pie/props.ts +++ b/website/src/data/components/pie/props.ts @@ -508,6 +508,60 @@ const props: ChartProperty[] = [ max: 50, }, }, + { + key: 'activeId', + flavors: ['svg', 'canvas'], + help: `Programmatically control the \`activeId\`.`, + description: ` + This property should be used with \`onActiveIdChange\`, + allowing you to fully control which arc should be highlighted. + + You might want to use this in case: + + - You want to synchronize the \`activeId\` with other UI elements defined + outside of nivo, or other nivo charts. + - You're creating some kind of *story-telling* app where you want to highlight + certain arcs based on external input. + - You want to change the default behavior and highlight arcs depending on clicks + rather than \`onMouseEnter\`, which can be desirable on mobile for example. + `, + type: 'string | number | null', + required: false, + group: 'Interactivity', + }, + { + key: 'onActiveIdChange', + flavors: ['svg', 'canvas'], + help: `Programmatically control the \`activeId\`.`, + description: ` + This property should be used with \`activeId\`, + allowing you to fully control which arc should be highlighted. + + You might want to use this in case: + + - You want to synchronize the \`activeId\` with other UI elements defined + outside of nivo, or other nivo charts. + - You're creating some kind of *story-telling* app where you want to highlight + certain arcs based on external input. + - You want to change the default behavior and highlight arcs depending on clicks + rather than \`onMouseEnter\`, which can be desirable on mobile for example. + `, + type: '(id: string | number | null) => void', + required: false, + group: 'Interactivity', + }, + { + key: 'defaultActiveId', + flavors: ['svg', 'canvas'], + help: `Default \`activeId\`.`, + description: ` + You can use this property in case you want to define a default \`activeId\`, + but still don't want to control it by yourself (using \`activeId\` & \`onActiveIdChange\`). + `, + type: 'string | number | null', + required: false, + group: 'Interactivity', + }, { key: 'onMouseEnter', flavors: ['svg'], @@ -588,6 +642,28 @@ const props: ChartProperty[] = [ })), }, }, + { + key: 'forwardLegendData', + group: 'Legends', + type: '(data: LegendDatum[]) => void', + required: false, + flavors: ['svg', 'canvas'], + help: 'Can be used to get the computed legend data.', + description: ` + This property allows you to implement custom + legends, bypassing the limitations of SVG/Canvas. + + For example you could have a state in the parent component, + and then pass the setter. + + Please be very careful when using this property though, + you could end up with an infinite loop if the properties + defining the data don't have a stable reference. + + For example, using a non static/memoized function for \`valueFormat\` + would lead to such issue. + `, + }, { key: 'legends', flavors: ['svg', 'canvas'],