跳转到内容
打开/关闭搜索
搜索
打开/关闭菜单
451
条目
808
文件
22
用户
9249
编辑
Blockland Wiki
导航
首页
最近更改
随机页面
MediaWiki帮助
特殊页面
打开/关闭外观设置菜单
无法加载外观设置。请检查您的网络连接并重试。
重试
打开/关闭个人菜单
未登录
未登录用户的IP地址会在进行任意编辑后公开展示。
user-interface-preferences
个人工具
创建账号
登录
查看“︁模块:WeatherGraph”︁的源代码
来自Blockland Wiki
查看
阅读
查看源代码
查看历史
相关页面
模块
讨论
更多操作
更多
工具
链入页面
相关更改
页面信息
Cargo数据
←
模块:WeatherGraph
因为以下原因,您没有权限编辑该页面:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
local p = {} -- 柯本气候分类计算逻辑(保持不变) local function calculateKoppen(temps, precips) local t_mean_annual, p_total, t_hot, t_cold = 0, 0, -99, 99 local s_p, w_p = 0, 0 for i = 1, 12 do local t, p = tonumber(temps[i]) or 0, tonumber(precips[i]) or 0 t_mean_annual = t_mean_annual + t p_total = p_total + p if t > t_hot then t_hot = t end if t < t_cold then t_cold = t end if i >= 4 and i <= 9 then s_p = s_p + p else w_p = w_p + p end end t_mean_annual = t_mean_annual / 12 local pth = 20 * t_mean_annual + (s_p > p_total * 0.7 and 280 or (w_p > p_total * 0.7 and 0 or 140)) if p_total < pth then local suffix = t_mean_annual >= 18 and "h" or "k" return p_total < pth / 2 and "BW" .. suffix or "BS" .. suffix end if t_cold >= 18 then return "A" end if t_hot >= 10 then return t_cold > -3 and "C" or "D" end return t_hot > 0 and "ET" or "EF" 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 d = { high = {}, mean = {}, low = {}, rain = {} } local max_t, max_p = 50, 100 -- 初始刻度上限 for i, m in ipairs(months) do d.high[i] = tonumber(args[m .. ' high C']) or 0 d.mean[i] = tonumber(args[m .. ' mean C']) or 0 d.low[i] = tonumber(args[m .. ' low C']) or 0 d.rain[i] = tonumber(args[m .. ' rain mm']) or 0 if d.high[i] > max_t then max_t = d.high[i] end if d.rain[i] > max_p then max_p = d.rain[i] end end -- 向上取整到10的倍数 max_t = math.ceil(max_t / 10) * 10 max_p = math.ceil(max_p / 20) * 20 -- SVG 绘图配置 (3:4 画幅设计: Width=300, Height=400) local w, h = 300, 400 local padding = { t = 40, b = 40, l = 40, r = 40 } local chart_w, chart_h = w - padding.l - padding.r, h - padding.t - padding.b local function ty(v) return h - padding.b - (v / max_t * chart_h) end local function py(v) return h - padding.b - (v / max_p * chart_h) end local function tx(i) return padding.l + (i - 0.5) * (chart_w / 12) end local svg = string.format('<svg width="100%%" viewBox="0 0 %d %d" style="background:#fff; font-family:sans-serif;">', w, h) -- 1. 绘制网格线 for v = 0, max_t, 10 do local y = ty(v) svg = svg .. string.format('<line x1="%d" y1="%d" x2="%d" y2="%d" stroke="#eee" stroke-width="1" />', padding.l, y, w - padding.r, y) svg = svg .. string.format('<text x="%d" y="%d" font-size="10" fill="#999" text-anchor="end">%d°</text>', padding.l - 5, y + 4, v) end -- 2. 绘制降水柱状图 for i, p_val in ipairs(d.rain) do local bh = (p_val / max_p * chart_h) svg = svg .. string.format('<rect x="%f" y="%f" width="%f" height="%f" fill="#cee" opacity="0.8" />', tx(i)-8, py(p_val), 16, bh) end -- 3. 绘制三条气温曲线 local function drawLine(data, color, width) local pts = "" for i, v in ipairs(data) do pts = pts .. string.format("%f,%f ", tx(i), ty(v)) end return string.format('<polyline points="%s" fill="none" stroke="%s" stroke-width="%d" />', pts, color, width) end svg = svg .. drawLine(d.high, "#f44", 2) -- 高温红色 svg = svg .. drawLine(d.mean, "#f90", 2) -- 均温橙色 svg = svg .. drawLine(d.low, "#48f", 2) -- 低温蓝色 -- 4. 绘制月份标签 for i, m in ipairs({"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"}) do svg = svg .. string.format('<text x="%f" y="%d" font-size="10" fill="#666" text-anchor="middle">%s</text>', tx(i), h - padding.b + 15, m) end svg = svg .. '</svg>' -- 封装 HTML 并输出 local raw_svg = frame:extensionTag('html', svg) local koppen = calculateKoppen(d.mean, d.rain) return string.format([[ <div style="float: right; width: 280px; border: 1px solid #ccc; padding: 10px; background: #fff; box-shadow: 2px 2px 5px rgba(0,0,0,0.1);"> <div style="text-align:center; font-weight:bold; padding:5px; background:#f0f0f0;">%s</div> <div style="margin:10px 0;">%s</div> <div style="font-size:0.9em; line-height:1.4em;"> <b>柯本分类:</b>%s<br> <b>数据来源:</b>%s </div> </div>]], args['location'] or "未知", raw_svg, koppen, args['source'] or "JGWWC") end return p
此页面嵌入的页面:
模块:WeatherGraph/doc
(
查看源代码
)
返回
模块:WeatherGraph
。
查看“︁模块:WeatherGraph”︁的源代码
来自Blockland Wiki