Skip to content

Commit

Permalink
✨ (grapher) support ordinal columns
Browse files Browse the repository at this point in the history
  • Loading branch information
sophiamersmann committed Mar 28, 2024
1 parent e7b3b9b commit 971b72c
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 3 deletions.
13 changes: 13 additions & 0 deletions packages/@ourworldindata/core-table/src/CoreTableColumns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,18 @@ class BooleanColumn extends AbstractCoreColumn<boolean> {
}
}

class OrdinalColumn extends CategoricalColumn {
@imemo get allowedValuesSorted(): string[] | undefined {
return this.def.sort
}

@imemo get sortedUniqNonEmptyStringVals(): string[] {
return this.allowedValuesSorted
? this.allowedValuesSorted
: super.sortedUniqNonEmptyStringVals
}
}

abstract class AbstractColumnWithNumberFormatting<
T extends PrimitiveType,
> extends AbstractCoreColumn<T> {
Expand Down Expand Up @@ -882,6 +894,7 @@ export const ColumnTypeMap = {
String: StringColumn,
SeriesAnnotation: SeriesAnnotationColumn,
Categorical: CategoricalColumn,
Ordinal: OrdinalColumn,
Region: RegionColumn,
Continent: ContinentColumn,
NumberOrString: NumberOrStringColumn,
Expand Down
51 changes: 48 additions & 3 deletions packages/@ourworldindata/grapher/src/core/LegacyToOwidTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
OwidTableSlugs,
OwidColumnDef,
LegacyGrapherInterface,
OwidVariableDimensions,
} from "@ourworldindata/types"
import {
OwidTable,
Expand All @@ -33,6 +34,7 @@ import {
ColumnSlug,
EPOCH_DATE,
OwidChartDimensionInterface,
OwidVariableType,
} from "@ourworldindata/utils"

export const legacyToOwidTableAndDimensions = (
Expand Down Expand Up @@ -545,6 +547,37 @@ const fullJoinTables = (
)
}

const variableTypeToColumnType = (type: OwidVariableType): ColumnTypeNames => {
switch (type) {
case "ordinal":
return ColumnTypeNames.Ordinal
// TODO
// case "string":
// return ColumnTypeNames.String
// case "float":
// case "int":
// return ColumnTypeNames.Numeric
// case "mixed":
default:
return ColumnTypeNames.NumberOrString
}
}

const getSortFromDimensions = (
dimensions: OwidVariableDimensions
): string[] | undefined => {
const values = dimensions.values?.values
if (!values) return

const sort = values
.map((value) => value.name)
.filter((name): name is string => name !== undefined)

if (sort.length === 0) return

return sort
}

const columnDefFromOwidVariable = (
variable: OwidVariableWithSourceAndDimension
): OwidColumnDef => {
Expand Down Expand Up @@ -574,6 +607,19 @@ const columnDefFromOwidVariable = (
const isContinent = variable.id === 123
const name = isContinent ? "Continent" : variable.name

// The column's type
const type = isContinent
? ColumnTypeNames.Continent
: variable.type
? variableTypeToColumnType(variable.type)
: ColumnTypeNames.NumberOrString

// Sorted values for ordinal columns
const sort =
type === ColumnTypeNames.Ordinal
? getSortFromDimensions(variable.dimensions)
: undefined

return {
name,
slug,
Expand Down Expand Up @@ -604,9 +650,8 @@ const columnDefFromOwidVariable = (
owidVariableId: variable.id,
owidProcessingLevel: variable.processingLevel,
owidSchemaVersion: variable.schemaVersion,
type: isContinent
? ColumnTypeNames.Continent
: ColumnTypeNames.NumberOrString,
type,
sort,
}
}

Expand Down
4 changes: 4 additions & 0 deletions packages/@ourworldindata/types/src/OwidVariable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface OwidVariableWithSource {
updatePeriodDays?: number
datasetVersion?: string
licenses?: OwidLicense[]
type?: OwidVariableType

// omitted:
// code
Expand Down Expand Up @@ -107,6 +108,7 @@ export interface OwidVariableDimension {
export interface OwidVariableDimensions {
years: OwidVariableDimension
entities: OwidVariableDimension
values?: OwidVariableDimension
}

export type OwidVariableDataMetadataDimensions = {
Expand All @@ -129,3 +131,5 @@ export type OwidVariableDimensionValueFull =
export interface OwidEntityKey {
[id: string]: OwidVariableDimensionValuePartial
}

export type OwidVariableType = "string" | "float" | "int" | "mixed" | "ordinal"
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ export enum ColumnTypeNames {
Region = "Region",
SeriesAnnotation = "SeriesAnnotation",
Categorical = "Categorical",
Ordinal = "Ordinal",
Continent = "Continent",
EntityName = "EntityName",
EntityId = "EntityId",
Expand Down Expand Up @@ -207,6 +208,9 @@ export interface CoreColumnDef extends ColumnColorScale {
descriptionFromProducer?: string
note?: string // Any internal notes the author wants to record for display in admin interfaces

// Sorted values (in case of ordinal data)
sort?: string[]

// Color
color?: Color // A column can have a fixed color for use in charts where the columns are series

Expand Down
1 change: 1 addition & 0 deletions packages/@ourworldindata/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ export {
type OwidProcessingLevel,
type IndicatorTitleWithFragments,
joinTitleFragments,
type OwidVariableType,
} from "./OwidVariable.js"

export type { OwidSource } from "./OwidSource.js"
Expand Down

0 comments on commit 971b72c

Please sign in to comment.