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

99.37% Statements 157/158
62.5% Branches 15/24
100% Functions 11/11
99.37% Lines 157/158

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 1201x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 740x 740x 740x 740x 740x 740x 740x 740x 740x 740x 740x 740x 740x 740x 740x 740x 1x 1x 1x 1x 1x 1x 673x 673x 673x 673x 673x 673x 673x 1x 1x 1x 1x 1x 679x 679x 1x 1x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 1x 1x 18x 18x 1x 1x 1x 32x 32x   32x 1x 1x 1x 442x 442x 280x 162x 162x 1x 1x 32x 32x 32x 32x 1x 1x 32x 32x 1x 1x 1x 1x 8x 8x 1x 1x 8x 8x 1x 1x
import {
    BASE_FONT_SIZE,
    FacetAxisDomain,
    ScaleType,
} from "../core/GrapherConstants"
import { extend, trimObject } from "../../clientUtils/Util"
import { observable, computed } from "mobx"
import { HorizontalAxis, VerticalAxis } from "./Axis"
import {
    deleteRuntimeAndUnchangedProps,
    Persistable,
} from "../../clientUtils/persistable/Persistable"
import { AxisConfigInterface } from "./AxisConfigInterface"
import { ScaleSelectorManager } from "../controls/ScaleSelector"
import { Position } from "../../clientUtils/owidTypes"
 
export interface FontSizeManager {
    fontSize: number
}
 
class AxisConfigDefaults implements AxisConfigInterface {
    @observable.ref orient?: Position = undefined
    @observable.ref min?: number = undefined
    @observable.ref max?: number = undefined
    @observable.ref canChangeScaleType?: boolean = undefined
    @observable.ref removePointsOutsideDomain?: boolean = undefined
    @observable.ref minSize?: number = undefined
    @observable.ref hideAxis?: boolean = undefined
    @observable.ref labelPadding?: number = undefined
    @observable.ref nice?: boolean = undefined
    @observable.ref maxTicks?: number = undefined
    @observable.ref compactLabels?: boolean = undefined
    @observable.ref scaleType?: ScaleType = ScaleType.linear
    @observable.ref facetDomain?: FacetAxisDomain = undefined
    @observable.ref label: string = ""
}
 
export class AxisConfig
    extends AxisConfigDefaults
    implements AxisConfigInterface, Persistable, ScaleSelectorManager
{
    constructor(
        props?: AxisConfigInterface,
        fontSizeManager?: FontSizeManager
    ) {
        super()
        this.updateFromObject(props)
        this.fontSizeManager = fontSizeManager
    }
 
    fontSizeManager?: FontSizeManager
 
    // todo: test/refactor
    updateFromObject(props?: AxisConfigInterface): void {
        if (props) extend(this, props)
    }
 
    toObject(): AxisConfigInterface {
        const obj = trimObject({
            orient: this.orient,
            min: this.min,
            max: this.max,
            canChangeScaleType: this.canChangeScaleType,
            removePointsOutsideDomain: this.removePointsOutsideDomain,
            minSize: this.minSize,
            hideAxis: this.hideAxis,
            labelPadding: this.labelPadding,
            nice: this.nice,
            maxTicks: this.maxTicks,
            compactLabels: this.compactLabels,
            scaleType: this.scaleType,
            label: this.label ? this.label : undefined,
            facetDomain: this.facetDomain,
        })
 
        deleteRuntimeAndUnchangedProps(obj, new AxisConfigDefaults())
 
        return obj
    }
 
    @computed get fontSize(): number {
        return this.fontSizeManager?.fontSize || BASE_FONT_SIZE
    }
 
    // A log scale domain cannot have values <= 0, so we double check here
    @computed private get constrainedMin(): number {
        if (this.scaleType === ScaleType.log && (this.min ?? 0) <= 0)
            return Infinity
        return this.min ?? Infinity
    }
 
    // If the author has specified a min/max AND to remove points outside the domain, this should return true
    shouldRemovePoint(value: number): boolean {
        if (!this.removePointsOutsideDomain) return false
        if (this.min !== undefined && value < this.min) return true
        if (this.max !== undefined && value > this.max) return true
        return false
    }
 
    @computed private get constrainedMax(): number {
        if (this.scaleType === ScaleType.log && (this.max || 0) <= 0)
            return -Infinity
        return this.max ?? -Infinity
    }
 
    @computed get domain(): [number, number] {
        return [this.constrainedMin, this.constrainedMax]
    }
 
    // Convert axis configuration to a finalized axis spec by supplying
    // any needed information calculated from the data
    toHorizontalAxis(): HorizontalAxis {
        return new HorizontalAxis(this)
    }
 
    toVerticalAxis(): VerticalAxis {
        return new VerticalAxis(this)
    }
}