Module:Chart: Difference between revisions

Content deleted Content added
fixed wrapping problem
better xtick alignment
Line 35:
local defColors = require "Module:Plotter/DefaultColors"
local hideGroupLegends
local optint
 
local function nulOrWhitespace( s )
Line 192 ⟶ 193:
 
function barChart( frame )
local res = {}
local args = frame.args -- can be changed to frame:getParent().args
local values, xlegends, colors, tooltips, yscales, yscalesneg = {}, {}, {}, {} ,{}, {}, {}, {}
Line 202 ⟶ 203:
local numGroups, numValues
local scaleWidth
table.insert( res, frame:extensionTag{ name = 'templatestyles', args = { src = 'TemplateStyles sandbox/Ita140188/styles.css'} })
 
function createGroupList( tab, legends, cols )
local defColors = require "Module:Plotter/DefaultColors"
localif #legends > 1 and not hideGroupLegends then
table.insert( listtab, mw.text.tag( 'lidiv', {} style = string.format( "width:%spx;", spanchartWidth ) } ) )
local optint
local list = ){}
local spanStyle = "padding:0 1em;background-color:%s;border:1px solid %s;margin-right:1em;-webkit-print-color-adjust:exact;"
local function nulOrWhitespace( s )
returnfor notgi s= or1, mw.text.trim( s ) ==#legends ''do
local span = mw.text.tag( 'span', { style = string.format( spanStyle, cols[gi], cols[gi] ) }, ' ' ) .. ' '.. legends[gi]
end
table.insert( tablist, mw.text.tag( '</div>li', {}, span ) )
end
table.insert( res, frame:extensionTag{ name = 'templatestyles', args = { src = 'TemplateStyles sandbox/Ita140188/styles.css'} })
table.insert( tab,
 
mw.text.tag( 'ul',
function createGroupList( tab, legends, cols )
{style="width:100%;list-style:none;-webkit-column-width:12em;-moz-column-width:12em;column-width:12em;"},
if #legends > 1 and not hideGroupLegends then
table.insert( tab, mw.text.tag( 'div', { style = stringtable.formatconcat( "width:%spx;"list, chartWidth ) } )'\n' )
local list = {})
end)
local spanStyle = "padding:0 1em;background-color:%s;border:1px solid %s;margin-right:1em;-webkit-print-color-adjust:exact;"
table.insert( for gi = 1tab, #legends'</div>' do)
end
local span = mw.text.tag( 'span', { style = string.format( spanStyle, cols[gi], cols[gi] ) }, '&nbsp;' ) .. ' '.. legends[gi]
end
table.insert( list, mw.text.tag( 'li', {}, span ) )
end
table.insert( tab,
mw.text.tag( 'ul',
{style="width:100%;list-style:none;-webkit-column-width:12em;-moz-column-width:12em;column-width:12em;"},
table.concat( list, '\n' )
)
)
table.insert( tab, '</div>' )
end
end
 
 
Line 335 ⟶ 328:
local sumneg = math.min( unpack( sums ) )
for i = 1, #values do
yscales[i] = sum
yscalesneg[i] = 0 -- -sumneg
end
else
for i, group in ipairs( values ) do
yscales[i] = math.max( unpack( group ) )
yscalesneg[i] = -math.min( unpack( group ) )
end
end
Line 347 ⟶ 340:
for i, scale in ipairs( yscalesneg ) do yscalesneg[i] = roundup( scale * 0.9999 ) end
if not scalePerGroup then for i = 1, #values do
yscales[i] = math.max( unpack( yscales ) )
yscalesneg[i] = math.max ( 0 , math.max( unpack( yscalesneg ) ) )
end end
end
Line 361 ⟶ 354:
 
function calcHeights( gi, i, val )
local barHeight = 0
local top, base = 0, 0
if tonumber( val ) >= 0 then
barHeight = math.floor( val / ( yscales[gi] + yscalesneg[gi] ) * chartHeight + 0.5 ) -- add half to make it "round" instead of "trunc"
top, base = chartHeight - barHeight - yscalesneg[gi] / ( yscales[gi] + yscalesneg[gi] ) * chartHeight , 0
-- barHeight = math.floor( val / yscales[gi] * chartHeight + 0.5 ) -- add half to make it "round" instead of "trunc"
-- top, base = chartHeight - barHeight, 0
else
barHeight = math.floor( -val / ( yscales[gi] + yscalesneg[gi] ) * chartHeight + 0.5 ) -- add half to make it "round" instead of "trunc"
top, base = chartHeight - yscales[gi] / ( yscales[gi] + yscalesneg[gi] ) * chartHeight, 0
end
if stack then
local rawbase = 0
Line 392 ⟶ 385:
 
function groupBounds( i )
local setWidth = math.floor( chartWidth / numValues )
-- local setOffset = ( i - 1 ) * setWidth
local setOffset = ( i - 1 ) * setWidth
Line 401 ⟶ 394:
local setOffset, setWidth = groupBounds( i )
if stack or numGroups == 1 then
local barWidth = math.min( 38, math.floor( 0.8 * setWidth ) )
return setOffset + (setWidth - barWidth) / 2, barWidth
end
setWidth = 0.85 * setWidth
local barWidth = math.floor( 0.75 * setWidth / numGroups )
local left = setOffset + math.floor( ( gi - 1 ) / numGroups * setWidth )
return left, barWidth
end
Line 430 ⟶ 423:
 
function drawYScale()
function drawSingle( gi, color, single )
local yscale = yscales[gi]
Line 450 ⟶ 443:
local divNotch = mw.text.tag( 'div', { style = string.format( valStyleStrNotch, y, color ) }, '&nbsp;' )
table.insert( res, divCntnr )
if val~=math.floor(val) and optint then
else
table.insert( res, divNotch )
table.insert( res, divValue )
end
table.insert( res, '</div>' )
table.insert( res, '<div style="clear:right;display:block"></div>' )
Line 495 ⟶ 488:
for i = 1, numValues do
if not nulOrWhitespace( xlegends[i] ) then
--table.insert( res, mw.text.tag( 'div', { style = string.format( tickDivStyleFormat, setWidth/2, offsetleft+setWidth/2 ) }, xlegends[i] or '' ) )
table.insert( res, mw.text.tag( 'div', { style = string.format( legendDivStyleFormat, setWidth, offsetleft ) }, xlegends[i] or '' ) )
offsetleft=0;
else
offsetleft=offsetleft+setWidth;
end
Line 508 ⟶ 501:
end
 
function drawXticks()
local setOffset, setWidth
local tickDivStyleFormat = "display:block;float:left;position:relative;height:5px;vertical-align:top;border-left:1px solid black;width:%spx;text-align:center;margin:0px 0px 0px %spx;"
Line 515 ⟶ 508:
for i = 1, numValues do
if not nulOrWhitespace( xlegends[i] ) then
table.insert( res, mw.text.tag( 'div', { style = string.format( tickDivStyleFormat, math.floor(setWidth/2)-1, math.floor(offsetleft+setWidth/2) ) }, '' ) )
offsetleft=0;
else
offsetleft=offsetleft+setWidth;
end
end
Line 527 ⟶ 520:
table.insert( res, mw.text.tag( 'div', { style = string.format("position:relative;padding:1em 0em 1em 0em;") } ) ) -- container div
table.insert( res, mw.text.tag( 'div', { style = string.format("position:relative;white-space:nowrap;" ) } ) ) -- container div
table.insert( res, mw.text.tag( 'div', { style = string.format("position:relative;height:%spx;display:inline-block;text-align:right;vertical-align:top;", chartHeight ) } ) )
drawYScale()
table.insert( res, '</div><div style="position:relative;display:inline-block">' )