Module:Sandbox/Frietjes/political graph

This is an old revision of this page, as edited by Frietjes (talk | contribs) at 23:53, 26 February 2023. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
-- this module implements [[Template:Political graph]]
local p = {}

function p.chart(frame, flag)
	local chartfun = require('Module:Graph').chart
	local pfetch = require('Module:Political party')._fetch
	local tframe = {}
	tframe.args = {}
	local colors = mw.text.split(frame.args['colors'] or '', '%s*[,]%s*')
	local tracking = ''
	for k,v in pairs(frame.args) do
		if k:find('^y[%d]+Party$') then
			local digit = mw.ustring.match(k, '[%d]+')
			local color = colors[tonumber(digit)] or pfetch({v, 'color'})
			local pname = frame.args['y' .. digit .. 'Title'] or pfetch({v, 'abbrev'})
			if flag then
				tframe.args['y' .. digit .. 'Title'] = pname
			else
				tframe.args[k] = v
			end
			colors[tonumber(digit)] = color
		else
			tframe.args[k] = v
		end
	end
	tframe.args.colors = mw.ustring.gsub(table.concat(colors, ', '), '#', '#')
	return chartfun(tframe)
end

function p.chartWrapper(frame)
	return p.chart(frame, frame:getParent().args['debug'])
end

return p