PageSourceSearch

https://sempwn.github.io/js/d3.grid.js

js sempwn.github.io collected 2026-10-03 09:56:04 UTC 2,720 bytes, 104 lines download raw bytes

1(function() {
2  d3.layout.grid = function() {
3    var mode = "equal",
4        layout = _distributeEqually,
5        x = d3.scale.ordinal(),
6        y = d3.scale.ordinal(),
7        size = [1, 1],
8        actualSize = [0, 0],
9        nodeSize = false,
10        bands = false,
11        padding = [0, 0],
12        cols, rows;
13
14    function grid(nodes) {
15      return layout(nodes);
16    }
17
18    function _distributeEqually(nodes) {
19      var i = -1,
20          n = nodes.length,
21          _cols = cols ? cols : 0,
22          _rows = rows ? rows : 0,
23          col, row;
24
25      if (_rows && !_cols) {
26        _cols = Math.ceil(n / _rows)
27      } else {
28        _cols || (_cols = Math.ceil(Math.sqrt(n)));
29        _rows || (_rows = Math.ceil(n / _cols));
30      }
31
32      if (nodeSize) {
33        x.domain(d3.range(_cols)).range(d3.range(0, (size[0] + padding[0]) * _cols, size[0] + padding[0]));
34        y.domain(d3.range(_rows)).range(d3.range(0, (size[1] + padding[1]) * _rows, size[1] + padding[1]));
35        actualSize[0] = bands ? x(_cols - 1) + size[0] : x(_cols - 1);
36        actualSize[1] = bands ? y(_rows - 1) + size[1] : y(_rows - 1);
37      } else if (bands) {
38        x.domain(d3.range(_cols)).rangeBands([0, size[0]], padding[0], 0);
39        y.domain(d3.range(_rows)).rangeBands([0, size[1]], padding[1], 0);
40        actualSize[0] = x.rangeBand();
41        actualSize[1] = y.rangeBand();
42      } else {
43        x.domain(d3.range(_cols)).rangePoints([0, size[0]]);
44        y.domain(d3.range(_rows)).rangePoints([0, size[1]]);
45        actualSize[0] = x(1);
46        actualSize[1] = y(1);
47      }
48
49      while (++i < n) {
50        col = i % _cols;
51        row = Math.floor(i / _cols);
52
53        nodes[i].x = x(col);
54        nodes[i].y = y(row);
55      }
56
57      return nodes;
58    }
59
60    grid.size = function(value) {
61      if (!arguments.length) return nodeSize ? actualSize : size;
62      actualSize = [0, 0];
63      nodeSize = (size = value) == null;
64      return grid;
65    }
66
67    grid.nodeSize = function(value) {
68      if (!arguments.length) return nodeSize ? size : actualSize;
69      actualSize = [0, 0];
70      nodeSize = (size = value) != null;
71      return grid;
72    }
73
74    grid.rows = function(value) {
75      if (!arguments.length) return rows;
76      rows = value;
77      return grid;
78    }
79
80    grid.cols = function(value) {
81      if (!arguments.length) return cols;
82      cols = value;
83      return grid;
84    }
85
86    grid.bands = function() {
87      bands = true;
88      return grid;
89    }
90
91    grid.points = function() {
92      bands = false;
93      return grid;
94    }
95
96    grid.padding = function(value) {
97      if (!arguments.length) return padding;
98      padding = value;
99      return grid;
100    }
101
102    return grid;
103  };
104})();

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.