모듈:국제조사

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={{"다이쇼",1912},{"쇼와",1926},{"헤이세이",1989}}
	for k,v in pairs(source) do
		if k>=start then
			local year=1920+(k-1)*5
			local gengo=gengotable[1]
			for gk,gv in pairs(gengotable) do
				if year>=gv[2] then
					gengo=gv
				end
			end
			table[#table+1]={year=year,population=math.max(v,0),japanyear=gengo[1]..(year-gengo[2]+1).."년"}
		end
	end
	return {
		width=384,
		height=256,
		padding={top=16,left=80,bottom=44,right=32},
		data={{name="table",values=table}},
		signals={
			{
				name="tip",
				init={},
				streams={
					{type="rect:mouseover",expr="datum"},
					{type="rect:mouseout",expr="{}"}
				}
			}
		},
		predicates={
			{
				name="tip",
				type="==",
				operands={{signal="tip._id"},{arg="id"}}
			}
		},
		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 "<div>"
					..frame:extensionTag('graph',mw.text.jsonEncode(result),{mode='interactive'})
					.."<br/>"..args.name..(string.sub(args.code,-3)~="000" and "(에 해당되는 지역)" or "").."의 인구추이</div>"
		else
			return ""
		end
	end
}