D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
aurelient
Full window
Github gist
adjacence_correction_2019
Built with
blockbuilder.org
<!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>
https://d3js.org/d3.v5.min.js