Modulo:Sandbox/M.casanova/Demografia

Da Wikipedia, l'enciclopedia libera.
Vai alla navigazione Vai alla ricerca
require("strict")
local getArgs = require('Module:Arguments').getArgs
local p = {}

-- =====================================================================
-- Legge gli argomenti per caricare un array di valori numerici.
-- Prima tenta di leggere il parametro <list_name> che deve contenere una
-- lista di valori separati da ",". 
-- Se fallisce tenta di leggere i singoli valori 
-- <base_name1>, <base_name2> .. <base_name20>
-- Eventuali valori non numerici sono ignorati
-- =====================================================================
local function read_array(args, base_name, list_name)
	local array = {}
	if args[list_name] then
		array = mw.text.split(string.gsub(args[list_name], "%s", ""), ",")
	end
	-- mi assicuro di tenere solo i primi venti valori
	if #array > 20 then
		for i = 21, #array do
			array[i] = nil
		end
	end 
	-- se l'array ha lunghezza nulla tento di leggere i valori con il vecchio metodo
	if #array == 0 then
		for i= 1,20 do
			array[i] = args[base_name .. tonumber(i)]
		end
	end
	-- converto in numeri
	for i =1,20 do
		array[i] = array[i] and tonumber(array[i])
	end
	return array
end
-- =====================================================================
-- Fonde le due liste years e population in un'unica lista
-- =====================================================================
local function merge_years_population(years, populations)
	local values = { }
	for i =1,#populations do
		values[i] = { year = years[i], population = populations[i], num = i }
	end
	return values
end


-- =====================================================================
-- Generazione del grafico demografico
-- =====================================================================
function p._demografia(args)
	-- Definizione base del grafico
	local graph = {
		version = 2,
		width = 450,
		height = 250,
		padding = "auto",
		data = {
			{
				name = "table",
				transform = {
					{ type = "formula", field = "population", expr = "round(datum.population)" },
					{ type = "sort", by = "year" }
				},
				values = { }
			},
			{
				name = "table2",
				transform = {
					{ type = "formula", field = "population", expr = "round(datum.population)" },
					{ type = "sort", by = "year" }
				},
				values = {  }
			}
		},
		scales = {
			{
				name = "x",
				type = "linear",
				range = "width",
				zero = false,
				domain = {fields = {{data = "table",field = "year"}, {data = "table2",field = "year"}}}
			},
			{
				name = "y",
				type = "linear",
				range = "height",
				nice = true,
				domain = {fields = {{data = "table",field = "population"}, {data = "table2",field = "population"}}}
			}
		},
		axes = {
			{
				type = "x",
				scale = "x",
				format = "d",
				title = "Anno",
				nice = true,
				grid = true,
			},
			{
				type = "y",
				scale = "y",
				title = "Abitanti",
				grid = true,
				layer = "back"
			}
		},
		marks = {
			{
				type = "area",
				from = {data = "table" },
				properties = {
					enter = {
						interpolate = {value = "linear"},
						x = {scale = "x",field = "year"},
						y = {scale = "y",field = "population"},
						y2 = {scale = "y", value = 0 },
						fill = {value = "#369"},
						opacity = {value = 0.5}
					}
				}
			},
			{
				type = "symbol",
				from = {data = "table2"},
				properties = {
					enter = {
						x = {scale = "x",field = "year"},
						y = {scale = "y",field = "population"},
						stroke = {value = "#80B3E6"},
						fill = {value = "#fff"},
						size = {value = 12}
					}
				}
			},
			{
				type = "symbol",
				from = {data = "table"},
				properties = {
					enter = {
						x = {scale = "x",field = "year"},
						y = {scale = "y",field = "population"},
						stroke = {value = "#369"},
						fill = {value = "#fff"},
						size = {value = 30}
					}
				}
			},
			{
				type = "text",
				from = {data = "table"},
				properties = {
					enter = {
						x = {scale = "x",field = "year"},
						y = {scale = "y",field = "population", offset = -8},
						align = {value = "center"},
						fill = {value = "#000"},
						text = {field = "population" }
					}
				}
			},
			{
				type = "text",
				from = {
					data = "table",
					transform = {
						{
						 type = "aggregate",
						 summarize = {year = {"min","max"}}
						}
					}
				},
				properties = {
					enter = {
						x = {signal = "width",mult = 0.5},
						y = {value = -10},
						text = {
						 template = ""
						},
						fill = {value = "black"},
						fontSize = {value = 16},
						align = {value = "center"},
						fontWeight = {value = "bold"}
					}
				}
			}
		}
	}
	local titolo = args.titolo or args[1] or "Abitanti censiti"
	if args.fonte then
		titolo = titolo .. '<ref>' .. args.fonte .. '</ref>'
	end
	titolo = titolo .. '\n'
	local populations = read_array(args, "p", "popolazione")
	local years = read_array(args, "a", "anni")
	local populations2 = read_array(args, "pb", "popolazione2")
	local years2 = read_array(args, "b", "anni2")
	graph['data'][1]['values'] =  merge_years_population(years, populations)
	graph['data'][2]['values'] =  merge_years_population(years2, populations2)
	graph['width'] = args.dimx and tonumber(args.dimx) and tonumber(args.dimx)
	graph['height'] = args.dimy and tonumber(args.dimy) and tonumber(args.dimy)
	if args.etichette then
		if args.etichette == "1" then
			graph['marks'][4]['from']['transform'] = {{ type = "filter", test = "datum.num % 2 == 1"}}
		elseif args.etichette == "2" then 
			graph['marks'][4]['from']['transform'] = {{ type = "filter", test = "datum.num % 2 == 0"}}
		end
	end 
	--if true then return mw.text.jsonEncode(graph) end
	local template_text = "Censimenti dal {{datum.min_year}} al {{datum.max_year}}"
	if #years2 > 0 then 
		graph['marks'][5]['properties']['enter']['text']['template'] = template_text .. " e dati annuali"
	else
		graph['marks'][5]['properties']['enter']['text']['template'] = template_text 
	end
	return titolo .. mw.getCurrentFrame():extensionTag('graph', mw.text.jsonEncode(graph))
end

-- ======================================================================================================
-- Funzione di intefaccia con template:Demografia
-- ======================================================================================================
function p.demografia(frame)
	local args = getArgs(frame)
	return p._demografia(args)
end

return p