モジュール:国勢調査/都道府県内自治体
表示
local function createGraph(code)
local width=768
local height=512
local max=0
local first=8
if code=="00000" then
first=0
elseif code=="19000" then
first=18
end
local ptable={}
for _,v in pairs(mw.loadData("Module:国勢調査/"..string.sub(code,1,2))) do
if v.code~="00000" and string.sub(v.name,-3)~="局" then
for y=1,20 do
local p=v.value[y]
if p>max then
max=p
end
end
local hue=0
local tail=string.sub(v.name,-3)
if string.sub(v.code,3,3)=="1" then
hue=270
elseif tail=="町" then
hue=90
elseif tail=="村" then
hue=180
end
ptable[#ptable+1]={
code=v.code,
name=v.name,
value=v.value,
hue=hue
}
end
end
local marks={}
return {
width=width,
height=height,
padding=16,
signals={
{
name="yr",
init=19,
streams={
{
type="mousemove",
expr="min(max(floor(eventY()/"..(height/(20-first)).."+"..first.."),"..first.."),19)"
}
}
},
{
name="yrtext",
init={
"2015年",
"(平成27年)"
},
streams={
{
type="mousemove",
expr="[(1920+yr*5)+'年','('+(1920+yr*5<1926?'大正'+(1920+yr*5-1911):1920+yr*5<1989?'昭和'+(1920+yr*5-1925):'平成'+(1920+yr*5-1988))+'年)']"
}
}
},
{
name="count",
init=#ptable
},
{
name="rankrange",
init=1,
streams={
{
type="click",
expr="count<=50?1:rankrange==1?count-49:1"
}
}
}
},
data={
{
name="table",
values=ptable,
transform={
{
type="formula",
field="last",
expr="datum.value[19]"
},
{
type="sort",
by={
"-last",
"code"
}
},
{
type="rank"
},
{
type="filter",
test="rankrange<=datum.rank && datum.rank<=49+rankrange"
},
{
type="formula",
field="active",
expr="datum.value[yr]"
}
}
}
},
scales={
{
name="xscale",
type="ordinal",
points=true,
padding=0.75,
domain={
data="table",
field="rank"
},
range={
48,
width
}
},
{
name="yscale",
domain={
0,
max
},
nice=true,
range={
height-72,
0
}
}
},
axes={
{
type="y",
scale="yscale",
offset=-48,
grid=true
}
},
marks={
{
type="rect",
from={
data="table"
},
properties={
update={
xc={
scale="xscale",
field="rank"
},
width={
value=12
},
y={
scale="yscale",
field="active"
},
y2={
scale="yscale",
value=0
},
fill={
h={
field="hue"
},
s={
value=0.5
},
l={
value=0.5
}
}
}
}
},
{
type="group",
from={
data="table",
transform={
{
type="facet",
groupby={
"rank"
}
}
}
},
marks={
{
type="text",
from={
transform={
{
type="countpattern",
field="name",
pattern="."
},
{
type="rank"
}
}
},
properties={
update={
align={
value="center"
},
baseline={
value="top"
},
fill={
value="black"
},
x={
scale="xscale",
field={
parent="rank"
}
},
y={
field="rank",
offset=height-72-8,
mult=12
},
text={
field="text"
},
font={
value="TakaoGothic"
}
}
}
}
}
},
{
type="text",
properties={
update={
align={
value="center"
},
baseline={
value="top"
},
fill={
value="black"
},
fontSize={
value=32
},
x={
value=width-80
},
y={
value=4
},
text={
signal="yrtext[0]"
},
font={
value="TakaoGothic"
}
}
}
},
{
type="text",
properties={
update={
align={
value="center"
},
baseline={
value="top"
},
fill={
value="black"
},
fontSize={
value=18
},
x={
value=width-80
},
y={
value=40
},
text={
signal="yrtext[1]"
},
font={
value="TakaoGothic"
}
}
}
}
}
}
end
return {
main=function(frame)
local result=createGraph(frame:getParent().args.code)
if result~=nil then
return frame:extensionTag('graph',mw.text.jsonEncode(result),{mode='interactive'})
else
return ""
end
end
}