All files / owid-grapher/grapher/chart ChartDimension.ts

100% Statements 85/85
100% Branches 6/6
100% Functions 5/5
100% Lines 85/85

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 941x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 176x 176x 176x 176x 176x 176x 176x 176x 176x 176x 176x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 176x 176x 176x 176x 176x 176x 176x 1x 1x 24x 24x 1x 1x 176x 176x 176x 176x 176x 176x 176x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                  
// Todo: remove this.
// Any display changes really can be computed columns. And then charts just need xColumnSlug, sizeColumnSlug, yColumnSlug (or yColumnSlugs) et cetera
 
import { observable, computed } from "mobx"
import { trimObject } from "../../clientUtils/Util"
import { OwidTable } from "../../coreTable/OwidTable"
import { OwidVariableDisplayConfig } from "../../clientUtils/OwidVariable"
import {
    ColumnSlug,
    DimensionProperty,
    OwidVariableId,
} from "../../clientUtils/owidTypes"
import { Time } from "../../coreTable/CoreTableConstants"
import {
    Persistable,
    deleteRuntimeAndUnchangedProps,
    updatePersistables,
} from "../../clientUtils/persistable/Persistable"
import { CoreColumn } from "../../coreTable/CoreTableColumns"
import { OwidChartDimensionInterface } from "../../clientUtils/OwidVariableDisplayConfigInterface"
 
// A chart "dimension" represents a binding between a chart
// and a particular variable that it requests as data
class ChartDimensionDefaults implements OwidChartDimensionInterface {
    @observable property!: DimensionProperty
    @observable variableId!: OwidVariableId
 
    // check on: malaria-deaths-comparisons and computing-efficiency
 
    @observable display = new OwidVariableDisplayConfig() // todo: make persistable
 
    // XXX move this somewhere else, it's only used for scatter x override and Marimekko override
    @observable targetYear?: Time = undefined
}
 
// todo: remove when we remove dimensions
export interface LegacyDimensionsManager {
    table: OwidTable
}
 
export class ChartDimension
    extends ChartDimensionDefaults
    implements Persistable
{
    private manager: LegacyDimensionsManager
 
    constructor(
        obj: OwidChartDimensionInterface,
        manager: LegacyDimensionsManager
    ) {
        super()
        this.manager = manager
        if (obj) this.updateFromObject(obj)
    }
 
    @computed private get table(): OwidTable {
        return this.manager.table
    }
 
    updateFromObject(obj: OwidChartDimensionInterface): void {
        if (obj.display) updatePersistables(this, { display: obj.display })
 
        this.targetYear = obj.targetYear
        this.variableId = obj.variableId
        this.property = obj.property
        this.slug = obj.slug
    }
 
    toObject(): OwidChartDimensionInterface {
        return trimObject(
            deleteRuntimeAndUnchangedProps(
                {
                    property: this.property,
                    variableId: this.variableId,
                    display: this.display,
                    targetYear: this.targetYear,
                },
                new ChartDimensionDefaults()
            )
        )
    }
 
    // Do not persist yet, until we migrate off VariableIds
    @observable slug?: ColumnSlug
 
    @computed get column(): CoreColumn {
        return this.table.get(this.columnSlug)
    }
 
    @computed get columnSlug(): string {
        return this.slug ?? this.variableId.toString()
    }
}