Module:Television ratings graph: Difference between revisions

Content deleted Content added
Add "Citation needed" date parameter
oops typo
 
(24 intermediate revisions by 10 users not shown)
Line 34:
function TVRG.new(frame,args)
args = args or {}
local categories = ''
local title = mw.title.getCurrentTitle()
-- Variables
local timeline = {}''
local longestseason = -1
local average = args.average and 1 or 0
Line 48 ⟶ 50:
local numberargs = 0
for k,v in pairs(args) do
if (string.lower(v) == 'n/a') or (not string.match(k,'[^%d]+') and not string.match(v,'[^%d\%.]+')) then numberargs = numberargs + 1 end
end
 
Line 58 ⟶ 60:
num_seasons = thisseason
end
end
if num_seasons < 1 then
num_seasons = 1
end
 
Line 70 ⟶ 75:
-- Bar and graph width
local barwidth
if num_episodes >= 80 then barwidth = 9
elseifif num_episodes >= 50args.bar_width then barwidth = 10args.bar_width
elseif num_episodes >= 20120 then barwidth = 116
elseelseif num_episodes >= 100 then barwidth = 127
elseif num_episodes >= 80 then barwidth = 8
elseif num_episodes >= 50 then barwidth = 9
elseif num_episodes >= 20 then barwidth = 10
else barwidth = 11
end
 
local graphwidth = num_episodes*barwidth
-- Determine maximum viewer figure
local maxviewers = -1
Line 95 ⟶ 102:
end
end
local is_singular = num_episodes == 1
 
-- Basis parameters
timeline = timeline .. "\nImageSize = width:" .. (is_singular and 200 or 'auto') .. " height:"..(args.height or 500).. (is_singular and '' or " barincrement:"..(barwidth+(num_episodes == 2 and 16 or 4))).."\n"
timeline['type'] = 'stackedrect'
timeline = timeline .. "\nPlotArea = left:50 bottom:50 top:10 right:100\n"
timeline['width'] = (args.width or graphwidth)
timeline['height'] = (argstimeline ..height or"\nAlignbars = 300)justify\n"
timeline = timeline .. "\nPeriod = from:0 till:"..math.ceil(maxviewers).."\n"
timeline['legend'] = season_title
timeline = timeline .. "\nTimeAxis = orientation:vertical format:y\n"
timeline['colors'] = ''
-- Colors
timeline['x'] = ''
timeline['xType'] = 'string'timeline .. "\nColors =\n"
timeline['xAxisAngle'] = '-90'
timeline['xAxisTitle'] = 'Episode'
timeline['yGrid'] = 'y'
-- Color and legend variables
for season = 1,num_seasons do
timelineargs['y'"color" .. season..'Title'] = args["legendcolor" .. season] or season'#CCCCFF'
timeline['colors']hex = timeline['colors'] .. args["color" .. season] .. ':gsub("#",'"")
if #hex == 3 then
end
-- If it's 3 hex chars then make it six instead
hex = hex:sub(1,1) .. hex:sub(1,1) .. hex:sub(2,2) .. hex:sub(2,2) .. hex:sub(3,3) .. hex:sub(3,3)
-- Axis labels
local countryDisplayUS, countryDisplayUK, countryDisplayOther
if args.country ~= nil and args.country ~= '' then
if args.country == "U.S." or args.country == "US" or args.country == "United States" then countryDisplayUS = 'U.S.'
elseif args.country == "U.K." or args.country == "UK" or args.country == "United Kingdom" then countryDisplayUK = 'UK'
else countryDisplayOther = args.country
end
rgbR = tonumber("0x"..hex:sub(1,2))/256
rgbG = tonumber("0x"..hex:sub(3,4))/256
rgbB = tonumber("0x"..hex:sub(5,6))/256
local legend = season_title.."_"..(args["legend" .. season] and args["legend" .. season] or season )
timeline = timeline .. "\n id:season"..season.." value:rgb("..rgbR..", "..rgbG..", "..rgbB..") legend:"..legend:gsub(" ", "_").."\n"
end
timeline = timeline .. "\n id:bars value:gray(0.95)"
timeline['yAxisTitle'] = ((countryDisplayUS or countryDisplayUK or countryDisplayOther) or "") .. ((countryDisplayUS or countryDisplayUK or countryDisplayOther) and " v" or "V") .. "iewers (" .. multiple .. ")\n"
timeline = timeline .. "\nLegend = orientation:vertical position:right\n"
timeline = timeline .. "\nBackgroundColors = bars:bars\n"
timeline = timeline .. "\nScaleMajor = unit:year increment:"..(multiple == 'thousands' and 100 or 1).." start:0\n"
timeline = timeline .. "\nScaleMinor = unit:year increment:"..(multiple == 'thousands' and 100 or 1).." start:0\n"
 
local x_intervals = args.x_intervals or (num_episodes >= 80 and 2 or 1)
 
timeline = timeline .. "\nBarData=\n"
for episode = 1,num_episodes do
episodetext = ((episode % x_intervals == 0) and episode or "")
timeline = timeline .. "\n bar:"..episode.." text:"..episodetext.."\n"
end
-- Add bars to timeline, one per viewer figure
local bar = 1
Line 132 ⟶ 149:
local counted_episodes = 0
timeline = timeline .. "\nPlotData=\n"
for k,v in pairs(args) do
timeline = timeline .. "\n width:"..barwidth.." textcolor:black align:left anchor:from shift:(10,-4)\n"
for k,v in ipairs(args) do
if string.lower(v) == 'n/a' then v = '' end
if tonumber(k) ~= nil then
if v == '-' then
-- Hyphen means new season
season = season + 1
 
timeline['y'..season] = ''
for-- epDetermine =highest number of 1,counted_episodes doin a season
if thisseason > longestseason then
timeline['y'..season] = timeline['y'..season] .. ','
longestseason = thisseason
end
-- Determine highest number of counted_episodes in a season
if thisseason > longestseason then
longestseason = thisseason
end
thisseason = 0
elseif average == 0 or (average == 1 and args[k+1] ~= '-' and args[k+1] ~= nil) then
-- Include bar for viewer figure, do not include if averages are included and the next parameter is a new season marker
timeline['y'..season] = timeline['y'..season] .. (timeline['y'..season] and ',' or '') .. (v ~= '' and v or 0)
-- Increment tracking variables
counted_episodes = counted_episodes + 1
thisseason = thisseason + 1
bar = bar + 1
end
thisseason = 0
elseif average == 0 or (average == 1 and args[k+1] ~= '-' and args[k+1] ~= nil) then
-- Include bar for viewer figure, do not include if averages are included and the next parameter is a new season marker
timeline = timeline .. "\n bar:"..bar.." from:0 till:"..(v ~= '' and v or 0).." color:season"..season.."\n"
-- Increment tracking variables
counted_episodes = counted_episodes + 1
thisseason = thisseason + 1
bar = bar + 1
end
end
Line 164 ⟶ 179:
end
-- XAxis axis variableslabels
local countryDisplayUS, countryDisplayUK, countryDisplayOther
for ep = 1,num_episodes do
if args.country ~= nil and args.country ~= '' then
timeline['x'] = timeline['x'] .. (timeline['x'] and ',' or '') .. ep
if args.country == "U.S." or args.country == "US" or args.country == "United States" then countryDisplayUS = 'U.S.'
elseif args.country == "U.K." or args.country == "UK" or args.country == "United Kingdom" then countryDisplayUK = 'UK'
else countryDisplayOther = args.country end
end
Line 176 ⟶ 194:
end
root:tag('div'):css('clear','both')
 
-- Add timeline to div
if args.no_graph == nil then
if num_episodes > 100 then
root:node(frame:expandTemplate{title='Graph:Chart', args=timeline})
root:tag('div'):csswikitext('clear','both'"Too many episodes to display graph (maximum 100).")
if title.namespace == 0 then
categories = categories .. '[[Category:Articles using Template:Television ratings graph with excessive figures]]'
end
else
timelineBase = frame:preprocess("<timeline>"..timeline.."</timeline>")
root:node(timelineBase)
root:tag('div'):css('clear','both')
end
end
Line 192 ⟶ 218:
local row = rtable:tag('tr')
row:tag('th'):wikitext(season_title)
:attr('scope','col')
:attr('colspan','2')
:attr('rowspan','2')
Line 198 ⟶ 225:
row:tag('th')
:attr('scope','colgroup')
:attr('colspan',longestseason)
:wikitext("Episode number")
Line 232 ⟶ 260:
-- Spanning empty cells with {{N/A}}
if thisseason < longestseason then
row:node(TVRG.NACell(frame,"N/A"):attr('colspan',longestseason-thisseason))
end
Line 252 ⟶ 280:
row:tag('th')
:css('background-color', args['color' .. season])
:css('width','10px14px')
:css('padding','0')
row:tag('th')
Line 278 ⟶ 307:
-- Finish by checking if final row needs {{N/A}} cells
if average == 0 and thisseason < longestseason then
row:node(TVRG.NACell(frame,"N/A"):attr('colspan',longestseason-thisseason))
end
Line 290 ⟶ 319:
if countryDisplayUS then
root:wikitext("<small>Audience measurement performed by [[Nielsen Media Research]].</small>" .. (args.refs ~= '' and args.refs or tostring(span)))
elseif countryDisplayUK then
root:wikitext("<small>Audience measurement performed by [[Broadcasters' Audience Research Board]].</small>" .. (args.refs ~= '' and args.refs or tostring(span)))
else
root:wikitext("<small>Source: </small>" .. (args.refs ~= '' and args.refs or tostring(span)))
end
return tostring(root) .. categories
end