@keneth80/k-chart 1.17.1 → 1.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,654 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
14
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
15
+ if (ar || !(i in from)) {
16
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
17
+ ar[i] = from[i];
18
+ }
19
+ }
20
+ return to.concat(ar || Array.prototype.slice.call(from));
21
+ };
22
+ Object.defineProperty(exports, "__esModule", { value: true });
23
+ exports.createTreeSeries = exports.resolveTreeData = void 0;
24
+ var d3_hierarchy_1 = require("d3-hierarchy");
25
+ var d3_zoom_1 = require("d3-zoom");
26
+ var custom_1 = require("./custom");
27
+ var defaultTreePalette = [
28
+ '#5db8ff',
29
+ '#56d08f',
30
+ '#f3b45b',
31
+ '#d876ff',
32
+ '#ff6b8a',
33
+ '#72e4ff',
34
+ '#a8d95f',
35
+ '#ff9f5a'
36
+ ];
37
+ var resolveMetric = function (value) {
38
+ var numeric = Number(value);
39
+ return Number.isFinite(numeric) ? numeric : 0;
40
+ };
41
+ var normalizeId = function (value) { return String(value !== null && value !== void 0 ? value : '').trim(); };
42
+ var escapeTooltipHtml = function (value) { return String(value !== null && value !== void 0 ? value : '')
43
+ .replace(/&/g, '&amp;')
44
+ .replace(/</g, '&lt;')
45
+ .replace(/>/g, '&gt;')
46
+ .replace(/"/g, '&quot;')
47
+ .replace(/'/g, '&#39;'); };
48
+ var hashTreeValue = function (seed, value) {
49
+ var text = String(value !== null && value !== void 0 ? value : '');
50
+ var hash = seed;
51
+ for (var index = 0; index < text.length; index += 1) {
52
+ hash = Math.imul(hash ^ text.charCodeAt(index), 16777619);
53
+ }
54
+ return hash;
55
+ };
56
+ var resolveTreeDataSignature = function (data, configuration) {
57
+ var hash = Math.imul(2166136261, data.length + 1);
58
+ data.forEach(function (row) {
59
+ hash = hashTreeValue(hash, row[configuration.idField]);
60
+ hash = hashTreeValue(hash, row[configuration.parentField]);
61
+ if (configuration.labelField)
62
+ hash = hashTreeValue(hash, row[configuration.labelField]);
63
+ if (configuration.valueField)
64
+ hash = hashTreeValue(hash, row[configuration.valueField]);
65
+ if (configuration.categoryField)
66
+ hash = hashTreeValue(hash, row[configuration.categoryField]);
67
+ });
68
+ return hash >>> 0;
69
+ };
70
+ var resolveTreeData = function (data, configuration) {
71
+ if (data.length === 0) {
72
+ return { root: null, nodes: [], links: [] };
73
+ }
74
+ var nodes = new Map();
75
+ var parentIds = new Map();
76
+ data.forEach(function (row, index) {
77
+ var id = normalizeId(row[configuration.idField]);
78
+ if (!id) {
79
+ throw new Error("KChart tree data requires a non-empty id at row ".concat(index, "."));
80
+ }
81
+ if (nodes.has(id)) {
82
+ throw new Error("KChart tree data requires unique id values; duplicate id \"".concat(id, "\" found."));
83
+ }
84
+ var parentId = normalizeId(row[configuration.parentField]) || undefined;
85
+ var rawLabel = configuration.labelField ? row[configuration.labelField] : undefined;
86
+ var rawCategory = configuration.categoryField ? row[configuration.categoryField] : undefined;
87
+ nodes.set(id, {
88
+ id: id,
89
+ parentId: parentId,
90
+ label: rawLabel === null || rawLabel === undefined || String(rawLabel).trim() === ''
91
+ ? id
92
+ : String(rawLabel),
93
+ value: configuration.valueField ? resolveMetric(row[configuration.valueField]) : 0,
94
+ category: rawCategory === null || rawCategory === undefined ? undefined : String(rawCategory),
95
+ row: row,
96
+ children: [],
97
+ depth: 0,
98
+ height: 0,
99
+ x: 0,
100
+ y: 0
101
+ });
102
+ parentIds.set(id, parentId);
103
+ });
104
+ var rootIds = Array.from(parentIds.entries())
105
+ .filter(function (_a) {
106
+ var parentId = _a[1];
107
+ return !parentId;
108
+ })
109
+ .map(function (_a) {
110
+ var id = _a[0];
111
+ return id;
112
+ });
113
+ if (rootIds.length !== 1) {
114
+ throw new Error("KChart tree data requires a single root node; found ".concat(rootIds.length, "."));
115
+ }
116
+ parentIds.forEach(function (parentId, id) {
117
+ if (parentId && !nodes.has(parentId)) {
118
+ throw new Error("KChart tree data references missing parent \"".concat(parentId, "\" from node \"").concat(id, "\"."));
119
+ }
120
+ });
121
+ // Validate parent chains iteratively so malformed or very deep business hierarchies
122
+ // fail before D3 layout without risking a JavaScript call-stack overflow.
123
+ var visited = new Set();
124
+ nodes.forEach(function (_node, startId) {
125
+ if (visited.has(startId))
126
+ return;
127
+ var path = [];
128
+ var pathIndexes = new Map();
129
+ var currentId = startId;
130
+ while (currentId && !visited.has(currentId)) {
131
+ var cycleStart = pathIndexes.get(currentId);
132
+ if (cycleStart !== undefined) {
133
+ var cycle = path.slice(cycleStart).concat(currentId);
134
+ throw new Error("KChart tree data must be acyclic; cycle detected in ".concat(cycle.join(' -> '), "."));
135
+ }
136
+ pathIndexes.set(currentId, path.length);
137
+ path.push(currentId);
138
+ currentId = parentIds.get(currentId);
139
+ }
140
+ path.forEach(function (id) { return visited.add(id); });
141
+ });
142
+ var layoutRows = Array.from(nodes.values()).map(function (node) { return ({
143
+ node: node,
144
+ id: node.id,
145
+ parentId: node.parentId
146
+ }); });
147
+ var root = (0, d3_hierarchy_1.stratify)()
148
+ .id(function (row) { return row.id; })
149
+ .parentId(function (row) { var _a; return (_a = row.parentId) !== null && _a !== void 0 ? _a : null; })(layoutRows);
150
+ var links = [];
151
+ root.each(function (hierarchyNode) {
152
+ var _a, _b;
153
+ var node = hierarchyNode.data.node;
154
+ node.depth = hierarchyNode.depth;
155
+ node.height = hierarchyNode.height;
156
+ node.parent = (_a = hierarchyNode.parent) === null || _a === void 0 ? void 0 : _a.data.node;
157
+ node.children = ((_b = hierarchyNode.children) !== null && _b !== void 0 ? _b : []).map(function (child) { return child.data.node; });
158
+ if (node.parent) {
159
+ links.push({
160
+ id: "".concat(node.parent.id, "\0").concat(node.id),
161
+ source: node.parent,
162
+ target: node,
163
+ value: node.value,
164
+ row: node.row
165
+ });
166
+ }
167
+ });
168
+ return {
169
+ root: root.data.node,
170
+ nodes: root.descendants().map(function (node) { return node.data.node; }),
171
+ links: links
172
+ };
173
+ };
174
+ exports.resolveTreeData = resolveTreeData;
175
+ var resolveTreeLinkPath = function (link, layout, orientation) {
176
+ var source = link.source;
177
+ var target = link.target;
178
+ if (layout === 'radial') {
179
+ return "M ".concat(source.x, " ").concat(source.y, " L ").concat(target.x, " ").concat(target.y);
180
+ }
181
+ if (orientation === 'left-right' || orientation === 'right-left') {
182
+ var midX = (source.x + target.x) / 2;
183
+ return "M ".concat(source.x, " ").concat(source.y, " L ").concat(midX, " ").concat(source.y, " L ").concat(midX, " ").concat(target.y, " L ").concat(target.x, " ").concat(target.y);
184
+ }
185
+ var midY = (source.y + target.y) / 2;
186
+ return "M ".concat(source.x, " ").concat(source.y, " L ").concat(source.x, " ").concat(midY, " L ").concat(target.x, " ").concat(midY, " L ").concat(target.x, " ").concat(target.y);
187
+ };
188
+ var distanceToSegment = function (x, y, x1, y1, x2, y2) {
189
+ var dx = x2 - x1;
190
+ var dy = y2 - y1;
191
+ var lengthSquared = dx * dx + dy * dy;
192
+ if (lengthSquared === 0) {
193
+ return Math.hypot(x - x1, y - y1);
194
+ }
195
+ var ratio = Math.max(0, Math.min(1, ((x - x1) * dx + (y - y1) * dy) / lengthSquared));
196
+ return Math.hypot(x - (x1 + ratio * dx), y - (y1 + ratio * dy));
197
+ };
198
+ var distanceToLink = function (x, y, link, layout, orientation) {
199
+ var source = link.source;
200
+ var target = link.target;
201
+ if (layout === 'radial') {
202
+ return distanceToSegment(x, y, source.x, source.y, target.x, target.y);
203
+ }
204
+ if (orientation === 'left-right' || orientation === 'right-left') {
205
+ var midX = (source.x + target.x) / 2;
206
+ return Math.min(distanceToSegment(x, y, source.x, source.y, midX, source.y), distanceToSegment(x, y, midX, source.y, midX, target.y), distanceToSegment(x, y, midX, target.y, target.x, target.y));
207
+ }
208
+ var midY = (source.y + target.y) / 2;
209
+ return Math.min(distanceToSegment(x, y, source.x, source.y, source.x, midY), distanceToSegment(x, y, source.x, midY, target.x, midY), distanceToSegment(x, y, target.x, midY, target.x, target.y));
210
+ };
211
+ var createFocusSet = function (node, emphasis) {
212
+ var focus = new Set([node.id]);
213
+ if (emphasis === 'none')
214
+ return focus;
215
+ if (emphasis === 'ancestor' || emphasis === 'both') {
216
+ var current = node.parent;
217
+ while (current) {
218
+ focus.add(current.id);
219
+ current = current.parent;
220
+ }
221
+ }
222
+ if (emphasis === 'descendant' || emphasis === 'both') {
223
+ var queue = __spreadArray([], node.children, true);
224
+ for (var index = 0; index < queue.length; index += 1) {
225
+ var child = queue[index];
226
+ focus.add(child.id);
227
+ queue.push.apply(queue, child.children);
228
+ }
229
+ }
230
+ return focus;
231
+ };
232
+ var resolveLabelPosition = function (node, configured, layout, orientation, width) {
233
+ if (configured !== 'auto')
234
+ return configured;
235
+ if (layout === 'radial')
236
+ return node.x < width / 2 ? 'left' : 'right';
237
+ switch (orientation) {
238
+ case 'right-left': return 'left';
239
+ case 'top-bottom': return 'bottom';
240
+ case 'bottom-top': return 'top';
241
+ default: return 'right';
242
+ }
243
+ };
244
+ var labelAnchor = function (position) {
245
+ if (position === 'left')
246
+ return 'end';
247
+ if (position === 'right')
248
+ return 'start';
249
+ return 'middle';
250
+ };
251
+ var labelDx = function (position, size, offset) {
252
+ if (position === 'left')
253
+ return -(size / 2 + offset);
254
+ if (position === 'right')
255
+ return size / 2 + offset;
256
+ return 0;
257
+ };
258
+ var labelDy = function (position, size, offset) {
259
+ if (position === 'top')
260
+ return -(size / 2 + offset);
261
+ if (position === 'bottom')
262
+ return size / 2 + offset;
263
+ return 0;
264
+ };
265
+ var createTreeSeries = function (configuration) {
266
+ var cachedTree = { root: null, nodes: [], links: [] };
267
+ var treeTransform = d3_zoom_1.zoomIdentity;
268
+ var hoveredId;
269
+ var lastSignature = -1;
270
+ var lastWidth = -1;
271
+ var lastHeight = -1;
272
+ var lastLayout = '';
273
+ var lastOrientation = '';
274
+ var resolveSymbolSize = function () { return 12; };
275
+ var resolveEdgeStrokeWidth = function () { return 1.5; };
276
+ var applyLayout = function (width, height) {
277
+ var _a, _b, _c;
278
+ if (!cachedTree.root)
279
+ return;
280
+ var layout = (_a = configuration.layout) !== null && _a !== void 0 ? _a : 'orthogonal';
281
+ var orientation = (_b = configuration.orientation) !== null && _b !== void 0 ? _b : 'left-right';
282
+ var fitPadding = Math.max(0, (_c = configuration.fitPadding) !== null && _c !== void 0 ? _c : 28);
283
+ var layoutRows = cachedTree.nodes.map(function (node) { return ({
284
+ node: node,
285
+ id: node.id,
286
+ parentId: node.parentId
287
+ }); });
288
+ var root = (0, d3_hierarchy_1.stratify)()
289
+ .id(function (row) { return row.id; })
290
+ .parentId(function (row) { var _a; return (_a = row.parentId) !== null && _a !== void 0 ? _a : null; })(layoutRows);
291
+ var hierarchyRoot = root;
292
+ // D3 returns logical breadth/depth coordinates. Convert them here so the
293
+ // renderer and hit-test share one KChart plot-space coordinate system.
294
+ if (layout === 'radial') {
295
+ var radius = Math.max(1, Math.min(width, height) / 2 - fitPadding);
296
+ (0, d3_hierarchy_1.tree)()
297
+ .size([Math.PI * 2, radius])(hierarchyRoot);
298
+ var centerX_1 = width / 2;
299
+ var centerY_1 = height / 2;
300
+ hierarchyRoot.each(function (hierarchyNode) {
301
+ var angle = hierarchyNode.x - Math.PI / 2;
302
+ var node = hierarchyNode.data.node;
303
+ node.x = centerX_1 + Math.cos(angle) * hierarchyNode.y;
304
+ node.y = centerY_1 + Math.sin(angle) * hierarchyNode.y;
305
+ });
306
+ return;
307
+ }
308
+ var horizontal = orientation === 'left-right' || orientation === 'right-left';
309
+ var breadth = Math.max(1, (horizontal ? height : width) - fitPadding * 2);
310
+ var depth = Math.max(1, (horizontal ? width : height) - fitPadding * 2);
311
+ (0, d3_hierarchy_1.tree)()
312
+ .size([breadth, depth])(hierarchyRoot);
313
+ hierarchyRoot.each(function (hierarchyNode) {
314
+ var node = hierarchyNode.data.node;
315
+ if (horizontal) {
316
+ node.x = orientation === 'left-right'
317
+ ? fitPadding + hierarchyNode.y
318
+ : width - fitPadding - hierarchyNode.y;
319
+ node.y = fitPadding + hierarchyNode.x;
320
+ }
321
+ else {
322
+ node.x = fitPadding + hierarchyNode.x;
323
+ node.y = orientation === 'top-bottom'
324
+ ? fitPadding + hierarchyNode.y
325
+ : height - fitPadding - hierarchyNode.y;
326
+ }
327
+ });
328
+ if (cachedTree.nodes.length === 1) {
329
+ cachedTree.nodes[0].x = width / 2;
330
+ cachedTree.nodes[0].y = height / 2;
331
+ }
332
+ };
333
+ return (0, custom_1.createCustomSeries)({
334
+ selector: configuration.selector,
335
+ displayName: configuration.displayName,
336
+ color: configuration.color,
337
+ pointerEvents: 'series',
338
+ render: function (_a) {
339
+ var _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w;
340
+ var group = _a.group, data = _a.data, plotSize = _a.plotSize, color = _a.color, animation = _a.animation;
341
+ hoveredId = undefined;
342
+ var width = Math.max(1, plotSize.width);
343
+ var height = Math.max(1, plotSize.height);
344
+ var layout = (_b = configuration.layout) !== null && _b !== void 0 ? _b : 'orthogonal';
345
+ var orientation = (_c = configuration.orientation) !== null && _c !== void 0 ? _c : 'left-right';
346
+ // Layout is substantially more expensive than SVG style updates, so reuse it
347
+ // until hierarchy fields, plot size, or the selected layout actually changes.
348
+ var signature = resolveTreeDataSignature(data, configuration);
349
+ var layoutChanged = signature !== lastSignature
350
+ || width !== lastWidth
351
+ || height !== lastHeight
352
+ || layout !== lastLayout
353
+ || orientation !== lastOrientation;
354
+ if (layoutChanged) {
355
+ cachedTree = (0, exports.resolveTreeData)(data, configuration);
356
+ applyLayout(width, height);
357
+ lastSignature = signature;
358
+ lastWidth = width;
359
+ lastHeight = height;
360
+ lastLayout = layout;
361
+ lastOrientation = orientation;
362
+ }
363
+ var nodes = cachedTree.nodes;
364
+ var links = cachedTree.links;
365
+ var symbol = (_d = configuration.symbol) !== null && _d !== void 0 ? _d : 'circle';
366
+ var nodeStroke = (_e = configuration.nodeStroke) !== null && _e !== void 0 ? _e : 'rgba(246, 250, 255, 0.9)';
367
+ var edgeBaseColor = (_f = configuration.edgeColor) !== null && _f !== void 0 ? _f : 'rgba(158, 181, 199, 0.62)';
368
+ var palette = ((_g = configuration.palette) === null || _g === void 0 ? void 0 : _g.length) ? configuration.palette : defaultTreePalette;
369
+ var categories = new Map();
370
+ nodes.forEach(function (node) {
371
+ if (node.category && !categories.has(node.category)) {
372
+ categories.set(node.category, categories.size);
373
+ }
374
+ });
375
+ resolveSymbolSize = function (node) {
376
+ var _a;
377
+ return Math.max(2, typeof configuration.symbolSize === 'function'
378
+ ? configuration.symbolSize(node)
379
+ : (_a = configuration.symbolSize) !== null && _a !== void 0 ? _a : 13);
380
+ };
381
+ resolveEdgeStrokeWidth = function (link, index) {
382
+ var _a;
383
+ return Math.max(0.5, typeof configuration.edgeStrokeWidth === 'function'
384
+ ? configuration.edgeStrokeWidth(link, index)
385
+ : (_a = configuration.edgeStrokeWidth) !== null && _a !== void 0 ? _a : 1.45);
386
+ };
387
+ var resolveNodeColor = function (node, index) {
388
+ var _a, _b;
389
+ if (typeof configuration.nodeColor === 'function')
390
+ return configuration.nodeColor(node, index);
391
+ if (configuration.nodeColor)
392
+ return configuration.nodeColor;
393
+ if (node.category)
394
+ return palette[((_a = categories.get(node.category)) !== null && _a !== void 0 ? _a : 0) % palette.length];
395
+ return (_b = configuration.color) !== null && _b !== void 0 ? _b : color;
396
+ };
397
+ var resolveEdgeColor = function (link, index) { return (typeof edgeBaseColor === 'function' ? edgeBaseColor(link, index) : edgeBaseColor); };
398
+ group.selectAll('rect.kchart-tree-interaction')
399
+ .data([undefined])
400
+ .join('rect')
401
+ .attr('class', 'kchart-tree-interaction')
402
+ .attr('width', width)
403
+ .attr('height', height)
404
+ .style('fill', 'transparent')
405
+ .style('pointer-events', 'all')
406
+ .style('cursor', ((_h = configuration.roam) !== null && _h !== void 0 ? _h : 'both') === 'disabled' ? 'default' : 'grab')
407
+ .lower();
408
+ var viewport = group.selectAll('g.kchart-tree-viewport')
409
+ .data([undefined])
410
+ .join('g')
411
+ .attr('class', 'kchart-tree-viewport')
412
+ .attr('transform', treeTransform.toString());
413
+ var edgeLayer = viewport.selectAll('g.kchart-tree-edges')
414
+ .data([undefined])
415
+ .join('g')
416
+ .attr('class', 'kchart-tree-edges');
417
+ var nodeLayer = viewport.selectAll('g.kchart-tree-nodes')
418
+ .data([undefined])
419
+ .join('g')
420
+ .attr('class', 'kchart-tree-nodes');
421
+ var applyInteractionStyle = function () {
422
+ var _a;
423
+ var activeNode = hoveredId ? nodes.find(function (node) { return node.id === hoveredId; }) : undefined;
424
+ var emphasis = (_a = configuration.emphasis) !== null && _a !== void 0 ? _a : 'both';
425
+ var hasFocus = Boolean(activeNode) && emphasis !== 'none';
426
+ var focusIds = activeNode ? createFocusSet(activeNode, emphasis) : new Set();
427
+ edgeLayer.selectAll('path.kchart-tree-link')
428
+ .style('opacity', function (link) {
429
+ var _a, _b;
430
+ if (!hasFocus)
431
+ return (_a = configuration.edgeOpacity) !== null && _a !== void 0 ? _a : 0.64;
432
+ return focusIds.has(link.source.id) && focusIds.has(link.target.id)
433
+ ? 0.96
434
+ : (_b = configuration.dimOpacity) !== null && _b !== void 0 ? _b : 0.12;
435
+ });
436
+ nodeLayer.selectAll('g.kchart-tree-node')
437
+ .style('opacity', function (node) {
438
+ var _a;
439
+ return !hasFocus || focusIds.has(node.id)
440
+ ? 1
441
+ : (_a = configuration.dimOpacity) !== null && _a !== void 0 ? _a : 0.12;
442
+ });
443
+ nodeLayer.selectAll('.kchart-tree-node-symbol')
444
+ .style('stroke', function (node) { return node.id === hoveredId ? '#ffffff' : nodeStroke; })
445
+ .style('stroke-width', function (node) {
446
+ var _a, _b;
447
+ return node.id === hoveredId
448
+ ? Math.max(2.4, (_a = configuration.nodeStrokeWidth) !== null && _a !== void 0 ? _a : 1.4)
449
+ : (_b = configuration.nodeStrokeWidth) !== null && _b !== void 0 ? _b : 1.4;
450
+ });
451
+ };
452
+ edgeLayer.selectAll('path.kchart-tree-link')
453
+ .data(links, function (link) { return link.id; })
454
+ .join('path')
455
+ .attr('class', 'kchart-tree-link')
456
+ .attr('d', function (link) { return resolveTreeLinkPath(link, layout, orientation); })
457
+ .style('fill', 'none')
458
+ .style('stroke', resolveEdgeColor)
459
+ .style('stroke-width', resolveEdgeStrokeWidth)
460
+ .style('stroke-linecap', 'round')
461
+ .style('stroke-linejoin', 'round')
462
+ .style('opacity', ((_j = configuration.edgeOpacity) !== null && _j !== void 0 ? _j : 0.64) * (animation.enabled ? animation.progress : 1))
463
+ .each(function appendLinkTitle(link) {
464
+ var title = this.querySelector('title');
465
+ if (!title) {
466
+ title = document.createElementNS('http://www.w3.org/2000/svg', 'title');
467
+ this.appendChild(title);
468
+ }
469
+ title.textContent = "".concat(link.source.label, " \u2192 ").concat(link.target.label);
470
+ });
471
+ var nodeGroups = nodeLayer.selectAll('g.kchart-tree-node')
472
+ .data(nodes, function (node) { return node.id; })
473
+ .join('g')
474
+ .attr('class', 'kchart-tree-node')
475
+ .attr('transform', function (node) { return "translate(".concat(node.x, ", ").concat(node.y, ")"); })
476
+ .style('cursor', configuration.onNodeClick ? 'pointer' : 'default')
477
+ .on('mouseenter', function (_event, node) {
478
+ hoveredId = node.id;
479
+ applyInteractionStyle();
480
+ })
481
+ .on('mouseleave', function () {
482
+ hoveredId = undefined;
483
+ applyInteractionStyle();
484
+ })
485
+ .on('click', function (event, node) {
486
+ if (!configuration.onNodeClick)
487
+ return;
488
+ event.stopPropagation();
489
+ configuration.onNodeClick({ node: node, event: event });
490
+ });
491
+ nodeGroups.selectAll('circle.kchart-tree-node-symbol')
492
+ .data(function (node) { return symbol === 'circle' ? [node] : []; })
493
+ .join('circle')
494
+ .attr('class', 'kchart-tree-node-symbol')
495
+ .attr('r', function (node) { return (resolveSymbolSize(node) / 2) * (animation.enabled ? animation.progress : 1); })
496
+ .style('fill', resolveNodeColor)
497
+ .style('fill-opacity', (_k = configuration.nodeOpacity) !== null && _k !== void 0 ? _k : 0.94)
498
+ .style('stroke', nodeStroke)
499
+ .style('stroke-width', (_l = configuration.nodeStrokeWidth) !== null && _l !== void 0 ? _l : 1.4);
500
+ nodeGroups.selectAll('rect.kchart-tree-node-symbol')
501
+ .data(function (node) { return symbol === 'square' ? [node] : []; })
502
+ .join('rect')
503
+ .attr('class', 'kchart-tree-node-symbol')
504
+ .attr('x', function (node) { return -resolveSymbolSize(node) / 2; })
505
+ .attr('y', function (node) { return -resolveSymbolSize(node) / 2; })
506
+ .attr('width', function (node) { return resolveSymbolSize(node) * (animation.enabled ? animation.progress : 1); })
507
+ .attr('height', function (node) { return resolveSymbolSize(node) * (animation.enabled ? animation.progress : 1); })
508
+ .attr('rx', 2)
509
+ .style('fill', resolveNodeColor)
510
+ .style('fill-opacity', (_m = configuration.nodeOpacity) !== null && _m !== void 0 ? _m : 0.94)
511
+ .style('stroke', nodeStroke)
512
+ .style('stroke-width', (_o = configuration.nodeStrokeWidth) !== null && _o !== void 0 ? _o : 1.4);
513
+ nodeGroups.selectAll('path.kchart-tree-node-symbol')
514
+ .data(function (node) { return symbol === 'diamond' ? [node] : []; })
515
+ .join('path')
516
+ .attr('class', 'kchart-tree-node-symbol')
517
+ .attr('d', function (node) {
518
+ var half = (resolveSymbolSize(node) / 2) * (animation.enabled ? animation.progress : 1);
519
+ return "M 0 ".concat(-half, " L ").concat(half, " 0 L 0 ").concat(half, " L ").concat(-half, " 0 Z");
520
+ })
521
+ .style('fill', resolveNodeColor)
522
+ .style('fill-opacity', (_p = configuration.nodeOpacity) !== null && _p !== void 0 ? _p : 0.94)
523
+ .style('stroke', nodeStroke)
524
+ .style('stroke-width', (_q = configuration.nodeStrokeWidth) !== null && _q !== void 0 ? _q : 1.4);
525
+ nodeGroups.selectAll('title')
526
+ .data(function (node) { return [node]; })
527
+ .join('title')
528
+ .text(function (node) { return "".concat(node.label).concat(configuration.valueField ? ": ".concat(node.value) : ''); });
529
+ var labelConfiguration = typeof configuration.labels === 'boolean'
530
+ ? { visible: configuration.labels }
531
+ : (_r = configuration.labels) !== null && _r !== void 0 ? _r : { visible: true };
532
+ nodeGroups.selectAll('text.kchart-tree-label')
533
+ .data(function (node) { return labelConfiguration.visible === false ? [] : [node]; })
534
+ .join('text')
535
+ .attr('class', 'kchart-tree-label')
536
+ .attr('x', function (node) {
537
+ var _a, _b;
538
+ var position = resolveLabelPosition(node, (_a = configuration.labelPosition) !== null && _a !== void 0 ? _a : 'auto', layout, orientation, width);
539
+ return labelDx(position, resolveSymbolSize(node), (_b = labelConfiguration.offset) !== null && _b !== void 0 ? _b : 8);
540
+ })
541
+ .attr('y', function (node) {
542
+ var _a, _b;
543
+ var position = resolveLabelPosition(node, (_a = configuration.labelPosition) !== null && _a !== void 0 ? _a : 'auto', layout, orientation, width);
544
+ return labelDy(position, resolveSymbolSize(node), (_b = labelConfiguration.offset) !== null && _b !== void 0 ? _b : 8);
545
+ })
546
+ .attr('text-anchor', function (node) {
547
+ var _a;
548
+ var position = resolveLabelPosition(node, (_a = configuration.labelPosition) !== null && _a !== void 0 ? _a : 'auto', layout, orientation, width);
549
+ return labelAnchor(position);
550
+ })
551
+ .attr('dominant-baseline', function (node) {
552
+ var _a;
553
+ var position = resolveLabelPosition(node, (_a = configuration.labelPosition) !== null && _a !== void 0 ? _a : 'auto', layout, orientation, width);
554
+ return position === 'top' ? 'text-after-edge' : position === 'bottom' ? 'text-before-edge' : 'middle';
555
+ })
556
+ .style('fill', (_s = labelConfiguration.color) !== null && _s !== void 0 ? _s : '#edf3f8')
557
+ .style('font-size', "".concat((_t = labelConfiguration.fontSize) !== null && _t !== void 0 ? _t : 11, "px"))
558
+ .style('font-weight', (_u = labelConfiguration.fontWeight) !== null && _u !== void 0 ? _u : 700)
559
+ .style('pointer-events', 'none')
560
+ .text(function (node) { var _a, _b; return (_b = (_a = labelConfiguration.formatter) === null || _a === void 0 ? void 0 : _a.call(labelConfiguration, node)) !== null && _b !== void 0 ? _b : node.label; });
561
+ applyInteractionStyle();
562
+ var roam = (_v = configuration.roam) !== null && _v !== void 0 ? _v : 'both';
563
+ var canMove = roam === 'move' || roam === 'both';
564
+ var canScale = roam === 'scale' || roam === 'both';
565
+ if (roam === 'disabled') {
566
+ group.on('.zoom', null);
567
+ return;
568
+ }
569
+ var zoomBehavior = (0, d3_zoom_1.zoom)()
570
+ .scaleExtent((_w = configuration.scaleExtent) !== null && _w !== void 0 ? _w : [0.45, 5])
571
+ .filter(function (event) {
572
+ if (event.type === 'wheel')
573
+ return canScale;
574
+ if (event.type === 'mousedown' || event.type === 'touchstart' || event.type === 'touchmove') {
575
+ return canMove;
576
+ }
577
+ return true;
578
+ })
579
+ .on('zoom', function (event) {
580
+ treeTransform = event.transform;
581
+ viewport.attr('transform', treeTransform.toString());
582
+ });
583
+ group.call(zoomBehavior);
584
+ var groupNode = group.node();
585
+ if (groupNode) {
586
+ groupNode.__zoom = treeTransform;
587
+ }
588
+ },
589
+ tooltip: function (_a) {
590
+ var _b, _c;
591
+ var mouseX = _a.mouseX, mouseY = _a.mouseY;
592
+ var layout = (_b = configuration.layout) !== null && _b !== void 0 ? _b : 'orthogonal';
593
+ var orientation = (_c = configuration.orientation) !== null && _c !== void 0 ? _c : 'left-right';
594
+ var activeNode;
595
+ var activeDistance = Number.POSITIVE_INFINITY;
596
+ cachedTree.nodes.forEach(function (node) {
597
+ var x = treeTransform.applyX(node.x);
598
+ var y = treeTransform.applyY(node.y);
599
+ var radius = (resolveSymbolSize(node) / 2) * treeTransform.k + 8;
600
+ var distance = Math.hypot(mouseX - x, mouseY - y);
601
+ if (distance <= radius && distance < activeDistance) {
602
+ activeNode = node;
603
+ activeDistance = distance;
604
+ }
605
+ });
606
+ if (activeNode) {
607
+ return {
608
+ data: activeNode.row,
609
+ x: treeTransform.applyX(activeNode.x),
610
+ y: treeTransform.applyY(activeNode.y),
611
+ distance: activeDistance,
612
+ color: configuration.color,
613
+ html: "<strong>".concat(escapeTooltipHtml(activeNode.label), "</strong>").concat(configuration.valueField ? "<br/>Value: ".concat(activeNode.value) : '').concat(activeNode.category ? "<br/>Category: ".concat(escapeTooltipHtml(activeNode.category)) : '')
614
+ };
615
+ }
616
+ // Roaming transforms SVG visually; applying the same transform to hit-test
617
+ // geometry keeps node/link tooltips aligned after pan and zoom.
618
+ var activeLink;
619
+ var linkDistance = Number.POSITIVE_INFINITY;
620
+ cachedTree.links.forEach(function (link, index) {
621
+ var transformedLink = __assign(__assign({}, link), { source: __assign(__assign({}, link.source), { x: treeTransform.applyX(link.source.x), y: treeTransform.applyY(link.source.y) }), target: __assign(__assign({}, link.target), { x: treeTransform.applyX(link.target.x), y: treeTransform.applyY(link.target.y) }) });
622
+ var distance = distanceToLink(mouseX, mouseY, transformedLink, layout, orientation);
623
+ if (distance <= Math.max(7, resolveEdgeStrokeWidth(link, index) * treeTransform.k + 5) && distance < linkDistance) {
624
+ activeLink = link;
625
+ linkDistance = distance;
626
+ }
627
+ });
628
+ if (!activeLink)
629
+ return undefined;
630
+ return {
631
+ data: activeLink.row,
632
+ x: treeTransform.applyX((activeLink.source.x + activeLink.target.x) / 2),
633
+ y: treeTransform.applyY((activeLink.source.y + activeLink.target.y) / 2),
634
+ distance: linkDistance,
635
+ color: typeof configuration.edgeColor === 'string' ? configuration.edgeColor : undefined,
636
+ html: "<strong>".concat(escapeTooltipHtml(activeLink.source.label), " \u2192 ").concat(escapeTooltipHtml(activeLink.target.label), "</strong>")
637
+ };
638
+ },
639
+ destroy: function (_a) {
640
+ var seriesGroup = _a.seriesGroup;
641
+ seriesGroup.selectAll("g.".concat(configuration.selector)).on('.zoom', null);
642
+ cachedTree = { root: null, nodes: [], links: [] };
643
+ treeTransform = d3_zoom_1.zoomIdentity;
644
+ hoveredId = undefined;
645
+ lastSignature = -1;
646
+ lastWidth = -1;
647
+ lastHeight = -1;
648
+ lastLayout = '';
649
+ lastOrientation = '';
650
+ }
651
+ });
652
+ };
653
+ exports.createTreeSeries = createTreeSeries;
654
+ //# sourceMappingURL=svg-tree.js.map