All files / owid-grapher/grapher/axis Axis.ts

93.02% Statements 573/616
79.46% Branches 89/112
98.11% Functions 52/53
93.02% Lines 573/616

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 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 6151x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 184x 184x 1x 1x 368x 368x 368x 282x 282x 48x 48x 368x 368x 164x 164x   164x 368x 368x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 40x 40x 40x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 16x 16x 1x 1x 16x 16x 1x 1x 18x 18x 1x 1x 18x 18x 1x 1x 18x 18x 1x 1x 2x 2x 1x 1x 20x 20x 1x 1x 16x 16x 1x 1x 16x 16x 1x 1x 4x 4x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x   4x 4x   4x 4x 4x 4x 1x 1x 1x 24x 24x 24x 24x 24x 24x 24x 24x 24x 1x 1x 18x 18x 18x 18x 18x 18x 18x 1x 1x 18x 18x 1x 1x 8x 8x 1x 1x 8x 8x 1x 1x 1x 18x 18x 18x 18x 18x 18x 18x 18x 1x 1x 18x 18x 18x 18x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 22x 22x 22x 18x 18x 18x 16x 16x 16x 14x 14x 4x 4x       84x 84x 54x 84x 84x 84x 84x 84x                       18x 14x 14x 14x 14x 14x 14x 14x 18x 18x 18x 18x 18x 18x 1x 1x 92x 92x     92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 68x 68x 92x 92x 92x 1x 1x 90x         90x     90x                   90x 90x 1x 1x 16x 16x 1x 1x 9x 9x 1x 1x 16x 16x 16x 16x 16x 16x 90x 212x 212x 212x 184x 184x 184x 184x 212x 212x 212x 212x 212x 212x 90x 16x 16x 1x 1x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 1x 1x 4x 4x 1x 1x 16x 16x 4x 12x 12x 12x 12x 12x 16x 1x 1x 1x 1x 12x 12x 1x 1x 8x 8x   8x 8x 1x 1x 8x 6x 6x 8x 1x 1x 2x 2x 1x 1x 8x 8x 8x 8x     8x 8x 1x 1x 8x 8x 1x 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x     9x 9x 9x 9x 9x 9x 1x 1x 46x 46x 46x 46x 46x 46x 46x 46x 46x 16x 16x 16x 46x 8x 8x 8x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 1x 1x 1x     1x 1x 1x 1x 12x 12x 1x 1x 8x 8x 1x 1x 2x 2x 1x 1x 8x 6x 6x 8x 1x 1x 8x 8x 8x 8x 8x     8x 8x 1x 1x 2x 2x 1x 1x 8x 8x 1x 1x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 1x 1x 4x 4x 4x 4x 1x 1x 4x 4x 4x 4x 1x 1x 1x 8x 8x 8x 8x 1x 1x 1x 8x 8x 8x 8x 1x 1x 1x 8x 8x 8x 8x 8x 1x 1x 8x 8x 1x 1x
import { scaleLog, scaleLinear, ScaleLinear, ScaleLogarithmic } from "d3-scale"
import { observable, computed } from "mobx"
import {
    rollingMap,
    min,
    uniq,
    sortBy,
    max,
    numberMagnitude,
} from "../../clientUtils/Util"
import { Bounds, DEFAULT_BOUNDS } from "../../clientUtils/Bounds"
import { TextWrap } from "../text/TextWrap"
import { AxisConfig } from "./AxisConfig"
import { CoreColumn } from "../../coreTable/CoreTableColumns"
import { ValueRange } from "../../coreTable/CoreTableConstants"
import {
    HorizontalAlign,
    Position,
    ScaleType,
    VerticalAlign,
} from "../../clientUtils/owidTypes"
import { TickFormattingOptions } from "../../clientUtils/formatValue"
 
interface Tickmark {
    value: number
    priority: number
    faint?: boolean
    gridLineOnly?: boolean
}
 
interface TickLabelPlacement {
    value: number
    formattedValue: string
    x: number
    y: number
    width: number
    height: number
    xAlign?: HorizontalAlign
    yAlign?: VerticalAlign
    isHidden: boolean
}
 
const doIntersect = (bounds: Bounds, bounds2: Bounds): boolean => {
    return bounds.intersects(bounds2)
}
 
const boundsFromLabelPlacement = (label: TickLabelPlacement): Bounds => {
    const { x, y, width, height, xAlign, yAlign } = label
    const xShift =
        xAlign === HorizontalAlign.center
            ? -width / 2
            : xAlign === HorizontalAlign.right
            ? -width
            : 0
    const yShift =
        yAlign === VerticalAlign.middle
            ? -height / 2
            : yAlign === VerticalAlign.bottom
            ? -height
            : 0
    return new Bounds(x + xShift, y + yShift, width, height)
}
 
abstract class AbstractAxis {
    config: AxisConfig
 
    @observable.ref domain: ValueRange
    @observable formatColumn?: CoreColumn // Pass the column purely for formatting reasons. Might be a better way to do this.
    @observable hideFractionalTicks = false
    @observable hideGridlines = false
    @observable.struct range: ValueRange = [0, 0]
    @observable private _scaleType?: ScaleType
    @observable private _label?: string
 
    constructor(config: AxisConfig) {
        this.config = config
        this.domain = [config.domain[0], config.domain[1]]
    }
 
    /**
     * The orthogonal size of the axis.
     * For horizontal axes, this is the height.
     * For vertical axes, this is the width.
     */
    abstract get size(): number
    abstract get orient(): Position
    abstract get labelWidth(): number
 
    abstract placeTickLabel(value: number): TickLabelPlacement
 
    @computed get hideAxis(): boolean {
        return this.config.hideAxis ?? false
    }
 
    @computed get labelPadding(): number {
        return this.config.labelPadding ?? 5
    }
 
    @computed get nice(): boolean {
        return this.config.nice ?? false
    }
 
    @computed get fontSize(): number {
        return this.config.fontSize
    }
 
    @computed private get maxTicks(): number {
        return this.config.maxTicks ?? 6
    }
 
    @computed get canChangeScaleType(): boolean | undefined {
        return this.config.canChangeScaleType
    }
 
    @computed get scaleType(): ScaleType {
        return this._scaleType ?? this.config.scaleType ?? ScaleType.linear
    }
 
    set scaleType(value: ScaleType) {
        this._scaleType = value
    }
 
    @computed get label(): string {
        return this._label ?? this.config.label
    }
 
    set label(value: string) {
        this._label = value
    }
 
    // This will expand the domain but never shrink.
    // This will change the min unless the user's min setting is less
    // This will change the max unless the user's max setting is greater
    // Undefined values are ignored
    updateDomainPreservingUserSettings(
        domain: [number | undefined, number | undefined]
    ): this {
        this.domain = [
            domain[0] !== undefined
                ? Math.min(this.domain[0], domain[0])
                : this.domain[0],
            domain[1] !== undefined
                ? Math.max(this.domain[1], domain[1])
                : this.domain[1],
        ]
        return this
    }
 
    // todo: refactor. switch to a parent pattern?
    _update(parentAxis: AbstractAxis): this {
        this.formatColumn = parentAxis.formatColumn
        this.domain = parentAxis.domain.slice() as ValueRange
        this.hideFractionalTicks = parentAxis.hideFractionalTicks
        this.hideGridlines = parentAxis.hideGridlines
        this.range = parentAxis.range.slice() as ValueRange
        this._scaleType = parentAxis._scaleType
        this._label = parentAxis._label
        return this
    }
 
    @computed private get d3_scale():
        | ScaleLinear<number, number>
        | ScaleLogarithmic<number, number> {
        const d3Scale =
            this.scaleType === ScaleType.log ? scaleLog : scaleLinear
        const scale = d3Scale().domain(this.domain).range(this.range)
        return this.nice ? scale.nice(this.totalTicksTarget) : scale
    }
 
    @computed get rangeSize(): number {
        return Math.abs(this.range[1] - this.range[0])
    }
 
    @computed get rangeMax(): number {
        return Math.max(this.range[1], this.range[0])
    }
 
    @computed get rangeMin(): number {
        return Math.min(this.range[1], this.range[0])
    }
 
    /** The number of ticks we should _aim_ to show, not necessarily a strict target. */
    @computed private get totalTicksTarget(): number {
        // Chose 1.8 here by trying a bunch of different faceted charts and figuring out what
        // a reasonable lower bound is.
        // NOTE: This setting is used between both log & linear axes, check both when tweaking.
        // -@danielgavrilov, 2021-06-15
        return Math.round(
            Math.min(this.maxTicks, this.rangeSize / (this.fontSize * 1.8))
        )
    }
 
    getTickValues(): Tickmark[] {
        const { scaleType, d3_scale } = this
 
        let ticks: Tickmark[]
        if (scaleType === ScaleType.log) {
            // Show a bit more ticks for log axes
            const maxLabelledTicks = Math.round(this.totalTicksTarget * 1.25)
            const maxTicks = Math.round(this.totalTicksTarget * 3)
 
            // This is a wild heuristic that decides how many tick lines and grid lines we want to
            // show for log charts.
            //
            // It tries to achive multiple goals:
            // * make it obvious for the user which values they're looking at
            // * ideally, make it very clear that this is a log axis by looking like log paper
            // * (but) don't overwhelm the user
            // * avoid cases where only one tick is shown for the whole axis (we had those!)
            //
            // This code roughly works as follows:
            // First, we let d3 generate ticks for the axis. d3 gives values of the form `y * 10^x`,
            // with 0 < y < 10.
            // We then assign priorities to these values:
            // * priority 1 (highest) to values of the form `1 * 10^x` (e.g. 100)
            // * priority 2 to values of the form `2 * 10^x` or `5 * 10^x` (e.g. 5, 2000)
            // * priority 3 (lowest) to all other ("in-between") values (e.g. 70, 300)
            //
            // We then decide depending on the number of tick candidates what to do:
            // * if we have less than `maxLabelledTicks`, just show all
            // * if we have between `maxLabelledTicks` and `maxTicks`, show all "in-between" lines
            //   as faint grid lines without labels to give the chart that log paper look.
            //   We also show all priority 1 and 2 lines with labels, because there aren't too many
            //   of them.
            // * otherwise, remove priority 3 and, if necessary, priority 2 labels until we're below
            //   `maxLabelledTicks` labels overall
            //
            // -@MarcelGerber, 2020-08-07
            const tickCandidates = d3_scale.ticks(maxLabelledTicks)
            ticks = tickCandidates.map((value) => {
                // 10^x
                if (Math.fround(Math.log10(value)) % 1 === 0)
                    return { value, priority: 1 }
                // 5 * 10^x
                else if (Math.fround(Math.log10(value * 2)) % 1 === 0)
                    return { value, priority: 2 }
                // 2 * 10^x
                else if (Math.fround(Math.log10(value / 2)) % 1 === 0)
                    return { value, priority: 2 }
                return { value, priority: 3 }
            })
 
            if (ticks.length > maxLabelledTicks) {
                if (ticks.length <= maxTicks) {
                    // Convert all "in-between" lines to faint grid lines without labels
                    ticks = ticks.map((tick) => {
                        if (tick.priority === 3)
                            tick = {
                                ...tick,
                                faint: true,
                                gridLineOnly: true,
                            }
                        return tick
                    })
                } else {
                    // Remove some tickmarks again because the chart would get too overwhelming
                    // otherwise
                    for (let priority = 3; priority > 1; priority--) {
                        if (ticks.length > maxLabelledTicks)
                            ticks = ticks.filter(
                                (tick) => tick.priority < priority
                            )
                    }
                }
            }
        } else {
            // Only use priority 2 here because we want the start / end ticks
            // to be priority 1
            ticks = d3_scale.ticks(this.totalTicksTarget).map((tickValue) => ({
                value: tickValue,
                priority: 2,
            }))
        }
 
        if (this.hideFractionalTicks)
            ticks = ticks.filter((t) => t.value % 1 === 0)
 
        return uniq(ticks)
    }
 
    private getTickFormattingOptions(): TickFormattingOptions {
        const options: TickFormattingOptions = {}
        if (this.config.compactLabels) {
            options.shortNumberPrefixes = true
        }
        // The chart's tick formatting function is used by default to format axis ticks. This means
        // that the chart's `numDecimalPlaces` is also used by default to format the axis ticks.
        //
        // However, the author-specified decimal places are not always appropriate for rendering
        // ticks, because:
        // 1. Subsets of the data may require higher fidelity, e.g. users can use the timeline to
        //    end up in a subset of the dataset where values happen to be much lower than usual.
        // 2. Ticks may be rendered at granularities that may not exist in the data, e.g. the data
        //    may only contain 0 and 1, but we may show ticks in between those values.
        //
        // Therefore, when formatting ticks, we determine the `numDecimalPlaces` automatically, by
        // finding the smallest difference between any pair of ticks and making sure that we have
        // sufficient decimal places to express the difference to the first significant figure (the
        // first non-zero digit).
        //
        // One significant figure is sufficient because we use D3's ticks() and that creates
        // "uniformly-spaced, nicely-rounded values [...] where each value is a power of ten
        // multiplied by 1, 2 or 5"
        // See: https://github.com/d3/d3-array/blob/master/README.md#ticks
        //
        // -@danielgavrilov, 2020-05-27
        const minDist = min(
            rollingMap(this.baseTicks, (a, b) => Math.abs(a.value - b.value))
        )
        if (minDist !== undefined) {
            // Find the decimal places required to reach the first non-zero digit
            const dp = -numberMagnitude(minDist) + 1
            if (dp >= 0) {
                options.numDecimalPlaces = dp
            }
        }
        return options
    }
 
    place(value: number): number {
        if (!this.range) {
            console.error(
                "Can't place value on scale without a defined output range"
            )
            return value
        } else if (this.scaleType === ScaleType.log && value <= 0) {
            console.error(`Can't have ${value} which is <= 0 on a log scale`)
            return value
        } else if (this.domain[0] === this.domain[1]) {
            // When the domain is a single value, the D3 scale will by default place
            // the value at the middle of the range.
            // We instead want to place it at the end, in order to avoid an axis
            // domain line being plotted in the middle of a chart (most of the time
            // this occurs, the domain is [0, 0]).
            //
            // -@danielgavrilov, 2021-08-02
            return value > this.domain[0] ? this.range[1] : this.range[0]
        }
        return parseFloat(this.d3_scale(value).toFixed(1))
    }
 
    @computed get tickFontSize(): number {
        return 0.9 * this.fontSize
    }
 
    @computed protected get baseTicks(): Tickmark[] {
        return this.getTickValues().filter((tick) => !tick.gridLineOnly)
    }
 
    @computed get tickLabels(): TickLabelPlacement[] {
        // Get ticks with coordinates, sorted by priority
        const tickLabels = sortBy(this.baseTicks, (tick) => tick.priority).map(
            (tick) => this.placeTickLabel(tick.value)
        )
        // Hide overlapping ticks
        for (let i = 0; i < tickLabels.length; i++) {
            for (let j = i + 1; j < tickLabels.length; j++) {
                const t1 = tickLabels[i],
                    t2 = tickLabels[j]
                if (t1 === t2 || t1.isHidden || t2.isHidden) continue
                if (
                    doIntersect(
                        // Expand bounds slightly so that labels aren't
                        // too close together.
                        boundsFromLabelPlacement(t1).expand(3),
                        boundsFromLabelPlacement(t2).expand(3)
                    )
                )
                    t2.isHidden = true
            }
        }
        return tickLabels.filter((t) => !t.isHidden)
    }
 
    formatTick(
        tick: number,
        formattingOptionsOverride?: TickFormattingOptions
    ): string {
        const tickFormattingOptions: TickFormattingOptions = {
            ...this.getTickFormattingOptions(),
            ...formattingOptionsOverride,
        }
        return (
            this.formatColumn?.formatForTick(tick, tickFormattingOptions) ??
            tick.toString()
        )
    }
 
    @computed get labelFontSize(): number {
        return 0.7 * this.fontSize
    }
 
    @computed get labelTextWrap(): TextWrap | undefined {
        const text = this.label
        return text
            ? new TextWrap({
                  maxWidth: this.labelWidth,
                  fontSize: this.labelFontSize,
                  text,
              })
            : undefined
    }
}
 
export class HorizontalAxis extends AbstractAxis {
    clone(): HorizontalAxis {
        return new HorizontalAxis(this.config)._update(this)
    }
 
    @computed get orient(): Position {
        // Default to `bottom` unless overriden to `top`.
        return this.config.orient === Position.top
            ? Position.top
            : Position.bottom
    }
 
    @computed get labelOffset(): number {
        return this.labelTextWrap
            ? this.labelTextWrap.height + this.labelPadding * 2
            : 0
    }
 
    @computed get labelWidth(): number {
        return this.rangeSize
    }
 
    @computed get height(): number {
        if (this.hideAxis) return 0
        const { labelOffset, labelPadding } = this
        const maxTickHeight = max(this.tickLabels.map((tick) => tick.height))
        const height = maxTickHeight
            ? maxTickHeight + labelOffset + labelPadding
            : 0
        return Math.max(height, this.config.minSize ?? 0)
    }
 
    @computed get size(): number {
        return this.height
    }
 
    @computed protected get baseTicks(): Tickmark[] {
        let ticks = this.getTickValues().filter(
            (tick): boolean => !tick.gridLineOnly
        )
        const { domain } = this
 
        // Make sure the start and end values are present, if they're whole numbers
        const startEndPrio = this.scaleType === ScaleType.log ? 2 : 1
        if (domain[0] % 1 === 0)
            ticks = [
                {
                    value: domain[0],
                    priority: startEndPrio,
                },
                ...ticks,
            ]
        if (domain[1] % 1 === 0 && this.hideFractionalTicks)
            ticks = [
                ...ticks,
                {
                    value: domain[1],
                    priority: startEndPrio,
                },
            ]
        return uniq(ticks)
    }
 
    placeTickLabel(value: number): TickLabelPlacement {
        const formattedValue = this.formatTick(value)
        const { width, height } = Bounds.forText(formattedValue, {
            fontSize: this.tickFontSize,
        })
        let x = this.place(value)
        let xAlign = HorizontalAlign.center
        const left = x - width / 2
        const right = x + width / 2
        if (left < this.rangeMin) {
            x = this.rangeMin
            xAlign = HorizontalAlign.left
        }
        if (right > this.rangeMax) {
            x = this.rangeMax
            xAlign = HorizontalAlign.right
        }
        return {
            value,
            formattedValue,
            x,
            y: 0,
            width,
            height,
            xAlign,
            isHidden: false,
        }
    }
 
    // Add some padding before checking for intersection
    protected doIntersect(bounds: Bounds, bounds2: Bounds): boolean {
        return bounds.intersects(bounds2.padWidth(-5))
    }
}
 
export class VerticalAxis extends AbstractAxis {
    clone(): VerticalAxis {
        return new VerticalAxis(this.config)._update(this)
    }
 
    @computed get orient(): Position {
        return Position.left
    }
 
    @computed get labelWidth(): number {
        return this.height
    }
 
    @computed get labelOffset(): number {
        return this.labelTextWrap
            ? this.labelTextWrap.height + this.labelPadding * 2
            : 0
    }
 
    @computed get width(): number {
        if (this.hideAxis) return 0
        const { labelOffset, labelPadding } = this
        const maxTickWidth = max(this.tickLabels.map((tick) => tick.width))
        const width =
            maxTickWidth !== undefined
                ? maxTickWidth + labelOffset + labelPadding
                : 0
        return Math.max(width, this.config.minSize ?? 0)
    }
 
    @computed get height(): number {
        return this.rangeSize
    }
 
    @computed get size(): number {
        return this.width
    }
 
    placeTickLabel(value: number): TickLabelPlacement {
        const formattedValue = this.formatTick(value)
        const { width, height } = Bounds.forText(formattedValue, {
            fontSize: this.tickFontSize,
        })
        const y = this.place(value)
        return {
            value,
            formattedValue,
            x: 0,
            y,
            width,
            height,
            xAlign: HorizontalAlign.right,
            yAlign: VerticalAlign.middle,
            isHidden: false,
        }
    }
}
 
interface DualAxisProps {
    bounds?: Bounds
    horizontalAxis: HorizontalAxis
    verticalAxis: VerticalAxis
}
 
// DualAxis has the important task of coordinating two axes so that they work together!
// There is a *two-way dependency* between the bounding size of each axis.
// e.g. if the y axis becomes wider because a label is present, the x axis then has less
// space to work with, and vice versa
export class DualAxis {
    private props: DualAxisProps
    constructor(props: DualAxisProps) {
        this.props = props
    }
 
    @computed get horizontalAxis(): HorizontalAxis {
        const axis = this.props.horizontalAxis.clone()
        axis.range = this.innerBounds.xRange()
        return axis
    }
 
    @computed get verticalAxis(): VerticalAxis {
        const axis = this.props.verticalAxis.clone()
        axis.range = this.innerBounds.yRange()
        return axis
    }
 
    // We calculate an initial height from the range of the input bounds
    @computed private get horizontalAxisSize(): number {
        const axis = this.props.horizontalAxis.clone()
        axis.range = [0, this.bounds.width]
        return axis.size
    }
 
    // We calculate an initial width from the range of the input bounds
    @computed private get verticalAxisSize(): number {
        const axis = this.props.verticalAxis.clone()
        axis.range = [0, this.bounds.height]
        return axis.size
    }
 
    // Now we can determine the "true" inner bounds of the dual axis
    @computed get innerBounds(): Bounds {
        return this.bounds.pad({
            [this.props.horizontalAxis.orient]: this.horizontalAxisSize,
            [this.props.verticalAxis.orient]: this.verticalAxisSize,
        })
    }
 
    @computed get bounds(): Bounds {
        return this.props.bounds ?? DEFAULT_BOUNDS
    }
}