-
-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🎉 (entity selector) sort by external indicators (#3466)
[Cycle 2024.2: Entity selector](#3349) | [Designs](https://www.figma.com/file/X5mOEX8zULS6qyHocUYdmh/Grapher-UI?type=design&node-id=2523%3A6266&mode=design&t=7edFp79OOjz6RENz-1) ## Summary Offers to sort by "Population" and "GDP per capita", even if the chart doesn't include population or GDP per capita indicators. ## Details - If the chart has a Population or GDP per capita indicator, then we re-use that data - If we need to download additional indicators, then we do that on demand, i.e. selecting "Population" or "GDP per capita" triggers their download - Indicator IDs for population and GDP per capita are hard-coded (but the data team might come up with a [better solution](owid/etl#2508)) - "Population" and "GDP per capita" are only offered for selection if entities are detected to include countries or regions - This is done by checking whether any of the available entities are listed in the [regions.json](https://github.com/owid/owid-grapher/blob/master/packages/%40ourworldindata/utils/src/regions.json) file - Testing the available entities against the `regions.json` file is not perfect since the default population and GDP per capita indicators that we are using have data for a few entities that are not listed in `regions.json` (see details below) - However, we only need a single matching entity to trigger sorting by population or GDP per capita, so in practice this works well - If we wanted to be more correct here, we could also download population and GDP per capita metadata when the entity selector is opened and then check the actual population/GDP per capita entities against the entities that are available for the chart <details><summary>Entities of the population or GDP per capita indicator that are not included in the `regions.json` file</summary> - For the population indicator: - Africa (UN) - Asia (UN) - Europe (UN) - High-income countries - Latin America and the Caribbean (UN) - Low-income countries - Lower-middle-income countries - Northern America (UN) - Oceania (UN) - Upper-middle-income countries - For the GDP per capita indicator: - East Asia and Pacific (WB) - Europe and Central Asia (WB) - High-income countries - Latin America and Caribbean (WB) - Low-income countries - Lower-middle-income countries - Middle East and North Africa (WB) - Middle-income countries - North America (WB) - South Asia (WB) - Sub-Saharan Africa (WB) - Upper-middle-income countries </details> ## SVG tester The SVG tester fails due to the changes in #3373
- Loading branch information
1 parent
a6e32bf
commit e186b8e
Showing
8 changed files
with
372 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { OwidVariableDataMetadataDimensions } from "@ourworldindata/types" | ||
|
||
export const getVariableDataRoute = ( | ||
dataApiUrl: string, | ||
variableId: number | ||
): string => { | ||
if (dataApiUrl.includes("v1/indicators/")) { | ||
// fetching from Data API, e.g. https://api.ourworldindata.org/v1/indicators/123.data.json | ||
return `${dataApiUrl}${variableId}.data.json` | ||
} else { | ||
throw new Error(`dataApiUrl format not supported: ${dataApiUrl}`) | ||
} | ||
} | ||
|
||
export const getVariableMetadataRoute = ( | ||
dataApiUrl: string, | ||
variableId: number | ||
): string => { | ||
if (dataApiUrl.includes("v1/indicators/")) { | ||
// fetching from Data API, e.g. https://api.ourworldindata.org/v1/indicators/123.metadata.json | ||
return `${dataApiUrl}${variableId}.metadata.json` | ||
} else { | ||
throw new Error(`dataApiUrl format not supported: ${dataApiUrl}`) | ||
} | ||
} | ||
|
||
export async function loadVariableDataAndMetadata( | ||
variableId: number, | ||
dataApiUrl: string | ||
): Promise<OwidVariableDataMetadataDimensions> { | ||
const dataPromise = fetch(getVariableDataRoute(dataApiUrl, variableId)) | ||
const metadataPromise = fetch( | ||
getVariableMetadataRoute(dataApiUrl, variableId) | ||
) | ||
const [dataResponse, metadataResponse] = await Promise.all([ | ||
dataPromise, | ||
metadataPromise, | ||
]) | ||
if (!dataResponse.ok) throw new Error(dataResponse.statusText) | ||
if (!metadataResponse.ok) throw new Error(metadataResponse.statusText) | ||
const data = await dataResponse.json() | ||
const metadata = await metadataResponse.json() | ||
return { data, metadata } | ||
} |
Oops, something went wrong.