UNPKG

@visactor/vchart

Version:

charts lib based @visactor/VGrammar

57 lines (48 loc) 2.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: !0 }), exports.sankeyLayout = exports.sankeyFormat = exports.collectHierarchyField = void 0; const vlayouts_1 = require("@visactor/vlayouts"), vutils_1 = require("@visactor/vutils"), collectHierarchyField = (set, data, field) => { data.forEach((obj => { (0, vutils_1.isNil)(obj[field]) || set.add(obj[field]), obj.children && obj.children.length > 0 && (0, exports.collectHierarchyField)(set, obj.children, field); })); }; exports.collectHierarchyField = collectHierarchyField; const sankeyFormat = data => { var _a; if (!data || !(0, vutils_1.isArray)(data)) return []; if (data.length > 1) { const updateData = { links: [], nodes: [] }; return data.forEach((datum => { "links" !== datum.id && "nodes" !== datum.id || (updateData[datum.id] = datum.values); })), [ updateData ]; } return (null === (_a = data[0]) || void 0 === _a ? void 0 : _a.latestData) ? data[0].latestData : data; }; exports.sankeyFormat = sankeyFormat; const sankeyLayout = (data, op) => { const options = (0, vutils_1.isFunction)(op) ? op() : op; if (!data || !(null == options ? void 0 : options.view) || !data.length) return []; const view = options.view(); if (view.x1 - view.x0 == 0 || view.y1 - view.y0 == 0 || view.x1 - view.x0 == -1 / 0 || view.x1 - view.x0 == 1 / 0 || view.y1 - view.y0 == -1 / 0 || view.y1 - view.y0 == 1 / 0) return []; const originalData = data[0], layoutData = normalizeSankeyData(originalData, options), layout = new vlayouts_1.SankeyLayout(options), result = []; return result.push(layout.layout(layoutData, view)), result; }; exports.sankeyLayout = sankeyLayout; const normalizeSankeyData = (data, options) => { if ("source" === options.sourceField && "target" === options.targetField && "value" === options.valueField) return data; const links = data.links; return links ? Object.assign(Object.assign({}, data), { links: links.map((link => { const updatedLink = {}; return Object.keys(link).forEach((key => { key === options.sourceField ? updatedLink.source = link[options.sourceField] : key === options.targetField ? updatedLink.target = link[options.targetField] : key === options.valueField ? updatedLink.value = link[options.valueField] : updatedLink[key] = link[key]; })), updatedLink; })) }) : data; }; //# sourceMappingURL=sankey.js.map