Most basic chord diagram in d3.js

This post describes how to build a very basic chord diagram with d3.js. You can see many other examples in the chord diagram section of the gallery. Learn more about the theory of chord diagram in

chord diagram section


  • The Html part of the code just creates a div that will be modified by d3 later on.

  • The first part of the javascript code set a svg area. It specify the chart size and its margin. Read more.

  • Input data is a square matrix (dummy data). It provides the flow between each entity.

  • This matrix is read by the d3.chord() function that computes the position of the outer arcs and inside connections.

  • These 2 information can be passed to d3.arc() and d3.ribbon() to be drawn.

  • Next step: add labels.
<!DOCTYPE html>
<meta charset="utf-8">

<!-- Load d3.js -->
<script src=""></script>

<!-- Create a div where the graph will take place -->
<div id="my_dataviz"></div>


// create the svg area
var svg ="#my_dataviz")
    .attr("width", 440)
    .attr("height", 440)
    .attr("transform", "translate(220,220)")

// create input data: a square matrix that provides flow between entities
var matrix = [
  [11975,  5871, 8916, 2868],
  [ 1951, 10048, 2060, 6171],
  [ 8010, 16145, 8090, 8045],
  [ 1013,   990,  940, 6907]

// give this matrix to d3.chord(): it will calculates all the info we need to draw arc and ribbon
var res = d3.chord()
    .padAngle(0.05)     // padding between entities (black arc)

// add the groups on the inner part of the circle
  .data(function(d) { return d.groups; })
    .style("fill", "grey")
    .style("stroke", "black")
    .attr("d", d3.arc()

// Add the links between groups
  .data(function(d) { return d; })
    .attr("d", d3.ribbon()
    .style("fill", "#69b3a2")
    .style("stroke", "black");


Related blocks →