Module:Climate chart
Appearance
![]() | This module is rated as ready for general use. It has reached a mature form and is thought to be relatively bug-free and ready for use wherever appropriate. It is ready to mention on help pages and other Wikipedia resources as an option for new users to learn. To reduce server load and bad output, it should be improved by sandbox testing rather than repeated trial-and-error editing. |
![]() | This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing. |
![]() | This module depends on the following other modules: |
![]() | This module uses TemplateStyles: |
Implements {{climate chart}}. At some point.
Usage
{{#invoke:Climate chart|function_name}}
local p = {}
-- from https://lua-users.org/wiki/SimpleRound
local function round(num, decimal_places)
local mult = 10^(decimal_places or 0)
return math.floor(num * mult + 0.5) / mult
end
local function c_to_f(temp_in_c)
return temp_in_c * 1.8 + 32
end
-- this implements the fahrenheit subtemplate, will need to look into what the
-- celsius template is doing
-- unit_system currently unused accordingly
local function month_column(low_temp, high_temp, precipitation, max_precipitation, unit_system)
local precipitation = precipitation or 80
local base_precipitation = max_precipitation or 500
local precipitation_scale = math.max(1, base_precipitation / 750) -- 750 mm is the maximum for height
local precipitation_bar_height = precipitation / 50 / precipitation_scale -- 50 is a magic constant
local precipitation_in_inches = precipitation / 25.4
local decimal_places = 0
if precipitation_in_inches < 10 then decimal_places = 1 end
local rounded_precipitation_inches = round(precipitation_in_inches, decimal_places)
local low_temp = low_temp or 10
local high_temp = high_temp or 20
local low_temp_shift_for_bar = low_temp / 5 + 8 -- magic numbers
local temp_bar_height = (high_temp - low_temp) / 5 -- magic numbers
local high_temp_shift = high_temp / 5 + 8
local low_temp_shift = low_temp / 5 + 6.5
local high_temp_f = c_to_f(high_temp)
local rounded_high_temp_f = round(high_temp_f, 0)
local high_temp_sign = ''
if rounded_high_temp_f < 0 then high_temp_sign = '−' end
local abs_high_temp = math.abs(rounded_high_temp_f)
local low_temp_f = c_to_f(low_temp)
local rounded_low_temp_f = round(low_temp_f, 0)
local low_temp_sign = ''
if rounded_low_temp_f < 0 then low_temp_sign = '−' end
local abs_low_temp = math.abs(rounded_low_temp_f)
local column = mw.html.create('div')
column:addClass('climate-chart-column')
:tag('div')
:addClass('climate-chart-column-spacer')
:wikitext(' ')
:done()
:tag('div')
:addClass('climate-chart-column-precip-bar')
:wikitext(' ')
:css('height', precipitation_bar_height .. 'em')
:css('print-color-adjust', 'exact') -- css sanitizer doesn't accept yet
:done()
:tag('div')
:addClass('climiate-chart-column-precip')
:tag('span')
:wikitext(rounded_precipitation_inches)
:done()
:done()
:tag('div')
:addClass('climate-chart-column-spacer2')
:wikitext(' ')
:done()
:tag('div')
:addClass('climate-chart-column-temp-bar')
:wikitext(' ')
:css('bottom', low_temp_shift_for_bar .. 'em' )
:css('height', temp_bar_height .. 'em')
:css('print-color-adjust', 'exact') -- css sanitizer doesn't accept yet
:done()
:tag('div')
:addClass('climate-chart-column-high-temp')
:css('bottom', high_temp_shift .. 'em')
:tag('span')
:wikitext(high_temp_sign .. abs_high_temp)
:done()
:done()
:tag('div')
:addClass('climate-chart-column-low-temp')
:css('bottom', low_temp_shift .. 'em')
:tag('span')
:wikitext(low_temp_sign .. abs_low_temp)
:done()
:done()
:done()
return column
end
local function month_header(letter)
ret = mw.html.create('div')
ret:cssText('width:1.5em;text-align:center;')
:wikitext(letter)
return ret
end
local function arg_or_default(args, from_arg, default)
local arg = args[from_arg]
local trimmed_arg = mw.text.trim(arg)
if arg and trimmed_arg ~= '' then
return trimmed_arg
else
return default
end
end
local function insane_table(args)
end
-- TODO moving the wikitext for the bar here is not very friendly for i18n :(
local function explain_bar(bar_type, text)
local ret = mw.html.create('p')
ret:addClass('climate-change-explain-bar-' .. bar_type)
:tag('span')
:wikitext('█')
:done()
:wikitext(text)
:done()
return ret
end
local function explain_temperature(unit)
return explain_bar(
'temp',
string.format('Average max. and min. temperature in %s', unit)
)
end
local function explain_precipitation(unit)
return explain_bar(
'precip',
string.format('Precipitation totals in %s', unit)
)
end
function p._main(args)
local width = arg_or_default(args, 'width', nil)
local float = arg_or_default(args, 'float', nil)
local clear = arg_or_default(args, 'clear', nil) or float
local float_class = nil
if float then
if float == 'right' then
float_class = 'climate-chart-right'
elseif float == 'left' then
float_class = 'climate-chart-left'
end
end
local climate_chart = mw.html.create('div')
climate_chart:addClass('climate-chart')
:css('width', width)
:css('float', float)
:css('clear', clear)
local a_table = insane_table(args)
local b_table = insane_table(different_args)
local fahrenheit_explanation = explain_temperature('°F')
local celsius_explanation = explain_temperature('°C')
local mm_explanation = explain_precipitation('mm')
local in_explanation = explain_precipitation('in')
end
function p.main(frame)
return p._main(frame:getParent().args)
end
return p