It’s possible to selectively specify the location of Sankey diagram labels, based on the position of their parent Nodes. This tutorial will show how.
Result JS HTML CSS
Open in: New tab CodePen (opens in a new tab) jsFiddle (opens in a new tab) Run * ---------------------------------------
* This demo was created using amCharts 4.
* For more information visit:
* https://www.amcharts.com/
* Documentation is available at:
* https://www.amcharts.com/docs/v4/
* ---------------------------------------
var chart = am4core . create ( " chartdiv " , am4charts . SankeyDiagram );
{ from: " A " , to: " D " , value: 10 },
{ from: " B " , to: " D " , value: 8 },
{ from: " B " , to: " E " , value: 4 },
{ from: " C " , to: " E " , value: 3 },
{ from: " D " , to: " G " , value: 5 },
{ from: " D " , to: " I " , value: 2 },
{ from: " D " , to: " H " , value: 3 },
{ from: " E " , to: " H " , value: 6 }
chart . dataFields . fromName = " from " ;
chart . dataFields . toName = " to " ;
chart . dataFields . value = " value " ;
var nodeTemplate = chart . nodes . template ;
nodeTemplate . stroke = am4core . color ( " #fff " );
nodeTemplate . strokeWidth = 2 ;
nodeTemplate . nameLabel . label . fontWeight = " bold " ;
< div id = " chartdiv " ></ div >
-apple-system , BlinkMacSystemFont, " Segoe UI " , Roboto, Helvetica , Arial , sans-serif ,
" Apple Color Emoji " , " Segoe UI Emoji " , " Segoe UI Symbol " ;
Relatively positioned labels on Sankey (1) relatively-positioned-labels-on-sankey-1
The labels are all placed to the right of their respective nodes.
To make the chart look nicer, lets make labels of the right-most nodes placed to the left of the nodes, and labels of the center nodes inside.
As you probably already know, the locationX property is responsible of label bullet position. Negative number will put the label to the left. Positive to the right. Zero will make the label appear directly on node.
However, if we set locationX directly on node template, it will affect labels of all the nodes.
What we need is an adapter .
We’ll set up a custom function (adapter) which will check label bullet’s parent, which is a SankeyNode and its level property, to dynamically modify locationX.
nodeTemplate . nameLabel . adapter . add ( " locationX " , function ( location , target ) {
switch (target . parent . level ) {
nodeTemplate . nameLabel . adapter . add ( " locationX " , function ( location , target ) {
switch ( target . parent . level ) {
"locationX" : function(location , target) {
switch (target.parent.level) {
Result JS HTML CSS
Open in: New tab CodePen (opens in a new tab) jsFiddle (opens in a new tab) Run * ---------------------------------------
* This demo was created using amCharts 4.
* For more information visit:
* https://www.amcharts.com/
* Documentation is available at:
* https://www.amcharts.com/docs/v4/
* ---------------------------------------
var chart = am4core . create ( " chartdiv " , am4charts . SankeyDiagram );
{ from: " A " , to: " D " , value: 10 },
{ from: " B " , to: " D " , value: 8 },
{ from: " B " , to: " E " , value: 4 },
{ from: " C " , to: " E " , value: 3 },
{ from: " D " , to: " G " , value: 5 },
{ from: " D " , to: " I " , value: 2 },
{ from: " D " , to: " H " , value: 3 },
{ from: " E " , to: " H " , value: 6 }
chart . dataFields . fromName = " from " ;
chart . dataFields . toName = " to " ;
chart . dataFields . value = " value " ;
var nodeTemplate = chart . nodes . template ;
nodeTemplate . stroke = am4core . color ( " #fff " );
nodeTemplate . strokeWidth = 2 ;
nodeTemplate . nameLabel . label . fontWeight = " bold " ;
nodeTemplate . nameLabel . adapter . add ( " locationX " , function ( location , target ) {
switch ( target . parent . level ) {
< div id = " chartdiv " ></ div >
-apple-system , BlinkMacSystemFont, " Segoe UI " , Roboto, Helvetica , Arial , sans-serif ,
" Apple Color Emoji " , " Segoe UI Emoji " , " Segoe UI Symbol " ;
Relatively positioned labels on Sankey (2) relatively-positioned-labels-on-sankey-2