MCPcopy Create free account
hub / github.com/breck7/scroll / squarifyRatio

Function squarifyRatio

external/.d3.js:14073–14120  ·  view source on GitHub ↗
(ratio, parent, x0, y0, x1, y1)

Source from the content-addressed store, hash-verified

14071var phi = (1 + Math.sqrt(5)) / 2;
14072
14073function squarifyRatio(ratio, parent, x0, y0, x1, y1) {
14074 var rows = [],
14075 nodes = parent.children,
14076 row,
14077 nodeValue,
14078 i0 = 0,
14079 i1 = 0,
14080 n = nodes.length,
14081 dx, dy,
14082 value = parent.value,
14083 sumValue,
14084 minValue,
14085 maxValue,
14086 newRatio,
14087 minRatio,
14088 alpha,
14089 beta;
14090
14091 while (i0 < n) {
14092 dx = x1 - x0, dy = y1 - y0;
14093
14094 // Find the next non-empty node.
14095 do sumValue = nodes[i1++].value; while (!sumValue && i1 < n);
14096 minValue = maxValue = sumValue;
14097 alpha = Math.max(dy / dx, dx / dy) / (value * ratio);
14098 beta = sumValue * sumValue * alpha;
14099 minRatio = Math.max(maxValue / beta, beta / minValue);
14100
14101 // Keep adding nodes while the aspect ratio maintains or improves.
14102 for (; i1 < n; ++i1) {
14103 sumValue += nodeValue = nodes[i1].value;
14104 if (nodeValue < minValue) minValue = nodeValue;
14105 if (nodeValue > maxValue) maxValue = nodeValue;
14106 beta = sumValue * sumValue * alpha;
14107 newRatio = Math.max(maxValue / beta, beta / minValue);
14108 if (newRatio > minRatio) { sumValue -= nodeValue; break; }
14109 minRatio = newRatio;
14110 }
14111
14112 // Position and record the row orientation.
14113 rows.push(row = {value: sumValue, dice: dx < dy, children: nodes.slice(i0, i1)});
14114 if (row.dice) treemapDice(row, x0, y0, x1, value ? y0 += dy * sumValue / value : y1);
14115 else treemapSlice(row, x0, y0, value ? x0 += dx * sumValue / value : x1, y1);
14116 value -= sumValue, i0 = i1;
14117 }
14118
14119 return rows;
14120}
14121
14122var squarify = (function custom(ratio) {
14123

Callers 2

squarifyFunction · 0.85
resquarifyFunction · 0.85

Calls 4

treemapDiceFunction · 0.85
treemapSliceFunction · 0.85
maxMethod · 0.80
sliceMethod · 0.80

Tested by

no test coverage detected