File size: 5,514 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import filter from 'lodash/filter.js'
import isNumber from 'lodash/isNumber.js'
import omit from 'lodash/omit.js'
import {
    radiansToDegrees,
    normalizeAngleDegrees,
    degreesToRadians,
    positionFromAngle,
} from '@nivo/core'
import { defaultProps } from './props'
import {
    AnnotationPositionGetter,
    AnnotationDimensionsGetter,
    BoundAnnotation,
    AnnotationMatcher,
    AnnotationInstructions,
} from './types'
import { isCircleAnnotation, isRectAnnotation } from './utils'

export const bindAnnotations = <
    Datum = {
        x: number
        y: number
    },
>({
    data,
    annotations,
    getPosition,
    getDimensions,
}: {
    data: readonly Datum[]
    annotations: readonly AnnotationMatcher<Datum>[]
    getPosition: AnnotationPositionGetter<Datum>
    getDimensions: AnnotationDimensionsGetter<Datum>
}): BoundAnnotation<Datum>[] =>
    annotations.reduce((acc: BoundAnnotation<Datum>[], annotation) => {
        const offset = annotation.offset || 0

        return [
            ...acc,
            ...filter<Datum>(data, annotation.match).map(datum => {
                const position = getPosition(datum)
                const dimensions = getDimensions(datum)

                if (isCircleAnnotation(annotation) || isRectAnnotation(annotation)) {
                    dimensions.size = dimensions.size + offset * 2
                    dimensions.width = dimensions.width + offset * 2
                    dimensions.height = dimensions.height + offset * 2
                }

                // acc.push({
                //     ...omit(annotation, ['match', 'offset']),
                //     ...position,
                //     ...dimensions,
                //     size: annotation.size || dimensions.size,
                //     datum,
                // } as any)
                // return [
                //     ...acc,
                //     {
                //         ...omit(annotation, ['match', 'offset']),
                //         ...position,
                //         ...dimensions,
                //         size: annotation.size || dimensions.size,
                //         datum,
                //     },
                // ]
                return {
                    ...omit(annotation, ['match', 'offset']),
                    ...position,
                    ...dimensions,
                    size: annotation.size || dimensions.size,
                    datum,
                } as Required<BoundAnnotation<Datum>>
            }),
        ]

        // return acc
    }, [])

export const getLinkAngle = (
    sourceX: number,
    sourceY: number,
    targetX: number,
    targetY: number
) => {
    const angle = Math.atan2(targetY - sourceY, targetX - sourceX)

    return normalizeAngleDegrees(radiansToDegrees(angle))
}

export const computeAnnotation = <Datum>(
    annotation: BoundAnnotation<Datum>
): AnnotationInstructions => {
    const {
        x,
        y,
        noteX,
        noteY,
        noteWidth = defaultProps.noteWidth,
        noteTextOffset = defaultProps.noteTextOffset,
    } = annotation

    let computedNoteX: number
    let computedNoteY: number

    if (isNumber(noteX)) {
        computedNoteX = x + noteX
    } else if (noteX.abs !== undefined) {
        computedNoteX = noteX.abs
    } else {
        throw new Error(`noteX should be either a number or an object containing an 'abs' property`)
    }

    if (isNumber(noteY)) {
        computedNoteY = y + noteY
    } else if (noteY.abs !== undefined) {
        computedNoteY = noteY.abs
    } else {
        throw new Error(`noteY should be either a number or an object containing an 'abs' property`)
    }

    let computedX = x
    let computedY = y

    const angle = getLinkAngle(x, y, computedNoteX, computedNoteY)

    if (isCircleAnnotation<Datum>(annotation)) {
        const position = positionFromAngle(degreesToRadians(angle), annotation.size / 2)
        computedX += position.x
        computedY += position.y
    }

    if (isRectAnnotation<Datum>(annotation)) {
        const eighth = Math.round((angle + 90) / 45) % 8
        if (eighth === 0) {
            computedY -= annotation.height / 2
        }
        if (eighth === 1) {
            computedX += annotation.width / 2
            computedY -= annotation.height / 2
        }
        if (eighth === 2) {
            computedX += annotation.width / 2
        }
        if (eighth === 3) {
            computedX += annotation.width / 2
            computedY += annotation.height / 2
        }
        if (eighth === 4) {
            computedY += annotation.height / 2
        }
        if (eighth === 5) {
            computedX -= annotation.width / 2
            computedY += annotation.height / 2
        }
        if (eighth === 6) {
            computedX -= annotation.width / 2
        }
        if (eighth === 7) {
            computedX -= annotation.width / 2
            computedY -= annotation.height / 2
        }
    }

    let textX = computedNoteX
    const textY = computedNoteY - noteTextOffset

    let noteLineX = computedNoteX
    const noteLineY = computedNoteY

    if ((angle + 90) % 360 > 180) {
        textX -= noteWidth
        noteLineX -= noteWidth
    } else {
        noteLineX += noteWidth
    }

    return {
        points: [
            [computedX, computedY],
            [computedNoteX, computedNoteY],
            [noteLineX, noteLineY],
        ] as [number, number][],
        text: [textX, textY],
        angle: angle + 90,
    }
}