Skip to content

Commit

Permalink
Merge pull request #4748 from mermaid-js/feature/4674_sankeyValues
Browse files Browse the repository at this point in the history
feat(sankey): Show values
  • Loading branch information
knsv authored Aug 23, 2023
2 parents 2ab393c + ce6992e commit c3160b2
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 23 deletions.
44 changes: 30 additions & 14 deletions demos/sankey.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,42 @@
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>States Mermaid Quick Test Page</title>
<title>Sankey Mermaid Quick Test Page</title>
<link rel="icon" type="image/png" href="data:image/png;base64,iVBORw0KGgo=" />
<style>
div.mermaid {
/* font-family: 'trebuchet ms', verdana, arial; */
font-family: 'Courier New', Courier, monospace !important;
}
</style>
</head>

<body>
<h1>Sankey diagram demos</h1>
<h2>FY20-21 Performance</h2>
<pre class="mermaid">
---
config:
sankey:
showValues: true
prefix: $
suffix: B
width: 800
nodeAlignment: left
---
sankey-beta
Revenue,Expenses,10
Revenue,Profit,10
Expenses,Manufacturing,5
Expenses,Tax,3
Expenses,Research,2
</pre>

<h2>Energy flow</h2>
<pre class="mermaid">
---
config:
sankey:
showValues: false
width: 1200
height: 600
linkColor: gradient
nodeAlignment: justify
---
sankey-beta

Agricultural 'waste',Bio-conversion,124.729
Expand Down Expand Up @@ -95,13 +117,7 @@ <h2>Energy flow</h2>
theme: 'default',
logLevel: 3,
securityLevel: 'loose',
sankey: {
title: 'Hey, this is Sankey-Beta',
width: 1200,
height: 600,
linkColor: 'gradient',
nodeAlignment: 'justify',
},
startOnLoad: true,
});
</script>
</body>
Expand Down
15 changes: 15 additions & 0 deletions packages/mermaid/src/config.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1298,6 +1298,21 @@ export interface SankeyDiagramConfig extends BaseDiagramConfig {
*/
nodeAlignment?: 'left' | 'right' | 'center' | 'justify';
useMaxWidth?: boolean;
/**
* Toggle to display or hide values along with title.
*
*/
showValues?: boolean;
/**
* The prefix to use for values
*
*/
prefix?: string;
/**
* The suffix to use for values
*
*/
suffix?: string;
}
/**
* This interface was referenced by `MermaidConfig`'s JSON-Schema
Expand Down
2 changes: 1 addition & 1 deletion packages/mermaid/src/diagrams/sankey/sankeyDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class SankeyLink {
/**
* @param source - Node where the link starts
* @param target - Node where the link ends
* @param value - number, float or integer, describes the amount to be passed
* @param value - Describes the amount to be passed
*/
const addLink = (source: SankeyNode, target: SankeyNode, value: number): void => {
links.push(new SankeyLink(source, target, value));
Expand Down
26 changes: 18 additions & 8 deletions packages/mermaid/src/diagrams/sankey/sankeyRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
} from 'd3-sankey';
import { configureSvgSize } from '../../setupGraphViewbox.js';
import { Uid } from '../../rendering-util/uid.js';
import type { SankeyLinkColor, SankeyNodeAlignment } from '../../config.type.js';
import type { SankeyNodeAlignment } from '../../config.type.js';

// Map config options to alignment functions
const alignmentsMap: Record<
Expand Down Expand Up @@ -62,10 +62,13 @@ export const draw = function (text: string, id: string, _version: string, diagOb

// Establish svg dimensions and get width and height
//
const width = conf?.width || defaultSankeyConfig.width!;
const height = conf?.height || defaultSankeyConfig.width!;
const useMaxWidth = conf?.useMaxWidth || defaultSankeyConfig.useMaxWidth!;
const nodeAlignment = conf?.nodeAlignment || defaultSankeyConfig.nodeAlignment!;
const width = conf?.width ?? defaultSankeyConfig.width!;
const height = conf?.height ?? defaultSankeyConfig.width!;
const useMaxWidth = conf?.useMaxWidth ?? defaultSankeyConfig.useMaxWidth!;
const nodeAlignment = conf?.nodeAlignment ?? defaultSankeyConfig.nodeAlignment!;
const prefix = conf?.prefix ?? defaultSankeyConfig.prefix!;
const suffix = conf?.suffix ?? defaultSankeyConfig.suffix!;
const showValues = conf?.showValues ?? defaultSankeyConfig.showValues!;

// FIX: using max width prevents height from being set, is it intended?
// to add height directly one can use `svg.attr('height', height)`
Expand Down Expand Up @@ -94,7 +97,7 @@ export const draw = function (text: string, id: string, _version: string, diagOb
const sankey = d3Sankey()
.nodeId((d: any) => d.id) // we use 'id' property to identify node
.nodeWidth(nodeWidth)
.nodePadding(10)
.nodePadding(10 + (showValues ? 15 : 0))
.nodeAlign(nodeAlign)
.extent([
[0, 0],
Expand Down Expand Up @@ -130,6 +133,13 @@ export const draw = function (text: string, id: string, _version: string, diagOb
.attr('width', (d: any) => d.x1 - d.x0)
.attr('fill', (d: any) => colorScheme(d.id));

const getText = ({ id, value }: { id: string; value: number }) => {
if (!showValues) {
return id;
}
return `${id}\n${prefix}${Math.round(value * 100) / 100}${suffix}`;
};

// Create labels for nodes
svg
.append('g')
Expand All @@ -141,9 +151,9 @@ export const draw = function (text: string, id: string, _version: string, diagOb
.join('text')
.attr('x', (d: any) => (d.x0 < width / 2 ? d.x1 + 6 : d.x0 - 6))
.attr('y', (d: any) => (d.y1 + d.y0) / 2)
.attr('dy', '0.35em')
.attr('dy', `${showValues ? '0' : '0.35'}em`)
.attr('text-anchor', (d: any) => (d.x0 < width / 2 ? 'start' : 'end'))
.text((d: any) => d.id);
.text(getText);

// Creates the paths that represent the links.
const link = svg
Expand Down
14 changes: 14 additions & 0 deletions packages/mermaid/src/schemas/config.schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1859,6 +1859,20 @@ $defs: # JSON Schema definition (maybe we should move these to a seperate file)
default: justify
useMaxWidth:
default: false
showValues:
description: |
Toggle to display or hide values along with title.
default: true
prefix:
description: |
The prefix to use for values
type: string
default: ''
suffix:
description: |
The suffix to use for values
type: string
default: ''

FontCalculator:
title: Font Calculator
Expand Down

0 comments on commit c3160b2

Please sign in to comment.