This is the code for Chapter 6, Figure 4 from D3.js in Action showing how to create a simple adjacency matrix from node and edge list formatted data.
forked from emeeks's block: Ch. 6, Fig. 4 - D3.js in Action
xxxxxxxxxx
<html>
<head>
<title>D3 in Action Chapter 6 - Example 1</title>
<meta charset="utf-8" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script src="https://d3js.org/queue.v1.min.js" type="text/JavaScript"></script>
</head>
<style>
svg {
height: 200px;
width: 1000px;
border: 1px solid gray;
}
</style>
<body>
<div id="viz">
<svg>
</svg>
</div>
</body>
<footer>
<script>
createAdjacencyMatrix();
function createAdjacencyMatrix() {
var matrix = [];
var data= [2,5,4,5,8,9,1,5,3,1,5,2,9,8,9,1,5,6,2,5,7,5,18,1];
var name=["sam","roy", "pris", "tully", "kim","mo","pat","lee","ali",
"sam2","roy2", "pris2", "tully2", "kim2","mo2","pat2","lee2","ali2",
"sam3","roy3", "pris3", "tully3", "kim3","mo3",];
for(a in data){
var grid = {id: name[0] + "-" + name[a], x: a, y: 1, weight: data[a]};
matrix.push(grid);
}
d3.select("svg")
.append("g")
.attr("transform", "translate(50,50)")
.attr("id", "adjacencyG")
.selectAll("rect")
.data(matrix)
.enter()
.append("rect")
.attr("width", 35)
.attr("height", 35)
.attr("x", function (d) {return d.x * 35})
.attr("y", function (d) {return d.y * 35})
.style("stroke", "black")
.style("stroke-width", "1px")
.style("fill", "green")
.style("fill-opacity", function (d) {return d.weight * .1})
.on("mouseover", gridOver)
.on("click", clicked);
// add values
//var inc =1;
d3.selectAll("svg")
.append("g")
.attr("transform", "translate(50,50)")
.attr("id", "adjacencyG")
.selectAll("rect")
.data(matrix)
.enter()
.append("text")
.attr("x", function (d) {return (d.x * 35)+12.5})
.attr("y", function (d) {return (d.y * 35)+12.5})
.attr("id", function(d,i){ return "text"+ i})
.attr("font-family", "sans-serif")
.attr("font-size", "11px")
.attr("fill", "black")
.attr("text-anchor","middle")
.attr("dy", ".35em")
.text(function(d){ return d.weight;})
;
var scaleSize = name.length * 35;
var nameScale = d3.scale.ordinal().domain(name.map(function (el) {return el})).rangePoints([0,scaleSize],1);
xAxis = d3.svg.axis().scale(nameScale).orient("top").tickSize(3);
d3.select("#adjacencyG").append("g").call(xAxis).attr("transform", "translate(0,35) rotate(0)").selectAll("text").style("text-anchor", "end").attr("transform", "translate(-10,-10) rotate(90)");
function gridOver(d,i) {
// d3.selectAll("rect").style("stroke-width", function (p) {return p.x == d.x || p.y == d.y ? "3px" : "1px"})
//console.log("mouseover");
// console.log(document.getElementById("text"+i));
}
function clicked(d,i){
var textval;
d3.selectAll("text").style("font-size", function (p) {
if(p.id){ if (p==d) textval= p.weight;}
if(p.id) return p==d ? "20px" : "11px"})
d3.selectAll("rect").style("stroke-width", function (p) {return p == d ? "3px" : "1px"})
if(document.getElementById("ss"))
document.getElementById("ss").remove();
var element = d3.select("body").append("div"). attr("id","ss");
element.style("position", "absolute");
element.style("top", "150px");
var str = d3.select(this)["0"]["0"].x.baseVal.value +"px";
element.style("left", str);
//console.log(d3.select(this)["0"]["0"].x.baseVal.value);
addCommentField(element,textval,d,i);
d3.event.stopPropagation();
}
const IDSTRING = "idinputfield";
function addCommentField(parent, val,clickedrect,index) {
parent.append("input")
.attr("type", "number")
.attr("placeholder", "Add a num ...")
.attr("id", IDSTRING)
.attr("value", val)
.on("change", function(d, i) {
var currentval=document.getElementById(IDSTRING).value;
d3.selectAll("text").text(function(d){
if(d.id) {if(clickedrect==d) d.weight= currentval;
return clickedrect==d ? currentval : d.weight;
}
else return d;});
d3.selectAll("rect").style("fill-opacity", function (p) {return p.weight * .1})
//console.log(clickedrect);
// console.log(document.getElementById("text22"));
// var ele= document.getElementById("text"+index);
// ele.text("hhh");
// change data array as well to keep updated data.
});
}
d3.select("svg").on("click", clickedsvg);
function clickedsvg(d,i){
//console.log(d);
//console.log(i);
if(document.getElementById("ss"))document.getElementById("ss").remove();
d3.selectAll("rect").style("stroke-width", "1px");
d3.selectAll("text").style("font-size", function(d){if(d.id) return "11px";});;
}
}
</script>
</footer>
</html>
https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js
https://d3js.org/queue.v1.min.js