Module:Table graph

MyWikiBiz, Author Your Legacy — Thursday December 26, 2024
Jump to navigationJump to search

Template:Module rating This module produces charts from wikitables using Module:Chart and Module:Graph.

Usage

It can be invoked directly or through its associated template:

Template:Tlx

Template:Sandbox other


local p = {}

function p.main(frame)
	local frame = mw.getCurrentFrame()
	local chart = frame:getParent().args[1]:lower():gsub('%s*(.+)%s*','%1')
	local pagename = frame.args[2] or ''
	local table_id = frame.args[3] or ''
	local source = ''
	local data = ''
	local first = frame.args['first']
	local count = 0
	if pagename == '' then
		source = frame:getParent():getTitle():getContent()
	else
		source = mw.title.new(pagename):getContent()
	end
	if table_id ~= '' then source = source:match('{|.-id -="?'..table_id..'"? -.-(\n|%-.+\n)|}') end
	for x, y in source:gmatch('\n|%-.-\n[!|]%s*(.-)%s*[|\n]|%s*([^|\n]+)') do
		count = count + 1
		data = data.."("..y..":"..x..")\n"
		if count == first then return require('Module:Chart')[chart](frame) end
	end
	if frame:getParent().args['debug'] then
		return string.format(' chart: '..chart..'\n pagename: '..pagename..'\n table: '..table_id..'\n data: ' .. data)
	else
		if chart == 'pie chart' then
			frame.args.slices = data
		end
		return require('Module:Chart')[chart](frame)
	end
end

return p