A matrix diagram of Les Miserables characters colored and ordered by their communities (determined using Louvain modularity). Darker colors indicate more interactions between characters, and light gray cells indicate no interaction.
In comparison with node-link diagrams, which can become inscrutable hairballs with dense networks, matrix diagrams can be simpler and easier to read. However, this is highly dependent on the row and column ordering, and it is difficult to follow paths connecting nodes. Furthermore, a matrix diagram of a graph with hundreds or thousands of nodes can be difficult to fit on one screen and still be able to read the row and column labels.
xxxxxxxxxx
<meta charset="utf-8">
<style>
svg {
font: 10px sans-serif;
}
.label {
fill: #999;
font-size: 8px;
text-anchor: end;
}
.column .label {
text-anchor: start;
}
rect {
fill: #eee;
stroke: #d62333;
stroke-width: 0;
}
rect:hover {
stroke-width: 1px;
}
</style>
<svg></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
var margin = {
top: 170,
right: 0,
bottom: 0,
left: 170
};
var width = 800 - margin.left - margin.right;
var height = 800 - margin.top - margin.bottom;
var color = d3.scaleOrdinal(["#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#9467bd", "#8c564b", "#e377c2", "#bcbd22", "#17becf"]);
var opacity = d3.scaleLinear()
.range([0.5, 1])
.clamp(true);
var x = d3.scaleBand()
.rangeRound([0, width])
.paddingInner(0.1)
.align(0);
var svg = d3.select('svg')
.attr('width', width + margin.left + margin.right)
.attr('height', height + margin.top + margin.bottom)
.append('g')
.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
d3.json('jean.json', function (error, graph) {
if (error) throw error;
var idToNode = {};
graph.nodes.forEach(function (n) {
n.degree = 0;
idToNode[n.id] = n;
});
graph.links.forEach(function (e) {
e.source = idToNode[e.source];
e.target = idToNode[e.target];
e.source.degree++;
e.target.degree++;
});
graph.nodes.sort(function (a, b) {
if (b.community != a.community)
return b.community - a.community;
else
return b.degree - a.degree;
});
x.domain(d3.range(graph.nodes.length));
opacity.domain([0, d3.max(graph.links, function (d) { return d.chapters.length; })]);
var matrix = graph.nodes.map(function (outer, i) {
outer.index = i;
return graph.nodes.map(function (inner, j) {
return {i: i, j: j, val: i === j ? inner.chapters.length : 0};
});
});
graph.links.forEach(function (l) {
matrix[l.source.index][l.target.index].val = l.chapters.length;
matrix[l.target.index][l.source.index].val = l.chapters.length;
});
var row = svg.selectAll('g.row')
.data(matrix)
.enter().append('g')
.attr('class', 'row')
.attr('transform', function (d, i) { return 'translate(0,' + x(i) + ')'; })
.each(makeRow);
row.append('text')
.attr('class', 'label')
.attr('x', -4)
.attr('y', x.bandwidth() / 2)
.attr('dy', '0.32em')
.text(function (d, i) { return graph.nodes[i].name; });
var column = svg.selectAll('g.column')
.data(matrix)
.enter().append('g')
.attr('class', 'column')
.attr('transform', function(d, i) { return 'translate(' + x(i) + ', 0)rotate(-90)'; })
.append('text')
.attr('class', 'label')
.attr('x', 4)
.attr('y', x.bandwidth() / 2)
.attr('dy', '0.32em')
.text(function (d, i) { return graph.nodes[i].name; });
function makeRow(rowData) {
var cell = d3.select(this).selectAll('rect.cell')
.data(rowData)
.enter().append('rect')
.attr('class', 'cell')
.attr('x', function (d, i) { return x(i); })
.attr('width', x.bandwidth())
.attr('height', x.bandwidth())
.style('fill-opacity', function (d) { return d.val > 0 ? opacity(d.val) : 1; })
.style('fill', function (d) {
if (d.val > 0 && graph.nodes[d.i].community === graph.nodes[d.j].community)
return color(graph.nodes[d.i].community);
else if (d.val > 0)
return '#555';
return null;
})
.on('mouseover', function (d) {
row.filter(function (_, i) { return d.i === i; })
.selectAll('text')
.style('fill', '#d62333')
.style('font-weight', 'bold');
column.filter(function (_, j) { return d.j === j; })
.style('fill', '#d62333')
.style('font-weight', 'bold');
})
.on('mouseout', function () {
row.selectAll('text')
.style('fill', null)
.style('font-weight', null);
column
.style('fill', null)
.style('font-weight', null);
});
cell.append('title')
.text(function (d) {
return graph.nodes[d.i].name + ' - ' + graph.nodes[d.j].name + ', chapters: ' + d.val;
});
}
});
</script>
https://d3js.org/d3.v4.min.js