2023-06-18 00:32:45 +02:00
|
|
|
import * as configApi from '../../config.js';
|
|
|
|
import common from '../common/common.js';
|
|
|
|
import {
|
2023-06-18 00:32:45 +02:00
|
|
|
setAccTitle,
|
|
|
|
getAccTitle,
|
|
|
|
getAccDescription,
|
|
|
|
setAccDescription,
|
2023-06-18 00:32:45 +02:00
|
|
|
setDiagramTitle,
|
|
|
|
getDiagramTitle,
|
|
|
|
clear as commonClear,
|
|
|
|
} from '../../commonDb.js';
|
|
|
|
|
2023-06-20 01:37:27 +02:00
|
|
|
// Variables where graph data is stored
|
|
|
|
// Sankey diagram represented by nodes and links between those nodes
|
2023-06-24 21:02:18 +02:00
|
|
|
// We have to track nodes uniqueness (by ID), thats why we need a mapping also
|
2023-06-20 01:37:27 +02:00
|
|
|
//
|
|
|
|
let links: Array<SankeyLink> = [];
|
|
|
|
let nodes: Array<SankeyNode> = [];
|
2023-06-24 21:02:18 +02:00
|
|
|
let nodesMap: Record<string, SankeyNode> = {};
|
2023-06-22 16:35:40 +02:00
|
|
|
let nodeAlign = 'justify';
|
2023-06-21 02:54:55 +02:00
|
|
|
|
2023-06-24 21:02:18 +02:00
|
|
|
const setNodeAlign = (alignment: string): void => {
|
2023-06-24 23:22:36 +02:00
|
|
|
const nodeAlignments: Set<string> = new Set(['left', 'right', 'center', 'justify']);
|
2023-06-24 21:02:18 +02:00
|
|
|
if (nodeAlignments.has(alignment)) {
|
2023-06-22 16:35:40 +02:00
|
|
|
nodeAlign = alignment;
|
|
|
|
}
|
2023-06-21 02:54:55 +02:00
|
|
|
};
|
|
|
|
|
2023-06-24 21:02:18 +02:00
|
|
|
const getNodeAlign = (): string => nodeAlign;
|
2023-06-18 00:32:45 +02:00
|
|
|
|
2023-06-24 21:02:18 +02:00
|
|
|
const clear = (): void => {
|
2023-06-18 00:32:45 +02:00
|
|
|
links = [];
|
2023-06-18 00:32:45 +02:00
|
|
|
nodes = [];
|
2023-06-24 21:02:18 +02:00
|
|
|
nodesMap = {};
|
2023-06-21 02:54:55 +02:00
|
|
|
nodeAlign = 'justify';
|
2023-06-18 00:32:45 +02:00
|
|
|
commonClear();
|
|
|
|
};
|
|
|
|
|
2023-06-20 01:37:27 +02:00
|
|
|
class SankeyLink {
|
|
|
|
constructor(public source: SankeyNode, public target: SankeyNode, public value: number = 0) {}
|
2023-06-18 00:32:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-06-18 00:32:45 +02:00
|
|
|
* @param source - Node where the link starts
|
|
|
|
* @param target - Node where the link ends
|
2023-06-18 03:33:20 +02:00
|
|
|
* @param value - number, float or integer, describes the amount to be passed
|
2023-06-18 00:32:45 +02:00
|
|
|
*/
|
2023-06-24 21:02:18 +02:00
|
|
|
const addLink = (source: SankeyNode, target: SankeyNode, value: number): void => {
|
|
|
|
links.push(new SankeyLink(source, target, value));
|
2023-06-18 00:32:45 +02:00
|
|
|
};
|
2023-06-18 00:32:45 +02:00
|
|
|
|
2023-06-20 01:37:27 +02:00
|
|
|
class SankeyNode {
|
2023-06-24 21:02:18 +02:00
|
|
|
constructor(public ID: string) {}
|
2023-06-18 00:32:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-06-20 01:37:27 +02:00
|
|
|
* @param ID - The id of the node
|
2023-06-18 00:32:45 +02:00
|
|
|
*/
|
2023-06-24 21:02:18 +02:00
|
|
|
const findOrCreateNode = (ID: string): SankeyNode => {
|
2023-06-18 00:32:45 +02:00
|
|
|
ID = common.sanitizeText(ID, configApi.getConfig());
|
2023-06-20 01:37:27 +02:00
|
|
|
let node: SankeyNode;
|
2023-06-24 21:02:18 +02:00
|
|
|
if (nodesMap[ID] === undefined) {
|
2023-06-20 01:37:27 +02:00
|
|
|
node = new SankeyNode(ID);
|
2023-06-24 21:02:18 +02:00
|
|
|
nodesMap[ID] = node;
|
2023-06-18 22:53:57 +02:00
|
|
|
nodes.push(node);
|
|
|
|
} else {
|
2023-06-24 21:02:18 +02:00
|
|
|
node = nodesMap[ID];
|
2023-06-18 00:32:45 +02:00
|
|
|
}
|
|
|
|
return node;
|
2023-06-18 00:32:45 +02:00
|
|
|
};
|
2023-06-18 00:32:45 +02:00
|
|
|
|
2023-06-18 03:33:20 +02:00
|
|
|
const getNodes = () => nodes;
|
|
|
|
const getLinks = () => links;
|
|
|
|
|
2023-06-20 01:37:27 +02:00
|
|
|
const getGraph = () => ({
|
2023-06-24 21:02:18 +02:00
|
|
|
nodes: nodes.map((node) => ({ id: node.ID })),
|
2023-06-20 01:37:27 +02:00
|
|
|
links: links.map((link) => ({
|
|
|
|
source: link.source.ID,
|
|
|
|
target: link.target.ID,
|
|
|
|
value: link.value,
|
|
|
|
})),
|
|
|
|
});
|
|
|
|
|
2023-06-18 00:32:45 +02:00
|
|
|
export default {
|
2023-06-24 21:02:18 +02:00
|
|
|
nodesMap,
|
2023-06-18 03:33:20 +02:00
|
|
|
getConfig: () => configApi.getConfig().sankey,
|
|
|
|
getNodes,
|
|
|
|
getLinks,
|
2023-06-20 01:37:27 +02:00
|
|
|
getGraph,
|
2023-06-18 00:32:45 +02:00
|
|
|
addLink,
|
2023-06-20 01:37:27 +02:00
|
|
|
findOrCreateNode,
|
2023-06-21 02:54:55 +02:00
|
|
|
setNodeAlign,
|
|
|
|
getNodeAlign,
|
2023-06-18 00:32:45 +02:00
|
|
|
getAccTitle,
|
|
|
|
setAccTitle,
|
|
|
|
getAccDescription,
|
|
|
|
setAccDescription,
|
2023-06-18 00:32:45 +02:00
|
|
|
getDiagramTitle,
|
|
|
|
setDiagramTitle,
|
2023-06-18 00:32:45 +02:00
|
|
|
clear,
|
2023-06-18 00:32:45 +02:00
|
|
|
};
|