mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-12-03 02:54:06 +01:00
Added options for coloring
This commit is contained in:
@@ -98,6 +98,12 @@
|
||||
flowchart: { curve: 'basis' },
|
||||
gantt: { axisFormat: '%m/%d/%Y' },
|
||||
sequence: { actorMargin: 50 },
|
||||
sankey: {
|
||||
title: 'Hey, this is Sankey-Beta',
|
||||
width: 1200,
|
||||
height: 600,
|
||||
linkColor: 'gradient',
|
||||
},
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
|
||||
@@ -412,7 +412,17 @@ export interface FlowchartDiagramConfig extends BaseDiagramConfig {
|
||||
wrappingWidth?: number;
|
||||
}
|
||||
|
||||
export type SankeyDiagramConfig = BaseDiagramConfig;
|
||||
export enum SankeyLinkColor {
|
||||
source = 'source',
|
||||
target = 'target',
|
||||
gradient = 'gradient',
|
||||
}
|
||||
|
||||
export interface SankeyDiagramConfig extends BaseDiagramConfig {
|
||||
width?: number;
|
||||
height?: number;
|
||||
linkColor?: SankeyLinkColor | string;
|
||||
}
|
||||
|
||||
export interface FontConfig {
|
||||
fontSize?: string | number;
|
||||
|
||||
@@ -2270,6 +2270,11 @@ const config: Partial<MermaidConfig> = {
|
||||
padding: 10,
|
||||
maxNodeWidth: 200,
|
||||
},
|
||||
sankey: {
|
||||
width: 800,
|
||||
height: 400,
|
||||
linkColor: 'gradient',
|
||||
},
|
||||
fontSize: 16,
|
||||
};
|
||||
|
||||
|
||||
@@ -20,9 +20,10 @@ import {
|
||||
} from 'd3-sankey';
|
||||
import { configureSvgSize } from '../../setupGraphViewbox.js';
|
||||
import { Uid } from './sankeyUtils.js';
|
||||
import { SankeyLinkColor } from '../../config.type.js';
|
||||
|
||||
/**
|
||||
* Draws a sequenceDiagram in the tag with id: id based on the graph definition in text.
|
||||
* Draws Sankey diagram.
|
||||
*
|
||||
* @param text - The text of the diagram
|
||||
* @param id - The id of the diagram which will be used as a DOM element id¨
|
||||
@@ -30,10 +31,14 @@ import { Uid } from './sankeyUtils.js';
|
||||
* @param diagObj - A standard diagram containing the db and the text and type etc of the diagram
|
||||
*/
|
||||
export const draw = function (text: string, id: string, _version: string, diagObj: Diagram): void {
|
||||
// TODO: Figure out what is happening there
|
||||
// Get Sankey config
|
||||
const { securityLevel, sankey: conf } = configApi.getConfig();
|
||||
|
||||
// TODO:
|
||||
// This code repeats for every diagram
|
||||
// Figure out what is happening there, probably it should be separated
|
||||
// The main thing is svg object that is a d3 wrapper for svg operations
|
||||
//
|
||||
const { securityLevel, sequence: conf } = configApi.getConfig();
|
||||
let sandboxElement: any;
|
||||
if (securityLevel === 'sandbox') {
|
||||
sandboxElement = d3select('#i' + id);
|
||||
@@ -43,18 +48,17 @@ export const draw = function (text: string, id: string, _version: string, diagOb
|
||||
if (securityLevel === 'sandbox' && sandboxElement) {
|
||||
root = d3select(sandboxElement.nodes()[0].contentDocument.body);
|
||||
}
|
||||
const doc = securityLevel === 'sandbox' ? sandboxElement.nodes()[0].contentDocument : document;
|
||||
const svg = securityLevel === 'sandbox' ? root.select(`[id="${id}"]`) : d3select(`[id="${id}"]`);
|
||||
|
||||
// Establish svg dimensions and get width and height
|
||||
//
|
||||
const elem = doc.getElementById(id);
|
||||
const width = elem.parentElement.offsetWidth;
|
||||
const height = 600;
|
||||
const width = conf?.width || 800;
|
||||
const height = conf?.height || 400;
|
||||
const useMaxWidth = conf?.useMaxWidth || false;
|
||||
|
||||
// FIX: using max width prevents height from being set
|
||||
configureSvgSize(svg, height, width, false);
|
||||
// svg.attr('height', height); // that's why we need this line
|
||||
// FIX: using max width prevents height from being set, is it intended?
|
||||
// to add height directly one can use `svg.attr('height', height)`
|
||||
configureSvgSize(svg, height, width, useMaxWidth);
|
||||
|
||||
// Prepare data for construction based on diagObj.db
|
||||
// This must be a mutable object with `nodes` and `links` properties:
|
||||
@@ -142,6 +146,9 @@ export const draw = function (text: string, id: string, _version: string, diagOb
|
||||
.attr('class', 'link')
|
||||
.style('mix-blend-mode', 'multiply');
|
||||
|
||||
const linkColor = conf?.linkColor || SankeyLinkColor.gradient;
|
||||
|
||||
if (linkColor === SankeyLinkColor.gradient) {
|
||||
const gradient = link
|
||||
.append('linearGradient')
|
||||
.attr('id', (d) => (d.uid = Uid.next('linearGradient-')).id)
|
||||
@@ -158,11 +165,27 @@ export const draw = function (text: string, id: string, _version: string, diagOb
|
||||
.append('stop')
|
||||
.attr('offset', '100%')
|
||||
.attr('stop-color', (d) => colorScheme(d.target.id));
|
||||
}
|
||||
|
||||
let coloring: any;
|
||||
switch (linkColor) {
|
||||
case SankeyLinkColor.gradient:
|
||||
coloring = (d) => d.uid;
|
||||
break;
|
||||
case SankeyLinkColor.source:
|
||||
coloring = (d) => d.source.id;
|
||||
break;
|
||||
case SankeyLinkColor.target:
|
||||
coloring = (d) => d.target.id;
|
||||
break;
|
||||
default:
|
||||
coloring = linkColor;
|
||||
}
|
||||
|
||||
link
|
||||
.append('path')
|
||||
.attr('d', d3SankeyLinkHorizontal())
|
||||
.attr('stroke', (d: any) => d.uid)
|
||||
.attr('stroke', coloring)
|
||||
.attr('stroke-width', (d: any) => Math.max(1, d.width));
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user