Jump to content

Module:Climate chart

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Izno (talk | contribs) at 01:21, 5 June 2023 (add month header). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

-- 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 = '&minus;' 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 = '&minus;' 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('&nbsp;')
		:done()
	:tag('div')
		:addClass('climate-chart-column-precip-bar')
		:wikitext('&nbsp;')
		: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('&nbsp;')
		:done()
	:tag('div')
		:addClass('climate-chart-column-temp-bar')
		:wikitext('&nbsp;')
		: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