Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/sandbox/RFinance2014/libraries/widgets/nvd3/js/d3-grid.js
1433 views
1
(function() {
2
var DEBUG = false;
3
4
d3.layout.grid = function() {
5
var mode = "equal",
6
layout = _distributeEqually,
7
x = d3.scale.ordinal(),
8
y = d3.scale.ordinal(),
9
size = [1, 1],
10
actualSize = [0, 0],
11
nodeSize = false,
12
bands = false,
13
padding = [0, 0],
14
cols, rows;
15
16
function grid(nodes) {
17
return layout(nodes);
18
}
19
20
function _distributeEqually(nodes) {
21
var i = -1,
22
n = nodes.length,
23
_cols = cols ? cols : 0,
24
_rows = rows ? rows : 0,
25
col, row;
26
27
// FIXME: make explicit rows/cols exclusive? Or find a smart way to deal with overflows (repeat?)
28
// FIXME: when rows are set, fill top-to-bottom (make test with 5 data points and 4 rows)
29
30
if (_rows && !_cols) {
31
_cols = Math.ceil(n / _rows)
32
} else {
33
_cols || (_cols = Math.ceil(Math.sqrt(n)));
34
_rows || (_rows = Math.ceil(n / _cols));
35
}
36
37
if (nodeSize) {
38
x.domain(d3.range(_cols)).range(d3.range(0, (size[0] + padding[0]) * _cols, size[0] + padding[0]));
39
y.domain(d3.range(_rows)).range(d3.range(0, (size[1] + padding[1]) * _rows, size[1] + padding[1]));
40
actualSize[0] = bands ? x(_cols - 1) + size[0] : x(_cols - 1);
41
actualSize[1] = bands ? y(_rows - 1) + size[1] : y(_rows - 1);
42
} else if (bands) {
43
x.domain(d3.range(_cols)).rangeBands([0, size[0]], padding[0], 0);
44
y.domain(d3.range(_rows)).rangeBands([0, size[1]], padding[1], 0);
45
actualSize[0] = x.rangeBand();
46
actualSize[1] = y.rangeBand();
47
} else {
48
x.domain(d3.range(_cols)).rangePoints([0, size[0]]);
49
y.domain(d3.range(_rows)).rangePoints([0, size[1]]);
50
actualSize[0] = x(1);
51
actualSize[1] = y(1);
52
}
53
54
if (DEBUG) console.log('cols/rows', _cols, _rows);
55
56
while (++i < n) {
57
col = i % _cols;
58
row = Math.floor(i / _cols);
59
60
if (DEBUG) console.log(i, col, row);
61
62
nodes[i].x = x(col);
63
nodes[i].y = y(row);
64
}
65
66
return nodes;
67
}
68
69
// grid.mode = function(value) {
70
// if (!arguments.length) return mode;
71
// switch(mode = value) {
72
// case "equal":
73
// layout = _distributeEqually;
74
// break;
75
// }
76
// return grid;
77
// }
78
79
grid.size = function(value) {
80
if (!arguments.length) return nodeSize ? actualSize : size;
81
actualSize = [0, 0];
82
nodeSize = (size = value) == null;
83
return grid;
84
}
85
86
grid.nodeSize = function(value) {
87
if (!arguments.length) return nodeSize ? size : actualSize;
88
actualSize = [0, 0];
89
nodeSize = (size = value) != null;
90
return grid;
91
}
92
93
grid.rows = function(value) {
94
if (!arguments.length) return rows;
95
rows = value;
96
return grid;
97
}
98
99
grid.cols = function(value) {
100
if (!arguments.length) return cols;
101
cols = value;
102
return grid;
103
}
104
105
grid.bands = function() {
106
bands = true;
107
return grid;
108
}
109
110
grid.points = function() {
111
bands = false;
112
return grid;
113
}
114
115
grid.padding = function(value) {
116
if (!arguments.length) return padding;
117
padding = value;
118
return grid;
119
}
120
121
return grid;
122
};
123
})();
124