Skip to content

Commit aed3590

Browse files
committedDec 31, 2021
feat(network): add support for canvas annotations
1 parent bfbe9b6 commit aed3590

File tree

5 files changed

+31
-8
lines changed

5 files changed

+31
-8
lines changed
 

‎packages/annotations/src/compute.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
} from '@nivo/core'
88
import { defaultProps } from './props'
99
import {
10-
AnnotationSpec,
1110
AnnotationPositionGetter,
1211
AnnotationDimensionsGetter,
1312
BoundAnnotation,
@@ -89,7 +88,7 @@ export const getLinkAngle = (
8988
}
9089

9190
export const computeAnnotation = <Datum>(
92-
annotation: Required<AnnotationSpec<Datum>>
91+
annotation: BoundAnnotation<Datum>
9392
): AnnotationInstructions => {
9493
const {
9594
x,

‎packages/annotations/src/hooks.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
AnnotationDimensionsGetter,
55
AnnotationMatcher,
66
AnnotationPositionGetter,
7-
AnnotationSpec,
7+
BoundAnnotation,
88
} from './types'
99

1010
/**
@@ -35,7 +35,7 @@ export const useAnnotations = <Datum>({
3535
export const useComputedAnnotations = <Datum>({
3636
annotations,
3737
}: {
38-
annotations: Required<AnnotationSpec<Datum>>[]
38+
annotations: BoundAnnotation<Datum>[]
3939
}) =>
4040
useMemo(
4141
() =>
@@ -48,5 +48,5 @@ export const useComputedAnnotations = <Datum>({
4848
[annotations]
4949
)
5050

51-
export const useComputedAnnotation = <Datum>(annotation: Required<AnnotationSpec<Datum>>) =>
51+
export const useComputedAnnotation = <Datum>(annotation: BoundAnnotation<Datum>) =>
5252
useMemo(() => computeAnnotation<Datum>(annotation), [annotation])

‎packages/network/src/NetworkCanvas.tsx

+25-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
import { useCallback, useRef, useEffect, createElement, MouseEvent } from 'react'
22
import { getDistance, getRelativeCursor, Container, useDimensions, useTheme } from '@nivo/core'
33
import { useTooltip } from '@nivo/tooltip'
4+
import { useComputedAnnotations, renderAnnotationsToCanvas } from '@nivo/annotations'
45
import { canvasDefaultProps } from './defaults'
5-
import { useNetwork } from './hooks'
6-
import { NetworkCanvasProps, InputNode, NodeTooltip, InputLink } from './types'
6+
import { useNetwork, useNodeAnnotations } from './hooks'
7+
import {
8+
NetworkCanvasProps,
9+
InputNode,
10+
ComputedNode,
11+
NodeTooltip,
12+
InputLink,
13+
NetworkSvgProps,
14+
} from './types'
715

816
type InnerNetworkCanvasProps<Node extends InputNode, Link extends InputLink> = Omit<
917
NetworkCanvasProps<Node, Link>,
@@ -39,6 +47,10 @@ const InnerNetworkCanvas = <Node extends InputNode, Link extends InputLink>({
3947
linkThickness = canvasDefaultProps.linkThickness,
4048
linkColor = canvasDefaultProps.linkColor,
4149

50+
annotations = canvasDefaultProps.annotations as NonNullable<
51+
NetworkSvgProps<Node, Link>['annotations']
52+
>,
53+
4254
isInteractive = canvasDefaultProps.isInteractive,
4355
nodeTooltip = canvasDefaultProps.nodeTooltip as NodeTooltip<Node>,
4456
onClick,
@@ -70,6 +82,11 @@ const InnerNetworkCanvas = <Node extends InputNode, Link extends InputLink>({
7082
linkColor,
7183
})
7284

85+
const boundAnnotations = useNodeAnnotations<Node>(nodes!, annotations)
86+
const computedAnnotations = useComputedAnnotations<ComputedNode<Node>>({
87+
annotations: boundAnnotations,
88+
})
89+
7390
const theme = useTheme()
7491

7592
useEffect(() => {
@@ -91,6 +108,11 @@ const InnerNetworkCanvas = <Node extends InputNode, Link extends InputLink>({
91108
links.forEach(link => renderLink(ctx, link))
92109
} else if (layer === 'nodes' && nodes !== null) {
93110
nodes.forEach(node => renderNode(ctx, node))
111+
} else if (layer === 'annotations') {
112+
renderAnnotationsToCanvas<ComputedNode<Node>>(ctx, {
113+
annotations: computedAnnotations as any,
114+
theme,
115+
})
94116
} else if (typeof layer === 'function' && nodes !== null && links !== null) {
95117
layer(ctx, {
96118
// ...props,
@@ -112,6 +134,7 @@ const InnerNetworkCanvas = <Node extends InputNode, Link extends InputLink>({
112134
links,
113135
renderNode,
114136
renderLink,
137+
computedAnnotations,
115138
])
116139

117140
const getNodeFromMouseEvent = useCallback(

‎packages/scatterplot/src/ScatterPlotCanvas.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ const InnerScatterPlotCanvas = <RawDatum extends ScatterPlotDatum>({
200200
debugMesh,
201201
voronoi,
202202
currentNode,
203+
boundAnnotations,
203204
])
204205

205206
const { showTooltipFromEvent, hideTooltip } = useTooltip()

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ const props: ChartProperty[] = [
311311
flavors: allFlavors,
312312
createDefaults: {
313313
type: 'circle',
314-
match: { id: '0' },
314+
match: { id: 'Node 0' },
315315
note: 'New annotation',
316316
noteX: 160,
317317
noteY: 36,

0 commit comments

Comments
 (0)
Please sign in to comment.