Module:Television ratings graph: Difference between revisions

Content deleted Content added
Undid revision 917149684 by Alex 21 (talk) My bad, wrong way
Implement Template:Graph:Chart for mobile and accessibility purposes
Line 9:
 
local TVRG = {}
 
-- Convert HEX codes to RGB values
function TVRG.hex2rgb(hex)
hex = hex:gsub('#', '')
if #hex == 3 then
-- #000 format
return tonumber("0x"..hex:sub(1,1)..hex:sub(1,1))/256, tonumber("0x"..hex:sub(2,2)..hex:sub(2,2))/256,
tonumber("0x"..hex:sub(3,3)..hex:sub(3,3))/256
else
-- #000000 format
return tonumber("0x"..hex:sub(1,2))/256, tonumber("0x"..hex:sub(3,4))/256, tonumber("0x"..hex:sub(5,6))/256
end
end
 
-- Allow usages of {{N/A}} cells
Line 49 ⟶ 36:
-- Variables
local timeline = ''{}
local longestseason = -1
local average = args.average and 1 or 0
Line 73 ⟶ 60:
end
 
-- Determine number of episodes and subtract averages if included (they should be equal to the number of seasons)
local num_episodes
if average == 1 then
-- Subtract averages if included (they should be equal to the number of seasons)
num_episodes = numberargs-num_seasons
if average == 1 then
else
num_episodes = numberargs-num_seasons
num_episodes = numberargs
else
end
num_episodes = numberargs
end
 
-- Bar and graph width
local barwidth
if num_episodes <>= 2080 then barwidth = 89
elseif num_episodes >= 20 and num_episodes < 50 then barwidth = 710
elseif num_episodes >= 50 and num_episodes < 8020 then barwidth = 611
elseif num_episodes >= 80 thenelse barwidth = 512
end
 
local graphwidth = num_episodes*barwidth
-- Graph width
local graphwidth
if args.bar_width == nil then
if num_episodes < 20 then graphwidth = num_episodes*13+98
elseif num_episodes >= 20 and num_episodes < 50 then graphwidth = num_episodes*12+98
elseif num_episodes >= 50 and num_episodes < 80 then graphwidth = num_episodes*11+98
elseif num_episodes >= 80 then graphwidth = num_episodes*10+98
end
else
if num_episodes < 20 then graphwidth = num_episodes*(13+args.bar_width-barwidth)+98
elseif num_episodes >= 20 and num_episodes < 50 then graphwidth = num_episodes*(12+args.bar_width-barwidth)+98
elseif num_episodes >= 50 and num_episodes < 80 then graphwidth = num_episodes*(11+args.bar_width-barwidth)+98
elseif num_episodes >= 80 then graphwidth = num_episodes*(10+args.bar_width-barwidth)+98
end
end
 
-- Basis parameters
timeline = timeline .. "ImageSize = width:" .. (args.width or graphwidth) .. " height:" .. (args.height or 300) .. "\n"
timeline = timeline .. "PlotArea = left:50 bottom:70 top:35 right:50\n"
timeline = timeline .. "AlignBars = justify\n"
timeline = timeline .. "Colors =\n"
timeline = timeline .. " id:gray value:gray(0.7)\n"
 
-- Colour and legend variables
local season = 1
for season = 1,num_seasons do
local r,g,b = TVRG.hex2rgb(args['color' .. season] or '#006600')
local GraphLegend = season_title .. " " .. season
if args["legend" .. season] then
local legendKey = string.sub(args["legend" .. season], 0, 1)
if type(tonumber(legendKey)) == "number" then
GraphLegend = season_title .. " " .. args["legend" .. season]
else
GraphLegend = args["legend" .. season]
end
end
timeline = timeline .. " id:season" .. season .. " value:rgb("..r..","..g..","..b..") " ..
(args.hidelegend == nil and "legend:" .. string.gsub(string.gsub(GraphLegend, ' ', '_'), "''(.-)''", '%1') or '') .. "\n"
season = season + 1
end
 
-- Determine maximum viewer figure
local maxviewers = -1
Line 152 ⟶ 96:
end
 
-- FurtherBasis parameters, with rounded-up viewer figures as maximum period
timeline['type'] = timeline .. "DateFormat = x.y\n"'stackedrect'
timeline['width'] = (args.width or graphwidth)
timeline = timeline .. "Period = from:0 till:" .. math.ceil(maxviewers) .. "\n"
timeline['height'] = (args.height or 300)
timeline = timeline .. "TimeAxis = orientation:vertical\n"
timeline['legend'] = season_title
timeline = timeline .. "ScaleMajor = gridcolor:gray increment:" .. (args.y_intervals or 10^math.ceil(math.log10(maxviewers)-math.log10(15))) .. " start:0\n"
timeline['colors'] = ''
timeline = timeline .. (args.hidelegend == nil and "Legend = orientation:horizontal\n" or "")
 
timeline['x'] = ''
-- Interval parameter set to prevent overlapping bars
timeline['xType'] = 'string'
local bar = 1
timeline['xAxisAngle'] = '-90'
if args.x_intervals then
timeline['xAxisTitle'] = 'Episode'
timeline = timeline .. "BarData =\n"
for k,v in pairs(args) do
timeline['yGrid'] = 'y'
if string.lower(v) == 'n/a' then v = '' end
if tonumber(k) ~= nil and (tonumber(v) ~= nil or v == '') and (average == 0 or (average == 1 and args[k+1] ~= '-' and args[k+1] ~= nil)) then
-- Color and legend variables
timeline = timeline .. " bar:"..bar.." text:"..((bar == 1 or bar % args.x_intervals == 0) and bar or '&nbsp;').."\n"
for season = 1,num_seasons do
bar = bar + 1
timeline['y'..season..'Title'] = args["legend" .. season] or season
end
timeline['colors'] = timeline['colors'] .. args["color" .. season] .. ','
end
-- 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 = 'U.K.'
else countryDisplayOther = args.country
end
end
timeline['yAxisTitle'] = ((countryDisplayUS or countryDisplayUK or countryDisplayOther) or "") .. ((countryDisplayUS or countryDisplayUK or countryDisplayOther) and " v" or "V") .. "iewers (" .. multiple .. ")\n"
 
-- Plot data
timeline = timeline .. "PlotData =\n"
timeline = timeline .. " width:" .. (args.bar_width or barwidth) .. "\n"
-- Add bars to timeline, one per viewer figure
local bar = 1
local season = 0
local thisseason = 0
local counted_episodes = 0
for k,v in pairs(args) do
Line 185 ⟶ 136:
if tonumber(k) ~= nil then
if v == '-' then
-- Hyphen means new season, so change season colour
season = season + 1
timeline['y'..season] = ''
for ep = 1,counted_episodes do
timeline['y'..season] = timeline['y'..season] .. ','
end
-- Determine highest number of episodescounted_episodes in a season
if thisseason > longestseason then
longestseason = thisseason
Line 194 ⟶ 149:
thisseason = 0
elseif average == 0 or (average == 1 and args[k+1] ~= '-' and args[k+1] ~= nil) then
-- Include white/black bar to act as border
local black_cr = contrast_ratio{args['color' .. season], 'black', ['error'] = 0}
local white_cr = contrast_ratio{'white', args['color' .. season], ['error'] = 0}
local bgrnd_cr = (black_cr > white_cr and 'black' or 'gray')
timeline = timeline .. " mark:(line," .. bgrnd_cr .. ")\n"
timeline = timeline .. " color:" .. bgrnd_cr .. "\n"
timeline = timeline .. " bar:" .. bar .. " width:" .. ((args.bar_width or barwidth)+2) .. " from:start till:" .. (v ~= '' and v or 'start') .. "\n"
-- 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'.. " color:season"] .. (timeline['y'..season] and ',' or '') .. "\n"(v ~= '' and v or 0)
timeline = timeline .. " bar:" .. bar .. " from:start till:" .. (v ~= '' and v or 'start') .. "\n"
-- Increment tracking variables
counted_episodes = counted_episodes + 1
thisseason = thisseason + 1
bar = bar + 1
Line 218 ⟶ 164:
end
-- AxisX labelsaxis variables
for ep = 1,num_episodes do
local countryDisplayUS, countryDisplayUK, countryDisplayOther
timeline['x'] = timeline['x'] .. (timeline['x'] and ',' or '') .. ep
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 = 'U.K.'
else countryDisplayOther = args.country
end
end
timeline = timeline .. "TextData =\n"
timeline = timeline .. " pos:(" .. ((args.width or graphwidth)/2-18) .. ",40) textcolor:black fontsize:M text:Episode\n"
timeline = timeline .. " pos:(10," .. ((args.height or 300)-20) .. ") textcolor:black fontsize:M text:" .. ((countryDisplayUS or countryDisplayUK or countryDisplayOther) or "") .. ((countryDisplayUS or countryDisplayUK or countryDisplayOther) and " v" or "V") .. "iewers (" .. multiple .. ")\n"
-- If there's a title, add it with the viewers caption, else just display the viewers caption by itself
if args.title ~= nil and args.title ~= '' then
root:wikitext("'''''" .. args.title .. "''" .. "&#8202;" .. ": " .. ((countryDisplayUS or countryDisplayUK or countryDisplayOther) or "") .. ((countryDisplayUS or countryDisplayUK or countryDisplayOther) and " v" or "V") .. "iewers per episode (" .. multiple .. ")'''"):css('margin-top', '1em')
root:wikitext("'''''" .. args.title .. "''" .. frame:expandTemplate{ title = 'hsp' } ..
else
": " .. ((countryDisplayUS or countryDisplayUK or countryDisplayOther) or "") .. ((countryDisplayUS or countryDisplayUK or countryDisplayOther) and " v" or "V") .. "iewers per episode (" .. multiple .. ")'''"):css('margin-top', '1em')
root:wikitext("'''Viewers per episode (" .. multiple .. ")'''"):css('margin-top', '1em')
else
end
root:wikitext("'''Viewers per episode (" .. multiple .. ")'''"):css('margin-top', '1em')
root:tag('div'):css('clear','both')
end
 
-- Add timeline to div
if args.no_graph == nil then
root:node(frame:extensionTag(expandTemplate{title='timelineGraph:Chart', args=timeline)})
root:tag('div'):css('clear','both')
end
Line 343 ⟶ 283:
-- Add table to div root and return
root:node(rtable)
root:tag('div'):css('clear','both')
end
local span = mw.html.create( 'span'):wikitext(frame:expandTemplate{title='Citation needed'})
span:css( 'color', 'red' )
:wikitext(frame:expandTemplate{ title='dummy reference', args={ '[[Wikipedia:Citation needed|citation needed]]', txtital = 'y' } })
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