Module:COVID-19 data
Appearance
From Wikipedia, the free encyclopedia
Main article: COVID-19 pandemic
Warning This Lua module is used on approximately 230 pages and changes may be widely noticed. Test changes in the module's /sandbox or /testcases subpages, or in your own module sandbox. Consider discussing changes on the talk page before implementing them.
This module and these templates show data on the COVID-19 pandemic.
Core components
- Module:COVID-19 data (module)
{{#invoke:COVID-19 data|main|columns=columns|sort=column to sort by}}(table)
- Template:COVID-19 data (main table)
{{COVID-19 data}}
- Template:COVID-19 data/text (plain text numbers)
{{COVID-19 data/text|location code|type}}
- Template:COVID-19 data/date (date last updated)
{{COVID-19 data/date|format=format}}
- Template:COVID-19 data/cite (citation for data)
{{COVID-19 data/cite}}
- Template:COVID-19 data/data (JSON data)
Tables
The body of a table can be made by using the main() function:
{{#invoke:COVID-19 data|main|columns=columns|sort=column to sort by}}
This only generates table contents (the body). Headers and titles can be customised independent of the module, to allow for styling.
Please use the following reference (preferably in the table title), to avoid duplicate references and have automatically updating access date and authors:
<ref name="Template:COVID-19 data">{{COVID-19 data/cite}}</ref>
| Page | C? | D? | VT? | VD? | VF? | Other? | Col? | Notes |
|---|---|---|---|---|---|---|---|---|
| Template:COVID-19 data | Main table | |||||||
| Template:COVID-19 pandemic data | Other "hub" template | |||||||
| Template:COVID-19 pandemic death rates | Deaths/million | |||||||
| Template:COVID-19 pandemic death rates by country | Larger, has row numbers | |||||||
| Template:COVID-19 vaccination data | % vaccinated | |||||||
| Template:COVID-19 vaccination data in Africa | ||||||||
| Template:COVID-19 vaccination data in North America |
References
This section may be blank, because not all pages with this documentation have references.
The above documentation is transcluded from Module:COVID-19 data/doc. (edit | history)
Editors can experiment in this module's sandbox (edit | diff) and testcases (create) pages.
Subpages of this module.
Editors can experiment in this module's sandbox (edit | diff) and testcases (create) pages.
Subpages of this module.
localp={} localdata_title=mw.title.new( 'Template:COVID-19 data/data' ) localdata_content=data_title:getContent() localdata=mw.text.jsonDecode(data_content) localfunctionformat_num(number) ifnotnumberthen return'' end local_,_,minus,int,_=tostring(number):find('([-]?)(%d+)([.]?%d*)') int=int:reverse():gsub("(%d%d%d)","%1,") int=int:reverse():gsub("^,","") int=minus..int returnint end functionp.main() localout='' localdata_s={} localhas_notelist=false localcount_row=0 localcount_col=0 -- Build new table locallocations={} ifmw.getCurrentFrame().args['locations']then forlocationinstring.gmatch( mw.getCurrentFrame().args['locations'], '([^,]+)' )do locations[location]=true end else locations=nil end forkey,valueinpairs(data)do localindex ifmw.getCurrentFrame().args['sort']then ifvalue[mw.getCurrentFrame().args['sort']]then index=value[mw.getCurrentFrame().args['sort']] else index=0 end else ifvalue.casesthen index=value.cases elseifvalue.deathsthen index=value.deaths elseifvalue.vaccine_dosesthen index=value.vaccine_doses elseifvalue.total_vaccinatedthen index=value.total_vaccinated elseifvalue.fully_vaccinatedthen index=value.fully_vaccinated elseindex=0 end end if(notlocations)orlocations[key]then data_s[#data_s+1]=value data_s[#data_s]['_index']=index data_s[#data_s]['_code']=key end end -- Sort the new table table.sort( data_s, function(x,y) ifx._code=='XW'then returntrue elseify._code=='XW'then returnfalse else return(x._index>y._index) end end ) -- Get columns localcolumns={ cases=false, deaths=false, total_vaccinated=false, vaccine_doses=false, fully_vaccinated=false, percent_vaccinated=false, percent_fully_vaccinated=false, deaths_per_million=false, population=false } localcolumns_index={} ifmw.getCurrentFrame().args['columns']then forcolumninstring.gmatch( mw.getCurrentFrame().args['columns'], '([^,]+)' )do columns[column]=true columns_index[#columns_index+1]=column end else columns={ cases=true, deaths=true, total_vaccinated=true, vaccine_doses=true, fully_vaccinated=true, percent_vaccinated=false, percent_fully_vaccinated=false, deaths_per_million=false, population=false } columns_index={ 'cases', 'deaths', 'total_vaccinated', 'vaccine_doses', 'fully_vaccinated', 'percent_vaccinated', 'percent_fully_vaccinated', 'deaths_per_million', 'population' } end -- Check if should show note_vaccination localshow_note_vaccination=false ifcolumns['total_vaccinated']orcolumns['vaccine_doses']orcolumns['fully_vaccinated']orcolumns['percent_vaccinated']orcolumns['percent_fully_vaccinated']then show_note_vaccination=true end -- Generate wikitext content for_,rowinipairs(data_s)do -- Test for empty rows localhas_data=false for_,columninpairs(columns_index)do ifcolumns[column]androw[column]then has_data=true end end ifhas_datathen-- Only add row if it has data count_row=count_row+1 out=out..'\n|-' ifrow._code=='XW'then out=out..'class="sorttop static-row-header"' end -- Add the flag ifnotmw.getCurrentFrame().args['noflag']then ifcount_row==1then count_col=count_col+1 end out=out..'\n| style="text-align: center;" data-sort-value="'..row.name..'" | ' ifrow._code=='XW'then out=out.. '[[File:OOjs UI icon globe.svg|16px|alt=|link=]]' else flag_params={row.name} -- So that it's not too large ifrow.name=='New Caledonia'then flag_params[2]='merged' end out=out.. mw.getCurrentFrame():expandTemplate{ title='Flag icon', args=flag_params } end end -- Add country name ifcount_row==1then count_col=count_col+1 end ifmw.getCurrentFrame().args['noflag']then out=out..'\n! scope="row" style="background-color: inherit;" data-sort-value="'..row.name..'" | ' else out=out..'\n! scope="row" style="background-color: inherit;" |' end ifrow._code=='XW'then out=out..'[[COVID-19 pandemic|'..row.name..']]' elseifrow._code=='GE'then out=out.. '[[COVID-19 pandemic in Georgia (country)'.. '|'..row.name..']]' elseifrow._code=='IM'then out=out.. '[[COVID-19 pandemic in the '..row.name.. '|'..row.name..']]' else out=out.. '[[COVID-19 pandemic in '..row.name.. '|'..row.name..']]' end -- Add notes ifrow.notethen has_notelist=true out=out.. mw.getCurrentFrame():expandTemplate{ title='Efn', args={row.note} } end ifrow.note_vaccinationandshow_note_vaccinationthen has_notelist=true out=out.. mw.getCurrentFrame():expandTemplate{ title='Efn', args={'Vaccination note: '..row.note_vaccination} } end -- Fill out columns for_,columninipairs(columns_index)do ifcolumns[column]then ifcount_row==1then count_col=count_col+1 end ifrow[column]then out=out..'\n| data-sort-value='.. tostring(row[column]).. '|' ifcolumn:find('^percent')then out=out..string.format('%.2f',row[column])..'%' else out=out..format_num(row[column]) end else out=out..'\n| data-sort-value=0 | —' end end end end end ifhas_notelistthen out=out..'\n|- class="sortbottom static-row-header" style="text-align: left;"'.. '\n| colspan="'..count_col..'" style="width: 0;" |'.. mw.getCurrentFrame():expandTemplate{ title='Notelist' } end returnout end functionp.vac() localout='' localdata_s={} localhas_country_num_doses=false localhas_country_num_fully=false localcount_row=0 localcount_col=0 -- Build new table forkey,valueinpairs(data)do ifvalue.total_vaccinatedthen data_s[#data_s+1]=value data_s[#data_s]['index']=value.total_vaccinated elseifvalue.vaccine_dosesthen data_s[#data_s+1]=value data_s[#data_s]['index']=value.vaccine_doses elseifvalue.fully_vaccinatedthen data_s[#data_s+1]=value data_s[#data_s]['index']=value.fully_vaccinated end end -- Sort the new table table.sort( data_s, function(x,y) return(x.index>y.index) end ) -- Generate wikitext content for_,rowinpairs(data_s)do count_row=count_row+1 out=out..'\n|-' -- Add the flag ifcount_row==1then count_col=count_col+2 end ifrow.name=='World'then out=out.. 'class="sorttop static-row-header"'.. '\n| style="text-align: center;" data-sort-value="'..row.name..'" | '.. '[[File:OOjs UI icon globe.svg|16px|alt=|link=]]'.. '\n! scope="row" style="background-color: inherit;" data-sort-value="'..row.name..'" | '.. '[[Deployment of COVID-19 vaccines|World]]' else flag_params={row.name} -- So that it's not too large ifrow.name=='New Caledonia'then flag_params[2]='merged' end out=out.. '\n| style="text-align: center;" data-sort-value="'..row.name..'" | '.. mw.getCurrentFrame():expandTemplate{ title='Flagicon', args=flag_params }.. '\n! scope="row" style="background-color: inherit;" data-sort-value="'..row.name..'" | '.. '[[COVID-19 vaccination in '..row.name..'|'..row.name..']]' end -- Add note ifrow.note_vaccinationthen out=out.. mw.getCurrentFrame():expandTemplate{ title='Efn', args={row.note_vaccination} } end -- Add the number ifcount_row==1then count_col=count_col+1 end ifrow.total_vaccinatedthen out=out..'\n| data-sort-value='.. tostring(row.total_vaccinated).. '|'..format_num(row.total_vaccinated) elseifrow.vaccine_dosesthen has_country_num_doses=true out=out..'\n| data-sort-value='.. tostring(row.vaccine_doses).. '|'.. mw.getCurrentFrame():expandTemplate{ title='Font color', args={'darkred',format_num(row.vaccine_doses)} }.. mw.getCurrentFrame():expandTemplate{ title='Efn', args={name='country_num_doses'} } elseifrow.fully_vaccinatedthen has_country_num_fully=true out=out..'\n| data-sort-value='.. tostring(row.fully_vaccinated).. '|'.. mw.getCurrentFrame():expandTemplate{ title='Font color', args={'darkorange',format_num(row.fully_vaccinated)} }.. mw.getCurrentFrame():expandTemplate{ title='Efn', args={name='country_num_fully'} } end -- Add the percentage ifcount_row==1then count_col=count_col+1 end ifrow.percent_vaccinatedthen out=out..'\n| data-sort-value='.. tostring(row.percent_vaccinated).. '|'..string.format("%.1f",row.percent_vaccinated)..'%' else out=out..'\n| data-sort-value=0 | —' end end ifhas_country_num_dosesorhas_country_num_fullythen notelist_refs='' ifhas_country_num_dosesthen notelist_refs=notelist_refs.. mw.getCurrentFrame():expandTemplate{ title='Efn', args={ name='country_num_doses', 'This country\'s data are the '.. mw.getCurrentFrame():expandTemplate{ title='Font color', args={ 'darkred', 'number of vaccine doses administered' } }.. ', not the first dose only.' } } end ifhas_country_num_fullythen notelist_refs=notelist_refs.. mw.getCurrentFrame():expandTemplate{ title='Efn', args={ name='country_num_fully', 'This country\'s data are the '.. mw.getCurrentFrame():expandTemplate{ title='Font color', args={ 'darkorange', 'number of people fully vaccinated' } }.. ', not the number of people '.. 'who have received at least one dose.' } } end out=out..'\n|- class="sortbottom static-row-header" style="text-align: left;"'.. '\n| colspan="'..count_col..'" style="width: 0;" |'.. mw.getCurrentFrame():expandTemplate{ title='Notelist', args={refs=notelist_refs} } end returnout end functionp.text() locallocation=mw.getCurrentFrame().args['location'] localcolumn=mw.getCurrentFrame().args['column'] returndata[location][column] end returnp