跳到正文
搜索
搜索
查看“︁模块:议会”︁的源代码
阅读
查看历史
查看源代码
讨论
←
模块:议会
因为以下原因,您没有权限编辑该页面:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
--[[ 模块:议会 草案(A 路线,2026-10-25) 基线:生产修订 2621(模块:议会),本文档为可直接替换的完整新版。 改动:抽出解析/半环图公共函数;新增“缩略”布局(供国家信息框「议会构成」字段使用)。 默认布局的输出与 2621 完全一致,其余页面仍可继续使用 {{议会席位}} 旧图例表。 数据单一来源:席位数据只由调用方传入一次;本模块不保存数据。 --]] local p = {} -- 解析 |席位= 多行参数,返回 groups, total, err local function parseGroups(args) local groups, total = {}, 0 for line in mw.text.gsplit(args['席位'] or '', '\n') do line = mw.text.trim(line) if line ~= '' then local name, count = line:match('^(.-)%s*=%s*(%d+)%s*$') if not name or mw.text.trim(name) == '' then return nil, 0, '<strong class="error">议会席位:每行请填写“名称=席数”。</strong>' end count = tonumber(count) groups[#groups + 1] = { name = mw.text.trim(name), count = count } total = total + count end end return groups, total, nil end local function checkCapacity(args, total) if total == 0 then return '<strong class="error">议会席位:请填写席位数据。</strong>' end local capacity = tonumber(args['总席位']) or total if capacity ~= total then return '<strong class="error">议会席位:分组合计' .. total .. '席,与总席位' .. capacity .. '席不一致。</strong>' end return nil end -- 半环图:完整版与缩略版共用,几何与 2621 一致(圆点即席位,颜色只区分组别) local function buildChamber(groups, total) local chamber = mw.html.create('div'):addClass('nv-parliament__chamber'):attr('aria-hidden', 'true') local positions, allocated = {}, 0 local radii = total < 30 and { 46 } or (total < 80 and { 46, 32, 18 } or { 46, 39, 32, 25, 18 }) local radiusSum = 0 for _, radius in ipairs(radii) do radiusSum = radiusSum + radius end for ring, radius in ipairs(radii) do local count = ring == #radii and total - allocated or math.floor(total * radius / radiusSum + 0.5) allocated = allocated + count for seat = 1, count do local angle = count == 1 and math.pi / 2 or (seat - 1) * math.pi / (count - 1) positions[#positions + 1] = { angle = angle, radius = radius } end end table.sort(positions, function(a, b) if a.angle == b.angle then return a.radius > b.radius end return a.angle > b.angle end) local seat = 1 for index, group in ipairs(groups) do local class = 'nv-parliament__party--' .. ((index - 1) % 12 + 1) group.class = class for _ = 1, group.count do local point = positions[seat] if point then local x = 50 + math.cos(point.angle) * point.radius local y = 98 - math.sin(point.angle) * point.radius * 2 chamber:tag('span'):addClass('nv-parliament__seat'):addClass(class) :css('left', string.format('%.3f%%', x)):css('top', string.format('%.3f%%', y)) end seat = seat + 1 end end return chamber end -- 旧版完整布局:标题 + 届次 + 半环图 + 可读图例表 + 说明。保持 2621 行为不变。 local function renderFull(groups, total, args) local root = mw.html.create('div'):addClass('nv-parliament') root:tag('div'):addClass('nv-parliament__title'):wikitext(args['名称'] or '议会席位') root:tag('p'):addClass('nv-parliament__session'):wikitext((args['届次'] or '') .. ' · 共' .. total .. '席') root:node(buildChamber(groups, total)) local legend = root:tag('table'):addClass('wikitable'):addClass('nv-parliament__legend') legend:tag('caption'):wikitext('席位构成') local header = legend:tag('tr') header:tag('th'):attr('scope', 'col'):wikitext('组别') header:tag('th'):attr('scope', 'col'):wikitext('席位') header:tag('th'):attr('scope', 'col'):wikitext('占比') for _, group in ipairs(groups) do local row = legend:tag('tr') local label = row:tag('th'):attr('scope', 'row') label:tag('span'):addClass('nv-parliament__key'):addClass(group.class):attr('aria-hidden', 'true') label:wikitext(mw.text.nowiki(group.name)) row:tag('td'):wikitext(tostring(group.count)) row:tag('td'):wikitext(string.format('%.1f%%', group.count / total * 100)) end root:tag('p'):addClass('nv-parliament__note'):wikitext('按表中顺序排布,不表示政治光谱。') return root end -- 缩略布局:给信息框字段使用。一行元信息 + 半环图 + 色点/组名/席数简列。 -- 不含卡片外框、不含“席位构成”表格标题、不含占比列、不含政治光谱说明。 local function renderCompact(groups, total, args) local name = mw.text.trim(args['名称'] or '') local session = mw.text.trim(args['届次'] or '') local label = (name ~= '' and name or '议会') .. '席位构成,共' .. total .. '席' local root = mw.html.create('div'):addClass('nv-seatmap') :attr('role', 'group'):attr('aria-label', label) local meta = {} if name ~= '' then meta[#meta + 1] = name end if session ~= '' then meta[#meta + 1] = session end meta[#meta + 1] = '共' .. total .. '席' root:tag('div'):addClass('nv-seatmap__meta'):wikitext(table.concat(meta, ' · ')) root:node(buildChamber(groups, total)) local list = root:tag('ul'):addClass('nv-seatmap__list') for _, group in ipairs(groups) do local item = list:tag('li'):addClass('nv-seatmap__item') item:tag('span'):addClass('nv-parliament__key'):addClass(group.class):attr('aria-hidden', 'true') item:tag('span'):addClass('nv-seatmap__name'):wikitext(mw.text.nowiki(group.name)) item:tag('span'):addClass('nv-seatmap__seats'):wikitext(tostring(group.count)) end return root end function p.render(frame) local args = frame:getParent().args local groups, total, err = parseGroups(args) if err then return err end err = checkCapacity(args, total) if err then return err end if args['布局'] == '缩略' or args['布局'] == 'compact' then return tostring(renderCompact(groups, total, args)) end return tostring(renderFull(groups, total, args)) end return p
该页面使用的模板:
模块:议会/doc
(
查看源代码
)
返回
模块:议会
。