Module:CountryFilter/doc

Category:Module documentation#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

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.

ParameterRequiredDefaultDescription
value One or more ISO 3166-1 alpha-3 codes, comma-separated (e.g. ABW or CAN,FRA,DEU)
lang enBCP 47 language code for country name resolution (e.g. eu, es, fr)
col CountryCodeName of the column in the .tab file that contains the country codes

Behaviour

Given a .tab file with the structure:

CountryCodeYearValue
ABW196064.049
ABW196164.215
ESP196069.150

The module:

  1. Parses value into a list of country codes.
  2. Looks up the Wikidata QID for each code via Data:ISO 3166-1 alpha-3.tab.
  3. Resolves each QID to a country name in lang, falling back to English, then to the raw code.
  4. Pivots the data: rows become years, columns become one per country.
  5. Returns the transformed table to the Chart renderer.

For a single country (value=ABW), the output is:

YearAruba
196064.049
196164.215

For multiple countries (value=ABW,ESP), the output is:

YearArubaSpain
196064.04969.150
196164.21569.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:

  1. mw.wikibase.getLabelByLang(qid, lang) — label in the requested language
  2. mw.wikibase.getLabel(qid) — label in the wiki's default language
  3. 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 via col)
  • 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 Value and Year. 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

Category:Chart transform modules Category:Lua modules
Category:Chart transform modules Category:Lua modules Category:Module documentation