From e2ef106ffc68f7b9ae9f6d23d8008e62f7032365 Mon Sep 17 00:00:00 2001 From: Marcel Gerber Date: Thu, 15 Aug 2024 12:12:07 +0100 Subject: [PATCH] enhance: with `MissingDataStrategy.auto`, drop entities with single non-error data point --- .../core-table/src/OwidTable.ts | 14 ++++++++++ .../stackedCharts/StackedDiscreteBarChart.tsx | 27 +++++++++++++------ 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/packages/@ourworldindata/core-table/src/OwidTable.ts b/packages/@ourworldindata/core-table/src/OwidTable.ts index fda142017bc..93e5a050130 100644 --- a/packages/@ourworldindata/core-table/src/OwidTable.ts +++ b/packages/@ourworldindata/core-table/src/OwidTable.ts @@ -277,6 +277,20 @@ export class OwidTable extends CoreTable { ) } + dropRowsWithAtLeastThisManyErrorValuesForColumns( + slugs: ColumnSlug[], + minErrorValues: number + ): this { + return this.rowFilter( + (row) => + slugs.filter((slug) => !isNotErrorValue(row[slug])).length >= + minErrorValues, + `Drop rows with at least ${minErrorValues} ErrorValues in every column: ${slugs.join( + ", " + )}` + ) + } + // Drop _all rows_ for an entity if there is any column that has no valid values for that entity. dropEntitiesThatHaveNoDataInSomeColumn(columnSlugs: ColumnSlug[]): this { const indexesByEntityName = this.rowIndicesByEntityName diff --git a/packages/@ourworldindata/grapher/src/stackedCharts/StackedDiscreteBarChart.tsx b/packages/@ourworldindata/grapher/src/stackedCharts/StackedDiscreteBarChart.tsx index f6fd375ec56..b1acb46152e 100644 --- a/packages/@ourworldindata/grapher/src/stackedCharts/StackedDiscreteBarChart.tsx +++ b/packages/@ourworldindata/grapher/src/stackedCharts/StackedDiscreteBarChart.tsx @@ -125,6 +125,23 @@ export class StackedDiscreteBarChart { base: React.RefObject = React.createRef() + private applyMissingDataStrategy(table: OwidTable): OwidTable { + if (this.missingDataStrategy === MissingDataStrategy.hide) { + // If MissingDataStrategy is explicitly set to hide, drop rows (= times) where one of + // the y columns has no data + return table.dropRowsWithErrorValuesForAnyColumn(this.yColumnSlugs) + } else if (this.missingDataStrategy === MissingDataStrategy.auto) { + // If MissingDataStrategy is set to auto, drop rows where there is only a single non-error value + if (this.yColumnSlugs.length > 1) { + return table.dropRowsWithAtLeastThisManyErrorValuesForColumns( + this.yColumnSlugs, + this.yColumnSlugs.length - 1 + ) + } + } + return table + } + transformTable(table: OwidTable): OwidTable { if (!this.yColumnSlugs.length) return table @@ -144,11 +161,7 @@ export class StackedDiscreteBarChart table = table.interpolateColumnWithTolerance(slug) }) - // If MissingDataStrategy is explicitly set to hide, drop rows (= times) where one of - // the y columns has no data - if (this.missingDataStrategy === MissingDataStrategy.hide) { - table = table.dropRowsWithErrorValuesForAnyColumn(this.yColumnSlugs) - } + table = this.applyMissingDataStrategy(table) if (this.manager.isRelativeMode) { table = table.toPercentageFromEachColumnForEachEntityAndTime( @@ -165,9 +178,7 @@ export class StackedDiscreteBarChart .replaceNegativeCellsWithErrorValues(this.yColumnSlugs) .dropRowsWithErrorValuesForAllColumns(this.yColumnSlugs) - if (this.missingDataStrategy === MissingDataStrategy.hide) { - table = table.dropRowsWithErrorValuesForAnyColumn(this.yColumnSlugs) - } + table = this.applyMissingDataStrategy(table) return table }