Skip to content

Commit

Permalink
🔨 fix some issues
Browse files Browse the repository at this point in the history
  • Loading branch information
danyx23 committed Dec 23, 2024
1 parent 6c220d3 commit a4d3539
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
10 changes: 8 additions & 2 deletions packages/@ourworldindata/grapher/src/core/Grapher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,11 @@ import {
DefaultChartClass,
} from "../chart/ChartTypeMap"
import { Entity, SelectionArray } from "../selection/SelectionArray"
import { legacyToOwidTableAndDimensions } from "./LegacyToOwidTable"
import {
addSelectedEntityColorsToTable,
computeActualDimensions,
legacyToOwidTableAndDimensions,
} from "./LegacyToOwidTable"
import { ScatterPlotManager } from "../scatterCharts/ScatterPlotChartConstants"
import {
autoDetectSeriesStrategy,
Expand Down Expand Up @@ -1155,7 +1159,9 @@ export class Grapher
legacyConfig.selectedEntityColors
)
: table
const dimensions = legacyConfig.dimensions ?? []
const dimensions = legacyConfig.dimensions
? computeActualDimensions(legacyConfig.dimensions)
: []
this.createPerformanceMeasurement(
"legacyToOwidTableAndDimensions",
startMark
Expand Down
24 changes: 15 additions & 9 deletions packages/@ourworldindata/grapher/src/core/LegacyToOwidTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,7 @@ export const legacyToOwidTableAndDimensions = (

// We need to create a column for each unique [variable, targetTime] pair. So there can be
// multiple columns for a single variable.
const newDimensions = dimensions.map((dimension) => ({
...dimension,
slug: dimension.targetYear
? `${dimension.variableId}-${dimension.targetYear}`
: `${dimension.variableId}`,
}))
const newDimensions = computeActualDimensions(dimensions)
const dimensionColumns = uniqBy(newDimensions, (dim) => dim.slug)

const variableTablesToJoinByYear: OwidTable[] = []
Expand Down Expand Up @@ -333,16 +328,16 @@ export const legacyToOwidTableAndDimensions = (

export const addSelectedEntityColorsToTable = (
table: OwidTable,
selectedEntityColors: { [entityName: string]: string }
selectedEntityColors: { [entityName: string]: string | undefined }
): OwidTable => {
const entityColorColumnSlug = OwidTableSlugs.entityColor

const valueFn = (entityId: EntityId | undefined) => {
// see comment above about entityMetaById[id]
if (!entityId) return ErrorValueTypes.UndefinedButShouldBeString
const entityName = table.entityIdToNameMap.get(entityId)
return entityName && selectedEntityColors
? selectedEntityColors[entityName]
? (selectedEntityColors[entityName] ??
ErrorValueTypes.UndefinedButShouldBeString)
: ErrorValueTypes.UndefinedButShouldBeString
}

Expand Down Expand Up @@ -743,6 +738,17 @@ const annotationsToMap = (annotations: string): Map<string, string> => {
return entityAnnotationsMap
}

export function computeActualDimensions(
dimensions: OwidChartDimensionInterface[]
): OwidChartDimensionInterface[] {
return dimensions.map((dimension) => ({
...dimension,
slug: dimension.targetYear
? `${dimension.variableId}-${dimension.targetYear}`
: `${dimension.variableId}`,
}))
}

/**
* Loads a single variable into an OwidTable.
*/
Expand Down

0 comments on commit a4d3539

Please sign in to comment.