-
Notifications
You must be signed in to change notification settings - Fork 10
/
d3-grid.js
123 lines (103 loc) · 3.21 KB
/
d3-grid.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
(function() {
var DEBUG = false;
d3.layout.grid = function() {
var mode = "equal",
layout = _distributeEqually,
x = d3.scale.ordinal(),
y = d3.scale.ordinal(),
size = [1, 1],
actualSize = [0, 0],
nodeSize = false,
bands = false,
padding = [0, 0],
cols, rows;
function grid(nodes) {
return layout(nodes);
}
function _distributeEqually(nodes) {
var i = -1,
n = nodes.length,
_cols = cols ? cols : 0,
_rows = rows ? rows : 0,
col, row;
// FIXME: make explicit rows/cols exclusive? Or find a smart way to deal with overflows (repeat?)
// FIXME: when rows are set, fill top-to-bottom (make test with 5 data points and 4 rows)
if (_rows && !_cols) {
_cols = Math.ceil(n / _rows)
} else {
_cols || (_cols = Math.ceil(Math.sqrt(n)));
_rows || (_rows = Math.ceil(n / _cols));
}
if (nodeSize) {
x.domain(d3.range(_cols)).range(d3.range(0, (size[0] + padding[0]) * _cols, size[0] + padding[0]));
y.domain(d3.range(_rows)).range(d3.range(0, (size[1] + padding[1]) * _rows, size[1] + padding[1]));
actualSize[0] = bands ? x(_cols - 1) + size[0] : x(_cols - 1);
actualSize[1] = bands ? y(_rows - 1) + size[1] : y(_rows - 1);
} else if (bands) {
x.domain(d3.range(_cols)).rangeBands([0, size[0]], padding[0], 0);
y.domain(d3.range(_rows)).rangeBands([0, size[1]], padding[1], 0);
actualSize[0] = x.rangeBand();
actualSize[1] = y.rangeBand();
} else {
x.domain(d3.range(_cols)).rangePoints([0, size[0]]);
y.domain(d3.range(_rows)).rangePoints([0, size[1]]);
actualSize[0] = x(1);
actualSize[1] = y(1);
}
if (DEBUG) console.log('cols/rows', _cols, _rows);
while (++i < n) {
col = i % _cols;
row = Math.floor(i / _cols);
if (DEBUG) console.log(i, col, row);
nodes[i].x = x(col);
nodes[i].y = y(row);
}
return nodes;
}
// grid.mode = function(value) {
// if (!arguments.length) return mode;
// switch(mode = value) {
// case "equal":
// layout = _distributeEqually;
// break;
// }
// return grid;
// }
grid.size = function(value) {
if (!arguments.length) return nodeSize ? actualSize : size;
actualSize = [0, 0];
nodeSize = (size = value) == null;
return grid;
}
grid.nodeSize = function(value) {
if (!arguments.length) return nodeSize ? size : actualSize;
actualSize = [0, 0];
nodeSize = (size = value) != null;
return grid;
}
grid.rows = function(value) {
if (!arguments.length) return rows;
rows = value;
return grid;
}
grid.cols = function(value) {
if (!arguments.length) return cols;
cols = value;
return grid;
}
grid.bands = function() {
bands = true;
return grid;
}
grid.points = function() {
bands = false;
return grid;
}
grid.padding = function(value) {
if (!arguments.length) return padding;
padding = value;
return grid;
}
return grid;
};
})();