Module:Television ratings graph
Appearance
![]() | This module depends on the following other modules: |
Usage
Creates a standard Television ratings graph with
{{#invoke:Television ratings graph|main}}
With the parameters defined in the documentation of the template.
Tracking categories
-- This module implements {{Television ratings graph}}.
--------------------------------------------------------------------------------
-- TVRG class
-- The main class.
--------------------------------------------------------------------------------
local TVRG = {}
function TVRG.hex2rgb(hex)
hex = hex:gsub('#', '')
return tonumber("0x"..hex:sub(1,2))/256, tonumber("0x"..hex:sub(3,4))/256, tonumber("0x"..hex:sub(5,6))/256
end
-- Allow usages of {{N/A}} cells
function TVRG.NACell(frame,text)
local cell = mw.html.create('td')
local attrMatch = '([%a-]*)="([^"]*)"'
infoParam = frame:expandTemplate{title='N/A',args={text}}
while true do
local a,b = string.match(infoParam,attrMatch)
if a == nil or b == nil then break end
cell:attr(a,b)
infoParam = string.gsub(infoParam,attrMatch,'',1)
end
infoParam = string.gsub(infoParam,'%s*|%s*','',1)
cell:wikitext(infoParam)
return cell
end
function TVRG.new(frame,args)
args = args or {}
local timelineCode = ''
local root = mw.html.create('div')
:attr('align', 'center')
timelineCode = timelineCode .. "ImageSize = width:" .. (args.width or 1000) .. " height:" .. (args.height or 300) .. "\n"
timelineCode = timelineCode .. "PlotArea = left:50 bottom:70 top:20 right:50\n"
timelineCode = timelineCode .. "AlignBars = justify\n"
timelineCode = timelineCode .. "Colors =\n"
timelineCode = timelineCode .. " id:a value:gray(0.7)\n"
local season_num = 1
while args['color' .. season_num] ~= nil do
local r,g,b = TVRG.hex2rgb(args['color' .. season_num])
timelineCode = timelineCode .. " id:season" .. season_num .. " value:rgb("..r..","..g..","..b..") legend:" ..
string.gsub((args["legend" .. season_num] or "Season " .. season_num), ' ', '_') .. "\n"
season_num = season_num + 1
end
local max_number = -1;
for k,v in pairs(args) do
local num = tonumber(v)
if num ~= nil and num > max_number then
max_number = num
end
end
timelineCode = timelineCode .. "DateFormat = x.y\n"
timelineCode = timelineCode .. "Period = from:0 till:" .. math.ceil(max_number) .. "\n"
timelineCode = timelineCode .. "TimeAxis = orientation:vertical\n"
timelineCode = timelineCode .. "ScaleMajor = gridcolor:a increment:1 start:0\n"
timelineCode = timelineCode .. "Legend = orientation:horizontal\n"
timelineCode = timelineCode .. "PlotData =\n"
timelineCode = timelineCode .. " width:7\n"
local bar = 1
local season = 1
local max_episodes = -1
local this_episodes = 0
for k,v in pairs(args) do
if tonumber(k) ~= nil then
if v == '-' then
timelineCode = timelineCode .. " color:season" .. season .. "\n"
season = season + 1
if this_episodes > max_episodes then
max_episodes = this_episodes
end
this_episodes = 0
else
timelineCode = timelineCode .. " bar:" .. bar .. " from:start till:" .. (v ~= '' and v or 'start') .. "\n"
this_episodes = this_episodes + 1
bar = bar + 1
end
end
end
local timeline = frame:extensionTag('timeline', timelineCode)
root:node(timeline)
local rtable = mw.html.create('table')
:addClass('wikitable')
:css('text-align', 'center')
rtable:tag('caption'):wikitext("''" .. args.title .. "'': Viewers per episode (millions)")
local row = rtable:tag('tr')
row:tag('td')
for i=1,max_episodes do
row:tag('th')
:attr('scope','col')
:wikitext('Ep. '..i)
end
local season = 1
local this_episodes = 0
for k,v in pairs(args) do
if tonumber(k) ~= nil then
if v == '-' then
if season > 1 and this_episodes ~= max_episodes then
row:node(TVRG.NACell(frame,"N/A"):attr('colspan',max_episodes-this_episodes))
end
row = rtable:tag('tr')
row:tag('th')
:attr('scope','col')
:wikitext(args["legend" .. season] and "''"..args["legend" .. season].."''" or 'Season ' .. season)
this_episodes = 0
season = season + 1
else
if v ~= '' then
row:tag('td'):wikitext(v)
else
row:node(TVRG.NACell(frame,"TBD"))
end
this_episodes = this_episodes + 1
end
end
end
if this_episodes ~= max_episodes then
row:node(TVRG.NACell(frame,"N/A"):attr('colspan',max_episodes-this_episodes))
end
root:node(rtable)
return tostring(root)
end
--------------------------------------------------------------------------------
-- Exports
--------------------------------------------------------------------------------
local p = {}
function p.main(frame)
local args = require('Module:Arguments').getArgs(frame, {
removeBlanks = false--,
--wrappers = 'Template:Episode table'
})
return TVRG.new(frame,args)
end
return p