模块:PChart

来自异世界百科
落雨枫讨论 | 贡献2022年4月23日 (六) 14:28的版本 (增加Debug提示)

此模块的文档可以在模块:PChart/doc创建

local p = {}

function getArg(args, key, default)
	local val = args[key]
	if (val == nil or #val == 0 or val == "{{{" .. key .. "}}}") then
		return default
	else
		return val
	end
end

function p.filterValue(names, values)
	-- 变换数据
	local newLines = {}
	
	if (names ~= nil and names ~= "") then
		table.insert(newLines, "," .. names)
	end
	
	local oldLines = mw.text.gsplit(values, "\n")
	for line in oldLines do
		if (line ~= "") then
			table.insert(newLines, line)
		end
	end
	return mw.text.listToText(newLines, '\n', '\n')
end

function p.radar(frame)
	local size = getArg(frame.args, "size", "320x300")
	local max = getArg(frame.args, "max", "5")
	local names = getArg(frame.args, "names", "")
	local float = getArg(frame.args, "float")
	local values = getArg(frame.args, "values", "")
	local legend = getArg(frame.args, "legend", "right")
	local isDebug = getArg(frame.args, "debug") == "true"
	
	local html = ""
	if (float ~= nil and float ~= "center") then
		html = html .. "<div class=\"float" .. float .. "\">"
	end
	
	local attributes = {
		size = size,
		ymax = max,
		filled = "",
		striped = "",
		legend = legend,
	}
	
	if (legend == "true" or legend == "1") then
		attributes["legend"] = "right"
	end
	
	-- 变换数据
	local fullValue = p.filterValue(names, values)
	
	if (not isDebug) then
		html = html ..
			frame:extensionTag("pRadar", fullValue, attributes)
	else
		html = html .. "pChart Debug: "
		html = html .. "<pRadar"
		for key, value in pairs(attributes) do      
			html = html .. " " .. key
			if (#value ~= 0) then
				html = html .. "=" .. value
			end
		end
		html = html .. ">\n"
		html = html .. fullValue .. "\n"
		html = html .. "</pRadar>"
	end
	if (float ~= nil and float ~= "center") then
		html = html .. "</div>"
	end
	
	return html
end

return p