class Geometry · Computations

Separate

Moves shapes apart so that they don't overlap, with as little movement as possible.

The functions compute once and are not reactive. For shapes that follow data, call them in Reactive.do with what the input shapes are computed from as senders, and pass the same result every time so that visuals made from it follow. boxes and circles move shapes in the plane; boxesAlongAxis and circlesAlongAxis move shapes along or off one axis (labels at the end of lines, beeswarms). The options (SeparateBoxesOptions, SeparateCirclesOptions, SeparateBoxesAlongAxisOptions, SeparateCirclesAlongAxisOptions) set the padding, weights, obstacles, the area or range and what happens when the shapes don't fit.

Static methods

boxesstatic

static boxes(boxes: Box[], options?: SeparateBoxesOptions | null, result?: Box[] | null): Box[]

Moves boxes in x and y so that they don't overlap, each as little as possible, e.g. a Demers cartogram: squares at their places on a map, sized by data, moved apart: Separate.boxes(homes, { padding: 1 }, squares).

Each pair of boxes that overlaps is moved apart either horizontally or vertically, in the direction in which they overlap less; for these directions the movement is the smallest possible (the sum of the squared shifts), so the result is small but not always the smallest of all arrangements. The boxes keep their size and may move anywhere; no box is fixed. The same input gives the same result. Boxes that don't overlap are returned unchanged. weights make single boxes move less. For updates (dragging, animated values), pass the last layout as previous, e.g. Separate.boxes(homes, { previous: squares }, squares): a pair that was apart there keeps its side of each other. Animate towards the result (e.g. Animate.shape.followAll) for smooth motion when the arrangement changes. A box with a coordinate that is not a finite number is returned unchanged and ignored. For labels in a column (e.g. at the end of lines), boxesAlongAxis keeps their order.

boxes
The boxes at their own positions; they are not changed (unless they are also result).
options optional
The padding, weights and previous layout (see SeparateBoxesOptions).
result optional
Optional list to store the result in: one box per input box, in the same order; its boxes are reused and written to, and boxes are added or removed at the end to match the number of input boxes. Its content is not read: for the previous layout, see options.previous.

Returns The moved boxes.

boxesAlongAxisstatic

static boxesAlongAxis(boxes: Box[], axis: "x" | "y", options?: SeparateBoxesAlongAxisOptions | null, result?: Box[] | null): Box[]

Moves boxes along an axis so that they don't overlap: each box moves as little as possible (the sum of the squared shifts is the smallest), and boxes keep their order along the axis. Used e.g. for labels at the end of lines: Separate.boxesAlongAxis(wanted, "y", { padding: 2, min: 0, max: height }, placed).

For labels, pass boxes of the size of each label at the place it should have, not the labels' own extent: an extent follows its label, so it would change when the label is moved to the result (and, as a sender, run the reaction again). E.g. per label Boxes.centeredBox({ x: end.x + w / 2, y: end.y }, w, h, wanted[i]) with w and h the width and height of its extent, then Separate.boxesAlongAxis(wanted, "y", options, placed), and the label moved to Boxes.center(placed[i]). With graphic.ready as a sender, the boxes follow when the fonts are loaded. Each box keeps its position across the axis. Two boxes are kept apart along the axis only when they overlap across it, or are less than padding apart there; boxes side by side across the axis don't interact. With min and max, the boxes stay between them. weights make single boxes move less (or not at all), and obstacles are boxes that are kept clear of but not moved. When the boxes don't fit, they keep their size and overlap evenly, or, with shrink, are made smaller until they fit. A box with a coordinate that is not a finite number is returned unchanged and ignored. The result is the same for the same input, whatever was in result before.

boxes
The boxes; they are not changed (unless they are also result).
axis
The axis the boxes move along: "x" (horizontally) or "y" (vertically).
options optional
The padding, range, weights and obstacles (see SeparateBoxesAlongAxisOptions).
result optional
Optional list to store the result in: one box per input box, in the same order; its boxes are reused and written to, and boxes are added or removed at the end to match the number of input boxes.

Returns The moved boxes.

circlesstatic

static circles(circles: Circle[], options?: SeparateCirclesOptions | null, result?: Circle[] | null): Circle[]

Moves circles in any direction so that they don't overlap, each as little as possible, e.g. a Dorling cartogram: circles at their places on a map, sized by data, moved apart: Separate.circles(homes, { padding: 1 }, bubbles).

A circle that is dragged or animated through others passes them instead of pushing them ahead. For the arrangement found (which circle lies on which side of which) the movement is then the smallest possible, so the result is small but not always the smallest of all arrangements. The same input gives the same result. Circles that don't overlap (with spacing: that are spacing apart) are returned unchanged. spacing adds a preferred gap on top of padding that gives way where the circles press on each other. within keeps the circles inside a polygon (its convex hull) or a circle (e.g. within: { polygon: frame }); the area always holds. weights make single circles move less (or not at all), obstacles are circles that are kept clear of but not moved (e.g. the one being dragged). When the circles don't fit, they keep their size and the overlap that remains is between circles, where they press on each other. For updates (dragging, animated values), pass the last layout as previous, e.g. Separate.circles(homes, { previous: bubbles }, bubbles): the circles keep their arrangement where nothing pushes, pass each other where their positions ask for it, and with the same input the layout comes to rest. A circle whose center or radius is not a finite number is returned unchanged and ignored.

circles
The circles at their own positions; they are not changed (unless they are also result).
options optional
The padding, spacing, weights, obstacles, area and previous layout (see SeparateCirclesOptions).
result optional
Optional list to store the result in: one circle per input circle, in the same order; its circles are reused and written to, and circles are added or removed at the end to match the number of input circles. Its content is not read: for the previous layout, see options.previous.

Returns The moved circles.

circlesAlongAxisstatic

static circlesAlongAxis(circles: Circle[], axis: "x" | "y" | Vector, options?: SeparateCirclesAlongAxisOptions | null, result?: Circle[] | null): Circle[]

Moves circles that sit along an axis off it so that they don't overlap, as little as possible, e.g. a beeswarm: circles at their data position on the x axis, moved up or down as far as needed: Separate.circlesAlongAxis(dots, "x", { padding: 1 }, placed).

Each circle keeps its position along the axis and moves across it. The axis is "x", "y" (pointing up, like a chart's y axis) or a vector (a tilted axis; a pin gives its direction); a zero vector returns the circles unchanged. Which side of its neighbours a circle ends up on comes from a placement in list order, each circle at the free position nearest to its own. For updates that should not rearrange the circles (dragging, a changing axis, circles added), pass the last layout as previous, e.g. Separate.circlesAlongAxis(dots, "x", { previous: { circles: placed } }, placed): each circle keeps its side (or with keep: "order" its order), circles without a previous place are placed around the others. side restricts the movement to the left or right of the axis (for "x": up or down), min and max to a range across it. weights and obstacles work as for boxes, and so does shrink (smaller radii). A circle whose center or radius is not a finite number is returned unchanged and ignored.

circles
The circles; they are not changed (unless they are also result).
axis
The axis the circles sit along: "x", "y" (pointing up) or a vector; its left and right are the sides for options.side.
options optional
The padding, side, range, weights and obstacles (see SeparateCirclesAlongAxisOptions).
result optional
Optional list to store the result in: one circle per input circle, in the same order; its circles are reused and written to, and circles are added or removed at the end to match the number of input circles. Its content is not read: for the previous layout, see options.previous.

Returns The moved circles.