diff --git a/packages/mermaid/src/diagram-api/diagram-orchestration.ts b/packages/mermaid/src/diagram-api/diagram-orchestration.ts index 0253be45d..6c9cc4a03 100644 --- a/packages/mermaid/src/diagram-api/diagram-orchestration.ts +++ b/packages/mermaid/src/diagram-api/diagram-orchestration.ts @@ -18,6 +18,7 @@ import errorDiagram from '../diagrams/error/errorDiagram.js'; import flowchartElk from '../diagrams/flowchart/elk/detector.js'; import timeline from '../diagrams/timeline/detector.js'; import mindmap from '../diagrams/mindmap/detector.js'; +import sankey from '../diagrams/sankey/detector.js'; import { registerLazyLoadedDiagrams } from './detectType.js'; import { registerDiagram } from './diagramAPI.js'; @@ -79,6 +80,7 @@ export const addDiagrams = () => { stateV2, state, journey, - quadrantChart + quadrantChart, + sankey, ); }; diff --git a/packages/mermaid/src/diagrams/sankey/db.js b/packages/mermaid/src/diagrams/sankey/db.js new file mode 100644 index 000000000..2c86752c6 --- /dev/null +++ b/packages/mermaid/src/diagrams/sankey/db.js @@ -0,0 +1,69 @@ +import { log } from '../../logger.js'; +import mermaidAPI from '../../mermaidAPI.js'; +import * as configApi from '../../config.js'; +import common from '../common/common.js'; +import { + setAccTitle, + getAccTitle, + setDiagramTitle, + getDiagramTitle, + getAccDescription, + setAccDescription, + clear as commonClear, +} from '../../commonDb.js'; + +let sections = {}; +let showData = false; + +export const parseDirective = function (statement, context, type) { + mermaidAPI.parseDirective(this, statement, context, type); +}; + +const addSection = function (id, value) { + id = common.sanitizeText(id, configApi.getConfig()); + if (sections[id] === undefined) { + sections[id] = value; + log.debug('Added new section :', id); + } +}; +const getSections = () => sections; + +const setShowData = function (toggle) { + showData = toggle; +}; + +const getShowData = function () { + return showData; +}; + +const cleanupValue = function (value) { + if (value.substring(0, 1) === ':') { + value = value.substring(1).trim(); + return Number(value.trim()); + } else { + return Number(value.trim()); + } +}; + +const clear = function () { + sections = {}; + showData = false; + commonClear(); +}; + +export default { + parseDirective, + getConfig: () => configApi.getConfig().pie, + addSection, + getSections, + cleanupValue, + clear, + setAccTitle, + getAccTitle, + setDiagramTitle, + getDiagramTitle, + setShowData, + getShowData, + getAccDescription, + setAccDescription, +}; diff --git a/packages/mermaid/src/diagrams/sankey/detector.ts b/packages/mermaid/src/diagrams/sankey/detector.ts new file mode 100644 index 000000000..6c0bcdae8 --- /dev/null +++ b/packages/mermaid/src/diagrams/sankey/detector.ts @@ -0,0 +1,20 @@ +import type { DiagramDetector, ExternalDiagramDefinition } from '../../diagram-api/types.js'; + +const id = 'sankey'; + +const detector: DiagramDetector = (txt) => { + return txt.match(/^\s*sankey/) !== null; +}; + +const loader = async () => { + const { diagram } = await import('./sankeyDiagram.js'); + return { id, diagram }; +}; + +const plugin: ExternalDiagramDefinition = { + id, + detector, + loader, +}; + +export default plugin; diff --git a/packages/mermaid/src/sankey/parser/desired_syntax.md b/packages/mermaid/src/diagrams/sankey/parser/desired_syntax.md similarity index 96% rename from packages/mermaid/src/sankey/parser/desired_syntax.md rename to packages/mermaid/src/diagrams/sankey/parser/desired_syntax.md index 1f0e58b41..52057fce9 100644 --- a/packages/mermaid/src/sankey/parser/desired_syntax.md +++ b/packages/mermaid/src/diagrams/sankey/parser/desired_syntax.md @@ -67,7 +67,15 @@ Graph is a list of flows (or links). Flow is a sequence `node -> value -> node -> value...` ``` a -> 30 -> b -a -> 40 -> c +a -> 40 -> b +``` + +2 separate streams between 2 nodes they can be grouped as well: +``` +a -> { + 30 + 40 +} -> b ``` All outflows from the node can be grouped: @@ -86,13 +94,15 @@ All inflows to the node can be grouped too: } -> c ``` -2 separate streams between 2 nodes they can be grouped as well: - +Chaining example: ``` a -> { 30 40 -} -> b +} -> b -> { + 20 -> d + 50 -> e +} ``` **Probably ambiguous!** diff --git a/packages/mermaid/src/diagrams/sankey/parser/sankey.jison b/packages/mermaid/src/diagrams/sankey/parser/sankey.jison new file mode 100644 index 000000000..394994927 --- /dev/null +++ b/packages/mermaid/src/diagrams/sankey/parser/sankey.jison @@ -0,0 +1,13 @@ +%lex +%options case-insensitive + +%% + +/lex + +%start graph + +%% + +graph + : node -> \ No newline at end of file diff --git a/packages/mermaid/src/diagrams/sankey/renderer.ts b/packages/mermaid/src/diagrams/sankey/renderer.ts new file mode 100644 index 000000000..cb1e2d33c --- /dev/null +++ b/packages/mermaid/src/diagrams/sankey/renderer.ts @@ -0,0 +1,27 @@ +// import { select, scaleOrdinal, pie as d3pie, arc } from 'd3'; + +// import { select, selectAll } from 'd3'; +// import { log } from '../../logger.js'; +// import common from '../common/common.js'; +// import * as svgDrawCommon from '../common/svgDrawCommon'; +import * as configApi from '../../config.js'; +// import assignWithDepth from '../../assignWithDepth.js'; +// import utils from '../../utils.js'; +// import { configureSvgSize } from '../../setupGraphViewbox.js'; +import { Diagram } from '../../Diagram.js'; + +// import { parseFontSize } from '../../utils.js'; + +const conf = configApi.getConfig(); + +/** + * Draws a Sankey Diagram with the data given in text. + * + * @param text + * @param id + */ +export const draw = function (_text: string, id: string, _version: string, _diagObj: Diagram) {} + +export default { + draw, +}; diff --git a/packages/mermaid/src/diagrams/sankey/sankeyDiagram.ts b/packages/mermaid/src/diagrams/sankey/sankeyDiagram.ts new file mode 100644 index 000000000..e6b502227 --- /dev/null +++ b/packages/mermaid/src/diagrams/sankey/sankeyDiagram.ts @@ -0,0 +1,13 @@ +import { DiagramDefinition } from '../../diagram-api/types.js'; +// @ts-ignore: TODO Fix ts errors +import parser from './parser/sankey.jison'; +import db from './db.js'; +import styles from './styles.js'; +import renderer from './renderer.js'; + +export const diagram: DiagramDefinition = { + parser, + db, + renderer, + styles, +}; diff --git a/packages/mermaid/src/diagrams/sankey/styles.js b/packages/mermaid/src/diagrams/sankey/styles.js new file mode 100644 index 000000000..54881fe6f --- /dev/null +++ b/packages/mermaid/src/diagrams/sankey/styles.js @@ -0,0 +1,5 @@ +const getStyles = (options) => + ` +`; + +export default getStyles;