Module:ChartSelectRename
Lua
CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules
This module provides a transform for chart renderings that selects specific columns and sets titles in multiple languages.
See mw:Extension:Chart/Transforms for more documentation on this transform feature of the Charts system.
UsageUsage
process: Select columns and set titles for multiple languages.
To use as a chart transform:
"transform": {
"module": "ChartSelectRename",
"function": "process",
"args": {
"cols": "population,année",
"translations": "fr:Population,Année;en:Population,Year;es:Población,Año"
}
}
To invoke as a utility function from another module:
local ChartSelectRename = require( "Module:ChartSelectRename" )
local tab = mw.ext.data.get( "Some dataset.tab" )
-- Note this may mutate the original tab object
tab = ChartSelectRename.process(tab, {
["cols"] = "population,année",
["translations"] = "fr:Population,Année;en:Population,Year"
})
Arguments:
cols: comma-separated list of column names to keep, otherwise returns all columnstranslations: semicolon-separated list of language blocks in the formatlang:Title1,Title2;lang2:TitleA,TitleB(setsf.title[lang]for each column)
Code
-- Module:ChartSelectRename
-- Selects columns and sets column titles in multiple languages.
-- Format: "lang1:title1,title2;lang2:titleA,titleB"
-- See: https://www.mediawiki.org/wiki/Extension:Chart/Transforms
local TabUtils = require("Module:TabUtils")
local p = {}
-- Main function: select columns and apply multilingual titles
-- @param tab Data table (from mw.ext.data.get, etc.)
-- @param args Arguments:
-- • cols : comma-separated column names to keep (optional)
-- • translations : "fr:Pop,Year;en:Population,Year;es:Población,Año"
-- @return Table with selected columns and localized titles
function p.process(tab, args)
-- Step 1: Select columns if 'cols' is specified
if args.cols and args.cols ~= "" then
tab = TabUtils.select(tab, {cols = args.cols})
end
-- Step 2: Apply multilingual titles
if args.translations and args.translations ~= "" then
local langEntries = mw.text.split(args.translations, ";")
local fromCols = mw.text.split(args.cols or "", ",")
-- Skip if no columns to rename
if #fromCols == 0 or (fromCols[1] == "" and #fromCols == 1) then
return tab
end
for _, entry in ipairs(langEntries) do
local lang, titlesStr = entry:match("^%s*([%a%-]+)%s*:(.*)$")
if lang and titlesStr then
local toNames = mw.text.split(titlesStr, ",")
for i, colName in ipairs(fromCols) do
colName = mw.text.trim(colName)
local newTitle = mw.text.trim(toNames[i] or "")
if newTitle ~= "" then
for _, field in ipairs(tab.schema.fields) do
if field.name == colName then
field.title = field.title or {}
field.title[lang] = newTitle
break
end
end
end
end
end
end
end
return tab
end
return p