@visactor/vchart
Version:
charts lib based @visactor/VGrammar
48 lines (42 loc) • 2.23 kB
JavaScript
import { SankeyLayout } from "@visactor/vlayouts";
import { isArray, isFunction, isNil } from "@visactor/vutils";
export const collectHierarchyField = (set, data, field) => {
data.forEach((obj => {
isNil(obj[field]) || set.add(obj[field]), obj.children && obj.children.length > 0 && collectHierarchyField(set, obj.children, field);
}));
};
export const sankeyFormat = data => {
var _a;
if (!data || !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;
};
export const sankeyLayout = (data, op) => {
const options = 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 SankeyLayout(options), result = [];
return result.push(layout.layout(layoutData, view)), result;
};
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