Skip to content

Commit

Permalink
fix(circle-packing): add support for controlled zoomed ID
Browse files Browse the repository at this point in the history
  • Loading branch information
plouc authored and wyze committed Apr 26, 2021
1 parent f83f2f1 commit faf00aa
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 18 deletions.
3 changes: 2 additions & 1 deletion packages/circle-packing/src/CirclePacking.tsx
Expand Up @@ -52,6 +52,7 @@ const InnerCirclePacking = <RawDatum,>({
onMouseLeave,
onClick,
tooltip = defaultProps.tooltip,
zoomedId,
role = defaultProps.role,
}: InnerCirclePackingProps<RawDatum>) => {
const { outerWidth, outerHeight, margin, innerWidth, innerHeight } = useDimensions(
Expand All @@ -74,7 +75,7 @@ const InnerCirclePacking = <RawDatum,>({
childColor,
})

const zoomedNodes = useCirclePackingZoom<RawDatum>(nodes, 'ppie', innerWidth, innerHeight)
const zoomedNodes = useCirclePackingZoom<RawDatum>(nodes, zoomedId, innerWidth, innerHeight)

const layerById: Record<CirclePackingLayerId, ReactNode> = {
circles: null,
Expand Down
3 changes: 2 additions & 1 deletion packages/circle-packing/src/CirclePackingCanvas.tsx
Expand Up @@ -35,6 +35,7 @@ const InnerCirclePackingCanvas = <RawDatum,>({
labelsTextColor = defaultProps.labelsTextColor as InheritedColorConfig<ComputedDatum<RawDatum>>,
// layers = defaultProps.layers,
isInteractive,
zoomedId,
role = defaultProps.role,
}: InnerCirclePackingCanvasProps<RawDatum>) => {
const canvasEl = useRef<HTMLCanvasElement | null>(null)
Expand All @@ -60,7 +61,7 @@ const InnerCirclePackingCanvas = <RawDatum,>({
childColor,
})

const zoomedNodes = useCirclePackingZoom<RawDatum>(nodes, 'node.64', innerWidth, innerHeight)
const zoomedNodes = useCirclePackingZoom<RawDatum>(nodes, zoomedId, innerWidth, innerHeight)

const labels = useCirclePackingLabels({
nodes: zoomedNodes,
Expand Down
3 changes: 2 additions & 1 deletion packages/circle-packing/src/CirclePackingHtml.tsx
Expand Up @@ -46,6 +46,7 @@ export const InnerCirclePackingHtml = <RawDatum,>({
onMouseLeave,
onClick,
tooltip = defaultProps.tooltip,
zoomedId,
role = defaultProps.role,
}: InnerCirclePackingHtmlProps<RawDatum>) => {
const { outerWidth, outerHeight, margin, innerWidth, innerHeight } = useDimensions(
Expand All @@ -68,7 +69,7 @@ export const InnerCirclePackingHtml = <RawDatum,>({
childColor,
})

const zoomedNodes = useCirclePackingZoom<RawDatum>(nodes, 'ppie', innerWidth, innerHeight)
const zoomedNodes = useCirclePackingZoom<RawDatum>(nodes, zoomedId, innerWidth, innerHeight)

const layerById: Record<CirclePackingLayerId, ReactNode> = {
circles: null,
Expand Down
5 changes: 3 additions & 2 deletions packages/circle-packing/src/hooks.ts
Expand Up @@ -105,13 +105,14 @@ export const useCirclePacking = <RawDatum extends DatumWithChildren<RawDatum>>({

export const useCirclePackingZoom = <RawDatum>(
nodes: ComputedDatum<RawDatum>[],
zoomedId: string,
zoomedId: CirclePackingCommonProps<RawDatum>['zoomedId'],
width: number,
height: number
) =>
useMemo(() => {
const zoomedNode = nodes.find(({ id }) => id === zoomedId)
if (!zoomedId) return nodes

const zoomedNode = nodes.find(({ id }) => id === zoomedId)
if (!zoomedNode) return nodes

const ratio = Math.min(width, height) / (zoomedNode.radius * 2)
Expand Down
1 change: 1 addition & 0 deletions packages/circle-packing/src/types.ts
Expand Up @@ -77,6 +77,7 @@ export interface CirclePackingCommonProps<RawDatum> {
layers: CirclePackingLayer<RawDatum>[]
isInteractive: boolean
tooltip: (props: ComputedDatum<RawDatum>) => JSX.Element
zoomedId?: string | null
animate: boolean
motionConfig: ModernMotionProps['motionConfig']
role: string
Expand Down
15 changes: 9 additions & 6 deletions website/src/data/components/circle-packing/props.js
Expand Up @@ -333,13 +333,16 @@ const props = [
required: false,
},
{
key: 'isZoomable',
flavors: ['svg', 'html'],
help: `Enable/disable zooming ('isInteractive' must also be 'true').`,
type: 'boolean',
key: 'zoomedId',
flavors: ['svg', 'html', 'canvas'],
help: `Zoom on a specific node.`,
description: `
If provided, zoom on the node having the provided ID,
this can be used to build a zoomable circle packing chart
when used in conjunction with the \`onClick\` property.
`,
type: 'string | null',
required: false,
defaultValue: defaultProps.isZoomable,
controlType: 'switch',
group: 'Interactivity',
},
...motionProperties(['svg', 'html'], defaultProps, 'react-spring'),
Expand Down
1 change: 0 additions & 1 deletion website/src/pages/circle-packing/canvas.js
Expand Up @@ -48,7 +48,6 @@ const initialProperties = {
modifiers: [['darker', 0.3]],
},
isInteractive: true,
isZoomable: true,
}

const CirclePackingCanvas = () => {
Expand Down
5 changes: 0 additions & 5 deletions website/src/pages/circle-packing/html.js
Expand Up @@ -20,10 +20,6 @@ const initialProperties = {
valueFormat: { format: '', enabled: false },
colors: { scheme: 'spectral' },
colorBy: 'depth',
//childColor: {
// from: 'color',
// modifiers: [['darker', 0.3]],
//},
childColor: 'noinherit',
padding: 2,
leavesOnly: false,
Expand All @@ -40,7 +36,6 @@ const initialProperties = {
animate: true,
motionConfig: 'gentle',
isInteractive: true,
isZoomable: true,
}

const CirclePackingHtml = () => {
Expand Down
1 change: 0 additions & 1 deletion website/src/pages/circle-packing/index.js
Expand Up @@ -47,7 +47,6 @@ const initialProperties = {
animate: true,
motionConfig: 'gentle',
isInteractive: true,
isZoomable: true,
}

const generateData = () => generateLibTree()
Expand Down

0 comments on commit faf00aa

Please sign in to comment.