UNPKG

42 kBJavaScriptView Raw
1import { c as getConfig, g as getAccTitle, s as setAccTitle, a as getAccDescription, b as setAccDescription, D as getDiagramTitle, C as setDiagramTitle, E as clear$1, f as common, ar as defaultConfig, j as d3select, t as setupGraphViewbox } from "./mermaid-dcacb631.js";
2import { o as ordinal } from "./ordinal-475e0c0c.js";
3import { d as d3schemeTableau10 } from "./Tableau10-31042135.js";
4import "./init-cc95ec8e.js";
5var parser = function() {
6 var o = function(k, v, o2, l) {
7 for (o2 = o2 || {}, l = k.length; l--; o2[k[l]] = v)
8 ;
9 return o2;
10 }, $V0 = [1, 9], $V1 = [1, 10], $V2 = [1, 5, 10, 12];
11 var parser2 = {
12 trace: function trace() {
13 },
14 yy: {},
15 symbols_: { "error": 2, "start": 3, "SANKEY": 4, "NEWLINE": 5, "csv": 6, "opt_eof": 7, "record": 8, "csv_tail": 9, "EOF": 10, "field[source]": 11, "COMMA": 12, "field[target]": 13, "field[value]": 14, "field": 15, "escaped": 16, "non_escaped": 17, "DQUOTE": 18, "ESCAPED_TEXT": 19, "NON_ESCAPED_TEXT": 20, "$accept": 0, "$end": 1 },
16 terminals_: { 2: "error", 4: "SANKEY", 5: "NEWLINE", 10: "EOF", 11: "field[source]", 12: "COMMA", 13: "field[target]", 14: "field[value]", 18: "DQUOTE", 19: "ESCAPED_TEXT", 20: "NON_ESCAPED_TEXT" },
17 productions_: [0, [3, 4], [6, 2], [9, 2], [9, 0], [7, 1], [7, 0], [8, 5], [15, 1], [15, 1], [16, 3], [17, 1]],
18 performAction: function anonymous(yytext, yyleng, yylineno, yy, yystate, $$, _$) {
19 var $0 = $$.length - 1;
20 switch (yystate) {
21 case 7:
22 const source = yy.findOrCreateNode($$[$0 - 4].trim().replaceAll('""', '"'));
23 const target = yy.findOrCreateNode($$[$0 - 2].trim().replaceAll('""', '"'));
24 const value2 = parseFloat($$[$0].trim());
25 yy.addLink(source, target, value2);
26 break;
27 case 8:
28 case 9:
29 case 11:
30 this.$ = $$[$0];
31 break;
32 case 10:
33 this.$ = $$[$0 - 1];
34 break;
35 }
36 },
37 table: [{ 3: 1, 4: [1, 2] }, { 1: [3] }, { 5: [1, 3] }, { 6: 4, 8: 5, 15: 6, 16: 7, 17: 8, 18: $V0, 20: $V1 }, { 1: [2, 6], 7: 11, 10: [1, 12] }, o($V1, [2, 4], { 9: 13, 5: [1, 14] }), { 12: [1, 15] }, o($V2, [2, 8]), o($V2, [2, 9]), { 19: [1, 16] }, o($V2, [2, 11]), { 1: [2, 1] }, { 1: [2, 5] }, o($V1, [2, 2]), { 6: 17, 8: 5, 15: 6, 16: 7, 17: 8, 18: $V0, 20: $V1 }, { 15: 18, 16: 7, 17: 8, 18: $V0, 20: $V1 }, { 18: [1, 19] }, o($V1, [2, 3]), { 12: [1, 20] }, o($V2, [2, 10]), { 15: 21, 16: 7, 17: 8, 18: $V0, 20: $V1 }, o([1, 5, 10], [2, 7])],
38 defaultActions: { 11: [2, 1], 12: [2, 5] },
39 parseError: function parseError(str, hash) {
40 if (hash.recoverable) {
41 this.trace(str);
42 } else {
43 var error = new Error(str);
44 error.hash = hash;
45 throw error;
46 }
47 },
48 parse: function parse(input) {
49 var self = this, stack = [0], tstack = [], vstack = [null], lstack = [], table = this.table, yytext = "", yylineno = 0, yyleng = 0, TERROR = 2, EOF = 1;
50 var args = lstack.slice.call(arguments, 1);
51 var lexer2 = Object.create(this.lexer);
52 var sharedState = { yy: {} };
53 for (var k in this.yy) {
54 if (Object.prototype.hasOwnProperty.call(this.yy, k)) {
55 sharedState.yy[k] = this.yy[k];
56 }
57 }
58 lexer2.setInput(input, sharedState.yy);
59 sharedState.yy.lexer = lexer2;
60 sharedState.yy.parser = this;
61 if (typeof lexer2.yylloc == "undefined") {
62 lexer2.yylloc = {};
63 }
64 var yyloc = lexer2.yylloc;
65 lstack.push(yyloc);
66 var ranges = lexer2.options && lexer2.options.ranges;
67 if (typeof sharedState.yy.parseError === "function") {
68 this.parseError = sharedState.yy.parseError;
69 } else {
70 this.parseError = Object.getPrototypeOf(this).parseError;
71 }
72 function lex() {
73 var token;
74 token = tstack.pop() || lexer2.lex() || EOF;
75 if (typeof token !== "number") {
76 if (token instanceof Array) {
77 tstack = token;
78 token = tstack.pop();
79 }
80 token = self.symbols_[token] || token;
81 }
82 return token;
83 }
84 var symbol, state, action, r, yyval = {}, p, len, newState, expected;
85 while (true) {
86 state = stack[stack.length - 1];
87 if (this.defaultActions[state]) {
88 action = this.defaultActions[state];
89 } else {
90 if (symbol === null || typeof symbol == "undefined") {
91 symbol = lex();
92 }
93 action = table[state] && table[state][symbol];
94 }
95 if (typeof action === "undefined" || !action.length || !action[0]) {
96 var errStr = "";
97 expected = [];
98 for (p in table[state]) {
99 if (this.terminals_[p] && p > TERROR) {
100 expected.push("'" + this.terminals_[p] + "'");
101 }
102 }
103 if (lexer2.showPosition) {
104 errStr = "Parse error on line " + (yylineno + 1) + ":\n" + lexer2.showPosition() + "\nExpecting " + expected.join(", ") + ", got '" + (this.terminals_[symbol] || symbol) + "'";
105 } else {
106 errStr = "Parse error on line " + (yylineno + 1) + ": Unexpected " + (symbol == EOF ? "end of input" : "'" + (this.terminals_[symbol] || symbol) + "'");
107 }
108 this.parseError(errStr, {
109 text: lexer2.match,
110 token: this.terminals_[symbol] || symbol,
111 line: lexer2.yylineno,
112 loc: yyloc,
113 expected
114 });
115 }
116 if (action[0] instanceof Array && action.length > 1) {
117 throw new Error("Parse Error: multiple actions possible at state: " + state + ", token: " + symbol);
118 }
119 switch (action[0]) {
120 case 1:
121 stack.push(symbol);
122 vstack.push(lexer2.yytext);
123 lstack.push(lexer2.yylloc);
124 stack.push(action[1]);
125 symbol = null;
126 {
127 yyleng = lexer2.yyleng;
128 yytext = lexer2.yytext;
129 yylineno = lexer2.yylineno;
130 yyloc = lexer2.yylloc;
131 }
132 break;
133 case 2:
134 len = this.productions_[action[1]][1];
135 yyval.$ = vstack[vstack.length - len];
136 yyval._$ = {
137 first_line: lstack[lstack.length - (len || 1)].first_line,
138 last_line: lstack[lstack.length - 1].last_line,
139 first_column: lstack[lstack.length - (len || 1)].first_column,
140 last_column: lstack[lstack.length - 1].last_column
141 };
142 if (ranges) {
143 yyval._$.range = [
144 lstack[lstack.length - (len || 1)].range[0],
145 lstack[lstack.length - 1].range[1]
146 ];
147 }
148 r = this.performAction.apply(yyval, [
149 yytext,
150 yyleng,
151 yylineno,
152 sharedState.yy,
153 action[1],
154 vstack,
155 lstack
156 ].concat(args));
157 if (typeof r !== "undefined") {
158 return r;
159 }
160 if (len) {
161 stack = stack.slice(0, -1 * len * 2);
162 vstack = vstack.slice(0, -1 * len);
163 lstack = lstack.slice(0, -1 * len);
164 }
165 stack.push(this.productions_[action[1]][0]);
166 vstack.push(yyval.$);
167 lstack.push(yyval._$);
168 newState = table[stack[stack.length - 2]][stack[stack.length - 1]];
169 stack.push(newState);
170 break;
171 case 3:
172 return true;
173 }
174 }
175 return true;
176 }
177 };
178 var lexer = function() {
179 var lexer2 = {
180 EOF: 1,
181 parseError: function parseError(str, hash) {
182 if (this.yy.parser) {
183 this.yy.parser.parseError(str, hash);
184 } else {
185 throw new Error(str);
186 }
187 },
188 // resets the lexer, sets new input
189 setInput: function(input, yy) {
190 this.yy = yy || this.yy || {};
191 this._input = input;
192 this._more = this._backtrack = this.done = false;
193 this.yylineno = this.yyleng = 0;
194 this.yytext = this.matched = this.match = "";
195 this.conditionStack = ["INITIAL"];
196 this.yylloc = {
197 first_line: 1,
198 first_column: 0,
199 last_line: 1,
200 last_column: 0
201 };
202 if (this.options.ranges) {
203 this.yylloc.range = [0, 0];
204 }
205 this.offset = 0;
206 return this;
207 },
208 // consumes and returns one char from the input
209 input: function() {
210 var ch = this._input[0];
211 this.yytext += ch;
212 this.yyleng++;
213 this.offset++;
214 this.match += ch;
215 this.matched += ch;
216 var lines = ch.match(/(?:\r\n?|\n).*/g);
217 if (lines) {
218 this.yylineno++;
219 this.yylloc.last_line++;
220 } else {
221 this.yylloc.last_column++;
222 }
223 if (this.options.ranges) {
224 this.yylloc.range[1]++;
225 }
226 this._input = this._input.slice(1);
227 return ch;
228 },
229 // unshifts one char (or a string) into the input
230 unput: function(ch) {
231 var len = ch.length;
232 var lines = ch.split(/(?:\r\n?|\n)/g);
233 this._input = ch + this._input;
234 this.yytext = this.yytext.substr(0, this.yytext.length - len);
235 this.offset -= len;
236 var oldLines = this.match.split(/(?:\r\n?|\n)/g);
237 this.match = this.match.substr(0, this.match.length - 1);
238 this.matched = this.matched.substr(0, this.matched.length - 1);
239 if (lines.length - 1) {
240 this.yylineno -= lines.length - 1;
241 }
242 var r = this.yylloc.range;
243 this.yylloc = {
244 first_line: this.yylloc.first_line,
245 last_line: this.yylineno + 1,
246 first_column: this.yylloc.first_column,
247 last_column: lines ? (lines.length === oldLines.length ? this.yylloc.first_column : 0) + oldLines[oldLines.length - lines.length].length - lines[0].length : this.yylloc.first_column - len
248 };
249 if (this.options.ranges) {
250 this.yylloc.range = [r[0], r[0] + this.yyleng - len];
251 }
252 this.yyleng = this.yytext.length;
253 return this;
254 },
255 // When called from action, caches matched text and appends it on next action
256 more: function() {
257 this._more = true;
258 return this;
259 },
260 // When called from action, signals the lexer that this rule fails to match the input, so the next matching rule (regex) should be tested instead.
261 reject: function() {
262 if (this.options.backtrack_lexer) {
263 this._backtrack = true;
264 } else {
265 return this.parseError("Lexical error on line " + (this.yylineno + 1) + ". You can only invoke reject() in the lexer when the lexer is of the backtracking persuasion (options.backtrack_lexer = true).\n" + this.showPosition(), {
266 text: "",
267 token: null,
268 line: this.yylineno
269 });
270 }
271 return this;
272 },
273 // retain first n characters of the match
274 less: function(n) {
275 this.unput(this.match.slice(n));
276 },
277 // displays already matched input, i.e. for error messages
278 pastInput: function() {
279 var past = this.matched.substr(0, this.matched.length - this.match.length);
280 return (past.length > 20 ? "..." : "") + past.substr(-20).replace(/\n/g, "");
281 },
282 // displays upcoming input, i.e. for error messages
283 upcomingInput: function() {
284 var next = this.match;
285 if (next.length < 20) {
286 next += this._input.substr(0, 20 - next.length);
287 }
288 return (next.substr(0, 20) + (next.length > 20 ? "..." : "")).replace(/\n/g, "");
289 },
290 // displays the character position where the lexing error occurred, i.e. for error messages
291 showPosition: function() {
292 var pre = this.pastInput();
293 var c = new Array(pre.length + 1).join("-");
294 return pre + this.upcomingInput() + "\n" + c + "^";
295 },
296 // test the lexed token: return FALSE when not a match, otherwise return token
297 test_match: function(match, indexed_rule) {
298 var token, lines, backup;
299 if (this.options.backtrack_lexer) {
300 backup = {
301 yylineno: this.yylineno,
302 yylloc: {
303 first_line: this.yylloc.first_line,
304 last_line: this.last_line,
305 first_column: this.yylloc.first_column,
306 last_column: this.yylloc.last_column
307 },
308 yytext: this.yytext,
309 match: this.match,
310 matches: this.matches,
311 matched: this.matched,
312 yyleng: this.yyleng,
313 offset: this.offset,
314 _more: this._more,
315 _input: this._input,
316 yy: this.yy,
317 conditionStack: this.conditionStack.slice(0),
318 done: this.done
319 };
320 if (this.options.ranges) {
321 backup.yylloc.range = this.yylloc.range.slice(0);
322 }
323 }
324 lines = match[0].match(/(?:\r\n?|\n).*/g);
325 if (lines) {
326 this.yylineno += lines.length;
327 }
328 this.yylloc = {
329 first_line: this.yylloc.last_line,
330 last_line: this.yylineno + 1,
331 first_column: this.yylloc.last_column,
332 last_column: lines ? lines[lines.length - 1].length - lines[lines.length - 1].match(/\r?\n?/)[0].length : this.yylloc.last_column + match[0].length
333 };
334 this.yytext += match[0];
335 this.match += match[0];
336 this.matches = match;
337 this.yyleng = this.yytext.length;
338 if (this.options.ranges) {
339 this.yylloc.range = [this.offset, this.offset += this.yyleng];
340 }
341 this._more = false;
342 this._backtrack = false;
343 this._input = this._input.slice(match[0].length);
344 this.matched += match[0];
345 token = this.performAction.call(this, this.yy, this, indexed_rule, this.conditionStack[this.conditionStack.length - 1]);
346 if (this.done && this._input) {
347 this.done = false;
348 }
349 if (token) {
350 return token;
351 } else if (this._backtrack) {
352 for (var k in backup) {
353 this[k] = backup[k];
354 }
355 return false;
356 }
357 return false;
358 },
359 // return next match in input
360 next: function() {
361 if (this.done) {
362 return this.EOF;
363 }
364 if (!this._input) {
365 this.done = true;
366 }
367 var token, match, tempMatch, index;
368 if (!this._more) {
369 this.yytext = "";
370 this.match = "";
371 }
372 var rules = this._currentRules();
373 for (var i = 0; i < rules.length; i++) {
374 tempMatch = this._input.match(this.rules[rules[i]]);
375 if (tempMatch && (!match || tempMatch[0].length > match[0].length)) {
376 match = tempMatch;
377 index = i;
378 if (this.options.backtrack_lexer) {
379 token = this.test_match(tempMatch, rules[i]);
380 if (token !== false) {
381 return token;
382 } else if (this._backtrack) {
383 match = false;
384 continue;
385 } else {
386 return false;
387 }
388 } else if (!this.options.flex) {
389 break;
390 }
391 }
392 }
393 if (match) {
394 token = this.test_match(match, rules[index]);
395 if (token !== false) {
396 return token;
397 }
398 return false;
399 }
400 if (this._input === "") {
401 return this.EOF;
402 } else {
403 return this.parseError("Lexical error on line " + (this.yylineno + 1) + ". Unrecognized text.\n" + this.showPosition(), {
404 text: "",
405 token: null,
406 line: this.yylineno
407 });
408 }
409 },
410 // return next match that has a token
411 lex: function lex() {
412 var r = this.next();
413 if (r) {
414 return r;
415 } else {
416 return this.lex();
417 }
418 },
419 // activates a new lexer condition state (pushes the new lexer condition state onto the condition stack)
420 begin: function begin(condition) {
421 this.conditionStack.push(condition);
422 },
423 // pop the previously active lexer condition state off the condition stack
424 popState: function popState() {
425 var n = this.conditionStack.length - 1;
426 if (n > 0) {
427 return this.conditionStack.pop();
428 } else {
429 return this.conditionStack[0];
430 }
431 },
432 // produce the lexer rule set which is active for the currently active lexer condition state
433 _currentRules: function _currentRules() {
434 if (this.conditionStack.length && this.conditionStack[this.conditionStack.length - 1]) {
435 return this.conditions[this.conditionStack[this.conditionStack.length - 1]].rules;
436 } else {
437 return this.conditions["INITIAL"].rules;
438 }
439 },
440 // return the currently active lexer condition state; when an index argument is provided it produces the N-th previous condition state, if available
441 topState: function topState(n) {
442 n = this.conditionStack.length - 1 - Math.abs(n || 0);
443 if (n >= 0) {
444 return this.conditionStack[n];
445 } else {
446 return "INITIAL";
447 }
448 },
449 // alias for begin(condition)
450 pushState: function pushState(condition) {
451 this.begin(condition);
452 },
453 // return the number of states currently on the stack
454 stateStackSize: function stateStackSize() {
455 return this.conditionStack.length;
456 },
457 options: { "case-insensitive": true },
458 performAction: function anonymous(yy, yy_, $avoiding_name_collisions, YY_START) {
459 switch ($avoiding_name_collisions) {
460 case 0:
461 this.pushState("csv");
462 return 4;
463 case 1:
464 return 10;
465 case 2:
466 return 5;
467 case 3:
468 return 12;
469 case 4:
470 this.pushState("escaped_text");
471 return 18;
472 case 5:
473 return 20;
474 case 6:
475 this.popState("escaped_text");
476 return 18;
477 case 7:
478 return 19;
479 }
480 },
481 rules: [/^(?:sankey-beta\b)/i, /^(?:$)/i, /^(?:((\u000D\u000A)|(\u000A)))/i, /^(?:(\u002C))/i, /^(?:(\u0022))/i, /^(?:([\u0020-\u0021\u0023-\u002B\u002D-\u007E])*)/i, /^(?:(\u0022)(?!(\u0022)))/i, /^(?:(([\u0020-\u0021\u0023-\u002B\u002D-\u007E])|(\u002C)|(\u000D)|(\u000A)|(\u0022)(\u0022))*)/i],
482 conditions: { "csv": { "rules": [1, 2, 3, 4, 5, 6, 7], "inclusive": false }, "escaped_text": { "rules": [6, 7], "inclusive": false }, "INITIAL": { "rules": [0, 1, 2, 3, 4, 5, 6, 7], "inclusive": true } }
483 };
484 return lexer2;
485 }();
486 parser2.lexer = lexer;
487 function Parser() {
488 this.yy = {};
489 }
490 Parser.prototype = parser2;
491 parser2.Parser = Parser;
492 return new Parser();
493}();
494parser.parser = parser;
495const parser$1 = parser;
496let links = [];
497let nodes = [];
498let nodesMap = {};
499const clear = () => {
500 links = [];
501 nodes = [];
502 nodesMap = {};
503 clear$1();
504};
505class SankeyLink {
506 constructor(source, target, value2 = 0) {
507 this.source = source;
508 this.target = target;
509 this.value = value2;
510 }
511}
512const addLink = (source, target, value2) => {
513 links.push(new SankeyLink(source, target, value2));
514};
515class SankeyNode {
516 constructor(ID) {
517 this.ID = ID;
518 }
519}
520const findOrCreateNode = (ID) => {
521 ID = common.sanitizeText(ID, getConfig());
522 if (!nodesMap[ID]) {
523 nodesMap[ID] = new SankeyNode(ID);
524 nodes.push(nodesMap[ID]);
525 }
526 return nodesMap[ID];
527};
528const getNodes = () => nodes;
529const getLinks = () => links;
530const getGraph = () => ({
531 nodes: nodes.map((node) => ({ id: node.ID })),
532 links: links.map((link2) => ({
533 source: link2.source.ID,
534 target: link2.target.ID,
535 value: link2.value
536 }))
537});
538const db = {
539 nodesMap,
540 getConfig: () => getConfig().sankey,
541 getNodes,
542 getLinks,
543 getGraph,
544 addLink,
545 findOrCreateNode,
546 getAccTitle,
547 setAccTitle,
548 getAccDescription,
549 setAccDescription,
550 getDiagramTitle,
551 setDiagramTitle,
552 clear
553};
554function max(values, valueof) {
555 let max2;
556 if (valueof === void 0) {
557 for (const value2 of values) {
558 if (value2 != null && (max2 < value2 || max2 === void 0 && value2 >= value2)) {
559 max2 = value2;
560 }
561 }
562 } else {
563 let index = -1;
564 for (let value2 of values) {
565 if ((value2 = valueof(value2, ++index, values)) != null && (max2 < value2 || max2 === void 0 && value2 >= value2)) {
566 max2 = value2;
567 }
568 }
569 }
570 return max2;
571}
572function min(values, valueof) {
573 let min2;
574 if (valueof === void 0) {
575 for (const value2 of values) {
576 if (value2 != null && (min2 > value2 || min2 === void 0 && value2 >= value2)) {
577 min2 = value2;
578 }
579 }
580 } else {
581 let index = -1;
582 for (let value2 of values) {
583 if ((value2 = valueof(value2, ++index, values)) != null && (min2 > value2 || min2 === void 0 && value2 >= value2)) {
584 min2 = value2;
585 }
586 }
587 }
588 return min2;
589}
590function sum(values, valueof) {
591 let sum2 = 0;
592 if (valueof === void 0) {
593 for (let value2 of values) {
594 if (value2 = +value2) {
595 sum2 += value2;
596 }
597 }
598 } else {
599 let index = -1;
600 for (let value2 of values) {
601 if (value2 = +valueof(value2, ++index, values)) {
602 sum2 += value2;
603 }
604 }
605 }
606 return sum2;
607}
608function targetDepth(d) {
609 return d.target.depth;
610}
611function left(node) {
612 return node.depth;
613}
614function right(node, n) {
615 return n - 1 - node.height;
616}
617function justify(node, n) {
618 return node.sourceLinks.length ? node.depth : n - 1;
619}
620function center(node) {
621 return node.targetLinks.length ? node.depth : node.sourceLinks.length ? min(node.sourceLinks, targetDepth) - 1 : 0;
622}
623function constant$1(x2) {
624 return function() {
625 return x2;
626 };
627}
628function ascendingSourceBreadth(a, b) {
629 return ascendingBreadth(a.source, b.source) || a.index - b.index;
630}
631function ascendingTargetBreadth(a, b) {
632 return ascendingBreadth(a.target, b.target) || a.index - b.index;
633}
634function ascendingBreadth(a, b) {
635 return a.y0 - b.y0;
636}
637function value(d) {
638 return d.value;
639}
640function defaultId(d) {
641 return d.index;
642}
643function defaultNodes(graph) {
644 return graph.nodes;
645}
646function defaultLinks(graph) {
647 return graph.links;
648}
649function find(nodeById, id) {
650 const node = nodeById.get(id);
651 if (!node)
652 throw new Error("missing: " + id);
653 return node;
654}
655function computeLinkBreadths({ nodes: nodes2 }) {
656 for (const node of nodes2) {
657 let y0 = node.y0;
658 let y1 = y0;
659 for (const link2 of node.sourceLinks) {
660 link2.y0 = y0 + link2.width / 2;
661 y0 += link2.width;
662 }
663 for (const link2 of node.targetLinks) {
664 link2.y1 = y1 + link2.width / 2;
665 y1 += link2.width;
666 }
667 }
668}
669function Sankey() {
670 let x0 = 0, y0 = 0, x1 = 1, y1 = 1;
671 let dx = 24;
672 let dy = 8, py;
673 let id = defaultId;
674 let align = justify;
675 let sort;
676 let linkSort;
677 let nodes2 = defaultNodes;
678 let links2 = defaultLinks;
679 let iterations = 6;
680 function sankey() {
681 const graph = { nodes: nodes2.apply(null, arguments), links: links2.apply(null, arguments) };
682 computeNodeLinks(graph);
683 computeNodeValues(graph);
684 computeNodeDepths(graph);
685 computeNodeHeights(graph);
686 computeNodeBreadths(graph);
687 computeLinkBreadths(graph);
688 return graph;
689 }
690 sankey.update = function(graph) {
691 computeLinkBreadths(graph);
692 return graph;
693 };
694 sankey.nodeId = function(_) {
695 return arguments.length ? (id = typeof _ === "function" ? _ : constant$1(_), sankey) : id;
696 };
697 sankey.nodeAlign = function(_) {
698 return arguments.length ? (align = typeof _ === "function" ? _ : constant$1(_), sankey) : align;
699 };
700 sankey.nodeSort = function(_) {
701 return arguments.length ? (sort = _, sankey) : sort;
702 };
703 sankey.nodeWidth = function(_) {
704 return arguments.length ? (dx = +_, sankey) : dx;
705 };
706 sankey.nodePadding = function(_) {
707 return arguments.length ? (dy = py = +_, sankey) : dy;
708 };
709 sankey.nodes = function(_) {
710 return arguments.length ? (nodes2 = typeof _ === "function" ? _ : constant$1(_), sankey) : nodes2;
711 };
712 sankey.links = function(_) {
713 return arguments.length ? (links2 = typeof _ === "function" ? _ : constant$1(_), sankey) : links2;
714 };
715 sankey.linkSort = function(_) {
716 return arguments.length ? (linkSort = _, sankey) : linkSort;
717 };
718 sankey.size = function(_) {
719 return arguments.length ? (x0 = y0 = 0, x1 = +_[0], y1 = +_[1], sankey) : [x1 - x0, y1 - y0];
720 };
721 sankey.extent = function(_) {
722 return arguments.length ? (x0 = +_[0][0], x1 = +_[1][0], y0 = +_[0][1], y1 = +_[1][1], sankey) : [[x0, y0], [x1, y1]];
723 };
724 sankey.iterations = function(_) {
725 return arguments.length ? (iterations = +_, sankey) : iterations;
726 };
727 function computeNodeLinks({ nodes: nodes3, links: links3 }) {
728 for (const [i, node] of nodes3.entries()) {
729 node.index = i;
730 node.sourceLinks = [];
731 node.targetLinks = [];
732 }
733 const nodeById = new Map(nodes3.map((d, i) => [id(d, i, nodes3), d]));
734 for (const [i, link2] of links3.entries()) {
735 link2.index = i;
736 let { source, target } = link2;
737 if (typeof source !== "object")
738 source = link2.source = find(nodeById, source);
739 if (typeof target !== "object")
740 target = link2.target = find(nodeById, target);
741 source.sourceLinks.push(link2);
742 target.targetLinks.push(link2);
743 }
744 if (linkSort != null) {
745 for (const { sourceLinks, targetLinks } of nodes3) {
746 sourceLinks.sort(linkSort);
747 targetLinks.sort(linkSort);
748 }
749 }
750 }
751 function computeNodeValues({ nodes: nodes3 }) {
752 for (const node of nodes3) {
753 node.value = node.fixedValue === void 0 ? Math.max(sum(node.sourceLinks, value), sum(node.targetLinks, value)) : node.fixedValue;
754 }
755 }
756 function computeNodeDepths({ nodes: nodes3 }) {
757 const n = nodes3.length;
758 let current = new Set(nodes3);
759 let next = /* @__PURE__ */ new Set();
760 let x2 = 0;
761 while (current.size) {
762 for (const node of current) {
763 node.depth = x2;
764 for (const { target } of node.sourceLinks) {
765 next.add(target);
766 }
767 }
768 if (++x2 > n)
769 throw new Error("circular link");
770 current = next;
771 next = /* @__PURE__ */ new Set();
772 }
773 }
774 function computeNodeHeights({ nodes: nodes3 }) {
775 const n = nodes3.length;
776 let current = new Set(nodes3);
777 let next = /* @__PURE__ */ new Set();
778 let x2 = 0;
779 while (current.size) {
780 for (const node of current) {
781 node.height = x2;
782 for (const { source } of node.targetLinks) {
783 next.add(source);
784 }
785 }
786 if (++x2 > n)
787 throw new Error("circular link");
788 current = next;
789 next = /* @__PURE__ */ new Set();
790 }
791 }
792 function computeNodeLayers({ nodes: nodes3 }) {
793 const x2 = max(nodes3, (d) => d.depth) + 1;
794 const kx = (x1 - x0 - dx) / (x2 - 1);
795 const columns = new Array(x2);
796 for (const node of nodes3) {
797 const i = Math.max(0, Math.min(x2 - 1, Math.floor(align.call(null, node, x2))));
798 node.layer = i;
799 node.x0 = x0 + i * kx;
800 node.x1 = node.x0 + dx;
801 if (columns[i])
802 columns[i].push(node);
803 else
804 columns[i] = [node];
805 }
806 if (sort)
807 for (const column of columns) {
808 column.sort(sort);
809 }
810 return columns;
811 }
812 function initializeNodeBreadths(columns) {
813 const ky = min(columns, (c) => (y1 - y0 - (c.length - 1) * py) / sum(c, value));
814 for (const nodes3 of columns) {
815 let y2 = y0;
816 for (const node of nodes3) {
817 node.y0 = y2;
818 node.y1 = y2 + node.value * ky;
819 y2 = node.y1 + py;
820 for (const link2 of node.sourceLinks) {
821 link2.width = link2.value * ky;
822 }
823 }
824 y2 = (y1 - y2 + py) / (nodes3.length + 1);
825 for (let i = 0; i < nodes3.length; ++i) {
826 const node = nodes3[i];
827 node.y0 += y2 * (i + 1);
828 node.y1 += y2 * (i + 1);
829 }
830 reorderLinks(nodes3);
831 }
832 }
833 function computeNodeBreadths(graph) {
834 const columns = computeNodeLayers(graph);
835 py = Math.min(dy, (y1 - y0) / (max(columns, (c) => c.length) - 1));
836 initializeNodeBreadths(columns);
837 for (let i = 0; i < iterations; ++i) {
838 const alpha = Math.pow(0.99, i);
839 const beta = Math.max(1 - alpha, (i + 1) / iterations);
840 relaxRightToLeft(columns, alpha, beta);
841 relaxLeftToRight(columns, alpha, beta);
842 }
843 }
844 function relaxLeftToRight(columns, alpha, beta) {
845 for (let i = 1, n = columns.length; i < n; ++i) {
846 const column = columns[i];
847 for (const target of column) {
848 let y2 = 0;
849 let w = 0;
850 for (const { source, value: value2 } of target.targetLinks) {
851 let v = value2 * (target.layer - source.layer);
852 y2 += targetTop(source, target) * v;
853 w += v;
854 }
855 if (!(w > 0))
856 continue;
857 let dy2 = (y2 / w - target.y0) * alpha;
858 target.y0 += dy2;
859 target.y1 += dy2;
860 reorderNodeLinks(target);
861 }
862 if (sort === void 0)
863 column.sort(ascendingBreadth);
864 resolveCollisions(column, beta);
865 }
866 }
867 function relaxRightToLeft(columns, alpha, beta) {
868 for (let n = columns.length, i = n - 2; i >= 0; --i) {
869 const column = columns[i];
870 for (const source of column) {
871 let y2 = 0;
872 let w = 0;
873 for (const { target, value: value2 } of source.sourceLinks) {
874 let v = value2 * (target.layer - source.layer);
875 y2 += sourceTop(source, target) * v;
876 w += v;
877 }
878 if (!(w > 0))
879 continue;
880 let dy2 = (y2 / w - source.y0) * alpha;
881 source.y0 += dy2;
882 source.y1 += dy2;
883 reorderNodeLinks(source);
884 }
885 if (sort === void 0)
886 column.sort(ascendingBreadth);
887 resolveCollisions(column, beta);
888 }
889 }
890 function resolveCollisions(nodes3, alpha) {
891 const i = nodes3.length >> 1;
892 const subject = nodes3[i];
893 resolveCollisionsBottomToTop(nodes3, subject.y0 - py, i - 1, alpha);
894 resolveCollisionsTopToBottom(nodes3, subject.y1 + py, i + 1, alpha);
895 resolveCollisionsBottomToTop(nodes3, y1, nodes3.length - 1, alpha);
896 resolveCollisionsTopToBottom(nodes3, y0, 0, alpha);
897 }
898 function resolveCollisionsTopToBottom(nodes3, y2, i, alpha) {
899 for (; i < nodes3.length; ++i) {
900 const node = nodes3[i];
901 const dy2 = (y2 - node.y0) * alpha;
902 if (dy2 > 1e-6)
903 node.y0 += dy2, node.y1 += dy2;
904 y2 = node.y1 + py;
905 }
906 }
907 function resolveCollisionsBottomToTop(nodes3, y2, i, alpha) {
908 for (; i >= 0; --i) {
909 const node = nodes3[i];
910 const dy2 = (node.y1 - y2) * alpha;
911 if (dy2 > 1e-6)
912 node.y0 -= dy2, node.y1 -= dy2;
913 y2 = node.y0 - py;
914 }
915 }
916 function reorderNodeLinks({ sourceLinks, targetLinks }) {
917 if (linkSort === void 0) {
918 for (const { source: { sourceLinks: sourceLinks2 } } of targetLinks) {
919 sourceLinks2.sort(ascendingTargetBreadth);
920 }
921 for (const { target: { targetLinks: targetLinks2 } } of sourceLinks) {
922 targetLinks2.sort(ascendingSourceBreadth);
923 }
924 }
925 }
926 function reorderLinks(nodes3) {
927 if (linkSort === void 0) {
928 for (const { sourceLinks, targetLinks } of nodes3) {
929 sourceLinks.sort(ascendingTargetBreadth);
930 targetLinks.sort(ascendingSourceBreadth);
931 }
932 }
933 }
934 function targetTop(source, target) {
935 let y2 = source.y0 - (source.sourceLinks.length - 1) * py / 2;
936 for (const { target: node, width } of source.sourceLinks) {
937 if (node === target)
938 break;
939 y2 += width + py;
940 }
941 for (const { source: node, width } of target.targetLinks) {
942 if (node === source)
943 break;
944 y2 -= width;
945 }
946 return y2;
947 }
948 function sourceTop(source, target) {
949 let y2 = target.y0 - (target.targetLinks.length - 1) * py / 2;
950 for (const { source: node, width } of target.targetLinks) {
951 if (node === source)
952 break;
953 y2 += width + py;
954 }
955 for (const { target: node, width } of source.sourceLinks) {
956 if (node === target)
957 break;
958 y2 -= width;
959 }
960 return y2;
961 }
962 return sankey;
963}
964var pi = Math.PI, tau = 2 * pi, epsilon = 1e-6, tauEpsilon = tau - epsilon;
965function Path() {
966 this._x0 = this._y0 = // start of current subpath
967 this._x1 = this._y1 = null;
968 this._ = "";
969}
970function path() {
971 return new Path();
972}
973Path.prototype = path.prototype = {
974 constructor: Path,
975 moveTo: function(x2, y2) {
976 this._ += "M" + (this._x0 = this._x1 = +x2) + "," + (this._y0 = this._y1 = +y2);
977 },
978 closePath: function() {
979 if (this._x1 !== null) {
980 this._x1 = this._x0, this._y1 = this._y0;
981 this._ += "Z";
982 }
983 },
984 lineTo: function(x2, y2) {
985 this._ += "L" + (this._x1 = +x2) + "," + (this._y1 = +y2);
986 },
987 quadraticCurveTo: function(x1, y1, x2, y2) {
988 this._ += "Q" + +x1 + "," + +y1 + "," + (this._x1 = +x2) + "," + (this._y1 = +y2);
989 },
990 bezierCurveTo: function(x1, y1, x2, y2, x3, y3) {
991 this._ += "C" + +x1 + "," + +y1 + "," + +x2 + "," + +y2 + "," + (this._x1 = +x3) + "," + (this._y1 = +y3);
992 },
993 arcTo: function(x1, y1, x2, y2, r) {
994 x1 = +x1, y1 = +y1, x2 = +x2, y2 = +y2, r = +r;
995 var x0 = this._x1, y0 = this._y1, x21 = x2 - x1, y21 = y2 - y1, x01 = x0 - x1, y01 = y0 - y1, l01_2 = x01 * x01 + y01 * y01;
996 if (r < 0)
997 throw new Error("negative radius: " + r);
998 if (this._x1 === null) {
999 this._ += "M" + (this._x1 = x1) + "," + (this._y1 = y1);
1000 } else if (!(l01_2 > epsilon))
1001 ;
1002 else if (!(Math.abs(y01 * x21 - y21 * x01) > epsilon) || !r) {
1003 this._ += "L" + (this._x1 = x1) + "," + (this._y1 = y1);
1004 } else {
1005 var x20 = x2 - x0, y20 = y2 - y0, l21_2 = x21 * x21 + y21 * y21, l20_2 = x20 * x20 + y20 * y20, l21 = Math.sqrt(l21_2), l01 = Math.sqrt(l01_2), l = r * Math.tan((pi - Math.acos((l21_2 + l01_2 - l20_2) / (2 * l21 * l01))) / 2), t01 = l / l01, t21 = l / l21;
1006 if (Math.abs(t01 - 1) > epsilon) {
1007 this._ += "L" + (x1 + t01 * x01) + "," + (y1 + t01 * y01);
1008 }
1009 this._ += "A" + r + "," + r + ",0,0," + +(y01 * x20 > x01 * y20) + "," + (this._x1 = x1 + t21 * x21) + "," + (this._y1 = y1 + t21 * y21);
1010 }
1011 },
1012 arc: function(x2, y2, r, a0, a1, ccw) {
1013 x2 = +x2, y2 = +y2, r = +r, ccw = !!ccw;
1014 var dx = r * Math.cos(a0), dy = r * Math.sin(a0), x0 = x2 + dx, y0 = y2 + dy, cw = 1 ^ ccw, da = ccw ? a0 - a1 : a1 - a0;
1015 if (r < 0)
1016 throw new Error("negative radius: " + r);
1017 if (this._x1 === null) {
1018 this._ += "M" + x0 + "," + y0;
1019 } else if (Math.abs(this._x1 - x0) > epsilon || Math.abs(this._y1 - y0) > epsilon) {
1020 this._ += "L" + x0 + "," + y0;
1021 }
1022 if (!r)
1023 return;
1024 if (da < 0)
1025 da = da % tau + tau;
1026 if (da > tauEpsilon) {
1027 this._ += "A" + r + "," + r + ",0,1," + cw + "," + (x2 - dx) + "," + (y2 - dy) + "A" + r + "," + r + ",0,1," + cw + "," + (this._x1 = x0) + "," + (this._y1 = y0);
1028 } else if (da > epsilon) {
1029 this._ += "A" + r + "," + r + ",0," + +(da >= pi) + "," + cw + "," + (this._x1 = x2 + r * Math.cos(a1)) + "," + (this._y1 = y2 + r * Math.sin(a1));
1030 }
1031 },
1032 rect: function(x2, y2, w, h) {
1033 this._ += "M" + (this._x0 = this._x1 = +x2) + "," + (this._y0 = this._y1 = +y2) + "h" + +w + "v" + +h + "h" + -w + "Z";
1034 },
1035 toString: function() {
1036 return this._;
1037 }
1038};
1039function constant(x2) {
1040 return function constant2() {
1041 return x2;
1042 };
1043}
1044function x(p) {
1045 return p[0];
1046}
1047function y(p) {
1048 return p[1];
1049}
1050var slice = Array.prototype.slice;
1051function linkSource(d) {
1052 return d.source;
1053}
1054function linkTarget(d) {
1055 return d.target;
1056}
1057function link(curve) {
1058 var source = linkSource, target = linkTarget, x$1 = x, y$1 = y, context = null;
1059 function link2() {
1060 var buffer, argv = slice.call(arguments), s = source.apply(this, argv), t = target.apply(this, argv);
1061 if (!context)
1062 context = buffer = path();
1063 curve(context, +x$1.apply(this, (argv[0] = s, argv)), +y$1.apply(this, argv), +x$1.apply(this, (argv[0] = t, argv)), +y$1.apply(this, argv));
1064 if (buffer)
1065 return context = null, buffer + "" || null;
1066 }
1067 link2.source = function(_) {
1068 return arguments.length ? (source = _, link2) : source;
1069 };
1070 link2.target = function(_) {
1071 return arguments.length ? (target = _, link2) : target;
1072 };
1073 link2.x = function(_) {
1074 return arguments.length ? (x$1 = typeof _ === "function" ? _ : constant(+_), link2) : x$1;
1075 };
1076 link2.y = function(_) {
1077 return arguments.length ? (y$1 = typeof _ === "function" ? _ : constant(+_), link2) : y$1;
1078 };
1079 link2.context = function(_) {
1080 return arguments.length ? (context = _ == null ? null : _, link2) : context;
1081 };
1082 return link2;
1083}
1084function curveHorizontal(context, x0, y0, x1, y1) {
1085 context.moveTo(x0, y0);
1086 context.bezierCurveTo(x0 = (x0 + x1) / 2, y0, x0, y1, x1, y1);
1087}
1088function linkHorizontal() {
1089 return link(curveHorizontal);
1090}
1091function horizontalSource(d) {
1092 return [d.source.x1, d.y0];
1093}
1094function horizontalTarget(d) {
1095 return [d.target.x0, d.y1];
1096}
1097function d3SankeyLinkHorizontal() {
1098 return linkHorizontal().source(horizontalSource).target(horizontalTarget);
1099}
1100const _Uid = class _Uid2 {
1101 static next(name) {
1102 return new _Uid2(name + ++_Uid2.count);
1103 }
1104 constructor(id) {
1105 this.id = id;
1106 this.href = `#${id}`;
1107 }
1108 toString() {
1109 return "url(" + this.href + ")";
1110 }
1111};
1112_Uid.count = 0;
1113let Uid = _Uid;
1114const alignmentsMap = {
1115 left,
1116 right,
1117 center,
1118 justify
1119};
1120const draw = function(text, id, _version, diagObj) {
1121 const { securityLevel, sankey: conf } = getConfig();
1122 const defaultSankeyConfig = defaultConfig.sankey;
1123 let sandboxElement;
1124 if (securityLevel === "sandbox") {
1125 sandboxElement = d3select("#i" + id);
1126 }
1127 const root = securityLevel === "sandbox" ? d3select(sandboxElement.nodes()[0].contentDocument.body) : d3select("body");
1128 const svg = securityLevel === "sandbox" ? root.select(`[id="${id}"]`) : d3select(`[id="${id}"]`);
1129 const width = (conf == null ? void 0 : conf.width) ?? defaultSankeyConfig.width;
1130 const height = (conf == null ? void 0 : conf.height) ?? defaultSankeyConfig.width;
1131 const useMaxWidth = (conf == null ? void 0 : conf.useMaxWidth) ?? defaultSankeyConfig.useMaxWidth;
1132 const nodeAlignment = (conf == null ? void 0 : conf.nodeAlignment) ?? defaultSankeyConfig.nodeAlignment;
1133 const prefix = (conf == null ? void 0 : conf.prefix) ?? defaultSankeyConfig.prefix;
1134 const suffix = (conf == null ? void 0 : conf.suffix) ?? defaultSankeyConfig.suffix;
1135 const showValues = (conf == null ? void 0 : conf.showValues) ?? defaultSankeyConfig.showValues;
1136 const graph = diagObj.db.getGraph();
1137 const nodeAlign = alignmentsMap[nodeAlignment];
1138 const nodeWidth = 10;
1139 const sankey = Sankey().nodeId((d) => d.id).nodeWidth(nodeWidth).nodePadding(10 + (showValues ? 15 : 0)).nodeAlign(nodeAlign).extent([
1140 [0, 0],
1141 [width, height]
1142 ]);
1143 sankey(graph);
1144 const colorScheme = ordinal(d3schemeTableau10);
1145 svg.append("g").attr("class", "nodes").selectAll(".node").data(graph.nodes).join("g").attr("class", "node").attr("id", (d) => (d.uid = Uid.next("node-")).id).attr("transform", function(d) {
1146 return "translate(" + d.x0 + "," + d.y0 + ")";
1147 }).attr("x", (d) => d.x0).attr("y", (d) => d.y0).append("rect").attr("height", (d) => {
1148 return d.y1 - d.y0;
1149 }).attr("width", (d) => d.x1 - d.x0).attr("fill", (d) => colorScheme(d.id));
1150 const getText = ({ id: id2, value: value2 }) => {
1151 if (!showValues) {
1152 return id2;
1153 }
1154 return `${id2}
1155${prefix}${Math.round(value2 * 100) / 100}${suffix}`;
1156 };
1157 svg.append("g").attr("class", "node-labels").attr("font-family", "sans-serif").attr("font-size", 14).selectAll("text").data(graph.nodes).join("text").attr("x", (d) => d.x0 < width / 2 ? d.x1 + 6 : d.x0 - 6).attr("y", (d) => (d.y1 + d.y0) / 2).attr("dy", `${showValues ? "0" : "0.35"}em`).attr("text-anchor", (d) => d.x0 < width / 2 ? "start" : "end").text(getText);
1158 const link2 = svg.append("g").attr("class", "links").attr("fill", "none").attr("stroke-opacity", 0.5).selectAll(".link").data(graph.links).join("g").attr("class", "link").style("mix-blend-mode", "multiply");
1159 const linkColor = (conf == null ? void 0 : conf.linkColor) || "gradient";
1160 if (linkColor === "gradient") {
1161 const gradient = link2.append("linearGradient").attr("id", (d) => (d.uid = Uid.next("linearGradient-")).id).attr("gradientUnits", "userSpaceOnUse").attr("x1", (d) => d.source.x1).attr("x2", (d) => d.target.x0);
1162 gradient.append("stop").attr("offset", "0%").attr("stop-color", (d) => colorScheme(d.source.id));
1163 gradient.append("stop").attr("offset", "100%").attr("stop-color", (d) => colorScheme(d.target.id));
1164 }
1165 let coloring;
1166 switch (linkColor) {
1167 case "gradient":
1168 coloring = (d) => d.uid;
1169 break;
1170 case "source":
1171 coloring = (d) => colorScheme(d.source.id);
1172 break;
1173 case "target":
1174 coloring = (d) => colorScheme(d.target.id);
1175 break;
1176 default:
1177 coloring = linkColor;
1178 }
1179 link2.append("path").attr("d", d3SankeyLinkHorizontal()).attr("stroke", coloring).attr("stroke-width", (d) => Math.max(1, d.width));
1180 setupGraphViewbox(void 0, svg, 0, useMaxWidth);
1181};
1182const renderer = {
1183 draw
1184};
1185const prepareTextForParsing = (text) => {
1186 const textToParse = text.replaceAll(/^[^\S\n\r]+|[^\S\n\r]+$/g, "").replaceAll(/([\n\r])+/g, "\n").trim();
1187 return textToParse;
1188};
1189const originalParse = parser$1.parse.bind(parser$1);
1190parser$1.parse = (text) => originalParse(prepareTextForParsing(text));
1191const diagram = {
1192 parser: parser$1,
1193 db,
1194 renderer
1195};
1196export {
1197 diagram
1198};