Module:Sandbox/Qwerfjkl/graph

.mw-parser-output .documentation,.mw-parser-output .documentation-metadata{border:1px solid var(--border-color-base,#a2a9b1);background-color:#ecfcf4;color:inhe

Module:Sandbox/Qwerfjkl/graph
local p = {}
local canvas = require('Module:Sandbox/Bawolff/canvas')

local function convertToPixelHoriz(value, minValue, maxValue, minPixel, maxPixel)
    return ((value - minValue) / (maxValue - minValue)) * (maxPixel - minPixel) + minPixel
end

local function convertToPixelVert(value, minValue, maxValue, minPixel, maxPixel)
    return maxPixel - ((value - minValue) / (maxValue - minValue)) * (maxPixel - minPixel) - minPixel
end

local function writeVertical(ctx, text, x, y)
    ctx:save()
    local height = ctx:getContextAttributes()['height']
    ctx:translate(0, height)
    ctx:rotate(-math.pi / 2)
    ctx.textBaseline = 'top'
    ctx:fillText(text, y, x)
    ctx:restore()
end

function p.draw(frame)
    local width = tonumber(frame.args.width) or 350
    local height = tonumber(frame.args.height) or 250
    local ctx = canvas.getContext('2d', { width = width, height = height })

    local vertGutter = tonumber(frame.args.vertGutter) or 40
    local horizGutter = tonumber(frame.args.horizGutter) or 25

    ctx:moveTo(vertGutter, 0)
    ctx:lineTo(vertGutter, height - horizGutter)
    ctx:lineTo(width, height - horizGutter)

    -- Load JSON data
    local data = mw.loadJsonData(frame.args.data)
    assert(data ~= nil)

    -- Find min/max values for date and value
    local minDate, maxDate = math.huge, -math.huge
    local maxValue = -math.huge
    for _, entry in ipairs(data) do
        local date = tonumber(entry.date)
        local value = tonumber(entry.value)
        if date < minDate then minDate = date end
        if date > maxDate then maxDate = date end
        if value > maxValue then maxValue = value end
    end

    -- Draw axes labels and grid
    ctx.fillStyle = 'var(--color-base,#202122)'
    ctx.font = '10px sans-serif'
    ctx.textAlign = 'end'
    ctx.textBaseline = 'bottom'
    ctx:fillText('0', vertGutter - 3, height - horizGutter)
    ctx.textBaseline = 'top'
    ctx:fillText(maxValue, vertGutter - 3, 0)

    ctx.textAlign = 'start'
    ctx.textBaseline = 'bottom'
    ctx.font = 'bold 12px sans-serif'
    writeVertical(ctx, frame.args.yAxis or "Value", 0, (height - horizGutter) / 2 - 15)

    ctx.font = '10px sans-serif'
    ctx.textBaseline = 'top'
    ctx:fillText(minDate, vertGutter, height - horizGutter + 3)
    ctx.textAlign = 'end'
    ctx:fillText(maxDate, width, height - horizGutter + 3)
    ctx.textBaseline = 'bottom'
    ctx.font = 'bold 12px sans-serif'
    ctx:fillText(frame.args.xAxis or "Date", (width - vertGutter) / 2 + vertGutter + 10, height)

    -- Draw data line
    ctx.strokeStyle = 'blue'
    ctx:beginPath()
    for i, entry in ipairs(data) do
        local xPt = convertToPixelHoriz(tonumber(entry.date), minDate, maxDate, vertGutter, width)
        local yPt = convertToPixelVert(tonumber(entry.value), 0, maxValue, horizGutter, height)
        if i == 1 then
            ctx:moveTo(xPt, yPt)
        else
            ctx:lineTo(xPt, yPt)
        end
    end
    ctx:stroke()

    return tostring(ctx)
end

return p

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.