Skip to content
This repository has been archived by the owner on Nov 7, 2018. It is now read-only.

Expose raw array for hexagon #107

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 149 additions & 0 deletions adjacencyMatrix/d3.layout.adjacencyMatrix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
(function() {
d3.layout.adjacencyMatrix = function() {
var directed = true,
size = [1,1],
nodes = [],
edges = [],
edgeWeight = function (d) {return 1},
nodeID = function (d) {return d.id};

function matrix() {
var width = size[0],
height = size[1],
nodeWidth = width / nodes.length,
nodeHeight = height / nodes.length,
constructedMatrix = [],
matrix = [],
edgeHash = {},
xScale = d3.scale.linear().domain([0,nodes.length]).range([0,width]),
yScale = d3.scale.linear().domain([0,nodes.length]).range([0,height]);

nodes.forEach(function(node, i) {
node.sortedIndex = i;
})

edges.forEach(function(edge) {
var constructedEdge = {source: edge.source, target: edge.target, weight: edgeWeight(edge)};
if (typeof edge.source == "number") {
constructedEdge.source = nodes[edge.source];
}
if (typeof edge.target == "number") {
constructedEdge.target = nodes[edge.target];
}
var id = nodeID(constructedEdge.source) + "-" + nodeID(constructedEdge.target);

if (directed === false && constructedEdge.source.sortedIndex < constructedEdge.target.sortedIndex) {
id = nodeID(constructedEdge.target) + "-" + nodeID(constructedEdge.source);
}
if (!edgeHash[id]) {
edgeHash[id] = constructedEdge;
}
else {
edgeHash[id].weight = edgeHash[id].weight + constructedEdge.weight;
}
});

console.log("nodes", nodes, nodes.length)

nodes.forEach(function (sourceNode, a) {
nodes.forEach(function (targetNode, b) {
var grid = {id: nodeID(sourceNode) + "-" + nodeID(targetNode), source: sourceNode, target: targetNode, x: xScale(b), y: yScale(a), weight: 0, height: nodeHeight, width: nodeWidth};
var edgeWeight = 0;
if (edgeHash[grid.id]) {
edgeWeight = edgeHash[grid.id].weight;
grid.weight = edgeWeight;
};
if (directed === true || b < a) {
matrix.push(grid);
if (directed === false) {
var mirrorGrid = {id: nodeID(sourceNode) + "-" + nodeID(targetNode), source: sourceNode, target: targetNode, x: xScale(a), y: yScale(b), weight: 0, height: nodeHeight, width: nodeWidth};
mirrorGrid.weight = edgeWeight;
matrix.push(mirrorGrid);
}
}
});
});

console.log("matrix", matrix, matrix.length)

return matrix;
}

matrix.directed = function(x) {
if (!arguments.length) return directed;
directed = x;
return matrix;
}

matrix.size = function(x) {
if (!arguments.length) return size;
size = x;
return matrix;
}

matrix.nodes = function(x) {
if (!arguments.length) return nodes;
nodes = x;
return matrix;
}

matrix.links = function(x) {
if (!arguments.length) return edges;
edges = x;
return matrix;
}

matrix.edgeWeight = function(x) {
if (!arguments.length) return edgeWeight;
if (typeof x === "function") {
edgeWeight = x;
}
else {
edgeWeight = function () {return x};
}
return matrix;
}

matrix.nodeID = function(x) {
if (!arguments.length) return nodeID;
if (typeof x === "function") {
nodeID = x;
}
return matrix;
}

matrix.xAxis = function(calledG) {
var nameScale = d3.scale.ordinal()
.domain(nodes.map(nodeID))
.rangePoints([0,size[0]],1);

var xAxis = d3.svg.axis().scale(nameScale).orient("top").tickSize(4);

calledG
.append("g")
.attr("class", "am-xAxis am-axis")
.call(xAxis)
.selectAll("text")
.style("text-anchor", "end")
.attr("transform", "translate(-10,-10) rotate(90)");

}

matrix.yAxis = function(calledG) {
var nameScale = d3.scale.ordinal()
.domain(nodes.map(nodeID))
.rangePoints([0,size[1]],1);

yAxis = d3.svg.axis().scale(nameScale)
.orient("left")
.tickSize(4);

calledG.append("g")
.attr("class", "am-yAxis am-axis")
.call(yAxis);
}

return matrix;
}

})();
20 changes: 20 additions & 0 deletions adjacencyMatrix/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Adjacency Matrix

[Demo](http://bl.ocks.org/emeeks/15c005cba60aad26e11a).

**#matrix.size** An array of [width, height] that is used to calculate grid x, y, height and width.

**#matrix.nodes** An array of the nodes of your network.

**#matrix.links** An array of the edges of your network. As with other D3 network layouts, if the source and target properties are numbers, they are assumed to be array position within the nodes array, otherwise objects are assumed.

**#matrix.edgeWeight** The function to return the weight of the edges of your links, if any. Defaults to returning 1.

**#matrix.nodeID** The function to return the id value of a node, defaults to returning #node.id. The id is used to build the matrix and drive the axes.

**#matrix.xAxis** Cannot be set. Call this from the same place where you've put your matrix cells and it will build a simple horizontal axis with labels from your node id.

**#matrix.yAxis** Cannot be set. Call this from the same place where you've put your matrix cells and it will build a simple vertical axis with labels from your node id.

**#matrix.directed** Set to false if you want to mirror undirected networks.

5 changes: 5 additions & 0 deletions hexbin/hexbin.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ d3.hexbin = function() {
return "m" + hexagon(radius).join("l") + "z";
};

hexbin.hexagonArray = function(radius) {
if (arguments.length < 1) radius = r;
return hexagon(radius);
};

hexbin.centers = function() {
var centers = [];
for (var y = 0, odd = false, j = 0; y < height + r; y += dy, odd = !odd, ++j) {
Expand Down
70 changes: 65 additions & 5 deletions sankey/sankey.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,40 @@ d3.sankey = function() {
return sankey;
};

sankey.linkAdjusted = function() {
var curvature = .5;

function link(d, adjustment, segmentOffset) {
var x0 = d.source.x + d.source.dx,
x1 = d.target.x,
xi = d3.interpolateNumber(x0, x1),
x2 = xi(curvature),
x3 = xi(1 - curvature),
y0 = d.source.y + d.sy + segmentOffset,
y1 = d.target.y + d.ty + segmentOffset,
y2 = d.target.y + d.ty + (d.dy * adjustment) + segmentOffset,
y3 = d.source.y + d.sy + (d.dy * adjustment) + segmentOffset;

return "M" + x0 + "," + y0
+ "C" + x2 + "," + y0
+ " " + x3 + "," + y1
+ " " + x1 + "," + y1
+ "L" + x1 + "," + y2
+ "C" + x3 + "," + y2
+ " " + x2 + "," + y3
+ " " + x0 + "," + y3
+ "Z";
}

link.curvature = function(_) {
if (!arguments.length) return curvature;
curvature = +_;
return link;
};

return link;
};

sankey.link = function() {
var curvature = .5;

Expand All @@ -59,12 +93,38 @@ d3.sankey = function() {
xi = d3.interpolateNumber(x0, x1),
x2 = xi(curvature),
x3 = xi(1 - curvature),
y0 = d.source.y + d.sy + d.dy / 2,
y1 = d.target.y + d.ty + d.dy / 2;
y0 = d.source.y + d.sy,
y1 = d.target.y + d.ty,
y2 = d.target.y + d.ty + d.dy,
y3 = d.source.y + d.sy + d.dy;

if (y3 - y0 < 30000) {

return "M" + x0 + "," + y0
+ "C" + x2 + "," + y0
+ " " + x3 + "," + y1
+ " " + x1 + "," + y1;
+ " " + x1 + "," + y1
+ "L" + x1 + "," + y2
+ "C" + x3 + "," + y2
+ " " + x2 + "," + y3
+ " " + x0 + "," + y3
+ "Z";
}
else {

var offset = (x1 - x0) /4;
return "M" + x0 + "," + y0
+ "C" + x2 + "," + y0
+ " " + x3 + "," + (y1)
+ " " + (x1 - offset) + "," + (y1 + 0)
+ "L" + (x1 - 6) + "," + ((y2 + y1)/2)
+ "L" + (x1 - offset) + "," + (y2 + 0)
+ "C" + x3 + "," + (y2)
+ " " + x2 + "," + y3
+ " " + x0 + "," + y3
+ "Z";

}
}

link.curvature = function(_) {
Expand Down Expand Up @@ -108,6 +168,7 @@ d3.sankey = function() {
// nodes with no incoming links are assigned breadth zero, while
// nodes with no outgoing links are assigned the maximum breadth.
function computeNodeBreadths() {

var remainingNodes = nodes,
nextNodes,
x = 0;
Expand All @@ -127,7 +188,6 @@ d3.sankey = function() {
++x;
}

//
moveSinksRight(x);
scaleNodeBreadths((size[0] - nodeWidth) / (x - 1));
}
Expand Down Expand Up @@ -291,4 +351,4 @@ d3.sankey = function() {
}

return sankey;
};
};