Skip to content

Commit b6741dd

Browse files
committedDec 31, 2021
feat(network): update stories for custom components
1 parent cbe9b21 commit b6741dd

File tree

4 files changed

+59
-34
lines changed

4 files changed

+59
-34
lines changed
 

‎packages/network/stories/network.stories.tsx

+30-13
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { generateNetworkData } from '@nivo/generators'
55
import {
66
Network,
77
NetworkNodeProps,
8+
NetworkLinkProps,
89
NetworkNodeTooltipProps,
910
NetworkSvgProps,
1011
svgDefaultProps,
@@ -33,17 +34,6 @@ const commonProperties: NetworkSvgProps<NodeType> = {
3334

3435
export const Default = () => <Network<NodeType> {...commonProperties} />
3536

36-
const CustomNodeComponent = ({ node }: NetworkNodeProps<NodeType>) => (
37-
<g transform={`translate(${node.x - 6},${node.y - 8}) scale(${0.5})`}>
38-
<circle cx="12" cy="8" r="5" />
39-
<path d="M3,21 h18 C 21,12 3,12 3,21" />
40-
</g>
41-
)
42-
43-
export const CustomNode = () => (
44-
<Network<NodeType> {...commonProperties} nodeComponent={CustomNodeComponent} />
45-
)
46-
4737
const CustomNodeTooltipComponent = ({ node }: NetworkNodeTooltipProps<NodeType>) => (
4838
<div
4939
style={{
@@ -56,16 +46,43 @@ const CustomNodeTooltipComponent = ({ node }: NetworkNodeTooltipProps<NodeType>)
5646
>
5747
<strong>ID: {node.id}</strong>
5848
<br />
59-
Depth: {node.depth}
49+
depth: {node.depth}
6050
<br />
61-
Radius: {node.radius}
51+
radius: {node.radius}
6252
</div>
6353
)
6454

6555
export const CustomNodeTooltip = () => (
6656
<Network<NodeType> {...commonProperties} nodeTooltip={CustomNodeTooltipComponent} />
6757
)
6858

59+
const CustomNodeComponent = ({ node }: NetworkNodeProps<NodeType>) => (
60+
<g transform={`translate(${node.x - 6},${node.y - 8}) scale(${0.5})`}>
61+
<circle cx="12" cy="8" r="5" />
62+
<path d="M3,21 h18 C 21,12 3,12 3,21" />
63+
</g>
64+
)
65+
66+
export const CustomNode = () => (
67+
<Network<NodeType> {...commonProperties} nodeComponent={CustomNodeComponent} />
68+
)
69+
70+
const CustomLinkComponent = ({ link }: NetworkLinkProps<NodeType>) => (
71+
<line
72+
x1={link.source.x}
73+
y1={link.source.y}
74+
x2={link.target.x}
75+
y2={link.target.y}
76+
stroke={link.color}
77+
strokeWidth={1}
78+
strokeDasharray="5 7"
79+
/>
80+
)
81+
82+
export const CustomLink = () => (
83+
<Network<NodeType> {...commonProperties} linkComponent={CustomLinkComponent} />
84+
)
85+
6986
export const OnClickHandler = () => (
7087
<Network<NodeType> {...commonProperties} onClick={action('onClick')} />
7188
)

‎packages/network/stories/networkCanvas.stories.tsx

+16-16
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,6 @@ const commonProperties: NetworkCanvasProps<NodeType> = {
3333

3434
export const Default = () => <NetworkCanvas<NodeType> {...commonProperties} />
3535

36-
const customNodeRenderer = (ctx: CanvasRenderingContext2D, node: NetworkComputedNode<NodeType>) => {
37-
ctx.fillStyle = node.color
38-
39-
ctx.beginPath()
40-
ctx.moveTo(node.x, node.y - node.radius)
41-
ctx.lineTo(node.x + node.radius, node.y + node.radius)
42-
ctx.lineTo(node.x - node.radius, node.y + node.radius)
43-
ctx.fill()
44-
}
45-
46-
export const CustomNodeRenderer = () => (
47-
<NetworkCanvas<NodeType> {...commonProperties} renderNode={customNodeRenderer} />
48-
)
49-
5036
const CustomNodeTooltipComponent = ({ node }: NetworkNodeTooltipProps<NodeType>) => (
5137
<div
5238
style={{
@@ -59,16 +45,30 @@ const CustomNodeTooltipComponent = ({ node }: NetworkNodeTooltipProps<NodeType>)
5945
>
6046
<strong>ID: {node.id}</strong>
6147
<br />
62-
Depth: {node.depth}
48+
depth: {node.depth}
6349
<br />
64-
Radius: {node.radius}
50+
radius: {node.radius}
6551
</div>
6652
)
6753

6854
export const CustomNodeTooltip = () => (
6955
<NetworkCanvas<NodeType> {...commonProperties} nodeTooltip={CustomNodeTooltipComponent} />
7056
)
7157

58+
const customNodeRenderer = (ctx: CanvasRenderingContext2D, node: NetworkComputedNode<NodeType>) => {
59+
ctx.fillStyle = node.color
60+
61+
ctx.beginPath()
62+
ctx.moveTo(node.x, node.y - node.radius)
63+
ctx.lineTo(node.x + node.radius, node.y + node.radius)
64+
ctx.lineTo(node.x - node.radius, node.y + node.radius)
65+
ctx.fill()
66+
}
67+
68+
export const CustomNodeRenderer = () => (
69+
<NetworkCanvas<NodeType> {...commonProperties} renderNode={customNodeRenderer} />
70+
)
71+
7272
export const OnClickHandler = () => (
7373
<NetworkCanvas<NodeType> {...commonProperties} onClick={action('onClick')} />
7474
)

‎website/src/data/components/network/meta.yml

+9-1
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@ Network:
1111
- isomorphic
1212
- experimental
1313
stories:
14+
- label: Custom node tooltip
15+
link: network--custom-node-tooltip
1416
- label: Custom node component
1517
link: network--custom-node
18+
- label: Custom link component
19+
link: network--custom-link
1620
description: |
1721
A network component connecting nodes with links.
1822
@@ -28,8 +32,12 @@ NetworkCanvas:
2832
- canvas
2933
- experimental
3034
stories:
35+
- label: Custom node tooltip
36+
link: networkcanvas--custom-node-tooltip
3137
- label: Custom node renderer
32-
link: network--custom-node-renderer
38+
link: networkcanvas--custom-node-renderer
39+
- label: Custom link renderer
40+
link: networkcanvas--custom-link-renderer
3341
description: |
3442
A variation around the [Network](self:/network/) component.
3543
Well suited for large data sets as it does not impact DOM tree depth,

‎website/src/data/components/network/props.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ const props: ChartProperty[] = [
245245
type: 'boolean',
246246
required: false,
247247
help: 'Enable/disable interactivity.',
248-
flavors: ['svg'],
248+
flavors: ['svg', 'canvas'],
249249
defaultValue: commonDefaultProps.isInteractive,
250250
controlType: 'switch',
251251
},
@@ -257,9 +257,9 @@ const props: ChartProperty[] = [
257257
help: 'Custom tooltip component for nodes.',
258258
flavors: ['svg', 'canvas'],
259259
description: `
260-
A function allowing complete tooltip customisation,
261-
it must return a valid HTML
262-
element and will receive the node's data.
260+
An optional component allowing complete tooltip customisation,
261+
it must return a valid HTML element and will receive
262+
the node's data as a property.
263263
`,
264264
},
265265
{

0 commit comments

Comments
 (0)
Please sign in to comment.