Skip to content

Commit

Permalink
🔨 consolidate table for display logic
Browse files Browse the repository at this point in the history
  • Loading branch information
sophiamersmann committed Jan 10, 2025
1 parent 83b0f95 commit b013519
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 40 deletions.
45 changes: 40 additions & 5 deletions packages/@ourworldindata/grapher/src/core/Grapher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ import { isOnTheMap } from "../mapCharts/EntitiesOnTheMap"
import { ChartManager } from "../chart/ChartManager"
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome/index.js"
import { faExclamationTriangle } from "@fortawesome/free-solid-svg-icons"
import { SettingsMenuManager } from "../controls/SettingsMenu"
import { SettingsMenu, SettingsMenuManager } from "../controls/SettingsMenu"
import { TooltipContainer } from "../tooltip/Tooltip"
import {
EntitySelectorModal,
Expand Down Expand Up @@ -800,13 +800,48 @@ export class Grapher
return !this.hideLegend
}

@computed private get showsAllEntitiesInChart(): boolean {
return this.isScatter || this.isMarimekko
}

@computed private get settingsMenu(): SettingsMenu {
return new SettingsMenu({ manager: this, top: 0, bottom: 0, right: 0 })
}

/**
* If the table filter toggle isn't offered, then we default to
* to showing only the selected entities – unless there is a view
* that displays all data points, like a map or a scatter plot.
*/
@computed get forceShowSelectionOnlyInDataTable(): boolean {
return (
!this.settingsMenu.showTableFilterToggle &&
this.hasChartTab &&
!this.showsAllEntitiesInChart &&
!this.hasMapTab
)
}

// table that is used for display in the table tab
@computed get tableForDisplay(): OwidTable {
const table = this.table
let table = this.table

if (!this.isReady || !this.isOnTableTab) return table
return this.chartInstance.transformTableForDisplay
? this.chartInstance.transformTableForDisplay(table)
: table

if (this.chartInstance.transformTableForDisplay) {
table = this.chartInstance.transformTableForDisplay(table)
}

if (
this.forceShowSelectionOnlyInDataTable ||
this.showSelectionOnlyInDataTable
) {
table = table.filterByEntityNames(
this.selection.selectedEntityNames
)
}

return table
}

@computed get tableForSelection(): OwidTable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export const GrapherWithIncompleteData = (
]
return new Grapher({
tab: GRAPHER_TAB_OPTIONS.table,
selectedEntityNames: ["Iceland", "France", "Afghanistan"],
dimensions,
...props,
owidDataset: createOwidTestDataset([{ metadata, data }]),
Expand Down Expand Up @@ -127,6 +128,7 @@ export const GrapherWithAggregates = (
return new Grapher({
tab: GRAPHER_TAB_OPTIONS.table,
dimensions,
selectedEntityNames: ["Afghanistan", "Iceland", "World"],
...props,
owidDataset: createOwidTestDataset([
{ metadata: childMortalityMetadata, data: childMortalityData },
Expand Down
36 changes: 1 addition & 35 deletions packages/@ourworldindata/grapher/src/dataTable/DataTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ import {
IndicatorTitleWithFragments,
joinTitleFragments,
} from "@ourworldindata/utils"
import { makeSelectionArray } from "../chart/ChartUtils"
import { SelectionArray } from "../selection/SelectionArray"
import { DEFAULT_GRAPHER_ENTITY_TYPE } from "../core/GrapherConstants"

Expand Down Expand Up @@ -86,7 +85,6 @@ export interface DataTableManager {
endTime?: Time
startTime?: Time
dataTableSlugs?: ColumnSlug[]
showSelectionOnlyInDataTable?: boolean
entitiesAreCountryLike?: boolean
isSmall?: boolean
isMedium?: boolean
Expand Down Expand Up @@ -138,36 +136,8 @@ export class DataTable extends React.Component<{
}
}

@computed get showSelectionOnlyInDataTable(): boolean {
const {
showSelectionOnlyInDataTable,
canChangeAddOrHighlightEntities,
hasChartTab,
hasMapTab,
} = this.manager
const { selectionArray } = this

// filter the table if the "Show selection only" toggle is hidden
if (
!canChangeAddOrHighlightEntities &&
selectionArray.hasSelection &&
hasChartTab &&
// if we have a map tab, we want to show all entities
!hasMapTab
)
return true

return !!showSelectionOnlyInDataTable
}

@computed get table(): OwidTable {
let table = this.manager.tableForDisplay
if (this.showSelectionOnlyInDataTable) {
table = table.filterByEntityNames(
this.selectionArray.selectedEntityNames
)
}
return table
return this.manager.tableForDisplay
}

@computed get manager(): DataTableManager {
Expand Down Expand Up @@ -693,10 +663,6 @@ export class DataTable extends React.Component<{
)
}

@computed private get selectionArray(): SelectionArray {
return makeSelectionArray(this.manager.selection)
}

@computed private get entityNames(): string[] {
const tableForEntities = this.table
return union(
Expand Down

0 comments on commit b013519

Please sign in to comment.