UNPKG

devextreme

Version:

JavaScript/TypeScript Component Suite for Responsive Web Development

385 lines (381 loc) • 15.1 kB
/** * DevExtreme (cjs/__internal/viz/sankey/sankey.js) * Version: 26.1.6 * Build date: Fri Oct 16 2026 * * Copyright (c) 2012 - 2026 Developer Express Inc. ALL RIGHTS RESERVED * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/ */ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _component_registrator = require("../../core/component_registrator"); var _m_common = require("../../core/utils/m_common"); var _m_type = require("../../core/utils/m_type"); var _base_widget = _interopRequireDefault(require("../../viz/core/base_widget")); var _data_source = require("../../viz/core/data_source"); var _helpers = require("../../viz/core/helpers"); var _constants = require("../../viz/sankey/constants"); var _layout = require("../../viz/sankey/layout"); var _link_item = _interopRequireDefault(require("../../viz/sankey/link_item")); var _node_item = _interopRequireDefault(require("../../viz/sankey/node_item")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e } } function moveLabel(node, labelOptions, availableLabelWidth, rect) { if (node._label.getBBox().width > availableLabelWidth) { node.labelText.applyEllipsis(availableLabelWidth) } const bBox = node._label.getBBox(); const { verticalOffset: verticalOffset, horizontalOffset: horizontalOffset } = labelOptions; const [rectLeft, rectTop, rectRight, rectBottom] = rect; let labelOffsetY = Math.round(node.rect.y + node.rect.height / 2 - bBox.y - bBox.height / 2) + verticalOffset; let labelOffsetX = node.rect.x + horizontalOffset + node.rect.width - bBox.x; if (labelOffsetX + bBox.width >= rectRight - rectLeft) { labelOffsetX = node.rect.x - horizontalOffset - bBox.x - bBox.width } if (labelOffsetY >= rectBottom) { labelOffsetY = rectBottom } if (labelOffsetY - bBox.height < rectTop) { labelOffsetY = node.rect.y - bBox.y + verticalOffset } node.labelText.attr({ translateX: labelOffsetX, translateY: labelOffsetY }) } function getConnectedLinks(layout, nodeName, linkType) { const result = []; const attrName = "in" === linkType ? "_to" : "_from"; const invertedAttrName = "in" === linkType ? "_from" : "_to"; layout.links.forEach((link, idx) => { if (link[attrName]._name === nodeName) { result.push({ index: idx, weight: link._weight, node: link[invertedAttrName]._name }) } }); return result } class Sankey extends _base_widget.default { _getDefaultSize() { return { width: 400, height: 400 } } _initCore() { this._groupLinks = this._renderer.g().append(this._renderer.root); this._groupNodes = this._renderer.g().append(this._renderer.root); this._groupLabels = this._renderer.g().attr({ class: `${this._rootClassPrefix}-labels` }).append(this._renderer.root); this._drawLabels = true; this._nodes = []; this._links = []; this._gradients = [] } _applySize(rect) { this._rect = rect.slice(); const adaptiveLayout = this._getOption("adaptiveLayout"); this._drawLabels = Boolean(adaptiveLayout.keepLabels) || this._rect[2] - this._rect[0] > adaptiveLayout.width; this._change(["BUILD_LAYOUT"]); return this._rect } _dataSourceChangedHandler() { this._requestChange(["BUILD_LAYOUT"]) } _change_DRAWN() { this._drawn() } _change_DATA_SOURCE() { this._change(["DRAWN"]); this._updateDataSource() } _change_LABELS() { this._applyLabelsAppearance() } _change_BUILD_LAYOUT() { this._groupNodes.clear(); this._groupLinks.clear(); this._groupLabels.clear(); this._buildLayout() } _change_NODES_DRAW() { this._nodes.forEach(node => { node.element = this._renderer.rect().attr(node.rect).append(this._groupNodes) }); this._applyNodesAppearance() } _change_LINKS_DRAW() { this._links.forEach((link, index) => { const group = this._renderer.g().attr({ class: "link", "data-link-idx": index }).append(this._groupLinks); link.overlayElement = this._renderer.path([], "area").attr({ d: link.d }).append(group); link.element = this._renderer.path([], "area").attr({ d: link.d }).append(group) }); this._applyLinksAppearance() } _suspend() { if (!this._applyingChanges) { this._suspendChanges() } } _resume() { if (!this._applyingChanges) { this._resumeChanges() } } clearHover() { this._suspend(); this._nodes.forEach(node => { if (node.isHovered()) { node.hover(false) } }); this._links.forEach(link => { if (link.isHovered()) { link.hover(false) } if (link.isAdjacentNodeHovered()) { link.adjacentNodeHover(false) } }); this._resume() } _applyNodesAppearance() { this._nodes.forEach(node => { const state = node.getState(); node.element.smartAttr(node.states[state]) }) } _applyLinksAppearance() { this._links.forEach(link => { const state = link.getState(); link.element.smartAttr(link.states[state]); link.overlayElement.smartAttr(link.overlayStates[state]) }) } _hitTestTargets(x, y) { return this._proxyData.reduce((found, callback) => found || callback.call(this, x, y), void 0) } _getData() { const data = this._dataSourceItems() || []; const sourceField = this._getOption("sourceField", true); const targetField = this._getOption("targetField", true); const weightField = this._getOption("weightField", true); const processedData = []; data.forEach(item => { const hasItemOwnProperty = Object.prototype.hasOwnProperty.bind(item); if (!hasItemOwnProperty(sourceField)) { this._incidentOccurred("E2007", sourceField) } else if (!hasItemOwnProperty(targetField)) { this._incidentOccurred("E2007", targetField) } else if (!hasItemOwnProperty(weightField)) { this._incidentOccurred("E2007", weightField) } else if (!(0, _m_type.isString)(item[sourceField])) { this._incidentOccurred("E2008", sourceField) } else if (!(0, _m_type.isString)(item[targetField])) { this._incidentOccurred("E2008", targetField) } else if (!(0, _m_type.isNumeric)(item[weightField]) || item[weightField] <= 0) { this._incidentOccurred("E2009", weightField) } else { processedData.push([item[sourceField], item[targetField], item[weightField]]) } }); return processedData } _buildLayout() { const data = this._getData(); const availableRect = this._rect; const nodeOptions = this._getOption("node"); const sortData = this._getOption("sortData"); const layoutBuilder = this._getOption("layoutBuilder", true) || _layout.layout; const rect = { x: availableRect[0], y: availableRect[1], width: availableRect[2] - availableRect[0], height: availableRect[3] - availableRect[1] }; const layout = layoutBuilder.computeLayout(data, sortData, { availableRect: rect, nodePadding: nodeOptions.padding, nodeWidth: nodeOptions.width, nodeAlign: this._getOption("alignment", true) }, this._incidentOccurred); this._layoutMap = layout; if (!Object.prototype.hasOwnProperty.call(layout, "error")) { const nodeColors = {}; let nodeIdx = 0; const linkOptions = this._getOption("link"); const totalNodesNum = layout.nodes.map(item => item.length).reduce((previousValue, currentValue) => previousValue + currentValue, 0); const palette = this._themeManager.createPalette(this._getOption("palette", true), { useHighlight: true, extensionMode: this._getOption("paletteExtensionMode", true), count: totalNodesNum }); this._nodes = []; this._links = []; this._gradients.forEach(gradient => { gradient.dispose() }); this._gradients = []; if (this._shadowFilter) { this._shadowFilter.dispose() } layout.nodes.forEach(cascadeNodes => { cascadeNodes.forEach(node => { const color = nodeOptions.color || palette.getNextColor(); const nodeItem = new _node_item.default(this, { id: nodeIdx, color: color, rect: node, options: nodeOptions, linksIn: getConnectedLinks(layout, node._name, "in"), linksOut: getConnectedLinks(layout, node._name, "out") }); this._nodes.push(nodeItem); nodeIdx += 1; nodeColors[node._name] = color }) }); layout.links.forEach(link => { let gradient = null; if (linkOptions.colorMode === _constants.COLOR_MODE_GRADIENT) { gradient = this._renderer.linearGradient([{ offset: "0%", "stop-color": nodeColors[link._from._name] }, { offset: "100%", "stop-color": nodeColors[link._to._name] }]); this._gradients.push(gradient) } let { color: color } = linkOptions; if (linkOptions.colorMode === _constants.COLOR_MODE_SOURCE) { color = nodeColors[link._from._name] } else if (linkOptions.colorMode === _constants.COLOR_MODE_TARGET) { color = nodeColors[link._to._name] } const linkItem = new _link_item.default(this, { d: link.d, boundingRect: link._boundingRect, color: color, options: linkOptions, connection: { source: link._from._name, target: link._to._name, weight: link._weight }, gradient: gradient }); this._links.push(linkItem) }); this._renderer.initDefsElements(); this._change(["NODES_DRAW", "LINKS_DRAW", "LABELS"]) } this._change(["DRAWN"]) } _applyLabelsAppearance() { const labelOptions = this._getOption("label"); const availableWidth = this._rect[2] - this._rect[0]; const nodeOptions = this._getOption("node"); this._shadowFilter = this._renderer.shadowFilter("-50%", "-50%", "200%", "200%").attr(labelOptions.shadow); this._groupLabels.clear(); if (this._drawLabels && labelOptions.visible) { const cascadesCount = this._layoutMap.cascades.length; const freeSpace = availableWidth - (nodeOptions.width + labelOptions.horizontalOffset) - cascadesCount * nodeOptions.width; const availableLabelWidth = freeSpace / (cascadesCount - 1) - labelOptions.horizontalOffset; this._nodes.forEach(node => { this._createLabel(node, labelOptions, this._shadowFilter.id); moveLabel(node, labelOptions, availableLabelWidth, this._rect) }); if ("none" !== labelOptions.overlappingBehavior) { this._nodes.forEach(thisNode => { const thisBox = thisNode._label.getBBox(); this._nodes.forEach(otherNode => { const otherBox = otherNode._label.getBBox(); if (thisNode.id !== otherNode.id && _layout.layout.overlap(thisBox, otherBox)) { if ("ellipsis" === labelOptions.overlappingBehavior) { thisNode.labelText.applyEllipsis(otherBox.x - thisBox.x) } else if ("hide" === labelOptions.overlappingBehavior) { thisNode.labelText.remove() } } }) }) } } } _createLabel(node, labelOptions, filter) { const textData = labelOptions.customizeText(node); const settings = node.getLabelAttributes(labelOptions, filter); if (textData) { node._label = this._renderer.g().append(this._groupLabels); node.labelText = this._renderer.text(textData).attr(settings.attr).css(settings.css); node.labelText.append(node._label) } } _getMinSize() { const adaptiveLayout = this._getOption("adaptiveLayout"); return [adaptiveLayout.width, adaptiveLayout.height] } getAllNodes() { return this._nodes.slice() } getAllLinks() { return this._links.slice() } }(0, _helpers.setupWidgetPrototype)(Sankey, { _rootClass: "dxs-sankey", _rootClassPrefix: "dxs", _proxyData: [], _optionChangesMap: { dataSource: "DATA_SOURCE", sortData: "DATA_SOURCE", alignment: "DATA_SOURCE", node: "BUILD_LAYOUT", label: "LABELS", link: "BUILD_LAYOUT", palette: "BUILD_LAYOUT", paletteExtensionMode: "BUILD_LAYOUT" }, _themeDependentChanges: ["BUILD_LAYOUT"], _themeSection: "sankey", _fontFields: ["label.font"], _optionChangesOrder: ["DATA_SOURCE"], _initialChanges: ["DATA_SOURCE"], _eventsMap: { onNodeHoverChanged: { name: "nodeHoverChanged" }, onLinkHoverChanged: { name: "linkHoverChanged" } }, _customChangesOrder: ["BUILD_LAYOUT", "NODES_DRAW", "LINKS_DRAW", "LABELS", "DRAWN"], _disposeCore: _m_common.noop, _showTooltip: _m_common.noop, hideTooltip: _m_common.noop }); (0, _component_registrator.registerComponent)("dxSankey", Sankey); Sankey.addPlugin(_data_source.plugin); var _default = exports.default = Sankey;