Module:CountryFilter/doc
This module is a Chart extension transform that filters and pivots a tabular dataset by one or more ISO 3166-1 alpha-3 country codes, resolving country names from Wikidata in any language.
It is designed for datasets that follow the World Bank convention: one row per country and year, with a column containing ISO 3166-1 alpha-3 country codes. A single shared .tab file can serve charts for any country or group of countries without duplication.
Dependencies
- Data:ISO 3166-1 alpha-3.tab — lookup table mapping ISO 3166-1 alpha-3 codes to Wikidata QIDs
- ISO 3166-1 alpha-3 code (P298) — ISO 3166-1 alpha-3 code property used to identify countries on Wikidata
Parameters
All parameters are passed via args in the .chart file, and can be overridden at embed time using arg: prefixes in the {{#chart}} call.
| Parameter | Required | Default | Description |
|---|---|---|---|
value | — | One or more ISO 3166-1 alpha-3 codes, comma-separated (e.g. ABW or CAN,FRA,DEU) | |
lang | en | BCP 47 language code for country name resolution (e.g. eu, es, fr) | |
col | CountryCode | Name of the column in the .tab file that contains the country codes |
Behaviour
Given a .tab file with the structure:
| CountryCode | Year | Value |
|---|---|---|
| ABW | 1960 | 64.049 |
| ABW | 1961 | 64.215 |
| ESP | 1960 | 69.150 |
| … | … | … |
The module:
- Parses
valueinto a list of country codes. - Looks up the Wikidata QID for each code via Data:ISO 3166-1 alpha-3.tab.
- Resolves each QID to a country name in
lang, falling back to English, then to the raw code. - Pivots the data: rows become years, columns become one per country.
- Returns the transformed table to the Chart renderer.
For a single country (value=ABW), the output is:
| Year | Aruba |
|---|---|
| 1960 | 64.049 |
| 1961 | 64.215 |
For multiple countries (value=ABW,ESP), the output is:
| Year | Aruba | Spain |
|---|---|---|
| 1960 | 64.049 | 69.150 |
| 1961 | 64.215 | 69.340 |
Years where a country has no data appear as null, which the Chart extension renders as a gap in the line.
Usage
.chart file
Define the chart on Commons pointing to your shared .tab file. The default value and lang in args can be overridden at embed time.
{
"license": "CC0-1.0",
"version": 1,
"source": "API SP.DYN.LE00.IN DS2 ETL01.tab",
"type": "line",
"transform": {
"module": "CountryFilter",
"function": "filter",
"args": {
"col": "CountryCode",
"value": "ABW",
"lang": "en"
}
},
"title": { "en": "Life expectancy at birth" },
"xAxis": { "title": { "en": "Year" }, "format": "none" },
"yAxis": { "title": { "en": "Years" } }
}
Direct embed (single country)
{{#chart:Life expectancy.chart|arg:value=ABW|arg:lang=eu}}
Direct embed (multiple countries)
{{#chart:Life expectancy.chart|arg:value=CAN,FRA,DEU,ITA,JPN,GBR,USA|arg:lang=eu}}
Via a wrapper template (recommended)
To avoid repeating arg:lang on every call, create a thin wrapper template on each Wikipedia. For example, on Basque Wikipedia as Txantiloi:Herrialde grafikoa:
{{#chart:{{{1}}}|arg:value={{{2}}}|arg:lang=eu}}
Then in any article:
{{Herrialde grafikoa|Life expectancy.chart|ABW}}
{{Herrialde grafikoa|Life expectancy.chart|CAN,FRA,DEU,ITA,JPN,GBR,USA}}
Language resolution
Because transforms run in the context of Wikimedia Commons rather than the embedding Wikipedia, the language must be passed explicitly via arg:lang. Country names are resolved in this order:
mw.wikibase.getLabelByLang(qid, lang)— label in the requested languagemw.wikibase.getLabel(qid)— label in the wiki's default language- The raw ISO 3166-1 alpha-3 code (e.g.
ABW) — last resort fallback
The wrapper template pattern above is the recommended way to handle this, as it hardcodes the correct language code for each Wikipedia.
Extending to other datasets
The module is not specific to any particular dataset. It works with any .tab file that has:
- A column of ISO 3166-1 alpha-3 country codes (default name:
CountryCode, overridable viacol) - A column named
Year - A column named
Value
Known limitations
- Non-ISO codes: World Bank aggregate codes (e.g.
AFE,WLD) are not in Data:ISO 3166-1 alpha-3.tab and will display as the raw code. - Column names: the value and year columns are expected to be named
ValueandYear. Datasets with different column names require a small modification to the module. - Missing Wikidata labels: if a country has no label in the requested language on Wikidata, English is used as fallback.
See also
- mw:Extension:Chart — the chart extension this module is designed for
- Data:ISO 3166-1 alpha-3.tab — the country code lookup table used by this module