Mô đun:Dot chart
Giao diện
Mô đun này được sử dụng để thực thi {{Biểu đồ chấm}}.
Hàm graph được sử dụng để trả về đồ thị. Xem Bản mẫu:Biểu đồ chấm/doc.
local p = {}local getArgs = require("Mô đun:Arguments").getArgslocal yesno = require('Mô đun:Yesno')------------------------------------------------------------------------------------------ P . D O T S ------------------------------------------------------------------- Trả về tất cả các chấm (với vị trí tuyệt đối) -------------------------------------------------------------------------------------------------------function p.data(frame) -- Trả về dữ liệu của đồ thị local args = getArgs(frame) -- Liên quan đến chấm local yTable = {} local xTable = {} local xCount = 0 local yCount = 0 local isx; local dotTable = {} local x; -- Liên quan đến màu sắc local colorTable = {} local num; local colorTime; -- Liên quan đến nhãn X local xLabels = {} local xLCount = 0 -- Liên quan đến nhãn Y local yLabels = {} local yLCount = 0 if yesno(args["yx"]) == true then isx = false else isx = true end if args["x labels"] then -- Tạo xLabels for str in string.gmatch(args["x labels"], "([^,]+)") do table.insert(xLabels,str) xLCount = xLCount+1 end end if args["y labels"] then -- Tạo yLabels for str in string.gmatch(args["y labels"], "([^,]+)") do table.insert(yLabels,str) yLCount = yLCount+1 end end if args["dots"] then -- Tạo xTable từ các chấm local i = 0 local j = 0 for k,v in pairs(args) do if string.match(k,"%d+") and not string.match(k,"color%-") then table.insert(yTable,v) yCount = yCount+1 end end local cols = yCount / tonumber(args["dots"]) if cols ~= math.floor(cols) then return table.concat({'<span style="font-size:100%" class="error">Số lượng tham số y (',yCount,') ÷ tham số dots (',args["dots"],') không phải là số nguyên (',cols,')</span>'}) end while(cols>i) do local xValue = ((100/cols*i)+(100/cols/10))*1.1 i=i+1 while(tonumber(args["dots"])>j) do j=j+1 table.insert(xTable,xValue) xCount = xCount + 1 end j=0 end else -- Chia args vào yTable và xTable for k,v in pairs(args) do if string.match(k,"%d+") and not string.match(k,"color%-") then if isx == false then table.insert(yTable,v) yCount = yCount + 1 isx = true elseif not args["dots"] then table.insert(xTable,v) xCount = xCount + 1 isx = false end end end end if xCount < yCount then return table.concat({'<span style="font-size:100%" class="error">Số lượng giá trị x (',xCount,') ít hơn số lượng giá trị y (',yCount,')</span>'}) elseif xCount > yCount then return table.concat({'<span style="font-size:100%" class="error">Số lượng giá trị x (',xCount,') nhiều hơn số lượng giá trị y (',yCount,')</span>'}) end if args["color-even"] then -- Tạo colorTable nếu color-even được thiết lập colorTime = false for k,v in pairs(yTable) do if colorTime == true then colorTable[k] = args["color-even"] colorTime = false else colorTime = true end end end if args["color-odd"] then -- Tạo colorTable nếu color-odd được thiết lập colorTime = true for k,v in pairs(yTable) do if colorTime == true then colorTable[k] = args["color-odd"] colorTime = false else colorTime = true end end end for k,v in pairs(args) do -- Thêm giá trị vào colorTable nếu color-# được thiết lập if k == mw.ustring.match(k,"color%-%d+") then num = mw.ustring.gsub(k,"color%-","") num = tonumber(num) colorTable[num] = v end end for k,y in pairs(yTable) do -- Tạo dotTable local InnerDiv = mw.html.create('div') local div = mw.html.create('div') local size; if args["size"] then size = tonumber(mw.ustring.match(args["size"],"(%d+)")) else size = 8 end x = xTable[k] InnerDiv :css('position','absolute') :css('top',table.concat({'-',size/2,'px'})) :css('left',table.concat({'-',size/2,'px'})) :css('line-height','0') :wikitext('[[Tập tin:Location dot ',colorTable[k] or 'red','.svg|',size,'x',size,'px]]') div :css('position','absolute') :css('bottom',table.concat({y*0.85+15,'%'})) :css('left',table.concat({x*0.85+15,'%'})) :wikitext(tostring(InnerDiv)) table.insert(dotTable,tostring(div)) end for k,v in pairs(xLabels) do local div = mw.html.create('div') div :css('position','absolute') :css('bottom','0%') :css('left',table.concat({((100/xLCount*k-100/xLCount)*0.85+15)-4,'%'})) :wikitext(v) table.insert(dotTable,tostring(div)) end for k,v in pairs(yLabels) do local div = mw.html.create('div') div :css('position','absolute') :css('bottom',table.concat({(((((100/yLCount*k-100/yLCount)-(100/yLCount/3))+5))+100/yLCount/2)*1.02,'%'})) :css('left','0%') :wikitext(v) table.insert(dotTable,tostring(div)) end return table.concat(dotTable)end---------- C H Ú G I Ả I ---------------------------------------------------------------- Tạo bảng chú giải ---------------------------------------------------local function legend(args) local color; local aValue; local Table = {} for k,v in pairs(args) do -- Thêm giá trị vào bảng if k == mw.ustring.match(k,"legend%-%a+") then color = mw.ustring.gsub(k,"legend%-","") v = table.concat({'<div>[[Tập tin:Location dot ',color or 'red','.svg|8x8px]] (',color,') = ',v,'</div>'}) table.insert(Table,v) aValue = true end end if aValue == true then return table.concat(Table) else return "" endend------------------------------------------------------------------------------------------ P . G R A P H ----------------------------------------------------------------- Trả về tất cả các chấm trong thẻ div ----------------------------------------------------------------------------------------------------------------function p.graph(frame) -- Trả về một đồ thị với các chấm trên đó if mw.ustring.match(p.data(frame),"<span") then -- Trả về thông báo lỗi từ p.data return p.data(frame) end local args = getArgs(frame) local picture = "Blank.png" local div = mw.html.create('div') local center = mw.html.create('div') local container = mw.html.create('div') local top = mw.html.create('div') local size; if args["size"] then size = tonumber(mw.ustring.match(args["size"],"(%d+)")) else size = 8 end if args["width"] then if args["width"] == mw.ustring.match(args["width"],"(%d+)") then args["width"] = table.concat({args["width"],'px'}) end end if args["picture"] then -- Thiết lập hình ảnh cục bộ picture = args["picture"] elseif yesno(args["square"]) == true then picture = "Transparent.png" end picture = mw.ustring.gsub(picture,'|.+','') picture = mw.ustring.gsub(picture,'.-:','') if p.data(frame) == "" then -- Không tạo hộp nếu trống return "" end if args["top"] then top -- Tạo văn bản trên cùng :css('font-weight','bold') :css('text-decoration','underline') :css('text-align','center') :wikitext(args["top"]) end container -- Tạo vùng chứa :css('width',args["width"] or '240px') :css('float','right') :css('position','relative') :wikitext('[[Tập tin:',picture,'|',args["width"] or '240px',']]') :wikitext(p.data(frame)) div -- Tạo hộp :css('width', args["width"] or '240px') :css('display', 'inline-block') :css('float', args["align"] or 'right') :css('margin', args["margin"] or '2px') :css('padding', args["padding"] or table.concat({size/2,'px'})) :wikitext(tostring(top)) :wikitext(tostring(container)) :wikitext(legend(args)) :wikitext(args["bottom"]) if args["color"] then div:css('background', args["color"]) end if yesno(args['border']) ~= false then -- Tạo viền hộp div :css('border-style','solid') :css('border-color','black') :css('border-width','3px') end if args['align'] == 'center' then -- Căn giữa đầu ra nếu cần center :addClass('center') :css('width','auto') :css('margin-left','auto') :css('margin-right','auto') :wikitext(tostring(div)) return center else return div endendreturn p