模块:WeatherGraph
来自Blockland Wiki
更多操作
此模块的文档可以在模块:WeatherGraph/doc创建
local p = {}
-- 柯本气候分类计算器
local function calculateKoppen(temps, precips)
local t_mean = 0
local p_total = 0
local t_hot = -99
local t_cold = 99
local p_min_summer = 9999
local p_max_summer = 0
local p_min_winter = 9999
local p_max_winter = 0
local summer_p_total = 0
local winter_p_total = 0
for i = 1, 12 do
local t = tonumber(temps[i]) or 0
local p = tonumber(precips[i]) or 0
t_mean = t_mean + t
p_total = p_total + p
if t > t_hot then t_hot = t end
if t < t_cold then t_cold = t end
-- 简单划分夏冬半季(北半球:4-9月为夏,10-3月为冬)
if i >= 4 and i <= 9 then
summer_p_total = summer_p_total + p
if p < p_min_summer then p_min_summer = p end
if p > p_max_summer then p_max_summer = p end
else
winter_p_total = winter_p_total + p
if p < p_min_winter then p_min_winter = p end
if p > p_max_winter then p_max_winter = p end
end
end
t_mean = t_mean / 12
-- 计算干燥阈值 (Pth)
local pth = 20 * t_mean
if summer_p_total > (p_total * 0.7) then
pth = pth + 280
elseif winter_p_total > (p_total * 0.7) then
pth = pth
else
pth = pth + 140
end
-- B类:干旱气候
if p_total < pth then
if p_total < (pth / 2) then
return t_mean >= 18 and "BWh (热带沙漠气候)" or "BWk (温带沙漠气候)"
else
return t_mean >= 18 and "BSh (热带草原/半干旱气候)" or "BSk (温带草原/半干旱气候)"
end
end
-- A类:热带气候
if t_cold >= 18 then
if (p_min_winter < 60 and p_min_summer < 60) then return "Aw/As (热带干湿季气候)" end
return "Af (热带雨林气候)"
end
-- C类与D类
if t_hot >= 10 and t_cold > -3 and t_cold < 18 then
return "C 类 (温带气候)"
end
if t_hot >= 10 and t_cold <= -3 then
return "D 类 (大陆性气候)"
end
-- E类:极地气候
if t_hot < 10 then
return t_hot > 0 and "ET (苔原气候)" or "EF (冰原气候)"
end
return "未分类"
end
function p.render(frame)
local args = frame:getParent().args
local months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}
local temps = {}
local precips = {}
-- 提取并清理数据
for i, m in ipairs(months) do
table.insert(temps, tonumber(args[m .. ' mean C']) or 0)
table.insert(precips, tonumber(args[m .. ' rain mm']) or 0)
end
local koppen = calculateKoppen(temps, precips)
local title = args['location'] or "气候数据"
-- ========================================================
-- 使用纯 CSS 绘制双轴图表(MediaWiki 原生完美兼容)
-- ========================================================
local chart_html = '<div style="position:relative; width:100%; height:200px; background:#f8f9fa; border-radius:4px; margin-top:10px; overflow:hidden;">'
-- 0度气温基准线
local zero_y = 50
chart_html = chart_html .. string.format('<div style="position:absolute; left:0; bottom:%dpx; width:100%%; height:1px; background:#d5d5d5; z-index:1;"></div>', zero_y)
-- 1. 绘制柱状图 (降水)
for i, p_val in ipairs(precips) do
local height = p_val * 1.5 -- 降水高度缩放因子
local x = (i - 1) * 25 + 10 -- X轴间距分配
chart_html = chart_html .. string.format('<div style="position:absolute; left:%dpx; bottom:0; width:16px; height:%fpx; background:#87CEFA; z-index:1; border-radius:2px 2px 0 0;"></div>', x, height)
end
-- 2. 绘制气温点和折线
for i = 1, 12 do
local t_val = temps[i]
local x1 = (i - 1) * 25 + 18 -- 当前点 X 坐标 (对齐柱子中心)
local y1 = zero_y + (t_val * 3) -- 当前点 Y 坐标 (气温缩放因子)
-- 计算两点之间的连线 (利用 CSS 旋转实现)
if i < 12 then
local next_t = temps[i+1]
local x2 = (i) * 25 + 18
local y2 = zero_y + (next_t * 3)
local dx = x2 - x1
local dy = y2 - y1
local length = math.sqrt(dx*dx + dy*dy)
local angle = math.atan2(dy, dx) * 180 / math.pi
chart_html = chart_html .. string.format('<div style="position:absolute; left:%dpx; bottom:%dpx; width:%fpx; height:2px; background:#FF6347; transform-origin: 0 50%%; transform: rotate(%fdeg); z-index:2;"></div>', x1, y1, length, angle)
end
-- 绘制气温数据点
chart_html = chart_html .. string.format('<div style="position:absolute; left:%dpx; bottom:%dpx; width:6px; height:6px; background:#DC143C; border-radius:50%%; z-index:3;"></div>', x1 - 3, y1 - 3)
end
chart_html = chart_html .. '</div>'
-- 组装最终的 Infobox HTML 结构
local html = string.format([[
<div style="float: right; clear: right; margin: 0 0 1em 1em; width: 320px; border: 1px solid #c8ccd1; border-radius: 8px; padding: 10px; background-color: var(--background-color-base, #ffffff); box-shadow: 0 2px 4px rgba(0,0,0,0.1); font-size: 90%%;">
<div style="text-align: center; font-weight: bold; font-size: 1.1em; margin-bottom: 5px; background: #eaecf0; padding: 5px; border-radius: 4px;">%s</div>
%s
<div style="text-align: center; font-weight: bold; color: #54595d; margin-top: 15px;">柯本气候分类:<br><span style="color: #2a4b8d;">%s</span></div>
<div style="font-size: 0.85em; color: #72777d; text-align: right; margin-top: 8px;">来源:%s</div>
</div>
]], title, chart_html, koppen, args['source'] or "未知")
return html
end
return p