@fluentui/react-charting 5.14.43 → 5.15.1

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.
Files changed (39) hide show
  1. package/CHANGELOG.json +37 -1
  2. package/CHANGELOG.md +21 -2
  3. package/dist/react-charting.js +1 -1
  4. package/dist/react-charting.min.js +1 -1
  5. package/lib/components/SankeyChart/SankeyChart.base.d.ts +79 -2
  6. package/lib/components/SankeyChart/SankeyChart.base.js +599 -32
  7. package/lib/components/SankeyChart/SankeyChart.base.js.map +1 -1
  8. package/lib/components/SankeyChart/SankeyChart.styles.js +39 -2
  9. package/lib/components/SankeyChart/SankeyChart.styles.js.map +1 -1
  10. package/lib/components/SankeyChart/SankeyChart.types.d.ts +20 -0
  11. package/lib/components/SankeyChart/SankeyChart.types.js.map +1 -1
  12. package/lib/types/IDataPoint.d.ts +5 -1
  13. package/lib/types/IDataPoint.js.map +1 -1
  14. package/lib/version.js +1 -1
  15. package/lib/version.js.map +1 -1
  16. package/lib-amd/components/SankeyChart/SankeyChart.base.d.ts +79 -2
  17. package/lib-amd/components/SankeyChart/SankeyChart.base.js +592 -31
  18. package/lib-amd/components/SankeyChart/SankeyChart.base.js.map +1 -1
  19. package/lib-amd/components/SankeyChart/SankeyChart.styles.js +38 -3
  20. package/lib-amd/components/SankeyChart/SankeyChart.styles.js.map +1 -1
  21. package/lib-amd/components/SankeyChart/SankeyChart.types.d.ts +20 -0
  22. package/lib-amd/components/SankeyChart/SankeyChart.types.js.map +1 -1
  23. package/lib-amd/types/IDataPoint.d.ts +5 -1
  24. package/lib-amd/types/IDataPoint.js.map +1 -1
  25. package/lib-amd/version.js +1 -1
  26. package/lib-amd/version.js.map +1 -1
  27. package/lib-commonjs/components/SankeyChart/SankeyChart.base.d.ts +79 -2
  28. package/lib-commonjs/components/SankeyChart/SankeyChart.base.js +597 -30
  29. package/lib-commonjs/components/SankeyChart/SankeyChart.base.js.map +1 -1
  30. package/lib-commonjs/components/SankeyChart/SankeyChart.styles.js +39 -2
  31. package/lib-commonjs/components/SankeyChart/SankeyChart.styles.js.map +1 -1
  32. package/lib-commonjs/components/SankeyChart/SankeyChart.types.d.ts +20 -0
  33. package/lib-commonjs/components/SankeyChart/SankeyChart.types.js.map +1 -1
  34. package/lib-commonjs/types/IDataPoint.d.ts +5 -1
  35. package/lib-commonjs/types/IDataPoint.js.map +1 -1
  36. package/lib-commonjs/version.js +1 -1
  37. package/lib-commonjs/version.js.map +1 -1
  38. package/package.json +5 -5
  39. package/src/components/SankeyChart/SankeyChart.types.ts +26 -0
@@ -5,15 +5,90 @@ var tslib_1 = require("tslib");
5
5
  var React = require("react");
6
6
  var Utilities_1 = require("@fluentui/react/lib/Utilities");
7
7
  var d3Sankey = require("d3-sankey");
8
+ var d3_shape_1 = require("d3-shape");
9
+ var d3_array_1 = require("d3-array");
10
+ var index_1 = require("../../index");
11
+ var Callout_1 = require("@fluentui/react/lib/Callout");
12
+ var d3_selection_1 = require("d3-selection");
13
+ var react_focus_1 = require("@fluentui/react-focus");
8
14
  var getClassNames = Utilities_1.classNamesFunction();
15
+ var PADDING_PERCENTAGE = 0.3;
16
+ var NON_SELECTED_NODE_AND_STREAM_COLOR = '#757575';
17
+ var DEFAULT_NODE_COLORS = [
18
+ { fillColor: '#00758F', borderColor: '#002E39' },
19
+ { fillColor: '#77004D', borderColor: '#43002C' },
20
+ { fillColor: '#4F6BED', borderColor: '#3B52B4' },
21
+ { fillColor: '#937600', borderColor: '#6D5700' },
22
+ { fillColor: '#286EA8', borderColor: '#00457E' },
23
+ { fillColor: '#A43FB1', borderColor: '#7C158A' },
24
+ { fillColor: '#CC3595', borderColor: '#7F215D' },
25
+ { fillColor: '#0E7878', borderColor: '#004E4E' },
26
+ { fillColor: '#8764B8', borderColor: '#4B3867' },
27
+ { fillColor: '#9C663F', borderColor: '#6D4123' },
28
+ ];
29
+ var MIN_HEIGHT_FOR_DOUBLINE_TYPE = 36;
30
+ var MIN_HEIGHT_FOR_TYPE = 24;
31
+ var REST_STREAM_OPACITY = 1;
32
+ var NON_SELECTED_OPACITY = 1;
33
+ var SELECTED_STREAM_OPACITY = 0.3;
34
+ var NON_SELECTED_STREAM_BORDER_OPACITY = 0.5;
35
+ var DEFAULT_TEXT_COLOR = '#323130';
36
+ var NON_SELECTED_TEXT_COLOR = '#FFFFFF';
9
37
  var SankeyChartBase = /** @class */ (function (_super) {
10
38
  tslib_1.__extends(SankeyChartBase, _super);
11
39
  function SankeyChartBase(props) {
12
40
  var _this = _super.call(this, props) || this;
41
+ _this._isRtl = Utilities_1.getRTL();
42
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
43
+ _this._onCloseCallout = function () {
44
+ _this.setState({
45
+ isCalloutVisible: false,
46
+ refSelected: undefined,
47
+ descriptionMessage: '',
48
+ });
49
+ };
50
+ _this._onBlur = function () {
51
+ /**/
52
+ };
53
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
54
+ _this._fillNodeColors = function (singleNode) {
55
+ if (!_this.state.selectedState) {
56
+ return singleNode.color;
57
+ }
58
+ else if (_this.state.selectedState && _this.state.selectedNodes.has(singleNode.index) && _this.state.selectedNode) {
59
+ return _this.state.selectedNode.color;
60
+ }
61
+ else if (_this.state.selectedState && !_this.state.selectedNode) {
62
+ return singleNode.color;
63
+ }
64
+ };
65
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
66
+ _this._fillNodeBorder = function (singleNode) {
67
+ if (!_this.state.selectedState) {
68
+ return singleNode.borderColor;
69
+ }
70
+ else if (_this.state.selectedState && _this.state.selectedNodes.has(singleNode.index) && _this.state.selectedNode) {
71
+ return _this.state.selectedNode.borderColor;
72
+ }
73
+ else if (_this.state.selectedState && _this.state.selectedNodes.has(singleNode.index)) {
74
+ return singleNode.borderColor;
75
+ }
76
+ return singleNode.borderColor;
77
+ };
13
78
  _this.state = {
14
- containerHeight: 0,
15
- containerWidth: 0,
79
+ containerHeight: 468,
80
+ containerWidth: 912,
81
+ selectedState: false,
82
+ selectedLinks: new Set(),
83
+ selectedNodes: new Set(),
84
+ shouldOverflow: false,
85
+ isCalloutVisible: false,
16
86
  };
87
+ _this._calloutId = Utilities_1.getId('callout');
88
+ _this._linkId = Utilities_1.getId('link');
89
+ _this._margins = { top: 36, right: 48, bottom: 32, left: 48 };
90
+ _this._preRenderLayout();
91
+ _this._normalizeData = Utilities_1.memoizeFunction(function (data) { return _this._normalizeSankeyData(data); });
17
92
  return _this;
18
93
  }
19
94
  SankeyChartBase.prototype.componentDidMount = function () {
@@ -37,58 +112,491 @@ var SankeyChartBase = /** @class */ (function (_super) {
37
112
  pathColor: pathColor,
38
113
  className: className,
39
114
  });
40
- var margin = { top: 10, right: 0, bottom: 10, left: 0 };
41
- var width = this.state.containerWidth - margin.left - margin.right;
42
- var height = this.state.containerHeight - margin.top - margin.bottom > 0
43
- ? this.state.containerHeight - margin.top - margin.bottom
44
- : 0;
45
- var sankey = d3Sankey
115
+ // We are using the this._margins.left and this._margins.top in sankey extent while constructing the layout
116
+ var _b = this._preRenderLayout(), height = _b.height, width = _b.width;
117
+ this._normalizeData(this.props.data.SankeyChartData);
118
+ var nodePadding = 8;
119
+ nodePadding = this._adjustPadding(this._sankey, height - 6, this._nodesInColumn);
120
+ this._sankey.nodePadding(nodePadding);
121
+ this._sankey(this.props.data.SankeyChartData);
122
+ this._populateNodeActualValue(this.props.data.SankeyChartData);
123
+ this._assignNodeColors();
124
+ var nodeData = this._createNodes(width);
125
+ var linkData = this._createLinks();
126
+ var calloutProps = {
127
+ isCalloutVisible: this.state.isCalloutVisible,
128
+ directionalHint: Callout_1.DirectionalHint.topAutoEdge,
129
+ id: "toolTip" + this._calloutId,
130
+ target: this.state.refSelected,
131
+ color: this.state.color,
132
+ XValue: this.state.xCalloutValue,
133
+ YValue: this.state.yCalloutValue ? this.state.yCalloutValue : this.state.dataForHoverCard,
134
+ descriptionMessage: this.state.descriptionMessage,
135
+ isBeakVisible: false,
136
+ gapSpace: 15,
137
+ onDismiss: this._onCloseCallout,
138
+ className: this._classNames.calloutContentRoot,
139
+ preventDismissOnLostFocus: true,
140
+ };
141
+ return (React.createElement("div", { className: this._classNames.root, role: 'presentation', ref: function (rootElem) { return (_this.chartContainer = rootElem); }, onMouseLeave: this._onCloseCallout },
142
+ React.createElement(react_focus_1.FocusZone, { direction: react_focus_1.FocusZoneDirection.bidirectional, isCircularNavigation: true, handleTabKey: react_focus_1.FocusZoneTabbableElements.all },
143
+ React.createElement("svg", { width: width, height: height, id: Utilities_1.getId('sankeyChart') },
144
+ React.createElement("g", { className: this._classNames.links, strokeOpacity: 1 }, linkData),
145
+ React.createElement("g", { className: this._classNames.nodes }, nodeData),
146
+ calloutProps.isCalloutVisible && (React.createElement(Callout_1.Callout, tslib_1.__assign({}, calloutProps),
147
+ React.createElement(index_1.ChartHoverCard, { XValue: calloutProps.XValue, YValue: calloutProps.YValue, color: calloutProps.color, descriptionMessage: calloutProps.descriptionMessage ? calloutProps.descriptionMessage : '' })))))));
148
+ };
149
+ SankeyChartBase.prototype._preRenderLayout = function () {
150
+ var width = this.state.containerWidth - this._margins.right;
151
+ var height = this.state.containerHeight - this._margins.bottom > 0 ? this.state.containerHeight - this._margins.bottom : 0;
152
+ this._sankey = d3Sankey
46
153
  .sankey()
47
- .nodeWidth(5)
48
- .nodePadding(6)
154
+ .nodeWidth(124)
49
155
  .extent([
50
- [1, 1],
156
+ [this._margins.left, this._margins.top],
51
157
  [width - 1, height - 6],
52
- ]);
53
- sankey(this.props.data.SankeyChartData);
54
- var nodeData = this._createNodes(width);
55
- var linkData = this._createLinks();
56
- return (React.createElement("div", { className: this._classNames.root, role: 'presentation', ref: function (rootElem) { return (_this.chartContainer = rootElem); } },
57
- React.createElement("svg", { width: width, height: height, id: Utilities_1.getId('sankeyChart') },
58
- React.createElement("g", { className: this._classNames.nodes }, nodeData),
59
- React.createElement("g", { className: this._classNames.links, strokeOpacity: 0.2 }, linkData))));
158
+ ])
159
+ .nodeAlign(this._isRtl ? d3Sankey.sankeyRight : d3Sankey.sankeyJustify);
160
+ return { height: height, width: width };
161
+ };
162
+ /**
163
+ * This is used for calculating the node non normalized value based on link non normalized value.
164
+ *
165
+ */
166
+ SankeyChartBase.prototype._populateNodeActualValue = function (data) {
167
+ data.links.forEach(function (link) {
168
+ if (!link.unnormalizedValue) {
169
+ link.unnormalizedValue = link.value;
170
+ }
171
+ });
172
+ data.nodes.forEach(function (node) {
173
+ node.actualValue = Math.max(d3_array_1.sum(node.sourceLinks, function (link) { return link.unnormalizedValue; }), d3_array_1.sum(node.targetLinks, function (link) { return link.unnormalizedValue; }));
174
+ });
175
+ };
176
+ SankeyChartBase.prototype._normalizeSankeyData = function (data) {
177
+ this._nodesInColumn = this._populateNodeInColumns(data, this._sankey);
178
+ this._adjustOnePercentHeightNodes(this._nodesInColumn);
179
+ };
180
+ /**
181
+ *
182
+ * This is used to group nodes by column index.
183
+ */
184
+ SankeyChartBase.prototype._populateNodeInColumns = function (graph, sankey) {
185
+ sankey(graph);
186
+ var nodesInColumn = {};
187
+ graph.nodes.forEach(function (node) {
188
+ var columnId = node.layer;
189
+ if (nodesInColumn[columnId]) {
190
+ nodesInColumn[columnId].push(node);
191
+ }
192
+ else {
193
+ nodesInColumn[columnId] = [node];
194
+ }
195
+ });
196
+ return nodesInColumn;
197
+ };
198
+ /**
199
+ * This is used to normalize the nodes value whose value is less than 1% of the total column value.
200
+ *
201
+ */
202
+ SankeyChartBase.prototype._adjustOnePercentHeightNodes = function (nodesInColumn) {
203
+ var _this = this;
204
+ var totalColumnValue = Object.values(nodesInColumn).map(function (column) {
205
+ return d3_array_1.sum(column, function (node) { return node.value; });
206
+ });
207
+ totalColumnValue.forEach(function (columnValue, index) {
208
+ var totalPercentage = 0;
209
+ nodesInColumn[index].forEach(function (node) {
210
+ var nodePercentage = (node.value / columnValue) * 100;
211
+ node.actualValue = node.value;
212
+ //if the value is less than 1% then we are making it as 1% of total .
213
+ if (nodePercentage < 1) {
214
+ node.value = 0.01 * columnValue;
215
+ totalPercentage = totalPercentage + 1;
216
+ }
217
+ else {
218
+ totalPercentage = totalPercentage + nodePercentage;
219
+ }
220
+ });
221
+ //since we have adjusted the value to be 1% but we need to keep the sum of the percentage value under 100.
222
+ var scalingRatio = totalPercentage !== 0 ? totalPercentage / 100 : 1;
223
+ if (scalingRatio > 1) {
224
+ nodesInColumn[index].forEach(function (node) {
225
+ node.value = node.value / scalingRatio;
226
+ _this._changeColumnValue(node, node.actualValue, node.value);
227
+ });
228
+ }
229
+ });
230
+ };
231
+ /**
232
+ *
233
+ * This is used for normalizing each links value for reflecting the normalized node value.
234
+ */
235
+ SankeyChartBase.prototype._changeColumnValue = function (node, originalValue, normalizedValue) {
236
+ node.sourceLinks.forEach(function (link) {
237
+ link.unnormalizedValue = link.value;
238
+ var linkRatio = link.value / originalValue;
239
+ link.value = normalizedValue * linkRatio;
240
+ });
241
+ node.targetLinks.forEach(function (link) {
242
+ link.unnormalizedValue = link.value;
243
+ var linkRatio = link.value / originalValue;
244
+ link.value = normalizedValue * linkRatio;
245
+ });
246
+ };
247
+ /**
248
+ *
249
+ * This is used to introduce dynamic padding for cases where the number of nodes in a column is huge
250
+ * so that we maintain a node to space ratio for such columns as if we fail to do so the
251
+ * chart is devoid of nodes and only shows links.
252
+ */
253
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
254
+ SankeyChartBase.prototype._adjustPadding = function (sankey, height, nodesInColumn) {
255
+ var padding = this._sankey.nodePadding();
256
+ Object.values(nodesInColumn).forEach(function (column) {
257
+ var minPadding = PADDING_PERCENTAGE * height;
258
+ var toatlPaddingInColumn = height - d3_array_1.sum(column, function (node) { return node.y1 - node.y0; });
259
+ if (minPadding < toatlPaddingInColumn) {
260
+ //Here we are calculating the min of default and calculated padding, we will not increase the padding
261
+ //in any scenario.
262
+ padding = Math.min(padding, minPadding / (column.length - 1));
263
+ }
264
+ });
265
+ return padding;
60
266
  };
61
267
  SankeyChartBase.prototype._createLinks = function () {
268
+ var _this = this;
62
269
  var links = [];
63
270
  if (this.props.data.SankeyChartData) {
64
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
65
271
  this.props.data.SankeyChartData.links.forEach(function (singleLink, index) {
66
- var path = d3Sankey.sankeyLinkHorizontal();
67
- var pathValue = path(singleLink);
68
- var link = (React.createElement("path", { key: index, d: pathValue ? pathValue : undefined, strokeWidth: Math.max(1, singleLink.width), id: Utilities_1.getId('link') },
69
- React.createElement("title", null,
70
- React.createElement("text", null, singleLink.source.name + ' → ' + singleLink.target.name + '\n' + singleLink.value))));
272
+ var onMouseOut = function () {
273
+ _this._onStreamLeave(singleLink);
274
+ };
275
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
276
+ var data = function (d) {
277
+ return [
278
+ { x: d.source.x1, y0: d.y0 + d.width / 2, y1: d.y0 - d.width / 2 },
279
+ { x: d.target.x0, y0: d.y1 + d.width / 2, y1: d.y1 - d.width / 2 },
280
+ ];
281
+ };
282
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
283
+ var dataPoints = data(singleLink);
284
+ var linkArea = d3_shape_1.area()
285
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
286
+ .x(function (p) { return p.x; })
287
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
288
+ .y0(function (p) { return p.y0; })
289
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
290
+ .y1(function (p) { return p.y1; })
291
+ .curve(d3_shape_1.curveBumpX);
292
+ var gradientUrl = "url(#gradient-" + _this._linkId + "-" + index + ")";
293
+ var link = (React.createElement("g", { key: _this._linkId + "-" + index },
294
+ React.createElement("defs", null,
295
+ React.createElement("linearGradient", { id: "gradient-" + _this._linkId + "-" + index, x1: "0%", y1: "0%", x2: "100%", y2: "0%" },
296
+ React.createElement("stop", { offset: "0", stopColor: singleLink.source.color }),
297
+ React.createElement("stop", { offset: "100%", stopColor: singleLink.target.color }))),
298
+ React.createElement("path", { d: linkArea(dataPoints), id: Utilities_1.getId('link'), fill: _this._fillStreamColors(singleLink, gradientUrl), stroke: _this._fillStreamBorder(singleLink, gradientUrl), strokeWidth: "2", strokeOpacity: _this._getOpacityStreamBorder(singleLink), onMouseOver: _this._onStreamHover.bind(_this, singleLink), onMouseOut: onMouseOut, onFocus: _this._onFocusLink.bind(_this, singleLink), onBlur: _this._onBlur, fillOpacity: _this._getOpacityStream(singleLink), "data-is-focusable": true, "aria-label": 'link from' +
299
+ singleLink.source.name +
300
+ 'to' +
301
+ singleLink.target.name +
302
+ 'with weight' +
303
+ singleLink.unnormalizedValue, role: "img" })));
71
304
  links.push(link);
72
305
  });
73
306
  }
74
307
  return links;
75
308
  };
76
309
  SankeyChartBase.prototype._createNodes = function (width) {
310
+ var _this = this;
77
311
  var nodes = [];
78
312
  if (this.props.data.SankeyChartData) {
79
313
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
80
314
  this.props.data.SankeyChartData.nodes.forEach(function (singleNode, index) {
315
+ var onMouseOut = function () {
316
+ _this._onLeave(singleNode);
317
+ };
81
318
  var height = singleNode.y1 - singleNode.y0 > 0 ? singleNode.y1 - singleNode.y0 : 0;
82
- var node = (React.createElement("g", { id: Utilities_1.getId('nodeGElement'), key: index },
83
- React.createElement("rect", { x: singleNode.x0, y: singleNode.y0, height: height, width: singleNode.x1 - singleNode.x0, fill: singleNode.color, id: Utilities_1.getId('nodeBar') }),
84
- React.createElement("text", { x: singleNode.x0 < width / 2 ? singleNode.x1 + 6 : singleNode.x0 - 6, y: (singleNode.y1 + singleNode.y0) / 2, dy: '0.35em', textAnchor: singleNode.x0 < width / 2 ? 'start' : 'end' }, singleNode.name),
85
- React.createElement("title", null,
86
- React.createElement("text", null, singleNode.name + '\n' + singleNode.value))));
319
+ var padding = 8;
320
+ var textLengthForNodeWeight = 0;
321
+ // If the nodeWeight is in the same line as node description an extra padding
322
+ // of 6 px is required between node description and node weight.
323
+ if (height < MIN_HEIGHT_FOR_DOUBLINE_TYPE) {
324
+ padding = padding + 6;
325
+ var tspan = d3_selection_1.select('.nodeName').append('text').attr('class', 'tempText').append('tspan').text(null);
326
+ tspan.text(singleNode.actualValue);
327
+ if (tspan.node() !== null) {
328
+ textLengthForNodeWeight = tspan.node().getComputedTextLength();
329
+ padding = padding + textLengthForNodeWeight;
330
+ }
331
+ tspan.text(null);
332
+ d3_selection_1.selectAll('.tempText').remove();
333
+ }
334
+ // Since the total width of the node is 124 and we are giving margin of 8px from the left .
335
+ //So the actual value on which it will be truncated is 124-8=116.
336
+ var truncatedname = _this._truncateText(singleNode.name, 116, padding);
337
+ var isTruncated = truncatedname.slice(-3) === '...';
338
+ var id = Utilities_1.getId('tooltip');
339
+ var div = d3_selection_1.select('body')
340
+ .append('div')
341
+ .attr('id', id)
342
+ .attr('class', _this._classNames.toolTip)
343
+ .style('opacity', 0);
344
+ var nodeId = Utilities_1.getId('nodeBar');
345
+ var node = (React.createElement("g", { key: index, id: Utilities_1.getId('nodeGElement') },
346
+ React.createElement("rect", { x: singleNode.x0, y: singleNode.y0, height: height, width: singleNode.x1 - singleNode.x0, fill: _this._fillNodeColors(singleNode), id: nodeId, onMouseOver: _this._onHover.bind(_this, singleNode), onMouseOut: onMouseOut, onFocus: _this._onCloseCallout.bind(_this), stroke: _this._fillNodeBorder(singleNode), strokeWidth: "2", opacity: "1", "data-is-focusable": true, "aria-label": 'node' + ("" + singleNode.name) + 'with weight' + ("" + singleNode.actualValue), role: "img" }),
347
+ singleNode.y1 - singleNode.y0 > MIN_HEIGHT_FOR_TYPE && (React.createElement("g", { className: _this._classNames.nodeTextContainer },
348
+ React.createElement("g", { className: "nodeName" },
349
+ React.createElement("text", { id: nodeId + "-name", x: singleNode.x0, y: singleNode.y0, dy: '1.2em', dx: '0.4em', textAnchor: _this._isRtl ? 'end' : 'start', fontWeight: "regular", "aria-hidden": "true", fill: !(!_this.state.selectedState ||
350
+ (_this.state.selectedState &&
351
+ _this.state.selectedNodes.has(singleNode.index) &&
352
+ _this.state.selectedNode) ||
353
+ (_this.state.selectedState && !_this.state.selectedNode))
354
+ ? DEFAULT_TEXT_COLOR
355
+ : NON_SELECTED_TEXT_COLOR, fontSize: 10, onMouseOver: _this._showTooltip.bind(_this, singleNode.name, isTruncated, div), onMouseOut: _this._hideTooltip.bind(_this, div) }, truncatedname)),
356
+ React.createElement("text", { x: height > MIN_HEIGHT_FOR_DOUBLINE_TYPE ? singleNode.x0 : singleNode.x1 - textLengthForNodeWeight - 8, y: singleNode.y0, dy: height > MIN_HEIGHT_FOR_DOUBLINE_TYPE ? '2em' : '1em', dx: height > MIN_HEIGHT_FOR_DOUBLINE_TYPE ? '0.4em' : '0em', textAnchor: _this._isRtl ? 'end' : 'start', fontWeight: "bold", "aria-hidden": "true", fill: !(!_this.state.selectedState ||
357
+ (_this.state.selectedState &&
358
+ _this.state.selectedNodes.has(singleNode.index) &&
359
+ _this.state.selectedNode) ||
360
+ (_this.state.selectedState && !_this.state.selectedNode))
361
+ ? DEFAULT_TEXT_COLOR
362
+ : NON_SELECTED_TEXT_COLOR, fontSize: 14 }, singleNode.actualValue)))));
87
363
  nodes.push(node);
88
364
  });
89
365
  return nodes;
90
366
  }
91
367
  };
368
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
369
+ SankeyChartBase.prototype._onLeave = function (singleNode) {
370
+ if (this.state.selectedState) {
371
+ this.setState({
372
+ selectedState: false,
373
+ selectedNodes: new Set(),
374
+ selectedLinks: new Set(),
375
+ selectedNode: undefined,
376
+ });
377
+ }
378
+ };
379
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
380
+ SankeyChartBase.prototype._onHover = function (singleNode, mouseEvent) {
381
+ mouseEvent.persist();
382
+ this._onCloseCallout();
383
+ if (!this.state.selectedState) {
384
+ var selectedLinks = this._getSelectedLinks(singleNode);
385
+ var selectedNodes = this._getSelectedNodes(selectedLinks);
386
+ selectedNodes.push(singleNode);
387
+ this.setState({
388
+ selectedState: true,
389
+ selectedNodes: new Set(Array.from(selectedNodes).map(function (node) { return node.index; })),
390
+ selectedLinks: new Set(Array.from(selectedLinks).map(function (link) { return link.index; })),
391
+ selectedNode: singleNode,
392
+ refSelected: mouseEvent,
393
+ isCalloutVisible: singleNode.y1 - singleNode.y0 < MIN_HEIGHT_FOR_TYPE,
394
+ color: singleNode.color,
395
+ xCalloutValue: singleNode.name,
396
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
397
+ yCalloutValue: singleNode.actualValue,
398
+ });
399
+ }
400
+ };
401
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
402
+ SankeyChartBase.prototype._onStreamHover = function (singleLink, mouseEvent) {
403
+ mouseEvent.persist();
404
+ this._onCloseCallout();
405
+ if (!this.state.selectedState) {
406
+ var _a = this._getSelectedLinksforStreamHover(singleLink), selectedLinks = _a.selectedLinks, selectedNodes = _a.selectedNodes;
407
+ this.setState({
408
+ selectedState: true,
409
+ selectedNodes: new Set(Array.from(selectedNodes).map(function (node) { return node.index; })),
410
+ selectedLinks: new Set(Array.from(selectedLinks).map(function (link) { return link.index; })),
411
+ refSelected: mouseEvent,
412
+ selectedLink: singleLink,
413
+ isCalloutVisible: true,
414
+ color: singleLink.source.color,
415
+ xCalloutValue: singleLink.target.name,
416
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
417
+ yCalloutValue: singleLink.source.actualValue,
418
+ descriptionMessage: 'from ' + singleLink.source.name,
419
+ });
420
+ }
421
+ };
422
+ SankeyChartBase.prototype._onStreamLeave = function (singleLink) {
423
+ if (this.state.selectedState) {
424
+ this.setState({
425
+ selectedState: false,
426
+ selectedNodes: new Set(),
427
+ selectedLinks: new Set(),
428
+ selectedLink: undefined,
429
+ });
430
+ }
431
+ };
432
+ SankeyChartBase.prototype._onFocusLink = function (singleLink, element) {
433
+ element.persist();
434
+ this._onCloseCallout();
435
+ this.setState({
436
+ refSelected: element.currentTarget,
437
+ selectedLink: singleLink,
438
+ isCalloutVisible: true,
439
+ color: singleLink.source.color,
440
+ xCalloutValue: singleLink.target.name,
441
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
442
+ yCalloutValue: singleLink.source.actualValue,
443
+ descriptionMessage: 'from ' + singleLink.source.name,
444
+ });
445
+ };
446
+ /**
447
+ * This is used to assign node fillcolors and borderColor cyclically when the user doesnt
448
+ * provide color to individual node.
449
+ */
450
+ SankeyChartBase.prototype._assignNodeColors = function () {
451
+ var colors;
452
+ var borders;
453
+ if (this.props.colorsForNodes && this.props.borderColorsForNodes) {
454
+ colors = this.props.colorsForNodes;
455
+ borders = this.props.borderColorsForNodes;
456
+ }
457
+ else {
458
+ colors = DEFAULT_NODE_COLORS.map(function (color) { return color.fillColor; });
459
+ borders = DEFAULT_NODE_COLORS.map(function (color) { return color.borderColor; });
460
+ }
461
+ var currentIndex = 0;
462
+ this.props.data.SankeyChartData.nodes.forEach(function (node) {
463
+ if (!node.color && !node.borderColor) {
464
+ node.color = colors[currentIndex];
465
+ node.borderColor = borders[currentIndex];
466
+ }
467
+ else if (node.color && !node.borderColor) {
468
+ node.borderColor = '#757575';
469
+ }
470
+ else if (node.borderColor && !node.color) {
471
+ node.color = '#F5F5F5';
472
+ }
473
+ currentIndex = (currentIndex + 1) % colors.length;
474
+ });
475
+ };
476
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
477
+ SankeyChartBase.prototype._fillStreamColors = function (singleLink, gradientUrl) {
478
+ if (this.state.selectedState && this.state.selectedLinks.has(singleLink.index) && this.state.selectedNode) {
479
+ return this.state.selectedNode.color;
480
+ }
481
+ else if (this.state.selectedState && this.state.selectedLinks.has(singleLink.index)) {
482
+ return gradientUrl;
483
+ }
484
+ };
485
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
486
+ SankeyChartBase.prototype._fillStreamBorder = function (singleLink, gradientUrl) {
487
+ if (!this.state.selectedState) {
488
+ return NON_SELECTED_NODE_AND_STREAM_COLOR;
489
+ }
490
+ else if (this.state.selectedState && this.state.selectedLinks.has(singleLink.index) && this.state.selectedNode) {
491
+ return this.state.selectedNode.borderColor;
492
+ }
493
+ else if (this.state.selectedState && this.state.selectedLinks.has(singleLink.index)) {
494
+ return gradientUrl;
495
+ }
496
+ return NON_SELECTED_NODE_AND_STREAM_COLOR;
497
+ };
498
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
499
+ SankeyChartBase.prototype._getSelectedNodes = function (selectedLinks) {
500
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
501
+ var nodes = [];
502
+ selectedLinks.forEach(function (link) {
503
+ nodes.push(link.target);
504
+ if (nodes.indexOf(link.source) === -1) {
505
+ nodes.push(link.source);
506
+ }
507
+ });
508
+ return nodes;
509
+ };
510
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
511
+ SankeyChartBase.prototype._getSelectedLinks = function (singleNode) {
512
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, no-array-constructor
513
+ var q = new Array();
514
+ var finalLinks = new Set();
515
+ singleNode.sourceLinks.forEach(function (link) {
516
+ q.push(link);
517
+ finalLinks.add(link);
518
+ });
519
+ while (q.length > 0) {
520
+ var poppedLink = q.shift();
521
+ var node = poppedLink.target;
522
+ if (node && node.sourceLinks) {
523
+ node.sourceLinks.forEach(function (link) {
524
+ finalLinks.add(link);
525
+ q.push(link);
526
+ });
527
+ }
528
+ }
529
+ if (singleNode.targetLinks) {
530
+ singleNode.targetLinks.forEach(function (link) {
531
+ q.push(link);
532
+ finalLinks.add(link);
533
+ });
534
+ }
535
+ while (q.length > 0) {
536
+ var poppedLink = q.shift();
537
+ var node = poppedLink.source;
538
+ if (node && node.targetLinks) {
539
+ node.targetLinks.forEach(function (link) {
540
+ finalLinks.add(link);
541
+ q.push(link);
542
+ });
543
+ }
544
+ }
545
+ return finalLinks;
546
+ };
547
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
548
+ SankeyChartBase.prototype._getSelectedLinksforStreamHover = function (singleLink) {
549
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, no-array-constructor
550
+ var q = new Array();
551
+ var finalLinks = new Set();
552
+ var finalNodes = new Set();
553
+ q.push(singleLink.source);
554
+ finalLinks.add(singleLink);
555
+ while (q.length > 0) {
556
+ var poppedNode = q.shift();
557
+ finalNodes.add(poppedNode);
558
+ if (poppedNode.targetLinks && poppedNode.targetLinks.length > 0) {
559
+ poppedNode.targetLinks.forEach(function (link) {
560
+ q.push(link.source);
561
+ finalLinks.add(link);
562
+ });
563
+ }
564
+ }
565
+ q.push(singleLink.target);
566
+ while (q.length > 0) {
567
+ var poppedNode = q.shift();
568
+ finalNodes.add(poppedNode);
569
+ if (poppedNode.sourceLinks && poppedNode.sourceLinks.length > 0) {
570
+ poppedNode.sourceLinks.forEach(function (link) {
571
+ q.push(link.target);
572
+ finalLinks.add(link);
573
+ });
574
+ }
575
+ }
576
+ return {
577
+ selectedLinks: finalLinks,
578
+ selectedNodes: finalNodes,
579
+ };
580
+ };
581
+ SankeyChartBase.prototype._getOpacityStream = function (singleLink) {
582
+ if (this.state.selectedState) {
583
+ if (!this.state.selectedLinks.has(singleLink.index)) {
584
+ return NON_SELECTED_OPACITY;
585
+ }
586
+ else if (this.state.selectedState &&
587
+ this.state.selectedLinks.has(singleLink.index) &&
588
+ !this.state.selectedNode) {
589
+ return SELECTED_STREAM_OPACITY;
590
+ }
591
+ }
592
+ return REST_STREAM_OPACITY;
593
+ };
594
+ SankeyChartBase.prototype._getOpacityStreamBorder = function (singleLink) {
595
+ if (this.state.selectedState && !this.state.selectedLinks.has(singleLink.index) && !this.state.selectedNode) {
596
+ return NON_SELECTED_STREAM_BORDER_OPACITY;
597
+ }
598
+ return NON_SELECTED_OPACITY;
599
+ };
92
600
  SankeyChartBase.prototype._fitParentContainer = function () {
93
601
  var _this = this;
94
602
  var _a = this.state, containerWidth = _a.containerWidth, containerHeight = _a.containerHeight;
@@ -105,6 +613,65 @@ var SankeyChartBase = /** @class */ (function (_super) {
105
613
  }
106
614
  });
107
615
  };
616
+ /**
617
+ *
618
+ * @param text is the text which we are trying to truncate
619
+ * @param rectangleWidth is the width of the rectangle which will contain the text
620
+ * @param padding is the space we need to leave between the rect lines and other text
621
+ * @param nodeWeight is the text if present needs to be accomodate in the same line as text.
622
+ * @returns the truncated text , if truncated given the above parameters.
623
+ */
624
+ SankeyChartBase.prototype._truncateText = function (text, rectangleWidth, padding) {
625
+ var textLengthForNodeName = rectangleWidth - padding;
626
+ var elipsisLength = 0;
627
+ var tspan = d3_selection_1.select('.nodeName')
628
+ .append('text')
629
+ .attr('class', 'tempText')
630
+ .attr('font-size', '10')
631
+ .append('tspan')
632
+ .text(null);
633
+ tspan.text(text);
634
+ if (tspan.node() !== null && tspan.node().getComputedTextLength() <= textLengthForNodeName) {
635
+ return text;
636
+ }
637
+ tspan.text(null);
638
+ tspan.text('...');
639
+ if (tspan.node() !== null) {
640
+ elipsisLength = tspan.node().getComputedTextLength();
641
+ }
642
+ tspan.text(null);
643
+ var line = '';
644
+ for (var i = 0; i < text.length; i++) {
645
+ line += text[i];
646
+ tspan.text(line);
647
+ if (tspan.node() !== null) {
648
+ var w = tspan.node().getComputedTextLength();
649
+ if (w >= textLengthForNodeName - elipsisLength) {
650
+ line = line.slice(0, -1);
651
+ line += '...';
652
+ break;
653
+ }
654
+ }
655
+ }
656
+ tspan.text(null);
657
+ d3_selection_1.selectAll('.tempText').remove();
658
+ return line;
659
+ };
660
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
661
+ SankeyChartBase.prototype._showTooltip = function (text, checkTrcuncated, div, evt) {
662
+ if (checkTrcuncated) {
663
+ //Fixing tooltip position by attaching it to the element rather than page
664
+ div.style('opacity', 0.9);
665
+ div
666
+ .html(text)
667
+ .style('left', evt.pageX + 'px')
668
+ .style('top', evt.pageY - 28 + 'px');
669
+ }
670
+ };
671
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
672
+ SankeyChartBase.prototype._hideTooltip = function (div) {
673
+ div.style('opacity', 0);
674
+ };
108
675
  return SankeyChartBase;
109
676
  }(React.Component));
110
677
  exports.SankeyChartBase = SankeyChartBase;