File size: 3,328 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { useMemo } from 'react'
import { useValueFormatter, usePropertyAccessor } from '@nivo/core'
import { useOrdinalColorScale } from '@nivo/colors'
import { computeXYScalesForSeries } from '@nivo/scales'
import { useAnnotations } from '@nivo/annotations'
import { computePoints, getNodeSizeGenerator } from './compute'
import {
    ScatterPlotCommonProps,
    ScatterPlotDataProps,
    ScatterPlotDatum,
    ScatterPlotNodeData,
} from './types'

const useNodeSize = <RawDatum extends ScatterPlotDatum>(
    size: ScatterPlotCommonProps<RawDatum>['nodeSize']
) => useMemo(() => getNodeSizeGenerator<RawDatum>(size), [size])

export const useScatterPlot = <RawDatum extends ScatterPlotDatum>({
    data,
    xScaleSpec,
    xFormat,
    yScaleSpec,
    yFormat,
    width,
    height,
    nodeId,
    nodeSize,
    colors,
}: {
    data: ScatterPlotDataProps<RawDatum>['data']
    xScaleSpec: ScatterPlotCommonProps<RawDatum>['xScale']
    xFormat?: ScatterPlotCommonProps<RawDatum>['xFormat']
    yScaleSpec: ScatterPlotCommonProps<RawDatum>['yScale']
    yFormat?: ScatterPlotCommonProps<RawDatum>['yFormat']
    width: number
    height: number
    nodeId: ScatterPlotCommonProps<RawDatum>['nodeId']
    nodeSize: ScatterPlotCommonProps<RawDatum>['nodeSize']
    colors: ScatterPlotCommonProps<RawDatum>['colors']
}) => {
    const { series, xScale, yScale } = useMemo(
        () =>
            computeXYScalesForSeries<{ id: string | number }, RawDatum>(
                data,
                xScaleSpec,
                yScaleSpec,
                width,
                height
            ),
        [data, xScaleSpec, yScaleSpec, width, height]
    )

    const formatX = useValueFormatter(xFormat)
    const formatY = useValueFormatter(yFormat)
    const getNodeId = usePropertyAccessor(nodeId)
    const rawNodes = useMemo(
        () => computePoints<RawDatum>({ series, formatX, formatY, getNodeId }),
        [series, formatX, formatY, getNodeId]
    )

    const getNodeSize = useNodeSize<RawDatum>(nodeSize)

    const getColor = useOrdinalColorScale(colors, 'serieId')

    const nodes: ScatterPlotNodeData<RawDatum>[] = useMemo(
        () =>
            rawNodes.map(rawNode => ({
                ...rawNode,
                size: getNodeSize(rawNode),
                color: getColor({ serieId: rawNode.serieId }),
            })),
        [rawNodes, getNodeSize, getColor]
    )

    const legendData = useMemo(
        () =>
            series.map(serie => ({
                id: serie.id,
                label: serie.id,
                color: getColor({ serieId: serie.id }),
            })),
        [series, getColor]
    )

    return {
        xScale,
        yScale,
        nodes,
        legendData,
    }
}

export const useScatterPlotAnnotations = <RawDatum extends ScatterPlotDatum>(
    items: ScatterPlotNodeData<RawDatum>[],
    annotations: ScatterPlotCommonProps<RawDatum>['annotations']
) =>
    useAnnotations<ScatterPlotNodeData<RawDatum>>({
        data: items,
        annotations,
        getPosition: (node: ScatterPlotNodeData<RawDatum>) => ({
            x: node.x,
            y: node.y,
        }),
        getDimensions: (node: ScatterPlotNodeData<RawDatum>) => ({
            size: node.size,
            width: node.size,
            height: node.size,
        }),
    })