-
Notifications
You must be signed in to change notification settings - Fork 1
/
d3-adjacency-matrix-layout.js
171 lines (145 loc) · 4.99 KB
/
d3-adjacency-matrix-layout.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// https://github.com/micahstubbs/d3-adjacency-matrix-layout Version 1.0.0. Copyright 2016 contributors.
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('d3')) :
typeof define === 'function' && define.amd ? define(['exports', 'd3'], factory) :
(factory((global.d3 = global.d3 || {}),global.d3));
}(this, function (exports,d3) { 'use strict';
function d3AdjacencyMatrixLayout () {
var directed = true;
var size = [1, 1];
var nodes = [];
var edges = [];
var edgeWeight = function edgeWeight(d) {
return 1;
};
var nodeID = function nodeID(d) {
return d.id;
};
function matrix() {
var width = size[0];
var height = size[1];
var nodeWidth = width / nodes.length;
var nodeHeight = height / nodes.length;
// const constructedMatrix = [];
var matrix = [];
var edgeHash = {};
var xScale = d3.scaleLinear().domain([0, nodes.length]).range([0, width]);
var yScale = d3.scaleLinear().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 edgeWeight() {
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.scalePoint().domain(nodes.map(nodeID)).range([0, size[0]]).padding(1);
var xAxis = d3.axisTop().scale(nameScale).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.scalePoint().domain(nodes.map(nodeID)).range([0, size[1]]).padding(1);
var yAxis = d3.axisLeft().scale(nameScale).tickSize(4);
calledG.append('g').attr('class', 'am-yAxis am-axis').call(yAxis);
};
return matrix;
}
exports.adjacencyMatrixLayout = d3AdjacencyMatrixLayout;
Object.defineProperty(exports, '__esModule', { value: true });
}));