Module:Piechart/sandbox: Difference between revisions

Content deleted Content added
changes from Github • Wikiploy
Report unknown $var • Wikiploy
 
(5 intermediate revisions by the same user not shown)
Line 179:
return options
end
 
-- internal for testing legend rendering
p.__priv.legendDebug = false
 
--[[
Line 215 ⟶ 218:
if options.legend then
html = html .. p.renderLegend(data, options)
end
 
if p.__priv.legendDebug then
return html
end
 
Line 368 ⟶ 375:
local bcolor = entry.bcolor
local html = "\n<li>"
if p.__priv.legendDebug then
forPrinting = ""
end
html = html .. '<span class="l-color" style="'..forPrinting..bcolor..'"></span>'
html = html .. '<span class="l-label">'..label..'</span>'
Line 423 ⟶ 433:
if (value < 0.03) then
mw.log('value too small', value, label)
mw.addWarning("pie chart: Value too small ↆ "..value.."% ("..label..")")
return ""
end
Line 583 ⟶ 593:
 
Typical tpl:
"$L: $v" (same as: "$label: $auto")
will result in:
"Abc: 23%" -- when values are percentages
Line 589 ⟶ 599:
Advanced tpl:
"Abc$L: $d ($p)" --e.g. only"Abc: works1234 with(23%)" autoscalefor {"label":"Abc", "value":1234}
"$L: $v" is the same as above when values are autoscaled
Long vs short variable names:
$label ($L)
$auto ($v)
$value ($d)
$percent ($p)
]]
function priv.prepareLabel(tpl, entry)
-- setup default tpl
if not tpl or tpl == "" then
-- simple if no label
Line 598 ⟶ 615:
tpl = "$v"
else
if stringentry.label:find(entry.label, '"%$'[a-z]") then
tpl = entry.label
else
Line 612 ⟶ 629:
local pp = pRaw .. "%%"
local label = "" tpl
-- aliases
label = tpl:gsub("%$L", labelLabel)
label = label
:gsub("%$label", "$L")
:gsub("%$auto", "$v")
:gsub("%$value", "$d")
:gsub("%$percent", "$p")
-- replace variables
label = tpllabel:gsub("%$L", labelLabel)
local d = priv.formatLargeNum(entry.raw and entry.raw or entry.value)
local v = entry.raw and (d .. " (" .. pp .. ")") or pp
Line 620 ⟶ 644:
:gsub("%$d", d)
:gsub("%$v", v)
 
-- Report unknown variables
for var in label:gmatch("%$[a-zA-Z]+") do
-- in preview
mw.addWarning("pie chart: Unknown variable (wrong format of your label): " .. var)
-- tracing links
_ = mw.title.new("Module:Piechart/tracing/unknown-variable").id
end
 
return label
end
Line 797 ⟶ 830:
for _, entry in ipairs(result) do
local label = entry.label
if label and not label:find("%$v[a-z]") then
-- autoscale will be forced, so use $v in labels
if willAutoscale then
entry.label = label .. " $v"
else
entry.label = label .. " (" .. entry.value .. "%$p)"
end
end
Line 818 ⟶ 851:
otherValue = 0
end
local otherEntry = { label = (args["other-label"] or langOther) .. " ("..priv.formatNum(otherValue).."%$p)" }
if args["other-color"] and args["other-color"] ~= "" then
otherEntry.color = args["other-color"]