-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
186 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import React, { createElement, Fragment, ReactNode } from 'react' | ||
import { | ||
// @ts-ignore | ||
bindDefs, | ||
useDimensions, | ||
Container, | ||
SvgWrapper, | ||
usePropertyAccessor, | ||
} from '@nivo/core' | ||
import { CirclePackLayerId, CirclePackSvgProps } from './types' | ||
import { useCirclePack } from './hooks' | ||
|
||
const defaultProps = { | ||
id: 'id', | ||
value: 'value', | ||
|
||
layers: ['circles', 'labels'] as CirclePackLayerId[], | ||
|
||
isInteractive: true, | ||
|
||
animate: true, | ||
motionConfig: 'gentle', | ||
|
||
role: 'img', | ||
} | ||
|
||
const InnerCirclePack = <RawDatum,>({ | ||
data, | ||
id = defaultProps.id, | ||
value = defaultProps.value, | ||
valueFormat, | ||
|
||
width, | ||
height, | ||
margin: partialMargin, | ||
|
||
layers = defaultProps.layers, | ||
|
||
role = defaultProps.role, | ||
}: Partial<CirclePackSvgProps<RawDatum>>) => { | ||
const { outerWidth, outerHeight, margin, innerWidth, innerHeight } = useDimensions( | ||
width, | ||
height, | ||
partialMargin | ||
) | ||
|
||
useCirclePack<RawDatum>({ | ||
data, | ||
id, | ||
value, | ||
}) | ||
const layerById: Record<CirclePackLayerId, ReactNode> = { | ||
circles: null, | ||
labels: null, | ||
} | ||
|
||
const layerContext = {} | ||
|
||
return ( | ||
<SvgWrapper | ||
width={outerWidth} | ||
height={outerHeight} | ||
margin={margin} | ||
//defs={boundDefs} | ||
role={role} | ||
> | ||
{layers.map((layer, i) => { | ||
if (layerById[layer as CirclePackLayerId] !== undefined) { | ||
return layerById[layer as CirclePackLayerId] | ||
} | ||
|
||
if (typeof layer === 'function') { | ||
return <Fragment key={i}>{createElement(layer, layerContext)}</Fragment> | ||
} | ||
|
||
return null | ||
})} | ||
</SvgWrapper> | ||
) | ||
} | ||
|
||
export const CirclePack = <RawDatum,>({ | ||
isInteractive = defaultProps.isInteractive, | ||
animate = defaultProps.animate, | ||
motionConfig = defaultProps.motionConfig, | ||
theme, | ||
...otherProps | ||
}: CirclePackSvgProps<RawDatum>) => ( | ||
<Container | ||
isInteractive={isInteractive} | ||
animate={animate} | ||
motionConfig={motionConfig} | ||
theme={theme} | ||
> | ||
<InnerCirclePack<RawDatum> isInteractive={isInteractive} {...otherProps} /> | ||
</Container> | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import React from 'react' | ||
import { ResponsiveWrapper } from '@nivo/core' | ||
import { CirclePack } from './CirclePack' | ||
import { CirclePackSvgProps } from './types' | ||
|
||
export const ResponsiveCirclePack = <RawDatum,>( | ||
props: Omit<CirclePackSvgProps<RawDatum>, 'width' | 'height'> | ||
) => ( | ||
<ResponsiveWrapper> | ||
{({ width, height }: { width: number; height: number }) => ( | ||
<CirclePack<RawDatum> width={width} height={height} {...props} /> | ||
)} | ||
</ResponsiveWrapper> | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { usePropertyAccessor } from '@nivo/core' | ||
import { DatumWithChildren, CirclePackSvgProps } from './types' | ||
|
||
export const useCirclePack = <RawDatum extends DatumWithChildren<RawDatum>>({ | ||
data, | ||
id, | ||
value, | ||
}: { | ||
data: RawDatum | ||
id: CirclePackSvgProps<RawDatum>['id'] | ||
value: CirclePackSvgProps<RawDatum>['value'] | ||
}) => { | ||
const getId = usePropertyAccessor<RawDatum, string | number>(id) | ||
const getValue = usePropertyAccessor<RawDatum, number>(value) | ||
|
||
console.log({ | ||
data, | ||
getId, | ||
getValue, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,11 @@ | ||
/* | ||
* This file is part of the nivo project. | ||
* | ||
* Copyright 2016-present, Raphaël Benitte. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
export { default as Bubble } from './Bubble' | ||
export { default as ResponsiveBubble } from './ResponsiveBubble' | ||
export { default as BubbleHtml } from './BubbleHtml' | ||
export { default as ResponsiveBubbleHtml } from './ResponsiveBubbleHtml' | ||
export { default as BubbleCanvas } from './BubbleCanvas' | ||
export { default as ResponsiveBubbleCanvas } from './ResponsiveBubbleCanvas' | ||
export * from './props' | ||
|
||
export * from './CirclePack' | ||
export * from './ResponsiveCirclePack' | ||
export * from './types' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import * as React from 'react' | ||
import { | ||
Box, | ||
Dimensions, | ||
ModernMotionProps, | ||
Theme, | ||
PropertyAccessor, | ||
ValueFormat, | ||
} from '@nivo/core' | ||
|
||
export interface DatumWithChildren<RawDatum extends DatumWithChildren<RawDatum>> { | ||
children?: RawDatum[] | ||
} | ||
|
||
export type CirclePackLayerId = 'circles' | 'labels' | ||
|
||
export interface CirclePackCustomLayerProps<RawDatum extends DatumWithChildren<RawDatum>> {} | ||
|
||
export type CirclePackCustomLayer<RawDatum> = React.FC<CirclePackCustomLayerProps<RawDatum>> | ||
|
||
export type CirclePackLayer<RawDatum> = CirclePackLayerId | CirclePackCustomLayer<RawDatum> | ||
|
||
export interface CirclePackSvgProps<RawDatum extends DatumWithChildren<RawDatum>> | ||
extends Dimensions { | ||
data: RawDatum | ||
|
||
id: PropertyAccessor<RawDatum, string | number> | ||
value: PropertyAccessor<RawDatum, number> | ||
valueFormat?: ValueFormat<number> | ||
|
||
margin: Box | ||
|
||
layers: CirclePackLayer<RawDatum>[] | ||
|
||
theme: Theme | ||
|
||
isInteractive: boolean | ||
|
||
animate: boolean | ||
motionConfig: ModernMotionProps['motionConfig'] | ||
|
||
role: string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters