Skip to content

Instantly share code, notes, and snippets.

@jwilber
Last active August 22, 2018 05:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jwilber/f6aa52e560e6b20a0e4c4d155f3789ed to your computer and use it in GitHub Desktop.
Save jwilber/f6aa52e560e6b20a0e4c4d155f3789ed to your computer and use it in GitHub Desktop.
adjacency matrix
license: mit
source target weight
Jim Irene 5
Susie Irene 5
Jim Susie 5
Susie Kai 5
Shirley Kai 5
Shelby Kai 5
Kai Susie 5
Kai Shirley 5
Kai Shelby 5
Erik Zan 5
Tony Zan 5
Tony Fil 5
Tony Ian 5
Tony Adam 5
Fil Tony 4
Ian Miles 1
Adam Tony 3
Miles Ian 2
Miles Ian 3
Erik Kai 2
Erik Nadieh 2
Jim Nadieh 2
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<script>
// Feel free to change or delete any of the code you see in this editor!
const margin = {top: 25, bottom: 25, left: 250, right: 25};
const width = 960 - margin.right - margin.left;
const height = 800 - margin.top - margin.bottom;
const svg = d3.select('body').append('svg')
.attr('width', width + margin.right + margin.left)
.attr('height', height + margin.top + margin.bottom)
.append('g')
.attr("id", "adjacencyG")
.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
var PromiseWrapper = d => new Promise(resolve => d3.csv(d, p => resolve(p)));
Promise
.all([
PromiseWrapper("nodelist.csv"),
PromiseWrapper("edgelist.csv")
])
.then(resolve => {
createAdjacencyMatrix(resolve[0], resolve[1]);
});
function createAdjacencyMatrix(nodes, edges) {
var edgeHash = {};
edges.forEach(edge => {
var id = `${edge.source}-${edge.target}`;
edgeHash[id] = edge;
});
var matrix = [];
nodes.forEach((source, a) => {
nodes.forEach((target, b) => {
var grid = {
id: `${source.id}-${target.id}`,
x: b,
y: a,
weight: 0
};
if (edgeHash[grid.id]) {
grid.weight = edgeHash[grid.id].weight;
}
matrix.push(grid);
});
});
d3.select("svg")
.append("g")
.attr("transform", "translate(50,50)")
.attr("id", "adjacencyG")
.selectAll("rect")
.data(matrix)
.enter()
.append("rect")
.attr("class", "grid")
.attr("width", 25)
.attr("height", 25)
.attr("x", d => d.x * 30)
.attr("y", d => d.y * 30)
.style("fill-opacity", d => d.weight * .2);
d3.select("svg")
.append("g")
.attr("transform", "translate(50,45)")
.selectAll("text")
.data(nodes)
.enter()
.append("text")
.attr("x", (d,i) => i * 30 + 12.5)
.text(d => d.id)
.style("text-anchor", "middle");
d3.select("svg")
.append("g")
.attr("transform", "translate(45,50)")
.selectAll("text")
.data(nodes)
.enter()
.append("text")
.attr("y", (d,i) => i * 30 + 12.5)
.text(d => d.id)
.style("text-anchor", "end");
d3.selectAll("rect.grid").on("mouseover", gridOver);
function gridOver(d) {
d3.selectAll("rect")
.style("stroke-width", function (p) {
return p.x == d.x || p.y == d.y ? "3px" : "1px";
});
}
}
</script>
</body>
id role salary
Irene manager 300000
Zan manager 380000
Jim employee 150000
Susie employee 90000
Kai employee 135000
Shirley employee 60000
Erik employee 90000
Shelby employee 150000
Tony employee 72000
Fil employee 35000
Adam employee 85000
Ian employee 83000
Miles employee 99000
Sarah employee 160000
Nadieh contractor 240000
Hajra contractor 280000
.grid {
stroke: #9A8B7A;
stroke-width: 1px;
fill: #CF7D1C;
}
.arc {
stroke: #9A8B7A;
fill: none;
}
.node {
fill: #EBD8C1;
stroke: #9A8B7A;
stroke-width: 1px;
}
text {
font-size: 8px;
}
circle.active {
fill: #FE9922;
}
path.active {
stroke: #FE9922;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment