Lens that counts the points beneath it
Drag a circular lens over a scatter plot and see the points inside counted per category as bars around its rim.
import {
VisQuill,
Reactive,
Animate,
Attach,
Boxes,
Points,
type StringValue,
type Real,
type Point,
type Polyline,
type RvgGroup,
Svg
} from "@visquill/visquill-gdk"
import {createPoints} from "../../utils/decoration"
import './styles.css'
// Configuration
// Each button cycles through its list; the lens animates to the new value.
const radii = [80, 100, 120, 140]
const spans = [Math.PI / 1.5, Math.PI / 1.25, Math.PI]
const rotations = [-Math.PI / 2, 0, Math.PI / 2, Math.PI]
const barOffsets = [10, 20, 30, 40]
const barWidth = 20
const BUTTON = {x: 10, y: 20, width: 140, height: 24, gap: 8}
const dataTypes = ["red", "green", "blue", "orange"]
const numberOfDataPoints = 100
// Bounding box for scatter points
const pointBox = Boxes.box([150, 50], 800, 600)
/**
* Creates an interactive lens that counts scattered data points by category.
* The lens is draggable and bars update reactively as points enter or leave.
*
* @param div - Container element for the graphic
*/
export function run(div: HTMLDivElement) {
const rvg = VisQuill.create(div, "demos-")
const canvas = rvg.layer("lenses-single-hedgehog-path-")
// Layers
const pointLayer = canvas.layer()
const lens = canvas.layer()
const handle = canvas.handles.disk("handle", Boxes.center(pointBox))
// Reactive state, animated by the buttons
const radius = lens.values.real(radii[0])
const span = lens.values.real(spans[0])
const rotation = lens.values.real(rotations[0])
const barOffset = lens.values.real(barOffsets[0])
const valueLabels: StringValue[] = []
const heights: Real[] = []
// Build
const points = createPoints(pointLayer, pointBox, numberOfDataPoints, dataTypes)
createLens()
createInteraction()
// Buttons last, so they sit above the points and the lens
const controls = canvas.layer()
const degrees = (v: number) => `${Math.round(v * 180 / Math.PI)}°`
const pixels = (v: number) => `${v} px`
cycleButton(controls, 0, "Radius", radii, pixels, radius)
cycleButton(controls, 1, "Span", spans, degrees, span)
cycleButton(controls, 2, "Rotation", rotations, degrees, rotation)
cycleButton(controls, 3, "Offset", barOffsets, pixels, barOffset)
function createLens() {
// Circular boundary
const ring = lens.visuals.circle("ring")
Attach.circleRadiusToValue(ring, radius)
// Polyline baseline (5 points create 4 segments)
const baseline = lens.visuals.polyline("hedgehog-path", 5)
Attach.polylineToCircle(baseline, ring, span, {distance: 10, angle: rotation})
// One bar per segment, with its caption and its count
const bars = barsAlongBaseline(lens, baseline, dataTypes, barOffset)
heights.push(...bars.map(bar => bar.height))
valueLabels.push(...bars.map(bar => bar.count))
// Pin lens to handle
Attach.pointToPoint(lens.anchor, handle)
}
function createInteraction() {
// Recount points when lens moves or resizes
Reactive.do([lens.anchor, radius], () => {
for (let i = 0; i < dataTypes.length; i++) {
const count = countPoints(points[i])
Animate.real.follow(heights[i], count / numberOfDataPoints * 400)
valueLabels[i].value = count.toString()
}
})
function countPoints(points: Point[]) {
let count = 0
for (const point of points) {
if (Points.distanceToPoint(point, lens.anchor).value < radius.value) {
count += 1
}
}
return count
}
}
}
// ─── Bars along the baseline ──────────────────────────────────────────────────
// One bar per segment of the baseline: a pin on the segment, moved out by
// barOffset, and a rectangle standing on it. The caption sits on the segment,
// the count on top of the bar.
function barsAlongBaseline(group: RvgGroup, baseline: Polyline, names: string[], barOffset: Real) {
return names.map((name, i) => {
const height = group.values.real()
const foot = group.values.pin()
Attach.pointToShapeSegment(foot, baseline, i, {distance: barOffset})
const bar = group.visuals.rectangle("bar-" + name)
Attach.rectangleToPoint(bar, foot, {width: barWidth, height, anchorY: "bottom"})
const caption = group.text.label(name, "caption", {oriented: true, flip: "auto"})
Attach.pointToShapeSegment(caption, baseline, i)
const count = group.text.label("", "value-label", {oriented: true, flip: "auto"})
Attach.pointToRectangleEdge(count, bar, {distance: 10, orientation: "relative"})
return {height, count}
})
}
// ─── Button ───────────────────────────────────────────────────────────────────
// A box with a label "<title>: <value>". A click shows the next value of the list
// and animates the bound value to it.
function cycleButton(group: RvgGroup, row: number, title: string, values: number[],
format: (value: number) => string, value: Real): void {
const top = BUTTON.y + row * (BUTTON.height + BUTTON.gap)
const box = group.visuals.box("button", [BUTTON.x, top], [BUTTON.x + BUTTON.width, top + BUTTON.height])
const label = group.text.labelAt("", Boxes.center(box), "button-label")
let index = 0
const show = () => label.value = `${title}: ${format(values[index])}`
show()
Svg.get(box).addEventListener("click", () => {
index = (index + 1) % values.length
show()
Animate.real.eased(value, values[index], 1000)
})
}A draggable lens counts the points inside it per category and shows the counts as bars around its
rim. Plots.labeledHedgeHogBars draws the bars, and Animate.real.follow moves them to each new
count.
How it works
The lens is a layer attached to a drag handle. A polyline with 5 points is laid along the ring with Attach.polylineToCircle; the buttons animate its span and rotation.
const baseline = lens.visuals.polyline("hedgehog-path", 5)
Attach.polylineToCircle(baseline, ring, span, {distance: 10, angle: rotation})
Each of its 4 segments gets a bar: a pin on the segment, moved out by the offset, and a rectangle standing on it.
Attach.pointToShapeSegment(foot, baseline, i, {distance: barOffset})
const bar = group.visuals.rectangle("bar-" + name)
Attach.rectangleToPoint(bar, foot, {width: barWidth, height, anchorY: "bottom"})
Whenever the lens moves or its radius changes, the points of each category inside the circle are counted. Animate.real.follow moves each bar height towards the new count.
Reactive.do([lens.anchor, radius], () => {
for (let i = 0; i < dataTypes.length; i++) {
const count = countPoints(points[i])
Animate.real.follow(heights[i], count / numberOfDataPoints * 400)
valueLabels[i].value = count.toString()
}
})
A point counts when Points.distanceToPoint to the lens centre is less than the radius.
API
Attach.polylineToCircle: lays a polyline along a circle, with a span, a distance and a start angleAttach.circleRadiusToValue: the ring's radiusAttach.pointToShapeSegment: a bar's foot and caption on a segmentAttach.rectangleToPoint: stands a bar on its footAttach.pointToRectangleEdge: puts the count on top of the barAttach.pointToPoint: moves the lens layer with the handleAnimate.real.follow: eases a value towards a targetPoints.distanceToPoint: the membership test
See also
lenses/03-two-data-types


