mermaid/packages/mermaid/src/diagrams/sankey/sankeyDB.ts

102 lines
2.3 KiB
TypeScript
Raw Normal View History

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';
// Variables where graph data is stored
// Sankey diagram represented by nodes and links between those nodes
// We have to track nodes uniqueness (by ID), thats why we need a mapping also
//
let links: Array<SankeyLink> = [];
let nodes: Array<SankeyNode> = [];
let nodesMap: Record<string, SankeyNode> = {};
let nodeAlign = 'justify';
2023-06-21 02:54:55 +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']);
if (nodeAlignments.has(alignment)) {
nodeAlign = alignment;
}
2023-06-21 02:54:55 +02:00
};
const getNodeAlign = (): string => nodeAlign;
2023-06-18 00:32:45 +02:00
const clear = (): void => {
2023-06-18 00:32:45 +02:00
links = [];
2023-06-18 00:32:45 +02:00
nodes = [];
nodesMap = {};
2023-06-21 02:54:55 +02:00
nodeAlign = 'justify';
2023-06-18 00:32:45 +02:00
commonClear();
};
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
*/
const addLink = (source: SankeyNode, target: SankeyNode, value: number): void => {
links.push(new SankeyLink(source, target, value));
};
2023-06-18 00:32:45 +02:00
class SankeyNode {
constructor(public ID: string) {}
2023-06-18 00:32:45 +02:00
}
/**
* @param ID - The id of the node
2023-06-18 00:32:45 +02:00
*/
const findOrCreateNode = (ID: string): SankeyNode => {
2023-06-18 00:32:45 +02:00
ID = common.sanitizeText(ID, configApi.getConfig());
let node: SankeyNode;
if (nodesMap[ID] === undefined) {
node = new SankeyNode(ID);
nodesMap[ID] = node;
2023-06-18 22:53:57 +02:00
nodes.push(node);
} else {
node = nodesMap[ID];
2023-06-18 00:32:45 +02:00
}
return node;
};
2023-06-18 00:32:45 +02:00
2023-06-18 03:33:20 +02:00
const getNodes = () => nodes;
const getLinks = () => links;
const getGraph = () => ({
nodes: nodes.map((node) => ({ id: node.ID })),
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 {
nodesMap,
2023-06-18 03:33:20 +02:00
getConfig: () => configApi.getConfig().sankey,
getNodes,
getLinks,
getGraph,
2023-06-18 00:32:45 +02:00
addLink,
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,
clear,
2023-06-18 00:32:45 +02:00
};