Skip to content

Instantly share code, notes, and snippets.

@aurelient
Last active December 12, 2019 01:11
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 aurelient/dfb04753cda35637885e18713caaff34 to your computer and use it in GitHub Desktop.
Save aurelient/dfb04753cda35637885e18713caaff34 to your computer and use it in GitHub Desktop.
adjacence_correction_2019
license: mit
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v5.min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
</style>
</head>
<body>
<script>
function createAdjacencyMatrix(nodes,edges) {
var edgeHash = {};
for (x in edges) {
var id = edges[x].source + "-" + edges[x].target;
edgeHash[id] = edges[x];
}
matrix = [];
//create all possible edges
for (const [a, node_a] of nodes.entries()) {
for (const [b, node_b] of nodes.entries()) {
var grid = {id: node_a.id + "-" + node_b.id, x: a, y: b, sharedfollowers: 0};
if (edgeHash[grid.id]) {
grid.sharedfollowers = parseInt(edgeHash[grid.id].sharedfollowers);
}
matrix.push(grid);
}
}
console.log(matrix)
return matrix;
}
function gridOver(d,i) {
console.log(d)
}
var width = 960;
var height = 500;
var margin = ({top: width/10, right: width/10, bottom: width/5, left: width/5})
var svg = d3.select("body")
.append("svg")
.attr("width", width )
.attr( "height", height );
var tooltip = d3.select('body').append('div')
.attr('class', 'hidden tooltip');
d3.csv("noeuds.csv")
.then(function(nodes) {
d3.csv("liens.csv")
.then(function(edges) {
matrix = createAdjacencyMatrix(nodes, edges);
var max_val = d3.max(
edges.map(function (el) {return el.sharedfollowers})
);
console.log (max_val)
var color = d3.scaleQuantize() //scaleLinear()
.domain([0, max_val])
.range(d3.schemeBlues[9]);
var matriceElt = d3.select("svg")
.append("g")
.attr("transform", "translate(50,50)")
.attr("id", "adjacencyMatrix");
var highlightColumn =
d3.select("svg").append("rect")
.attr("transform", "translate(50,50)")
.attr("width", 25)
.attr("height", nodes.length*25)
.attr("x", 0)
.attr("y", 0)
.style("stroke", "black")
.style("stroke-opacity", "100%")
.style("stroke-width", "0px")
.style("fill-opacity", "0%");
var highlightRow =
d3.select("svg").append("rect")
.attr("transform", "translate(50,50)")
.attr("width", nodes.length*25)
.attr("height", 25)
.attr("x", 0)
.attr("y", 0)
.style("stroke", "black")
.style("stroke-opacity", "100%")
.style("stroke-width", "0px")
.style("fill-opacity", "0%");
matriceElt.selectAll("rect")
.data(matrix)
.enter()
.append("rect")
.attr("width", 25)
.attr("height", 25)
.attr("x", function (d) {return d.x * 25 +1})
.attr("y", function (d) {return (nodes.length - d.y - 1) * 25 +1})
.style("stroke", "black")
//.style("stroke-opacity", "50%")
.style("stroke-width", ".3px")
.style("fill",
function (d) {return color(parseInt(d.sharedfollowers))}
)
.on("mouseover", function(d) {
highlightColumn.transition().duration(200)
.style("stroke-width", "2px")
.attr("x", 25*d.x);
highlightRow.transition().duration(200)
.style("stroke-width", "2px")
.attr("y", 25*(nodes.length - d.y - 1));
console.log(d);
});
var scaleSize = nodes.length * 25;
x = d3.scaleBand()
.domain(nodes.map(function (el) {return el.id}))
.range([0, scaleSize])
y = d3.scaleBand()
.domain(nodes.map(function (el) {return el.id}))//.reverse())
.range([scaleSize, 0])
d3.select("#adjacencyMatrix")
.append("g")
// .call(xAxis)
.attr("transform", `translate(0,${scaleSize})`)
.call(d3.axisBottom(x))
.selectAll("text")
.attr("transform", "translate(15,15) rotate(45)");
d3.select("#adjacencyMatrix")
.append("g")
// .call(yAxis);
.call(d3.axisLeft(y).tickSize(4))
})
})
</script>
</body>
</html>
source target sharedfollowers
valerie francois 1
charles francois 9
charles valerie 2
marie francois 5
marie gaston 12
marie nicolas 15
marie danielle 1
gaston nicolas 21
gaston danielle 1
danielle marie 3
danielle nicolas 4
danielle francois 4
nicolas marie 1
nicolas gaston 2
nicolas danielle 5
victorine gigi 3
id followers following
valerie 17 500
charles 83 80
francois 904 15
marie 7 5
gaston 11 50
danielle 80 85
nicolas 150 300
victorine 38 7
gigi 12 12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment