All files / owid-grapher/clientUtils Bounds.ts

86.87% Statements 225/259
88.1% Branches 37/42
70.59% Functions 24/34
86.87% Lines 225/259

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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 3971x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 20x 20x 20x 1x 1x     1x 1x 2x 2x 1x 1x 2x 2x 1x 1x 233x 233x 233x 233x 233x 233x 233x 1x 1x 1x 4x 4x 4x 4x 4x 808x 808x 808x 808x 808x 4x 4x 4x 4x 4x 1x 1x 98x 98x 1x 1x 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 306x 306x 1x 136x 136x 1x 306x 306x 1x 136x 136x 1x 6x 6x 1x 381x 381x 1x 202x 202x 1x     1x 1x     1x     1x     1x     1x 1x 8x 8x 8x 8x 8x 8x 8x 1x 1x 16x 16x 1x 1x 8x 8x 1x 1x 8x 8x 8x 8x 8x 8x 8x 1x 1x 1852x 1852x 1852x 1852x 1852x 1852x 1852x 1x 1x               1x 1x     1x 1x     1x 1x 376x 368x 368x 368x 368x 368x 368x 368x 8x 376x 376x 376x 376x 1x 1x 368x         1x 1x 20x 20x 20x 20x 20x 20x 20x 1x 1x               1x 1x 184x 184x 184x 184x 184x 22x 22x 22x 184x                                                                                                                                                                                                                                                                                    
import { isNumber, mapValues, range } from "./Util"
import { PointVector } from "./PointVector"
import pixelWidth from "string-pixel-width"
import { Box, GridParameters, Position, PositionMap } from "./owidTypes"
 
// Important utility class for all visualizations
// Since we want to be able to render charts headlessly and functionally, we
// can't rely on the DOM to do these calculations for us, and instead must
// calculate using geometry and first principles
 
type PadObject = PositionMap<number>
 
export interface GridBounds {
    col: number
    row: number
    edges: Set<Position>
    bounds: Bounds
}
 
export class Bounds {
    static ctx: CanvasRenderingContext2D
 
    static fromProps(props: Box): Bounds {
        const { x, y, width, height } = props
        return new Bounds(x, y, width, height)
    }
 
    static fromBBox(bbox: Box): Bounds {
        return this.fromProps(bbox)
    }
 
    static fromRect(rect: ClientRect): Bounds {
        return new Bounds(rect.left, rect.top, rect.width, rect.height)
    }
 
    static fromElement(el: HTMLElement): Bounds {
        return Bounds.fromRect(el.getBoundingClientRect())
    }
 
    static fromCorners(p1: PointVector, p2: PointVector): Bounds {
        const x1 = Math.min(p1.x, p2.x)
        const x2 = Math.max(p1.x, p2.x)
        const y1 = Math.min(p1.y, p2.y)
        const y2 = Math.max(p1.y, p2.y)
 
        return new Bounds(x1, y1, x2 - x1, y2 - y1)
    }
 
    // Merge a collection of bounding boxes into a single encompassing Bounds
    static merge(boundsList: Bounds[]): Bounds {
        let x1 = Infinity,
            y1 = Infinity,
            x2 = -Infinity,
            y2 = -Infinity
        for (const b of boundsList) {
            x1 = Math.min(x1, b.x)
            y1 = Math.min(y1, b.y)
            x2 = Math.max(x2, b.x + b.width)
            y2 = Math.max(y2, b.y + b.height)
        }
        return Bounds.fromCorners(
            new PointVector(x1, y1),
            new PointVector(x2, y2)
        )
    }
 
    static empty(): Bounds {
        return new Bounds(0, 0, 0, 0)
    }
 
    static forText(
        str: string = "",
        {
            x = 0,
            y = 0,
            fontSize = 16,
            fontWeight = 400,
        }: {
            x?: number
            y?: number
            fontSize?: number
            fontWeight?: number
            fontFamily?: string
        } = {}
    ): Bounds {
        // Collapse contiguous spaces into one
        str = str.replace(/ +/g, " ")
 
        const isBold = fontWeight >= 600
 
        let bounds = Bounds.empty()
        if (str) {
            // pixelWidth uses a precomputed character width table to quickly give a
            // rough estimate of string width based on characters in a string - it is probably not
            // worth caching further
            const width = pixelWidth(str, {
                font: "arial",
                size: fontSize,
                bold: isBold,
            })
            const height = fontSize
            bounds = new Bounds(x, y - height, width, height)
        }
 
        return bounds
    }
 
    readonly x: number
    readonly y: number
    readonly width: number
    readonly height: number
 
    constructor(x: number, y: number, width: number, height: number) {
        this.x = x
        this.y = y
        this.width = Math.max(width, 0)
        this.height = Math.max(height, 0)
    }
 
    get left(): number {
        return this.x
    }
    get top(): number {
        return this.y
    }
    get right(): number {
        return this.x + this.width
    }
    get bottom(): number {
        return this.y + this.height
    }
    get centerX(): number {
        return this.x + this.width / 2
    }
    get centerY(): number {
        return this.y + this.height / 2
    }
    get centerPos(): PointVector {
        return new PointVector(this.centerX, this.centerY)
    }
    get area(): number {
        return this.width * this.height
    }
 
    get topLeft(): PointVector {
        return new PointVector(this.left, this.top)
    }
    get topRight(): PointVector {
        return new PointVector(this.right, this.top)
    }
    get bottomLeft(): PointVector {
        return new PointVector(this.left, this.bottom)
    }
    get bottomRight(): PointVector {
        return new PointVector(this.right, this.bottom)
    }
 
    padLeft(amount: number): Bounds {
        return new Bounds(
            this.x + amount,
            this.y,
            this.width - amount,
            this.height
        )
    }
 
    padRight(amount: number): Bounds {
        return new Bounds(this.x, this.y, this.width - amount, this.height)
    }
 
    padBottom(amount: number): Bounds {
        return new Bounds(this.x, this.y, this.width, this.height - amount)
    }
 
    padTop(amount: number): Bounds {
        return new Bounds(
            this.x,
            this.y + amount,
            this.width,
            this.height - amount
        )
    }
 
    padWidth(amount: number): Bounds {
        return new Bounds(
            this.x + amount,
            this.y,
            this.width - amount * 2,
            this.height
        )
    }
 
    padHeight(amount: number): Bounds {
        return new Bounds(
            this.x,
            this.y + amount,
            this.width,
            this.height - amount * 2
        )
    }
 
    fromLeft(amount: number): Bounds {
        return this.padRight(this.width - amount)
    }
 
    fromBottom(amount: number): Bounds {
        return this.padTop(this.height - amount)
    }
 
    pad(amount: number | PadObject): Bounds {
        if (isNumber(amount)) {
            return new Bounds(
                this.x + amount,
                this.y + amount,
                this.width - amount * 2,
                this.height - amount * 2
            )
        }
        return this.padTop(amount.top ?? 0)
            .padRight(amount.right ?? 0)
            .padBottom(amount.bottom ?? 0)
            .padLeft(amount.left ?? 0)
    }
 
    expand(amount: number | PadObject): Bounds {
        if (isNumber(amount)) return this.pad(-amount)
        return this.pad(
            mapValues(amount, (v) => (v !== undefined ? -v : undefined))
        )
    }
 
    set(props: {
        x?: number
        y?: number
        width?: number
        height?: number
    }): Bounds {
        return Bounds.fromProps({ ...this, ...props })
    }
 
    scale(scale: number): Bounds {
        return new Bounds(
            this.x * scale,
            this.y * scale,
            this.width * scale,
            this.height * scale
        )
    }
 
    intersects(otherBounds: Bounds): boolean {
        const r1 = this
        const r2 = otherBounds
 
        return !(
            r2.left > r1.right ||
            r2.right < r1.left ||
            r2.top > r1.bottom ||
            r2.bottom < r1.top
        )
    }
 
    lines(): PointVector[][] {
        return [
            [this.topLeft, this.topRight],
            [this.topRight, this.bottomRight],
            [this.bottomRight, this.bottomLeft],
            [this.bottomLeft, this.topLeft],
        ]
    }
 
    boundedPoint(p: PointVector): PointVector {
        return new PointVector(
            Math.max(Math.min(p.x, this.right), this.left),
            Math.max(Math.min(p.y, this.bottom), this.top)
        )
    }
 
    containsPoint(x: number, y: number): boolean {
        return (
            x >= this.left &&
            x <= this.right &&
            y >= this.top &&
            y <= this.bottom
        )
    }
 
    contains(p: PointVector): boolean {
        return this.containsPoint(p.x, p.y)
    }
 
    encloses(bounds: Bounds): boolean {
        return (
            this.containsPoint(bounds.left, bounds.top) &&
            this.containsPoint(bounds.left, bounds.bottom) &&
            this.containsPoint(bounds.right, bounds.top) &&
            this.containsPoint(bounds.right, bounds.bottom)
        )
    }
 
    toCSS(): { left: string; top: string; width: string; height: string } {
        return {
            left: `${this.left}px`,
            top: `${this.top}px`,
            width: `${this.width}px`,
            height: `${this.height}px`,
        }
    }
 
    toProps(): { x: number; y: number; width: number; height: number } {
        return { x: this.x, y: this.y, width: this.width, height: this.height }
    }
 
    toArray(): [[number, number], [number, number]] {
        return [
            [this.left, this.top],
            [this.right, this.bottom],
        ]
    }
 
    xRange(): [number, number] {
        return [this.left, this.right]
    }
 
    grid(
        gridParams: GridParameters,
        padding: SplitBoundsPadding = {}
    ): GridBounds[] {
        const { columns, rows } = gridParams
        const { columnPadding = 0, rowPadding = 0, outerPadding = 0 } = padding
 
        const contentWidth =
            this.width - columnPadding * (columns - 1) - outerPadding * 2
        const contentHeight =
            this.height - rowPadding * (rows - 1) - outerPadding * 2
        const boxWidth = Math.floor(contentWidth / columns)
        const boxHeight = Math.floor(contentHeight / rows)
 
        return range(0, rows * columns).map((index: number) => {
            const col = index % columns
            const row = Math.floor(index / columns)
            const edges = new Set<Position>()
            if (col === 0) edges.add(Position.left)
            if (col === columns - 1) edges.add(Position.right)
            if (row === 0) edges.add(Position.top)
            if (row === rows - 1) edges.add(Position.bottom)
            return {
                row,
                col,
                edges,
                bounds: new Bounds(
                    this.x + outerPadding + col * (boxWidth + columnPadding),
                    this.y + outerPadding + row * (boxHeight + rowPadding),
                    boxWidth,
                    boxHeight
                ),
            }
        })
    }
 
    yRange(): [number, number] {
        return [this.bottom, this.top]
    }
 
    equals(bounds: Bounds): boolean {
        return (
            this.x === bounds.x &&
            this.y === bounds.y &&
            this.width === bounds.width &&
            this.height === bounds.height
        )
    }
 
    // Calculate squared distance between a given point and the closest border of the bounds
    // If the point is within the bounds, returns 0
    private distanceToPointSq(p: PointVector): number {
        if (this.contains(p)) return 0
 
        const cx = Math.max(Math.min(p.x, this.x + this.width), this.x)
        const cy = Math.max(Math.min(p.y, this.y + this.height), this.y)
        return (p.x - cx) * (p.x - cx) + (p.y - cy) * (p.y - cy)
    }
 
    distanceToPoint(p: PointVector): number {
        return Math.sqrt(this.distanceToPointSq(p))
    }
}
 
interface SplitBoundsPadding {
    columnPadding?: number
    rowPadding?: number
    outerPadding?: number
}
 
// Since nearly all our components need a bounds, but most tests don't care about bounds, have a default bounds
// to use so we don't have to create a bounds for every test.
export const DEFAULT_BOUNDS = new Bounds(0, 0, 640, 480)