Module:Demographics Basics

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 colours = Colgen(#tabule[country]) local to_return = "" local i = 1 for key,value in pairs(tabule[country]) do       if tabule[country][key] ~= nil and tabule[country][key] ~= 0 then to_return = to_return .. _BarPercent(key,colors[i],barwidth,(math.ceil(value/totalled*10000))/100,0) else to_return = to_return .. _BarPercent("Other",colors[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