Module:SVG Chart/init

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Lua
CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules

Documentation for this module may be created at Module:SVG Chart/init/doc

Code

-- require ("Module:Sandbox/Con-struct_(t)/Test1")
--require ("Module:Sandbox/Con-struct_(t)/Test1/standard")

---- predefined values ----
local PATTERNSCIENTIFIC = "[%+%-]?%d+%.?%d*"   --  %d - digits, following signs: + - >=1 repetitions, * - >=0 repetitions, % - escape for ., ? - optional, http://www.lua.org/pil/20.2.html, todo: add: E, e
--local PATTERNNUMBER     = "[0-9eE%+%-%.]"
local PATTERNNUMBER     = "[%w%p]+"
--local PATTERNNUMBER     = "[%d%.%-%+]+"
--local PATTERNSCIENTIFIC = "[%+%-]?%d+%.?%d*([eE][%+%-]?%d+)?"   -- todo: add: E, e 


---- Size () general, size of the diagram ----
function Size (dXMin, dXMax, dYMin, dYMax, dStretchX, dStretchY)
    local dDifX = absDif (dXMax, dXMin) * dStretchX / 100
    local dDifY = absDif (dYMax, dYMin) * dStretchY / 100
    
    gsDebug = gsDebug .. '  init.Size (): ' .. max (dDifX, dDifY) .. ', dXMin: ' .. dXMin .. ', dXMax: ' .. dXMax .. ', dYMin: ' .. dYMin .. ', dYMax: ' .. dYMax .. ', dStretchX: ' .. dStretchX .. ', dStretchY: ' .. dStretchY .. ', dDifX: ' .. dDifX .. ', dDifY: ' .. dDifY .. '\n'
    return max (dDifX, dDifY)
end


---- font size, px: ----
--[[
1: GeneralFontSize, px
2: Font size factor, %
  ]]
function FontSize (GeneralFontSizePx, FontSizeFactor100)
--   	if iserror () then return 777 end
    return round (GeneralFontSizePx * FontSizeFactor100 / 100,
        (4 - string.len (round (GeneralFontSizePx, 0))))  -- max 2 relevant digits for font size
end

---- line size for graphs, px ----
--[[ GraphStretchWidth and GraphStretchHeight are only used while missing the "non-scaling-stroke" feature in rsvg
  the line width is reduced in consideration of of stretching by the "scale" command
  in the other direction is the line thickness to imitate by arrange of several lines ]]
function LineWidth (ChartSize, LineWidth, GraphStretchWidth, GraphStretchHeight)
    if not ChartSize          then ChartSize          = 100 end  -- px
    if not LineWidth          then LineWidth          = 100 end  -- %
    if not GraphStretchWidth  then GraphStretchWidth  = 100 end  -- %
    if not GraphStretchHeight then GraphStretchHeight = 100 end  -- %
  
    local temp
    if GraphStretchWidth < GraphStretchHeight then
        temp = GraphStretchWidth / GraphStretchHeight else 
        temp = GraphStretchHeight / GraphStretchWidth end 
                
    return round (0.007                                              -- general relation of the line thickness
        * ChartSize
        * LineWidth / 100
        * temp
        , 5 - string.len (round (ChartSize, 0)))  -- only two relevant digits for font size
end

--[[ValueProcess1 (), process the string of one chart value --
    return: max, min, max len, max dot position, max number of digits
    ]]
local function ValueProcess1 (sValue, dMin, dMax, iDotPos, iDigits, iFirst, iLast)
    local dValue, iTmp

    dValue = tonumber (sValue)
    
    if dMin > dValue then dMin = dValue end
    if dMax < dValue then dMax = dValue end

    iTmp = lenInteger (sValue)
    if iDotPos < iTmp then iDotPos = iTmp end
    
    iTmp = lenDecimal (sValue)
    if iDigits < iTmp then iDigits = iTmp end
    
    return dMin, dMax, iDotPos, iDigits
end

--[[ ChartValues (),  process the whole input string of chart values --
	sValues: the user defined string list of pairs of values
    return: table x, table y, min x, max x, min y, max y, max x len, max y len, max x integer len, max y integer len, first x, last x, number of pairs
    ]]
function initChartValues (sValues, dXValueFactor, dYValueFactor, dXMinFix, dYMinFix)
    local dXMin        = MAXVALUE;  local dXMax        = MINVALUE
    local dYMin        = MAXVALUE;  local dYMax        = MINVALUE
    local iXLen        = 0;         local iYLen        = 0
    local iXDigits     = 0;         local iYDigits     = 0
    local iXIntegerLen = 0;         local iYIntegerLen = 0
    local iNumber      = 0;
    local tsX = {};             local tsY = {}
    local iFirst, iLast, sDateValue, sValue, dValue, sXFirst, sXLast

    iLast = 0
    gsDebug = gsDebug .. "ChartValues (): \n"
    while true do
      	-- 1st: x value    	
        iFirst, iLast = string.find (sValues, PATTERNNUMBER, iLast + 1)
        gsDebug = gsDebug .. 'iFirst: ' .. tostring(iFirst) .. ', iLast: ' .. tostring(iLast) .. '\n'
        if iFirst == nil then break end
        sValue = string.sub (sValues, iFirst, iLast)
        
       	sDateValue = datevalue (sValue)
  	    if sDateValue then             -- is a date value
  	    	sValue = sDateValue
        end

        if errorcheck (tonumber (sValue) == nil , 'init', 'ChartValues', '"' .. tostring(sValue) .. '" is not a valid value in "Graph_Values"' ) then return end
        if not sXFirst then 
            if not dXMinFix then 
                sXFirst = sValue
            else if tonumber (sValue) >= tonumber (dXMinFix) then    -- cut at the left side
            	sXFirst = sValue
            end end
        end
        sXLast = sValue
        table.insert (tsX, sValue * dXValueFactor)
       
        dXMin, dXMax, iXIntegerLen, iXDigits = ValueProcess1 (sValue, dXMin, dXMax, iXIntegerLen, iXDigits, iFirst, iLast)
        
        -- 2nd: y value
        iFirst, iLast = string.find (sValues, PATTERNSCIENTIFIC, iLast + 1)
        if iFirst == nil then break end
        iNumber = iNumber + 1
        sValue = string.sub (sValues, iFirst, iLast)
        table.insert (tsY, sValue * dYValueFactor)
        
        dYMin, dYMax, iYIntegerLen, iYDigits = ValueProcess1 (sValue, dYMin, dYMax, iYIntegerLen, iYDigits, iFirst, iLast)
    end

    return tsX, tsY, dXMin, dXMax, dYMin, dYMax, iXIntegerLen+iXDigits, iYIntegerLen+iYDigits, iXIntegerLen, iYIntegerLen, sXFirst, sXLast, iNumber
end