Labels collide. The usual fixes — a force simulation, simulated annealing — work well and give a slightly different arrangement on every run. This one hands each element an ordered list of sides and takes the first that is free, so the same input always produces the same layout. It does that in a rectangular frame and in a circular one, in two dimensions or three.
Each point is fixed — it is data, it cannot move. Each label is attached to its point and given an ordered list of sides to try. The first side that does not collide with anything already placed is the one it takes. Drag the point count up until labels start stepping around, then switch on hiding and watch the overlap count reach zero.
ThruSpace.layout('#chart', [
// the points first: fixed, they are the data
...points.map(p => ({ el: p.dot, x: p.x, y: p.y })),
// then the labels, each with sides to try in order
...points.map(p => ({
el: p.label,
at: p.dot,
gap: 3,
placement: ['right', 'top', 'left', 'bottom',
'top-right', 'bottom-right',
'top-left', 'bottom-left'],
hideIfBlocked: false
}))
]);
Anchors are listed before labels on purpose. A point never moves, so if a point were placed after a label it could land on top of it. With every point settled first, each label only has to find a gap among things that are already final.
Drag the arrangement slider: each value is a different set of points, and every one of them gives the same result every time it is drawn. Nothing here is annealed, sampled or randomised, so a chart rendered in a test matches the one on screen.
Past roughly sixteen points in this box there are not always eight free sides left, and labels start to collide. Switch on hiding and that count goes to zero at every size — checked across all eight arrangements. Points sitting on points is counted separately: that is the data, and no label strategy can move it.
The same engine, without the labels: every container carries a rectangular frame and a circle inscribed in it, sharing one origin. Drag either slider — the code below does not change while you do.
Amber is the inscribed circle, blue the square and its axes. Both are drawn only so you can see the frames — elements do not need them.
ThruSpace.layout('#space', [
...ThruSpace.ring('.node', { radius: 78 }),
{ el: '#center', x: 0, y: 0 },
{ el: '#corner', x: 70, y: 78 }
]);
Eight items on the circle and two in the square, in one call. No mode is declared.
angle and radius mean the circle; x and y mean the square.
const n = 8, R = Math.min(w, h) / 2 * 0.78;
for (let i = 0; i < n; i++) {
const a = (i * 2 * Math.PI) / n;
el[i].style.left = (w/2 + R * Math.cos(a)
- el[i].offsetWidth / 2) + 'px';
el[i].style.top = (h/2 - R * Math.sin(a)
- el[i].offsetHeight / 2) + 'px';
}
// re-run on every resize
// redo the maths when n changes
There is no coordinate mode to select. The frame is inferred from the properties you set, so a single list can mix both, and the two frames stay registered to the same origin.
x and y run from −100 to 100, as a share of half the container.
The origin is the centre, and y increases upward, the way it does in geometry rather than
in screen coordinates.
angle is in degrees from the positive x-axis, radius is 0–100 as a share
of the inscribed circle. ring() spreads a set of elements evenly, over a full turn
or any arc.
Because every unit is relative to the space rather than fixed in pixels, a layout stays correct when the container changes size without any responsive code of your own.
Label collision is an old problem with several good answers. Most resolve it by simulation, which works well and gives a slightly different result on each run. Chart libraries solve it internally — AG Charts takes an ordered placement array much like this one, and that is where the idea came from. So the pattern is not new; what is different is that it works outside any one library, it is strictly repeatable, and it does the same job in a circular frame.
| Moves labels | Ordered fallback | Repeatable byte for byte | Outside a chart library | Circular frame | |
|---|---|---|---|---|---|
| avoid-overlap | yes | yes | no | yes | no |
| D3 force layout | yes | no | no | yes | no |
| Chart-library built-ins | yes | some | varies | no | rarely |
| ThruSpace | yes | yes | yes | yes | yes |
The third column is the one worth arguing about: a chart rendered in a test matches the one on screen, which the simulation-based approaches cannot promise.
A constraint solver needs every position to be satisfiable exactly. A range says something looser and more human: put this somewhere between here and there, and sort it out if it collides. Both panels hold the same items at the same angles. The only difference is that the right-hand one gives the radius room to move.
{ angle: a, radius: 45 }
{ angle: a, radius: [30, 82] }
Add items until the fixed layout breaks. At six or seven it holds up perfectly well — a range is not magic, it is room to manoeuvre for the moment the fixed choice stops fitting. Neither code sample changes.
Everything above runs in the plane. Give an element a z and the square it lives in
becomes a cube; give it a phi and the circle becomes a sphere. The properties you were already
using keep their meaning, so a layout written for the plane returns the same positions it always did.
These are ordinary HTML elements, placed by the same engine.
ThruSpace.layout('#world', [
...ThruSpace.ball('.slot', { radius: 78 }),
{ el:'#c1', x:-92, y: 92, z: 92 },
{ el:'#c2', x: 92, y:-92, z:-92 }
]);
ThruSpace.layout('#space', [
...ThruSpace.ring('.node', { radius: 78 }),
{ el:'#center', x: 0, y: 0 },
{ el:'#corner', x: 70, y: 78 }
]);
ring becomes ball, and a third number joins the pairs. Nothing else is
different, and no coordinate mode is named in either one.
Drag the cube directly, or use the sliders. Items stay square to the viewer while the space around them turns; the corner labels are fixed to the cube, so they turn with it.
Everything so far places an element against the frame. An element can instead be placed against another element: touching it in a given direction, or sharing one of its axes. The label then follows whatever its anchor does, and nothing has to be recomputed when the anchor moves.
// the pin sits on the circle
{ el: pin, angle: a, radius: 62 }
// the label touches it, pointing outward
{ el: label, at: pin, angle: a, gap: 6 }
// and this one shares the hub's x axis
{ el: caption, at: '#hub', align: 'x', y: -64 }
Given a direction, the engine computes how far apart the two elements must sit for their edges to meet, and places the second one there. Move the gap slider and the labels step outward together; none of the angles change.
Contacts are resolved in repeated passes, so an element may name an anchor that appears later in the list, and chains work: a label attached to a pin attached to the circle.
Nothing above treats a container as special. Give any element a space of its own
and the same square, circle and axes appear inside it, in its own units, with its own conflict resolution.
What follows is one call, three levels deep.
ThruSpace.layout('#nest', [
{ el:'#tile', x:0, y:0, space: [
{ el:'#t1', x:-55, y:0 },
{ el:'#t2', x: 0, y:0 },
{ el:'#t3', x: 55, y:0 }
]},
...hubs.map(h => ({
el: h.el, angle: h.angle, radius: 78,
space: ThruSpace.ring(h.leaves, { radius: 72 })
}))
]);
The hubs sit on a circle in the outer square. Each hub is a circle in its own right, and the leaves inside it know nothing about the outer space — their coordinates are their own.
This is the part that replaces nesting as most frameworks do it. There is no parent container to declare, no wrapper to insert, and no coordinate to recompute when the outer arrangement changes. The rectangle at the centre is the same mechanism with a square frame instead of a round one.
Three functions and five property names. There is nothing else to learn, and nothing to configure before the first call.
ThruSpace.layout(container, items)
// items: [{ el, x, y, z, angle, phi, radius, space, at, gap }]
// returns { placed, notes, depth }
ThruSpace.ring(selector, { radius, start, end })
// even spread over a circle or any arc
ThruSpace.ball(selector, { radius })
// even spread over a sphere
notes tells you which elements the solver had to move and where it put them, which is
what the log in the range demo is printing.
x y | −100 to 100, share of half the container |
z | same, along the depth axis; its presence makes the frame a cube |
angle | degrees from the positive x-axis |
phi | degrees from the axis facing you; 90 is the screen plane |
radius | 0 to 100, share of the inscribed circle or sphere |
| any of them | may be [min, max] instead of a number |
space | a list of items placed inside this element, in its own units |
at | another element to position against instead of the frame |
gap | extra pixels beyond touching, used with at and angle |
Because no unit is a pixel, a layout holds its proportions at any
container size. Call layout again after a resize and the solver settles the ranges afresh.
Filed as a geometry model rather than a layout library, which is why the third axis slotted in without changing a single existing call.