编辑“
Module:Bar
”
跳转到导航
跳转到搜索
警告:
您没有登录。如果您做出任意编辑,您的IP地址将会公开可见。如果您
登录
或
创建
一个账户,您的编辑将归属于您的用户名,且将享受其他好处。
反垃圾检查。
不要
加入这个!
local p = {} local inner = {} --########## --## Public functions --########## --- Render a bar chart. -- @param frame The arguments passed to the script. See docs on renderFromLua. function p.format( frame ) -- extract args local width = frame.args['width'] local barCSS = frame.args['barCSS'] local zeroWidth = frame.args['zeroWidth'] local total = frame.args['total'] -- extract bar series from arguments like 'value,color,title' local series = {} for key, spec in ipairs(frame.args) do spec = mw.text.split(spec, ',') local data = {value = tonumber(spec[1] or 0), color = spec[2] or '#CCC', title = spec[3] or ''} if data['value'] > 0 then table.insert(series, data) end end return p.renderFromLua(series, total, width, barCSS, zeroWidth) end --- Render a bar chart from Lua. -- @param series A table representing the bars to render, consisting of a sequence of tables like {value = 14, color = '#CCC', title = 'tooltip text'}. -- @param total (optional) The total number of values represented by all bar series. -- @param width (optional) The CSS width of the bar. -- @param barCss (optional) Additional CSS to apply to the rendered bar table. -- @param zeroWidth (optional) function p.renderFromLua(series, total, width, barCSS, zeroWidth) -- parse arguments width = width or '100%' total = tonumber(total or 0) zeroWidth = zeroWidth or '1px' -- calculate total local seriesTotal = 0 for k,v in ipairs(series) do seriesTotal = seriesTotal + v['value'] end if total < seriesTotal then total = seriesTotal end -- inject empty series for uncharted values if(seriesTotal < total) then table.insert(series, {value = total - seriesTotal, color = 'transparent', title = ''}) end -- inject ratios for k,v in ipairs(series) do v['total'] = total v['ratio'] = inner.getRatio(v['value'], total) if v['ratio'] == 0 then v['width'] = zeroWidth end end -- render result = mw.html.create 'table' :attr('role', 'presentation') :css('width', width) :cssText(barCSS) :css('border-spacing', '0') :tag 'tr' for k, v in pairs(series) do result:node(inner.renderSeries(v)) end return result:allDone() end --########## --## Private functions --########## --- Render an individual bar series. -- @param series The bar series to render. function inner.renderSeries(series) -- ignore empty series if not series.value or series.value == 0 then return '' end -- set width local width = series.width if not(width) then width = series.ratio .. '%' end -- format return mw.html.create 'td' :attr('title', series.title) :css('width', width) :css('background', series.color) :attr('data-value', series.value) :css('height', '1em') :css('padding', '0') end --- Get the percentage ratio of two numbers as a decimal value. -- @param value The number of items in the subset. -- @param total The total number of items in the set. function inner.getRatio(value, total) if(total == 0) then error('the total for a series cannot be zero') end return math.floor(value / total * 10000) / 100 end return p
摘要:
请注意,您对Positive WiKi的所有贡献都可能被其他贡献者编辑,修改或删除。如果您不希望您的文字被任意修改和再散布,请不要提交。
您同时也要向我们保证您所提交的内容是您自己所作,或得自一个不受版权保护或相似自由的来源(参阅
Positive WiKi:版权
的细节)。
未经许可,请勿提交受版权保护的作品!
取消
编辑帮助
(在新窗口中打开)
本页使用的模板:
Module:Bar/doc
(
编辑
)
导航菜单
个人工具
未登录
讨论
贡献
创建账号
登录
命名空间
模块
讨论
English
查看
阅读
编辑源代码
查看历史
更多
搜索
导航
首页
最近更改
随机页面
MediaWiki帮助
工具
链入页面
相关更改
特殊页面
页面信息