Module:Bar percent

From Ways of Darkness
Jump to navigation Jump to search

Documentation for this module may be created at Module:Bar percent/doc

require "Module:ColourGen"
require "Module:SortedKeys"
function _BarPercent(name,color,barwidth,width,percentage)
	local to_return = "<tr><td colspan=\"2\" style=\"padding-left: 0.4em; padding-right: 0.4em; min-width: 8em;\">" .. name .. "</td><td style=\"width:"
	
	if barwidth == nil or barwidth == 0 or barwidth == "0" then
		to_return = to_return .. "100px"
	else
		to_return = to_return .. color
	end
	
	to_return = to_return ..  ";border-left: solid 1px silver; border-right: solid 1px silver;\"><div style=\"background-color:"
	if color == nil or color == 0 or color == "0" then
		to_return = to_return .. "gray"
	else
		to_return = to_return .. color
	end
	to_return = to_return .. "; width:"
	if width == nil or width == 0 or width == "0" then
		to_return = to_return .. "0"
	else
		to_return = to_return .. width
	end
	to_return = to_return .. "%; overflow: hidden;\">&nbsp;&thinsp;</div></td><td colspan=\"1\" style=\"padding-left: 0.4em; padding-right: 0.4em; text-align: right;\">"
	if percentage == nil or percentage == 0 or percentage == "0" then
		if width == nil or width == 0 or width == "0" then
			to_return = to_return .. "0%"
		else
			to_return = to_return .. width .. "%"
		end
	else
		to_return = to_return .. percentage
	end
	to_return = to_return .. "</td></tr>"
	return to_return
end

function GenerateBar(tabula,barwidth)
	local to_return = ""
    local colours = Colgen(#tabula)
    local i = 0
    for k,v in pairs(tabula) do
    	i = i + 1
    	to_return = to_return .. _BarPercent(k,colours[i],barwidth,v,0)
	end
    return to_return
    --return colours[i]
end

function ConstructPercentBar(tabula,barwidth)
	local to_return = ""
	local size = 0
	for _ in pairs(tabula) do
		size = size + 1
	end
    --local colours = Colgen(#tabula)
    --table.sort(tabula, function(a,b) return a < b end)
    local colours = Colgen(size)
    local total = 0
    for k,v in pairs(tabula) do
    	total = total + v
	end
	local temp_tbl = { }
    local i = 0
    for k,v in pairs(tabula) do
    	i = i + 1
    	temp_tbl[i] = {k, (math.ceil(v/total*10000))/100 }
	end
    table.sort(temp_tbl, function(a,b) return a[2] > b[2] end)
    for k,v in pairs(temp_tbl) do
    	to_return = to_return .. _BarPercent(v[1],colours[k],barwidth,v[2],0)
	end
    return to_return
    --return colours[i]
end

local p = { }
function p.BarPercent(frame)
	return _BarPercent(frame.args[1],frame.args[2],frame.args[3],frame.args[4],frame.args[5])
end
function p.code_BarPercent(frame)
	local to_return = "<code><nowiki>" .. _BarPercent(frame.args[1],frame.args[2],frame.args[3],frame.args[4],frame.args[5]) .. "</nowiki></code>"
	return to_return
end
function p.GenerateBar(frame)
	--local tbl = { 10, 20, 30, 40, 50, 60 }
	local tbl = { }
	tbl["Area"] = 55
	tbl["Bar"] = 66
	tbl["Cic"] = 88
	return ConstructPercentBar(tbl,frame.args[1])
end
return p