モジュール:国勢調査

local function createGraph(code)
	local source=nil
	for k,v in pairs(mw.loadData("Module:国勢調査/data")math.floor(code/1000)+1) do
		if v"code"==code then
			source=v"value"
		end
	end
	if source==nil then
		return nil
	end
	local start
	for k,v in pairs(source) do
		if v>=0 then
			start=k
			break
		end
	end
	local table={}
	local gengotable=
	for k,v in pairs(source) do
		if k>=start then
			local year=1920+(k-1)*5
			local gengo=gengotable1
			for gk,gv in pairs(gengotable) do
				if year>=gv2 then
					gengo=gv
				end
			end
			table#table+1={year=year,population=math.max(v,0),japanyear=gengo1..(year-gengo2+1).."年"}
		end
	end
	return {
		width=384,
		height=256,
		padding={top=16,left=80,bottom=44,right=32},
		data=,
		signals={
			{
				name="tip",
				init={},
				streams={
					{type="rect:mouseover",expr="datum"},
					{type="rect:mouseout",expr="{}"}
				}
			}
		},
		predicates={
			{
				name="tip",
				type="==",
				operands=
			}
		},
		scales={
			{
				name="xscale",
				range="width",
				zero=false,
				domain={1911+start*5,2019}
			},
			{
				name="yscale",
				range="height",
				nice=true,
				domain={data="table",field="population"}
			}
		},
		axes={
			{type="x",scale="xscale",format="f"},
			{type="y",scale="yscale",grid=true}
		},
		marks={
			{
				type="rect",
				properties={
					enter={
						x={scale="xscale",value=math.max(1926,1911+start*5)},
						x2={scale="xscale",value=1989},
						y={value=0},
						y2={scale="yscale",value=0},
						fill={value="blue"},
						fillOpacity={value="0.05"}
					},
				}
			},
			{
				type="rect",
				from={data="table"},
				properties={
					enter={
						xc={scale="xscale",field="year"},
						width={value=12},
						y={scale="yscale",field="population"},
						y2={scale="yscale",value=0}
					},
					update={
						fill={value="lightblue"}
					},
					hover={
						fill={value="deeppink"}
					}
				}
			},
			{
				type="text",
				properties={
					enter={
						align={value="center"},
						fill={value="black"},
						fontSize={value=16}
					},
					update={
						x={scale="xscale",signal="tip.year"},
						y={scale="yscale",signal="tip.population",mult=0.75},
						text={signal="tip.population"},
						fillOpacity={
							rule={
								{
									predicate={
										name="tip",
										id={value="null"}
									},
									value=0
								},
								{value=1}
							}
						}
					}
				}
			},
			{
				type="text",
				properties={
					enter={
						align={value="center"},
						fill={value="black"},
						fontSize={value=16}
					},
					update={
						x={scale="xscale",signal="tip.year"},
						y={scale="yscale",value=0,offset=40},
						text={signal="tip.japanyear"},
						fillOpacity={
							rule={
								{
									predicate={
										name="tip",
										id={value="null"}
									},
									value=0
								},
								{value=1}
							}
						}
					}
				}
			}
		}
	}
end
return {
	main=function(frame)
		local args=frame:getParent().args
		local result=createGraph(tonumber(args.code))
		if result~=nil then
			return ""
					..frame:extensionTag('graph',mw.text.jsonEncode(result),{mode='interactive'})
					..""..args.name..(string.sub(args.code,-3)~="000" and "（に相当する地域）" or "").."の人口の推移"
		else
			return ""
		end
	end
}