Difference between revisions of "Module:Demographics Basics"

From Ways of Darkness
Jump to navigation Jump to search
Line 62: Line 62:
 
function generate_bar(tabule,country,barwidth)
 
function generate_bar(tabule,country,barwidth)
 
     local totalled=total_table(tabule,country)
 
     local totalled=total_table(tabule,country)
 +
    local tab = tabule[country]
 
     local size = 0
 
     local size = 0
 
     --for _ in pairs(tabule[country]) do
 
     --for _ in pairs(tabule[country]) do
Line 67: Line 68:
 
--end
 
--end
 
     --local colours = Colgen(size)
 
     --local colours = Colgen(size)
     local colours = Colgen(#tabule[country])
+
     local colours = Colgen(#tab)
 
     local to_return = ""
 
     local to_return = ""
 
     local i = 1
 
     local i = 1
     for key,value in pairs(tabule[country]) do
+
     for key,value in pairs(tab) do
         if tabule[country][key] ~= nil and tabule[country][key] ~= 0 and key ~= "Other" then
+
         if tab[key] ~= nil and tab[key] ~= 0 and key ~= "Other" then
 
     to_return = to_return .. _BarPercent("[[" .. key .. "]]",colours[i],barwidth,(math.ceil(value/totalled*10000))/100,0)
 
     to_return = to_return .. _BarPercent("[[" .. key .. "]]",colours[i],barwidth,(math.ceil(value/totalled*10000))/100,0)
 
else
 
else

Revision as of 14:38, 21 January 2017

Documentation for this module may be created at Module:Demographics Basics/doc

require "Module:FormatNum"
require "Module:OrderedPairs"
require "Module:SortedKeys"
require "Module:Compare"
require "Module:ColourGen"
require "Module:Bar percent"

function total_table(table,country)
 local temp = 0
  if table == nil then
        return 0
  elseif table[country] == nil then
        return 0
    elseif table[country] == 0 then
        return 0
    else for i in pairs(table[country]) do
 temp=temp+table[country][i] 
 end
 return temp
end
end

function fetch_table(table,country,race)
    if table == nil then
        return 0
    elseif table[country] == nil then
        return 0
    elseif table[country] == 0 then
        return 0
    end
    if table[country][race] == nil then
        return 0
    elseif table[country][race] == 0 then
        return 0
    else
    return table[country][race]
end end

function compare(a,b)
  return a < b
end

function generate(tabule,country,type)
    local totalled=total_table(tabule,country)
    local Base= "{| class=\"sortable wikitable\" style=\"background:white;\"\n! style=\"text-align: center; font-weight: bold;\" | " .. type .."\n! style=\"text-align: center; font-weight: bold;\" | Amount\n! style=\"text-align: center; font-weight: bold;\" | Percentage\n"
    table.sort(tabule,compare)
    for key,value in pairs(tabule[country]) do
        if tabule[country][key] ~= nil and tabule[country][key] ~= 0 then
                if key ~= "O" and key ~= "Other" then
        Base = Base .. "|-\n| style=\"text-align: center;\" | [[" .. key .. "]]\n"
                else
        Base = Base .. "|-\n| style=\"text-align: center;\" | " .. "Other" .. "\n"
                        end
        Base = Base .. "| style=\"text-align: center;\" | " .. format_num(value,0) .. "\n"
        Base = Base .. "| style=\"text-align: center;\" | " .. (math.ceil(value/totalled*10000))/100 .. "%\n"
        end
    end
    Base = Base .. "|}"
    return Base
end

function generate_bar(tabule,country,barwidth)
    local totalled=total_table(tabule,country)
    local tab = tabule[country]
    local size = 0
    --for _ in pairs(tabule[country]) do
    --	size = size + 1
	--end
    --local colours = Colgen(size)
    local colours = Colgen(#tab)
    local to_return = ""
    local i = 1
    for key,value in pairs(tab) do
        if tab[key] ~= nil and tab[key] ~= 0 and key ~= "Other" then
    	to_return = to_return .. _BarPercent("[[" .. key .. "]]",colours[i],barwidth,(math.ceil(value/totalled*10000))/100,0)
		else
    	to_return = to_return .. _BarPercent("Other",colours[i],barwidth,(math.ceil(value/totalled*10000))/100,0)
		end
		i = i + 1
    end
    return to_return
end

function generate_historical(tabule,country,type)
	local Origin = {}
	local i = 1
	for key,value in pairs(tabule[country]) do
		Origin[i] = tabule[country][key]
		Origin[i]["OrgKey"] = tonumber(key)
		i = i + 1
	end
	table.sort(Origin, function(a,b) return a["OrgKey"]<b["OrgKey"] end)
	local Base= "{| class=\"sortable wikitable\"\n! style=\"text-align: center; font-weight: bold;\" | Year\n! style=\"text-align: center; font-weight: bold;\" | Pop\n! style=\"text-align: center; font-weight: bold;\" | ±%\n! style=\"text-align: center; font-weight: bold;\" | Notes\n|-\n"
	for key,value in pairs(Origin) do
		if Origin[key] ~= nil and Origin[key] ~= 0 then
	Base = Base .. "|-\n"
	Base = Base .. "| style=\"text-align: center; font-weight: bold;\" |" .. Origin[key]["OrgKey"] .. "\n"
	Base = Base .. "| style=\"text-align: center;\" |" .. format_num(Origin[key][1](),0) .. "\n"
	if key == 1 then
		Base = Base .. "| style=\"text-align: center;\" |" .. "-" .. "\n"
	else
		Base = Base .. "| style=\"text-align: center;\" |" .. Compare(Origin[key][1](),Origin[key-1][1](),true) .. "\n"
	end
	if Origin[key][2] ~= nil and Origin[key][2] ~= 0 then
		Base = Base .. "| style=\"text-align: center;\" |" .. Origin[key][2] .. "\n"
	else
		Base = Base .. "| style=\"text-align: center;\" | \n"
		end
	end
	end
	Base = Base .. "|}"
	return Base
end