Skip to content

Commit

Permalink
🔨 (owid table) add time to owidRows
Browse files Browse the repository at this point in the history
  • Loading branch information
sophiamersmann committed Nov 25, 2024
1 parent 1b31ea6 commit e9e2307
Show file tree
Hide file tree
Showing 11 changed files with 380 additions and 19 deletions.
8 changes: 5 additions & 3 deletions packages/@ourworldindata/core-table/src/CoreTableColumns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -536,14 +536,16 @@ export abstract class AbstractCoreColumn<JS_TYPE extends PrimitiveType> {
// assumes table is sorted by time
@imemo get owidRows(): OwidVariableRow<JS_TYPE>[] {
const entities = this.allEntityNames
const times = this.originalTimes
const times = this.allTimes
const values = this.values
const originalTimes = this.originalTimes
const originalValues = this.originalValues
return range(0, times.length).map((index) => {
return range(0, originalTimes.length).map((index) => {
return omitUndefinedValues({
entityName: entities[index],
time: times[index],
value: values[index],
originalTime: originalTimes[index],
originalValue: originalValues[index],
})
})
Expand Down Expand Up @@ -577,7 +579,7 @@ export abstract class AbstractCoreColumn<JS_TYPE extends PrimitiveType> {
valueByEntityNameAndTime.set(row.entityName, new Map())
valueByEntityNameAndTime
.get(row.entityName)!
.set(row.time, row.value)
.set(row.originalTime, row.value)
})
return valueByEntityNameAndTime
}
Expand Down
8 changes: 4 additions & 4 deletions packages/@ourworldindata/core-table/src/OwidTable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ it("can parse data to Javascript data structures", () => {
table.get("Population").owidRows.forEach((row) => {
expect(typeof row.entityName).toBe("string")
expect(row.value).toBeGreaterThan(100)
expect(row.time).toBeGreaterThan(1999)
expect(row.originalTime).toBeGreaterThan(1999)
})
})

Expand Down Expand Up @@ -632,7 +632,7 @@ describe("tolerance", () => {
})
})

it("assigns originalTime as 'time' in owidRows", () => {
it("assigns originalTime as 'originalTime' in owidRows", () => {
const csv = `gdp,year,entityName,entityId,entityCode
1000,2019,USA,,
1001,2020,UK,,`
Expand All @@ -642,7 +642,7 @@ it("assigns originalTime as 'time' in owidRows", () => {
expect.not.arrayContaining([
expect.objectContaining({
entityName: "USA",
time: 2020,
originalTime: 2020,
value: 1000,
}),
])
Expand All @@ -651,7 +651,7 @@ it("assigns originalTime as 'time' in owidRows", () => {
expect.not.arrayContaining([
expect.objectContaining({
entityName: "UK",
time: 2019,
originalTime: 2019,
value: 1001,
}),
])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ export class EntitySelector extends React.Component<{
const rows = column.owidRowsByEntityName.get(entityName) ?? []
searchableEntity[column.slug] = maxBy(
rows,
(row) => row.time
(row) => row.originalTime
)?.value
}

Expand Down
4 changes: 2 additions & 2 deletions packages/@ourworldindata/grapher/src/mapCharts/MapChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -346,12 +346,12 @@ export class MapChart

return mapColumn.owidRows
.map((row) => {
const { entityName, value, time } = row
const { entityName, value, originalTime } = row
const color = this.colorScale.getColor(value) || "red" // todo: color fix
if (!color) return undefined
return {
seriesName: entityName,
time,
time: originalTime,
value,
isSelected: selectionArray.selectedSet.has(entityName),
color,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export class MapSparkline extends React.Component<{
lineStrokeWidth: 2,
entityYearHighlight: {
entityName: this.manager.entityName,
year: this.manager.datum?.time,
year: this.manager.datum?.originalTime,
},
yAxisConfig: {
hideAxis: true,
Expand Down
6 changes: 3 additions & 3 deletions packages/@ourworldindata/grapher/src/mapCharts/MapTooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ export class MapTooltip
: targetTime?.toString()
const displayDatumTime =
timeColumn && datum
? timeColumn.formatValue(datum?.time)
: datum?.time.toString() ?? ""
? timeColumn.formatValue(datum?.originalTime)
: datum?.originalTime.toString() ?? ""
const valueColor: string | undefined = darkenColorForHighContrastText(
lineColorScale?.getColor(datum?.value) ?? "#333"
)
Expand All @@ -143,7 +143,7 @@ export class MapTooltip
const yColumn = this.mapTable.get(this.mapColumnSlug)

const targetNotice =
datum && datum.time !== targetTime ? displayTime : undefined
datum && datum.originalTime !== targetTime ? displayTime : undefined
const toleranceNotice = targetNotice
? {
icon: TooltipFooterIcon.notice,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,8 @@ export class AbstractStackedChart
const pointColor =
row.value > 0 ? POSITIVE_COLOR : NEGATIVE_COLOR
return {
position: row.time,
time: row.time,
position: row.originalTime,
time: row.originalTime,
value: row.value,
valueOffset: 0,
interpolated:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ export class MarimekkoChart
col.def.color ??
colorScheme.getColors(yColumns.length)[i],
points: col.owidRows.map((row) => ({
time: row.time,
time: row.originalTime,
position: row.entityName,
value: row.value,
valueOffset: 0,
Expand Down Expand Up @@ -416,7 +416,7 @@ export class MarimekkoChart
const points: SimplePoint[] = []
for (const row of rows) {
points.push({
time: row.time,
time: row.originalTime,
value: row.value,
entity: row.entityName,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1006,7 +1006,7 @@ export class StackedDiscreteBarChart
col.displayName
),
points: col.owidRows.map((row) => ({
time: row.time,
time: row.originalTime,
position: row.entityName,
value: row.value,
valueOffset: 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,5 +300,6 @@ export interface OwidVariableRow<ValueType extends PrimitiveType> {
entityName: EntityName
time: Time
value: ValueType
originalTime: Time
originalValue?: ValueType
}
Loading

0 comments on commit e9e2307

Please sign in to comment.