Module:Television ratings graph: Difference between revisions

Content deleted Content added
protect against null value
Update from sandbox
Line 36:
-- Variables
local timeline = {}''
local longestseason = -1
local average = args.average and 1 or 0
Line 100:
 
-- Basis parameters
timeline = timeline .. "\nImageSize = width:auto height:"..(args.height or 500).." barincrement:"..(barwidth+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
args["color" .. season] = args["color" .. season] or '#CCCCFF';
hex = args["color" .. season]:gsub("#","")
if num_seasons > 1 then
rgbR = tonumber("0x"..hex:sub(1,2))/256
timeline['y'..season..'Title'] = (args["legend" .. season] or season) .. " " -- The space after this is not a regular space, it is a copy-pasted non-breaking space so the graph registers season names that are numbers (such as 1984 for American Horror Story) as a string, not a number; if it registers as a number, it will register as the first season. Do not remove/change it.
rgbG = tonumber("0x"..hex:sub(3,4))/256
elseif timeline['legend'] then
rgbB = tonumber("0x"..hex:sub(5,6))/256
timeline['legend'] = nil
timeline = timeline .. "\n id:season"..season.." value:rgb("..rgbR..", "..rgbG..", "..rgbB..") legend:Season_"..season.."\n"
end
timeline['colors'] = timeline['colors'] .. args["color" .. season] .. ','
end
timeline = timeline .. "\n id:bars value:gray(0.95)"
timeline = timeline .. "\nLegend = orientation:vertical position:right\n"
-- Axis labels
timeline = timeline .. "\nBackgroundColors = bars:bars\n"
local countryDisplayUS, countryDisplayUK, countryDisplayOther
timeline = timeline .. "\nScaleMajor = unit:year increment:1 start:1\n"
if args.country ~= nil and args.country ~= '' then
timeline = timeline .. "\nScaleMinor = unit:year increment:1 start:1\n"
if args.country == "U.S." or args.country == "US" or args.country == "United States" then countryDisplayUS = 'U.S.'
timeline = timeline .. "\nBarData=\n"
elseif args.country == "U.K." or args.country == "UK" or args.country == "United Kingdom" then countryDisplayUK = 'UK'
for epepisode = 1,num_episodes do
else countryDisplayOther = args.country
timeline = timeline .. "\n bar:"..episode.." text:"..episode.."\n"
end
end
timeline['yAxisTitle'] = ((countryDisplayUS or countryDisplayUK or countryDisplayOther) or "") .. ((countryDisplayUS or countryDisplayUK or countryDisplayOther) and " v" or "V") .. "iewers (" .. multiple .. ")\n"
 
-- Add bars to timeline, one per viewer figure
local bar = 1
Line 139 ⟶ 133:
local thisseason = 0
local counted_episodes = 0
timeline = timeline .. "\nPlotData=\n"
timeline = timeline .. "\n width:"..barwidth.." textcolor:black align:left anchor:from shift:(10,-4)\n"
for k,v in ipairs(args) do
Line 146 ⟶ 143:
-- Hyphen means new season
season = season + 1
timeline['y'..season] = ''
for ep = 1,counted_episodes do
timeline['y'..season] = timeline['y'..season] .. ','
end
 
-- Determine highest number of counted_episodes in a season
Line 158 ⟶ 151:
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] or"\n '')bar:".. (timeline['y'bar..season]" and ',' or '')from:0 till:".. (v ~= '' and v or 0).." color:season"..season.."\n"
-- Increment tracking variables
Line 171 ⟶ 164:
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 183 ⟶ 179:
end
root:tag('div'):css('clear','both')
 
-- Add timeline to div
if args.no_graph == nil then
timelineBase = frame:preprocess("<timeline>"..timeline.."</timeline>")
timeline[1] = ''
root:node(timelineBase)
-- This reduces the [[WP:PEIS]]. [[Module:Graph:Chart]] pretends to be
-- [[Template:Graph:Chart]], and we pass it a modified frame with the
-- arguments we would have sent to [[Template:Graph:Chart]].
local oldArgs = frame.args
frame.args = timeline
root:wikitext(require('Module:Graph:Chart')[''](frame))
frame.args = oldArgs
root:tag('div'):css('clear','both')
end