/
compute.ts
407 lines (346 loc) · 10.4 KB
/
compute.ts
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
import { scaleLinear } from 'd3-scale'
import { getValueFormatter } from '@nivo/core'
import { computeContinuousColorScaleColorStops } from '@nivo/colors'
import {
BoxLegendSvgProps,
ContinuousColorsLegendProps,
LegendAnchor,
LegendItemDirection,
} from './types'
import { continuousColorsLegendDefaults } from './defaults'
const isObject = <T>(item: unknown): item is T =>
typeof item === 'object' && !Array.isArray(item) && item !== null
const zeroPadding = {
top: 0,
right: 0,
bottom: 0,
left: 0,
}
export const computeDimensions = ({
direction,
itemsSpacing,
padding: _padding,
itemCount,
itemWidth,
itemHeight,
}: Pick<BoxLegendSvgProps, 'direction' | 'padding'> &
Record<'itemsSpacing' | 'itemCount' | 'itemWidth' | 'itemHeight', number>) => {
if (typeof _padding !== 'number' && !isObject(_padding)) {
throw new Error('Invalid property padding, must be one of: number, object')
}
const padding =
typeof _padding === 'number'
? {
top: _padding,
right: _padding,
bottom: _padding,
left: _padding,
}
: {
...zeroPadding,
..._padding,
}
const horizontalPadding = padding.left + padding.right
const verticalPadding = padding.top + padding.bottom
let width = itemWidth + horizontalPadding
let height = itemHeight + verticalPadding
const spacing = (itemCount - 1) * itemsSpacing
if (direction === 'row') {
width = itemWidth * itemCount + spacing + horizontalPadding
} else if (direction === 'column') {
height = itemHeight * itemCount + spacing + verticalPadding
}
return { width, height, padding }
}
export const computePositionFromAnchor = ({
anchor,
translateX,
translateY,
containerWidth,
containerHeight,
width,
height,
}: { anchor: LegendAnchor } & Record<
'translateX' | 'translateY' | 'containerWidth' | 'containerHeight' | 'width' | 'height',
number
>) => {
let x = translateX
let y = translateY
switch (anchor) {
case 'top':
x += (containerWidth - width) / 2
break
case 'top-right':
x += containerWidth - width
break
case 'right':
x += containerWidth - width
y += (containerHeight - height) / 2
break
case 'bottom-right':
x += containerWidth - width
y += containerHeight - height
break
case 'bottom':
x += (containerWidth - width) / 2
y += containerHeight - height
break
case 'bottom-left':
y += containerHeight - height
break
case 'left':
y += (containerHeight - height) / 2
break
case 'center':
x += (containerWidth - width) / 2
y += (containerHeight - height) / 2
break
}
return { x, y }
}
export const computeItemLayout = ({
direction,
justify,
symbolSize,
symbolSpacing,
width,
height,
}: {
direction: LegendItemDirection
justify: boolean
} & Record<'symbolSize' | 'symbolSpacing' | 'width' | 'height', number>) => {
let symbolX
let symbolY
let labelX
let labelY
let labelAnchor: 'start' | 'middle' | 'end'
let labelAlignment: 'alphabetic' | 'central' | 'text-before-edge'
switch (direction) {
case 'left-to-right':
symbolX = 0
symbolY = (height - symbolSize) / 2
labelY = height / 2
labelAlignment = 'central'
if (justify) {
labelX = width
labelAnchor = 'end'
} else {
labelX = symbolSize + symbolSpacing
labelAnchor = 'start'
}
break
case 'right-to-left':
symbolX = width - symbolSize
symbolY = (height - symbolSize) / 2
labelY = height / 2
labelAlignment = 'central'
if (justify) {
labelX = 0
labelAnchor = 'start'
} else {
labelX = width - symbolSize - symbolSpacing
labelAnchor = 'end'
}
break
case 'top-to-bottom':
symbolX = (width - symbolSize) / 2
symbolY = 0
labelX = width / 2
labelAnchor = 'middle'
if (justify) {
labelY = height
labelAlignment = 'alphabetic'
} else {
labelY = symbolSize + symbolSpacing
labelAlignment = 'text-before-edge'
}
break
case 'bottom-to-top':
symbolX = (width - symbolSize) / 2
symbolY = height - symbolSize
labelX = width / 2
labelAnchor = 'middle'
if (justify) {
labelY = 0
labelAlignment = 'text-before-edge'
} else {
labelY = height - symbolSize - symbolSpacing
labelAlignment = 'alphabetic'
}
break
}
return {
symbolX,
symbolY,
labelX,
labelY,
labelAnchor,
labelAlignment,
}
}
export const computeContinuousColorsLegend = ({
// id,
scale,
ticks,
length = continuousColorsLegendDefaults.length,
thickness = continuousColorsLegendDefaults.thickness,
direction = continuousColorsLegendDefaults.direction,
tickPosition = continuousColorsLegendDefaults.tickPosition,
tickSize = continuousColorsLegendDefaults.tickSize,
tickSpacing = continuousColorsLegendDefaults.tickSpacing,
tickOverlap = continuousColorsLegendDefaults.tickOverlap,
tickFormat = continuousColorsLegendDefaults.tickFormat,
title,
titleAlign = continuousColorsLegendDefaults.titleAlign,
titleOffset = continuousColorsLegendDefaults.titleOffset,
}: ContinuousColorsLegendProps) => {
const domain = scale.domain()
const positionScale = scaleLinear().domain(domain)
if (domain.length === 2) {
// sequential, quantize
positionScale.range([0, length])
} else if (domain.length === 3) {
// diverging
positionScale.range([0, length / 2, length])
}
let values: number[]
if ('thresholds' in scale) {
// quantize
values = [domain[0], ...scale.thresholds(), domain[1]]
} else {
// sequential, diverging
values = Array.isArray(ticks) ? ticks : (scale as any).ticks(ticks)
}
const colorStops = computeContinuousColorScaleColorStops(scale, 32)
const formatValue = getValueFormatter(tickFormat)
const computedTicks: {
x1: number
y1: number
x2: number
y2: number
text: string
textX: number
textY: number
textHorizontalAlign: 'start' | 'middle' | 'end'
textVerticalAlign: 'alphabetic' | 'central' | 'hanging'
}[] = []
let width: number
let height: number
const gradientX1 = 0
const gradientY1 = 0
let gradientX2 = 0
let gradientY2 = 0
let titleX: number
let titleY: number
let titleRotation: number
let titleVerticalAlign: 'alphabetic' | 'hanging'
if (direction === 'row') {
width = length
height = thickness
gradientX2 = 1
let y1: number
let y2: number
let textY: number
const textHorizontalAlign = 'middle'
let textVerticalAlign: 'alphabetic' | 'hanging'
titleRotation = 0
if (titleAlign === 'start') {
titleX = 0
} else if (titleAlign === 'middle') {
titleX = length / 2
} else {
titleX = length
}
if (tickPosition === 'before') {
y1 = -tickSize
y2 = tickOverlap ? thickness : 0
textY = -tickSize - tickSpacing
textVerticalAlign = 'alphabetic'
titleY = thickness + titleOffset
titleVerticalAlign = 'hanging'
} else {
y1 = tickOverlap ? 0 : thickness
y2 = thickness + tickSize
textY = y2 + tickSpacing
textVerticalAlign = 'hanging'
titleY = -titleOffset
titleVerticalAlign = 'alphabetic'
}
values.forEach(value => {
const x = positionScale(value)
computedTicks.push({
x1: x,
y1,
x2: x,
y2,
text: formatValue(value),
textX: x,
textY,
textHorizontalAlign,
textVerticalAlign,
})
})
} else {
width = thickness
height = length
gradientY2 = 1
let x1: number
let x2: number
let textX: number
let textHorizontalAlign: 'start' | 'end'
const textVerticalAlign = 'central'
titleRotation = -90
if (titleAlign === 'start') {
titleY = length
} else if (titleAlign === 'middle') {
titleY = length / 2
} else {
titleY = 0
}
if (tickPosition === 'before') {
x1 = -tickSize
x2 = tickOverlap ? thickness : 0
textX = x1 - tickSpacing
textHorizontalAlign = 'end'
titleX = thickness + titleOffset
titleVerticalAlign = 'hanging'
} else {
x1 = tickOverlap ? 0 : thickness
x2 = thickness + tickSize
textX = x2 + tickSpacing
textHorizontalAlign = 'start'
titleX = -titleOffset
titleVerticalAlign = 'alphabetic'
}
values.forEach(value => {
const y = positionScale(value)
computedTicks.push({
x1,
y1: y,
x2,
y2: y,
text: formatValue(value),
textX,
textY: y,
textHorizontalAlign,
textVerticalAlign,
})
})
}
return {
width,
height,
gradientX1,
gradientY1,
gradientX2,
gradientY2,
colorStops,
ticks: computedTicks,
titleText: title,
titleX,
titleY,
titleRotation,
titleHorizontalAlign: titleAlign,
titleVerticalAlign,
}
}