Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 124 125 126 127 | import getBoundaries from "../utils/geometry/getBoundaries";
/**
* This middleware will rescale the graph such that it takes an optimal space
* on the renderer.
*
* As each middleware, this function is executed in the scope of the sigma
* instance.
*
* @param {?string} readPrefix The read prefix.
* @param {?string} writePrefix The write prefix.
* @param {object} options The parameters.
*/
export default function rescale(readPrefix, writePrefix, options) {
let i;
let l;
let a;
let b;
let c;
let d;
let scale;
const n = this.graph.nodes();
const e = this.graph.edges();
const settings = this.settings.embedObjects(options || {});
const bounds =
settings("bounds") || getBoundaries(this.graph, readPrefix, true);
let { minX, minY, maxX, maxY } = bounds;
const { sizeMax, weightMax } = bounds;
const w = settings("width") || 1;
const h = settings("height") || 1;
let rescaleSettings = settings("autoRescale");
const validSettings = {
nodePosition: 1,
nodeSize: 1,
edgeSize: 1
};
/**
* What elements should we rescale?
*/
if (!(rescaleSettings instanceof Array))
rescaleSettings = ["nodePosition", "nodeSize", "edgeSize"];
for (i = 0, l = rescaleSettings.length; i < l; i++)
if (!validSettings[rescaleSettings[i]])
throw new Error(
`The rescale setting "${rescaleSettings[i]}" is not recognized.`
);
const np = ~rescaleSettings.indexOf("nodePosition");
const ns = ~rescaleSettings.indexOf("nodeSize");
const es = ~rescaleSettings.indexOf("edgeSize");
/**
* First, we compute the scaling ratio, without considering the sizes
* of the nodes : Each node will have its center in the canvas, but might
* be partially out of it.
*/
scale =
settings("scalingMode") === "outside"
? Math.max(w / Math.max(maxX - minX, 1), h / Math.max(maxY - minY, 1))
: Math.min(w / Math.max(maxX - minX, 1), h / Math.max(maxY - minY, 1));
/**
* Then, we correct that scaling ratio considering a margin, which is
* basically the size of the biggest node.
* This has to be done as a correction since to compare the size of the
* biggest node to the X and Y values, we have to first get an
* approximation of the scaling ratio.
* */
const margin =
(settings("rescaleIgnoreSize")
? 0
: (settings("maxNodeSize") || sizeMax) / scale) +
(settings("sideMargin") || 0);
maxX += margin;
minX -= margin;
maxY += margin;
minY -= margin;
// Fix the scaling with the new extrema:
scale =
settings("scalingMode") === "outside"
? Math.max(w / Math.max(maxX - minX, 1), h / Math.max(maxY - minY, 1))
: Math.min(w / Math.max(maxX - minX, 1), h / Math.max(maxY - minY, 1));
// Size homothetic parameters:
if (!settings("maxNodeSize") && !settings("minNodeSize")) {
a = 1;
b = 0;
} else if (settings("maxNodeSize") === settings("minNodeSize")) {
a = 0;
b = +settings("maxNodeSize");
} else {
a = (settings("maxNodeSize") - settings("minNodeSize")) / sizeMax;
b = +settings("minNodeSize");
}
if (!settings("maxEdgeSize") && !settings("minEdgeSize")) {
c = 1;
d = 0;
} else if (settings("maxEdgeSize") === settings("minEdgeSize")) {
c = 0;
d = +settings("minEdgeSize");
} else {
c = (settings("maxEdgeSize") - settings("minEdgeSize")) / weightMax;
d = +settings("minEdgeSize");
}
// Rescale the nodes and edges:
for (i = 0, l = e.length; i < l; i++)
e[i][`${writePrefix}size`] =
e[i][`${readPrefix}size`] * (es ? c : 1) + (es ? d : 0);
for (i = 0, l = n.length; i < l; i++) {
n[i][`${writePrefix}size`] =
n[i][`${readPrefix}size`] * (ns ? a : 1) + (ns ? b : 0);
n[i][`${writePrefix}x`] =
(n[i][`${readPrefix}x`] - (maxX + minX) / 2) * (np ? scale : 1);
n[i][`${writePrefix}y`] =
(n[i][`${readPrefix}y`] - (maxY + minY) / 2) * (np ? scale : 1);
}
}
|