モジュール:国勢調査

    提供:ManyoWiki
    2023年8月1日 (火) 15:51時点におけるHisato Shima (トーク | 投稿記録)による版 (1版 をインポートしました)
    (差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)

    このモジュールについての説明文ページを モジュール:国勢調査/doc に作成できます

    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},{"令和",2019}}
    	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
    }