Path: blob/master/sandbox/RFinance2014/libraries/widgets/nvd3/js/d3-grid.js
1433 views
(function() {1var DEBUG = false;23d3.layout.grid = function() {4var mode = "equal",5layout = _distributeEqually,6x = d3.scale.ordinal(),7y = d3.scale.ordinal(),8size = [1, 1],9actualSize = [0, 0],10nodeSize = false,11bands = false,12padding = [0, 0],13cols, rows;1415function grid(nodes) {16return layout(nodes);17}1819function _distributeEqually(nodes) {20var i = -1,21n = nodes.length,22_cols = cols ? cols : 0,23_rows = rows ? rows : 0,24col, row;2526// FIXME: make explicit rows/cols exclusive? Or find a smart way to deal with overflows (repeat?)27// FIXME: when rows are set, fill top-to-bottom (make test with 5 data points and 4 rows)2829if (_rows && !_cols) {30_cols = Math.ceil(n / _rows)31} else {32_cols || (_cols = Math.ceil(Math.sqrt(n)));33_rows || (_rows = Math.ceil(n / _cols));34}3536if (nodeSize) {37x.domain(d3.range(_cols)).range(d3.range(0, (size[0] + padding[0]) * _cols, size[0] + padding[0]));38y.domain(d3.range(_rows)).range(d3.range(0, (size[1] + padding[1]) * _rows, size[1] + padding[1]));39actualSize[0] = bands ? x(_cols - 1) + size[0] : x(_cols - 1);40actualSize[1] = bands ? y(_rows - 1) + size[1] : y(_rows - 1);41} else if (bands) {42x.domain(d3.range(_cols)).rangeBands([0, size[0]], padding[0], 0);43y.domain(d3.range(_rows)).rangeBands([0, size[1]], padding[1], 0);44actualSize[0] = x.rangeBand();45actualSize[1] = y.rangeBand();46} else {47x.domain(d3.range(_cols)).rangePoints([0, size[0]]);48y.domain(d3.range(_rows)).rangePoints([0, size[1]]);49actualSize[0] = x(1);50actualSize[1] = y(1);51}5253if (DEBUG) console.log('cols/rows', _cols, _rows);5455while (++i < n) {56col = i % _cols;57row = Math.floor(i / _cols);5859if (DEBUG) console.log(i, col, row);6061nodes[i].x = x(col);62nodes[i].y = y(row);63}6465return nodes;66}6768// grid.mode = function(value) {69// if (!arguments.length) return mode;70// switch(mode = value) {71// case "equal":72// layout = _distributeEqually;73// break;74// }75// return grid;76// }7778grid.size = function(value) {79if (!arguments.length) return nodeSize ? actualSize : size;80actualSize = [0, 0];81nodeSize = (size = value) == null;82return grid;83}8485grid.nodeSize = function(value) {86if (!arguments.length) return nodeSize ? size : actualSize;87actualSize = [0, 0];88nodeSize = (size = value) != null;89return grid;90}9192grid.rows = function(value) {93if (!arguments.length) return rows;94rows = value;95return grid;96}9798grid.cols = function(value) {99if (!arguments.length) return cols;100cols = value;101return grid;102}103104grid.bands = function() {105bands = true;106return grid;107}108109grid.points = function() {110bands = false;111return grid;112}113114grid.padding = function(value) {115if (!arguments.length) return padding;116padding = value;117return grid;118}119120return grid;121};122})();123124