Sankey Diagrams in JavaScript

How to make D3.js-based sankey diagrams in Plotly.js.


New to Plotly?

Plotly is a free and open-source graphing library for JavaScript. We recommend you read our Getting Started guide for the latest installation or upgrade instructions, then move on to our Plotly Fundamentals tutorials or dive straight in to some Basic Charts tutorials.

var data = {
  type: "sankey",
  orientation: "h",
  node: {
    pad: 15,
    thickness: 30,
    line: {
      color: "black",
      width: 0.5
    },
   label: ["A1", "A2", "B1", "B2", "C1", "C2"],
   color: ["blue", "blue", "blue", "blue", "blue", "blue"]
      },

  link: {
    source: [0,1,0,2,3,3],
    target: [2,3,3,4,4,5],
    value:  [8,4,2,8,4,2]
  }
}

var data = [data]

var layout = {
  title: {
    text: "Basic Sankey"
  },
  font: {
    size: 10
  }
}

Plotly.react('myDiv', data, layout)
var data = {
  type: "sankey",
  domain: {
    x: [0,1],
    y: [0,1]
  },
  orientation: "h",
  valueformat: ".0f",
  valuesuffix: "TWh"
}

var data = [data]

var layout = {
  title: {
    text: "Energy forecast for 2050<br>Source: Department of Energy & Climate Change, Tom Counsell via <a href='https://bost.ocks.org/mike/sankey/'>Mike Bostock</a>"
  },
  width: 1118,
  height: 772,
  font: {
    size: 10
  }
}
d3.json('https://raw.githubusercontent.com/plotly/plotly.js/master/test/image/mocks/sankey_energy.json', function(fig){

var data = {
  type: "sankey",
  domain: {
    x: [0,1],
    y: [0,1]
  },
  orientation: "h",
  valueformat: ".0f",
  valuesuffix: "TWh",

  node: {
    pad: 15,
    thickness: 15,
    line: {
      color: "black",
      width: 0.5
    },
   label: fig.data[0].node.label,
   color: fig.data[0].node.color
      }
}

var data = [data]

var layout = {
  title: {
    text: "Energy forecast for 2050<br>Source: Department of Energy & Climate Change, Tom Counsell via <a href='https://bost.ocks.org/mike/sankey/'>Mike Bostock</a>"
  },
  width: 1118,
  height: 772,
  font: {
    size: 10
  }
}

Plotly.newPlot('myDiv', data, layout)

});
d3.json('https://raw.githubusercontent.com/plotly/plotly.js/master/test/image/mocks/sankey_energy.json', function(fig){

var data = {
  type: "sankey",
  domain: {
    x: [0,1],
    y: [0,1]
  },
  orientation: "h",
  valueformat: ".0f",
  valuesuffix: "TWh",
  node: {
    pad: 15,
    thickness: 15,
    line: {
      color: "black",
      width: 0.5
    },
   label: fig.data[0].node.label,
   color: fig.data[0].node.color
      },

  link: {
    source: fig.data[0].link.source,
    target: fig.data[0].link.target,
    value: fig.data[0].link.value,
    label: fig.data[0].link.label
  }
}

var data = [data]

var layout = {
  title: {
    text: "Energy forecast for 2050<br>Source: Department of Energy & Climate Change, Tom Counsell via <a href='https://bost.ocks.org/mike/sankey/'>Mike Bostock</a>"
  },
  width: 1118,
  height: 772,
  font: {
    size: 10
  }
}

Plotly.newPlot('myDiv', data, layout)
});
d3.json('https://raw.githubusercontent.com/plotly/plotly.js/master/test/image/mocks/sankey_energy_dark.json', function(fig){

var data = {
  type: "sankey",
  domain: {
    x: [0,1],
    y: [0,1]
  },
  orientation: "h",
  valueformat: ".0f",
  valuesuffix: "TWh",
  node: {
    pad: 15,
    thickness: 15,
    line: {
      color: "black",
      width: 0.5
    },
   label: fig.data[0].node.label,
   color: fig.data[0].node.color
      },
  link: {
    source: fig.data[0].link.source,
    target: fig.data[0].link.target,
    value: fig.data[0].link.value,
    label: fig.data[0].link.label
  }
}

var data = [data]

var layout = {
  title: {
    text: "Energy forecast for 2050<br>Source: Department of Energy & Climate Change, Tom Counsell via <a href='https://bost.ocks.org/mike/sankey/'>Mike Bostock</a>"
  },
  width: 1118,
  height: 772,
  font: {
    size: 10,
    color: 'white'
  },
  plot_bgcolor: 'black',
  paper_bgcolor: 'black'
}

Plotly.newPlot('myDiv', data, layout)
});

The following example sets node.x and node.y to place nodes in the specified locations, except in the snap arrangement (default behaviour when node.x and node.y are not defined) to avoid overlapping of the nodes, therefore, an automatic snapping of elements will be set to define the padding between nodes via nodepad. The other possible arrangements are: 1) perpendicular 2) freeform 3) fixed

var data = [{
  type: "sankey",
    arrangement: "snap",
    node:{
        label: ["A", "B", "C", "D", "E", "F"],
        x: [0.2, 0.1, 0.5, 0.7, 0.3, 0.5],
        y: [0.7, 0.5, 0.2, 0.4, 0.2, 0.3],
        pad:10}, // 10 Pixels
    link: {
        source: [0, 0, 1, 2, 5, 4, 3, 5],
        target: [5, 3, 4, 3, 0, 2, 2, 3],
        value: [1, 2, 1, 1, 1, 1, 1, 2]}
    }]

var layout = {"title": "Sankey with manually positioned node"}

Plotly.newPlot('myDiv', data, layout)


You can set the alignment of nodes using node.align. In this example, we align nodes to the "right". node.align can also be set to "left", "center", or "justify". The default is "justify" if `node.align is not set, and is similar to aligning to the "left", except that nodes without outgoing links are moved to the right of the figure.

var data = {
  type: "sankey",
  orientation: "h",
  node: {
    label: ["0", "1", "2", "3", "4", "5"],
    align: "right",
  },

  link: {
    source: [0, 1, 4, 2, 1],
    target: [1, 4, 5, 4, 3],
    value: [4, 2, 3, 1, 2],
  },
};

var data = [data];

var layout = {
  title: {
    text: "Align Nodes (Right)"
  },
  font: {
    size: 10,
  },
};

Plotly.newPlot('myDiv', data, layout);