-
Notifications
You must be signed in to change notification settings - Fork 27
/
annotation.ts
504 lines (478 loc) · 16.1 KB
/
annotation.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/**
* Copyright (c) 2020, 2021 Visa, Inc.
*
* This source code is licensed under the MIT license
* https://github.com/visa/visa-chart-components/blob/master/LICENSE
*
**/
import * as d3 from 'd3';
import {
annotation,
annotationBadge,
annotationLabel,
annotationCallout,
annotationCalloutElbow,
annotationCalloutCurve,
annotationCalloutCircle,
annotationCalloutRect,
annotationXYThreshold
} from '../../node_modules/d3-svg-annotation/indexRollupNext.js';
import { visaColorToHex } from './colors';
import { hideNode } from './accessibilityUtils';
import { select } from 'd3-selection';
import { roundTo } from './formatStats';
import { resolveLabelCollision } from './collisionDetection';
import { getTextWidth } from './textHelpers';
interface DatumObject {
[key: string]: any;
}
const types = {
annotationLabel: annotationLabel,
annotationCallout: annotationCallout,
annotationCalloutElbow: annotationCalloutElbow,
annotationCalloutCurve: annotationCalloutCurve,
annotationCalloutCircle: annotationCalloutCircle,
annotationCalloutRect: annotationCalloutRect,
annotationXYThreshold: annotationXYThreshold,
annotationBadge: annotationBadge
};
export function annotate({
source,
data,
xScale,
xAccessor,
yScale,
yAccessor,
ignoreScales,
width,
height,
padding,
margin,
bitmaps
}: {
source?: any;
data?: any;
xScale?: any;
xAccessor?: any;
yScale?: any;
yAccessor?: any;
ignoreScales?: boolean;
width?;
any;
height?: any;
padding?: any;
margin?: any;
bitmaps?: any;
}) {
d3.select(source)
.selectAll('.vcl-annotation-group')
.remove();
const annotations = d3
.select(source)
.append('g')
.attr('class', 'vcl-annotation-group')
.attr('data-testid', 'annotation-group');
hideNode(annotations.node());
const editable = d3
.select(source)
.append('g')
.attr('class', 'editable vcl-annotation-group')
.attr('data-testid', 'editable-annotation-group');
hideNode(editable.node());
if (data && data.length) {
const percentX = d3
.scaleLinear()
.range(xScale ? xScale.range() : [0, 100])
.domain([0, 100]);
const yRange = yScale ? yScale.range() : [0, 100];
const y0 = yRange[0] >= yRange[1] ? 100 : 0;
const y1 = 100 - y0;
const percentY = d3
.scaleLinear()
.range(yRange)
.domain([y0, y1]);
let invertedY = d => {
const height = yRange[0] >= yRange[1] ? yRange[0] : yRange[1];
return yScale(d) - height;
};
const annotationData = [];
const annotationEditableData = [];
data.forEach((datum, i) => {
const d: DatumObject = {};
const dKeys = Object.keys(datum);
dKeys.forEach(key => {
if (key !== 'data' && key !== 'subject' && key !== 'connector' && key !== 'note') {
if (datum[key] instanceof Array) {
d[key] = [...datum[key]];
} else {
d[key] = datum[key];
}
} else {
d[key] = {};
const innerKeys = Object.keys(datum[key]);
innerKeys.forEach(innerKey => {
if (datum[key][innerKey] instanceof Array) {
if (innerKey === 'points') {
// [ // datum[key][innerKey]
// [ // point
// 40,
// 50
// ],
// [
// 200,
// 100
// ]
// ]
// or
// [ // datum[key][innerKey]
// [ // point
// [val,val],
// [val]
// ],
// [
// [val],
// [val]
// ]
// ]
d[key][innerKey] = [];
datum[key][innerKey].forEach(point => {
const clonedPoint = [];
if (point[0] instanceof Array && point[1] instanceof Array) {
clonedPoint.push([...point[0]]);
clonedPoint.push([...point[1]]);
} else {
clonedPoint.push(point[0]);
clonedPoint.push(point[1]);
}
d[key][innerKey].push(clonedPoint);
});
} else {
d[key][innerKey] = [...datum[key][innerKey]];
}
} else {
d[key][innerKey] = datum[key][innerKey];
}
});
}
});
const yToDate = d.parseAsDates && (d.parseAsDates.includes('y') || d.parseAsDates.includes(yAccessor));
const xToDate = d.parseAsDates && (d.parseAsDates.includes('x') || d.parseAsDates.includes(xAccessor));
let diff = 0;
if (d.data) {
const keys = Object.keys(d.data);
let i = 0;
if (yToDate || xToDate) {
for (i = 0; i < keys.length; i++) {
if (keys[i] === 'x' || keys[i] === xAccessor) {
d.data[keys[i]] = checkDate(d.data[keys[i]], xToDate);
} else if (keys[i] === yAccessor || keys[i] === 'y') {
d.data[keys[i]] = checkDate(d.data[keys[i]], yToDate);
}
}
}
}
if (d.x) {
d.x = resolveValue(d.x, xScale, percentX, xToDate);
}
if (d.y) {
d.y = resolveValue(d.y, yScale, percentY, yToDate);
}
if (d.dx) {
if (d.dx instanceof Array) {
diff = d.data && d.data[xAccessor] !== undefined ? xScale(d.data[xAccessor]) : d.x ? d.x : 0;
}
d.dx = resolveValue(d.dx, xScale, percentX, xToDate) - diff;
}
if (d.dy) {
diff = 0;
if (d.dy instanceof Array) {
diff = d.data && d.data[yAccessor] !== undefined ? yScale(d.data[yAccessor]) : d.y ? d.y : 0;
}
d.dy = resolveValue(d.dy, yScale, percentY, yToDate) - diff;
}
if (d.subject) {
if (d.subject.x1) {
d.subject.x1 = resolveValue(d.subject.x1, xScale, percentX, xToDate);
}
if (d.subject.y1) {
d.subject.y1 = resolveValue(d.subject.y1, yScale, percentY, yToDate);
}
if (d.subject.x2) {
d.subject.x2 = resolveValue(d.subject.x2, xScale, percentX, xToDate);
}
if (d.subject.y2) {
d.subject.y2 = resolveValue(d.subject.y2, yScale, percentY, yToDate);
}
if (d.subject.width) {
d.subject.width = resolveValue(d.subject.width, xScale, percentX, xToDate);
}
if (d.subject.height) {
d.subject.height = resolveValue(d.subject.height, invertedY, percentY, yToDate);
}
}
if (d.connector) {
if (typeof d.connector.curve === 'string') {
d.connector.curve = d3[d.connector.curve];
}
if (d.connector.points instanceof Array) {
d.connector.points.forEach(point => {
point[0] = resolveValue(point[0], xScale, percentX, xToDate);
point[1] = resolveValue(point[1], yScale, percentY, yToDate);
});
}
}
if (d.type && typeof d.type === 'string') {
d.type = types[d.type];
}
if (!d.editMode) {
annotationData.push(d);
} else {
annotationEditableData.push(d);
}
if (d.color) {
d.color = visaColorToHex(d.color) || d.color;
}
if (d.collisionHideOnly) {
if (d.className) {
d.className = d.className + ' annotation-detect-collision';
} else {
d.className = 'annotation-detect-collision';
}
}
});
let makeAnnotations = annotation().annotations(annotationData);
let makeEditableAnnotations = annotation()
.editMode(true)
.annotations(annotationEditableData);
if (!ignoreScales) {
makeAnnotations = annotation()
.accessors({
x: function(d) {
return xScale(d[xAccessor]);
},
y: function(d) {
return yScale(d[yAccessor]);
}
})
.annotations(annotationData);
makeEditableAnnotations = annotation()
.editMode(true)
.accessors({
x: function(d) {
return xScale(d[xAccessor]);
},
y: function(d) {
return yScale(d[yAccessor]);
}
})
.annotations(annotationEditableData);
}
annotations.call(makeAnnotations);
editable.call(makeEditableAnnotations);
// checking if avoidCollision is set in any annotation object that user passed
if (annotationData && annotationData.some(e => e.collisionHideOnly === true)) {
hideOverlappingAnnotations(source, width, height, padding, margin, bitmaps);
}
if (annotationEditableData && annotationEditableData.some(e => e.collisionHideOnly === true)) {
hideOverlappingAnnotations(source, width, height, padding, margin, bitmaps);
}
}
// addStrokeUnder(
// d3
// .select(source)
// .selectAll('.vcl-annotation-group')
// .selectAll('text'),
// 'white'
// );
}
function resolveValue(d, scale, percentScale, shouldBeDate) {
if (d) {
if (d instanceof Array) {
if (d.length === 1) {
return checkZero(scale(checkDate(d[0], shouldBeDate)));
} else if (d.length === 2) {
return checkZero(scale(checkDate(d[0], shouldBeDate)) - scale(checkDate(d[1], shouldBeDate)));
}
} else if (typeof d === 'string' && d.substring(d.length - 1, d.length) === '%') {
return checkZero(percentScale(+d.substring(0, d.length - 1)));
}
}
return checkZero(d);
}
function checkZero(v) {
if (v === 0) {
return 0.000000001;
}
return v;
}
function checkDate(d, shouldCheck) {
if (shouldCheck) {
if (typeof d === 'object' && typeof d.getMonth === 'function') {
return d;
}
return new Date(d);
}
return d;
}
function hideOverlappingAnnotations(source, width, height, padding, margin, bitmaps) {
// now that we have annotated we can use existing bitmap to check for collisions
// all annotation shapes are drawn as paths
// circle.handle is used for edit mode, which we currently don't support
// annotation-connector
// path.connector
// annotation-subject
// path.subject
// annotation-note
// path.note-line
// annotation-note-content
// text.annotation-note-label
// text.annotation-note-title
// rect.annotation-note-bg
const annotationsG = select(source)
.select('.vcl-annotation-group')
.select('.annotations');
annotationsG.selectAll('.annotation').style('visibility', null);
annotationsG.selectAll('.annotation-detect-collision').each((_, i, n) => {
// annotationsG.selectAll('.annotation').each((_, i, n) => {
const me = n[i];
const annotationTransform = parseTransformSimple(select(me).attr('transform'));
const annotationTranslate = (annotationTransform && annotationTransform['translate']) || [0, 0];
// we will need to address transforms on:
// 1. g.annotation
// 2. g.annotation-note
// 3. g.annotation-note-content
// 4. g.connector path.connector-end.connector-dot
const annotationNoteTransform =
select(me)
.select('.annotation-note')
.attr('transform') &&
parseTransformSimple(
select(me)
.select('.annotation-note')
.attr('transform')
);
const annotationNoteTranslate = (annotationNoteTransform && annotationNoteTransform['translate']) || [0, 0];
const annotationNoteContentTransform =
select(me)
.select('.annotation-note')
.select('.annotation-note-content')
.attr('transform') &&
parseTransformSimple(
select(me)
.select('.annotation-note')
.select('.annotation-note-content')
.attr('transform')
);
const annotationNoteContentTranslate = (annotationNoteContentTransform &&
annotationNoteContentTransform['translate']) || [0, 0];
// first thing we can do is add data-d paths to draw all the annotation stuff to bitmap
// paths will also need to handle the various transforms
select(me)
.selectAll('path')
.each((_, i, n) => {
const pathMe = select(n[i]);
let translateX = annotationTranslate[0] + padding.left + margin.left;
let translateY = annotationTranslate[1] + padding.top + margin.top;
if (pathMe.classed('note-line')) {
translateX += annotationNoteTranslate[0];
translateY += annotationNoteTranslate[1];
}
pathMe
.attr('data-d', pathMe.attr('d'))
.attr('data-translate-x', translateX)
.attr('data-translate-y', translateY);
});
// next we need to try and handle our texts for the annotations
// this assumes all text is in the note arena
let maxDataWidth = 0;
select(me)
.selectAll('text')
.each((_, i, n) => {
const textElement = n[i];
const style = getComputedStyle(textElement);
const fontSize = parseFloat(style.fontSize);
const textHeight = Math.max(fontSize - 1, 1); // clone.getBBox().height;
const textWidth = getTextWidth(textElement.textContent, fontSize, true, style.fontFamily);
const textMe = select(textElement);
const translateX =
annotationTranslate[0] +
annotationNoteTranslate[0] +
annotationNoteContentTranslate[0] +
padding.left +
margin.left;
const translateY =
annotationTranslate[1] +
annotationNoteTranslate[1] +
annotationNoteContentTranslate[1] +
padding.top +
margin.top;
maxDataWidth = Math.max(textWidth, maxDataWidth);
textMe
.attr('data-x', +textMe.attr('x') || 0)
.attr('data-y', textHeight / 2 + +textMe.attr('y') || 0)
.attr('data-ignore', textWidth === 0)
.attr('data-width', textWidth)
.attr('data-height', textHeight)
.attr('data-translate-x', translateX)
.attr('data-translate-y', translateY)
.attr('data-no-text-anchor', true);
});
// if we have no text we set this flag for visibility check later
if (maxDataWidth === 0) {
select(me).attr('data-no-text', true);
}
});
bitmaps = resolveLabelCollision({
bitmaps: bitmaps,
labelSelection: select('.nothing-exists-here'),
avoidMarks: [annotationsG.selectAll('.annotation-detect-collision').selectAll('path')],
validPositions: ['middle'],
offsets: [1],
// offsets: [2, 2, 4, 2, 1, 1, 1, 1],
accessors: ['cidx'],
size: [roundTo(width, 0), roundTo(height, 0)] // we need the whole width for series labels
});
bitmaps = resolveLabelCollision({
bitmaps: bitmaps,
labelSelection: annotationsG.selectAll('.annotation-detect-collision text'), //.filter((_, i, n) => +select(n[i]).attr('data-width') > 0),
avoidMarks: [],
validPositions: ['middle'],
offsets: [1],
accessors: ['cidx'],
size: [roundTo(width, 0), roundTo(height, 0)] // we need the whole width for series labels
});
// now that we have hidden text, we need to hide the rest of the annotation
annotationsG.selectAll('.annotation-detect-collision').each((_, i, n) => {
const me = n[i];
let textShownIndicator = false;
select(me)
.selectAll('text')
.each((_, i, n) => {
const textElement = n[i];
const elementIndicator =
select(textElement).attr('data-label-hidden') === 'false' &&
select(textElement).attr('data-ignore') === 'false';
textShownIndicator = textShownIndicator || elementIndicator; // if we already have a true, we need to keep it
});
// this will flip visibility on the full element if we don't find any visible text
if (textShownIndicator || select(me).attr('data-no-text') === 'true') {
select(me).style('visibility', null);
} else {
select(me).style('visibility', 'hidden');
}
});
}
function parseTransformSimple(transform) {
const result = {};
transform
.trim()
.replace(', ', ',')
.split(' ')
.forEach(transformation => {
const step1 = transformation.split(')')[0].split(',');
const step2 = step1[0].split('(');
result[step2[0]] = [];
result[step2[0]].push(+step2[1], +step1[1]);
});
return result;
}