UNPKG

@qn-pandora/pandora-visualization

Version:

Pandora 通用可视化库

112 lines (111 loc) 4.13 kB
"use strict"; var __values = (this && this.__values) || function(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.transformSankeyData = exports.hasRing = void 0; var lodash_1 = require("lodash"); var chart_style_1 = require("../../../constants/chart-style"); function hasRing(nodes, links) { var names = nodes.map(function (e) { return e.name; }); var graph = []; var count = []; links.forEach(function (l) { var source = names.indexOf(l.source); var target = names.indexOf(l.target); if (source !== -1 && target !== -1) { graph[source] = graph[source] || []; graph[source][target] = true; count[target] = count[target] ? count[target] + 1 : 1; } }); var num = 0; var queue = nodes.map(function (_, i) { return i; }).filter(function (_, index) { return !count[index]; }); var _loop_1 = function () { var i = queue.shift(); num++; nodes.forEach(function (_, j) { graph[i] = graph[i] || []; if (graph[i][j]) { count[j] -= 1; if (count[j] === 0) { queue.push(j); } } }); }; while (queue.length !== 0) { _loop_1(); } return num !== nodes.length; } exports.hasRing = hasRing; function transformSankeyData(currentBuckets, dataset, mode, metricIndex) { var e_1, _a; if (metricIndex === -1 || currentBuckets.length === 0) { return { nodes: [], links: [] }; } var rows = dataset.rows; var nodes = []; var links = []; var bucketIndexs = []; var _loop_2 = function (bucket) { bucketIndexs.push(dataset.fields.findIndex(function (field) { return field.key === bucket; })); }; try { for (var currentBuckets_1 = __values(currentBuckets), currentBuckets_1_1 = currentBuckets_1.next(); !currentBuckets_1_1.done; currentBuckets_1_1 = currentBuckets_1.next()) { var bucket = currentBuckets_1_1.value; _loop_2(bucket); } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (currentBuckets_1_1 && !currentBuckets_1_1.done && (_a = currentBuckets_1.return)) _a.call(currentBuckets_1); } finally { if (e_1) throw e_1.error; } } rows.forEach(function (row) { var value = row[metricIndex][0]; bucketIndexs.forEach(function (index, i) { var displayName = String(row[index][0]); var name = currentBuckets.length > 2 || mode === chart_style_1.ESankeyMode.Level2 ? currentBuckets[i] + "-" + displayName : displayName; if (i !== 0) { var fromName = "" + nodes[nodes.length - 1].name; if (mode === chart_style_1.ESankeyMode.Multistage && currentBuckets.length === 2 && fromName === name) { return; } links.push({ title: nodes[nodes.length - 1].displayName + " -> " + displayName, source: nodes[nodes.length - 1].name, target: name, size: value, value: value, toBucket: currentBuckets[i], fromBucket: currentBuckets[i - 1] }); } nodes.push({ name: name, displayName: displayName }); }); }); return { nodes: lodash_1.uniqBy(nodes, function (n) { return n.name; }), links: links }; } exports.transformSankeyData = transformSankeyData;