Skip to content

Commit

Permalink
feat(scatter): sort legend items using the new metadata field "sort"
Browse files Browse the repository at this point in the history
  • Loading branch information
sophiamersmann committed Mar 27, 2023
1 parent d9f1b3b commit 41b0834
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 3 deletions.
3 changes: 3 additions & 0 deletions packages/@ourworldindata/core-table/src/CoreColumnDef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ export interface CoreColumnDef extends ColumnColorScale {
description?: 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
4 changes: 4 additions & 0 deletions packages/@ourworldindata/core-table/src/CoreTableColumns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,10 @@ export abstract class AbstractCoreColumn<JS_TYPE extends PrimitiveType> {
return new Set(this.uniqValues)
}

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

/**
* Returns all values including ErrorValues..
* Normally you want just the valid values, like `[45000, 50000, ...]`. But sometimes you
Expand Down
8 changes: 7 additions & 1 deletion packages/@ourworldindata/grapher/src/color/ColorScale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,13 @@ export class ColorScale {
}

@computed private get categoricalValues(): any[] {
return this.colorScaleColumn?.sortedUniqNonEmptyStringVals ?? []
const { sortedUniqNonEmptyStringVals, allowedValuesSorted } =
this.colorScaleColumn ?? {}

if (allowedValuesSorted && allowedValuesSorted.length > 0)
return allowedValuesSorted

return sortedUniqNonEmptyStringVals ?? []
}

@computed private get colorScheme(): ColorScheme {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,7 @@ const columnDefFromOwidVariable = (
source,
display,
nonRedistributable,
sort,
} = variable

// Without this the much used var 123 appears as "Countries Continent". We could rename in Grapher but not sure the effects of that.
Expand Down Expand Up @@ -581,6 +582,7 @@ const columnDefFromOwidVariable = (
type: isContinent
? ColumnTypeNames.Continent
: ColumnTypeNames.NumberOrString,
sort,
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/@ourworldindata/utils/src/OwidVariable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export interface OwidVariableTypeFields {
sort?: string[]
}

export interface OwidVariableWithSource {
export type OwidVariableWithSource = {
id: number
name?: string
description?: string
Expand All @@ -73,7 +73,7 @@ export interface OwidVariableWithSource {
coverage?: string
nonRedistributable?: boolean
source?: OwidSource
}
} & OwidVariableTypeFields

export type OwidVariableWithSourceAndDimension = OwidVariableWithSource & {
dimensions: OwidVariableDimensions
Expand Down

0 comments on commit 41b0834

Please sign in to comment.