Module:Indicator datasets table: Difference between revisions

From Visual Data Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Tag: Manual revert
 
(156 intermediate revisions by the same user not shown)
Line 3: Line 3:
function p.getDatasets(frame)
function p.getDatasets(frame)
     local articleids = frame.args.articleids
     local articleids = frame.args.articleids
    local currentPageId = frame.args.pageid
if not articleids or articleids == '' then
   
    return {}
    if not articleids then
end
        return "Error: articleids not provided"
 
    end
      
      
     -- Entferne alle Kommas und Leerzeichen am Ende von articleids
     -- Entferne alle Kommas und Leerzeichen am Ende von articleids
Line 22: Line 21:
         if existspage then
         if existspage then
             local content = titleobject:getContent()
             local content = titleobject:getContent()
             local jsonData = mw.text.jsonDecode(content)
             local dataset = mw.text.jsonDecode(content)
             -- Überprüfe, ob die pageid mit der aktuellen übereinstimmt
             -- Überprüfe, ob belongs to der aktuellen PageID entspricht
             if tonumber(jsonData.belongs_to) == tonumber(currentPageId) then
             if tonumber(dataset.schemas.Dataset['belongs to']) == mw.title.getCurrentTitle().id then
                -- Speichere den Inhalt der Seite
                 table.insert(datasets, dataset)
                 table.insert(datasets, jsonData)
            else
                return "Error: 'belongs to' does not match the current page ID"
             end
             end
         else
         else
Line 34: Line 30:
         end
         end
     end
     end
   
     return datasets
     return datasets
end
end
-- Funktion zur Formatierung des Datums gemäß den Nutzereinstellungen
function formatDate(date)
    return mw.language.getContentLanguage():formatDate(date)
end
function round(number, decimalPlaces)
    local shift = 10 ^ decimalPlaces
    return math.floor(number * shift + 0.5) / shift
end


function p.generateTable(frame)
function p.generateTable(frame)
     local datasets = p.getDatasets(frame)
     local datasets = p.getDatasets(frame)
     if type(datasets) == "string" then
     if not datasets or #datasets == 0 then
         return datasets -- Fehlermeldung zurückgeben
         return "No data" -- Keine Datensätze gefunden, gib leeren String zurück
    end
   
    -- Number or percent
    local entity = frame.args.entity
    -- local entity = mw.slots.slotContent('jsondata', 'Tasks/Test_Task').schemas['Task']['title']
    local suffix = ''
    if entity == 'percent' then
    suffix = '%'
     end
     end
   
    -- Sortiere die Datensätze nach dem Datum
    table.sort(datasets, function(a, b) return a.schemas.Dataset['date'] < b.schemas.Dataset['date'] end)
    local html = mw.html.create()
    local tableNode = html:tag('table'):addClass('wikitable sortable')


     local rows = {}
    -- Check if the dataset contains sub-classes and sub-sub-classes
      
     local dataset = datasets[1] -- Check first dataset
     -- Füge die Spaltenüberschriften hinzu
     local hasSubClasses = dataset.schemas.Dataset.classes[1]['sub-classes'][1]['sub-class name'] and dataset.schemas.Dataset.classes[1]['sub-classes'][1]['sub-class name'] ~= ''
     local headerRow = {'Class', 'Sub-class'}
     local hasSubSubClasses = dataset.schemas.Dataset.classes[1]['sub-classes'][1]['sub-sub-classes'][1]['sub-sub-class name'] and dataset.schemas.Dataset.classes[1]['sub-classes'][1]['sub-sub-classes'][1]['sub-sub-class name'] ~= ''
 
 
    -- Erstelle die Tabelle mit Legenden aus dem ersten Datensatz
     local headerRow = tableNode:tag('tr')
    headerRow:tag('th'):wikitext('Class')
    if hasSubClasses then headerRow:tag('th'):wikitext(dataset.schemas.Dataset.classes[1]['sub-classes'][1]['sub-class name']) end
    if hasSubSubClasses then headerRow:tag('th'):wikitext(dataset.schemas.Dataset.classes[1]['sub-classes'][1]['sub-sub-classes'][1]['sub-sub-class name']) end
     for _, dataset in ipairs(datasets) do
     for _, dataset in ipairs(datasets) do
         table.insert(headerRow, dataset['date'])
         local formattedDate = formatDate(dataset.schemas.Dataset['date'])
        headerRow:tag('th'):wikitext(formattedDate):tag('div'):tag('small'):wikitext(mw.message.new('webmo-' .. dataset.schemas.Dataset['dataset type']):plain()):tag('div'):tag('small')
     end
     end
    table.insert(rows, headerRow)


    local columnSums = {}
    local columnCounts = {}
     -- Füge die Datenzeilen hinzu
     -- Füge die Datenzeilen hinzu
     for _, dataset in ipairs(datasets) do
     for _, classData in ipairs(datasets[1].schemas.Dataset.classes) do
         for _, classData in ipairs(dataset.schemas.Dataset.classes) do
        local class = classData['class name']
             local class = classData['class name']
         for _, subClassData in ipairs(classData['sub-classes']) do
             for _, subClassData in ipairs(classData['sub-classes']) do
             local subClass = subClassData['sub-class name']
                 local subClass = subClassData['sub-class name']
             for _, subSubClassData in ipairs(subClassData['sub-sub-classes']) do
                 local row = {class, subClass}
                 local subSubClass = subSubClassData['sub-sub-class name']
                for _, currentSubClassData in ipairs(subClassData['sub-sub-classes']) do
                 local row = tableNode:tag('tr')
                    table.insert(row, currentSubClassData.value or "N/A")
                row:tag('td'):wikitext(class)
                if hasSubClasses then row:tag('td'):wikitext(subClass) end
                if hasSubSubClasses then row:tag('td'):wikitext(subSubClass) end
                for i, dataset in ipairs(datasets) do
                    local value = {}
                    for _, currentClassData in ipairs(dataset.schemas.Dataset.classes) do
                        if currentClassData['class name'] == class then
                            for _, currentSubClassData in ipairs(currentClassData['sub-classes']) do
                                if currentSubClassData['sub-class name'] == subClass then
                                    for _, currentSubSubClassData in ipairs(currentSubClassData['sub-sub-classes']) do
                                        if currentSubSubClassData['sub-sub-class name'] == subSubClass then
                                            local val = tonumber(currentSubSubClassData.value) or 0
                                            table.insert(value, val)
                                           
                                            -- Berechne Summen und Anzahlen für jede Spalte
                                            columnSums[i] = columnSums[i] or 0
                                            columnCounts[i] = columnCounts[i] or 0
                                            columnSums[i] = columnSums[i] + val
                                            columnCounts[i] = columnCounts[i] + 1
                                        end
                                    end
                                end
                            end
                        end
                    end
                    row:tag('td'):wikitext(table.concat(value, ", ") .. suffix)
                 end
                 end
                -- Füge die Zeile dem Array hinzu
                table.insert(rows, row)
             end
             end
         end
         end
     end
     end


     -- Erstelle die Tabelle
     -- Füge die Totalzeile hinzu
     local html = mw.html.create()
     local totalRow = tableNode:tag('tr'):addClass('sortbottom')
     local tableNode = html:tag('table'):addClass('wikitable')
     totalRow:tag('th'):wikitext('Total')
 
     if hasSubClasses then totalRow:tag('th'):wikitext('') end
     for _, row in ipairs(rows) do
    if hasSubSubClasses then totalRow:tag('th'):wikitext('') end
        local tr = tableNode:tag('tr')
    for i, dataset in ipairs(datasets) do
        for _, cell in ipairs(row) do
        local totalValue
             tr:tag('td'):wikitext(cell)
        if columnCounts[i] and columnCounts[i] > 0 then
        if entity == 'percent' then
            totalValue = columnSums[i] / columnCounts[i] -- Berechne Durchschnitt, falls Werte vorhanden sind
            else
            totalValue = columnSums[i]
             end
        else
            totalValue = 'N/A'
         end
         end
        totalRow:tag('th'):wikitext(round(totalValue, 2) .. suffix)
     end
     end



Latest revision as of 20:41, 9 May 2024

Documentation for this module may be created at Module:Indicator datasets table/doc

local p = {}

function p.getDatasets(frame)
    local articleids = frame.args.articleids
	if not articleids or articleids == '' then
	    return {}
	end

    
    -- Entferne alle Kommas und Leerzeichen am Ende von articleids
    articleids = mw.ustring.gsub(articleids, '[,%s]*$', '')
    
    local ids = mw.text.split(articleids, ",")
    local datasets = {}
    local titleobject
    
    for _, id in ipairs(ids) do
        titleobject = mw.title.new(tonumber(id))
        local existspage = titleobject.exists
        
        if existspage then
            local content = titleobject:getContent()
            local dataset = mw.text.jsonDecode(content)
            -- Überprüfe, ob belongs to der aktuellen PageID entspricht
            if tonumber(dataset.schemas.Dataset['belongs to']) == mw.title.getCurrentTitle().id then
                table.insert(datasets, dataset)
            end
        else
            return "Error: Page not found for ID " .. id
        end
    end
    return datasets
end


-- Funktion zur Formatierung des Datums gemäß den Nutzereinstellungen
function formatDate(date)
    return mw.language.getContentLanguage():formatDate(date)
end


function round(number, decimalPlaces)
    local shift = 10 ^ decimalPlaces
    return math.floor(number * shift + 0.5) / shift
end


function p.generateTable(frame)
    local datasets = p.getDatasets(frame)
    if not datasets or #datasets == 0 then
        return "No data" -- Keine Datensätze gefunden, gib leeren String zurück
    end
    
    -- Number or percent
    local entity = frame.args.entity
    -- local entity = mw.slots.slotContent('jsondata', 'Tasks/Test_Task').schemas['Task']['title']
    local suffix = ''
    if entity == 'percent' then
    	suffix = '%'
    end
    
    -- Sortiere die Datensätze nach dem Datum
    table.sort(datasets, function(a, b) return a.schemas.Dataset['date'] < b.schemas.Dataset['date'] end)

    local html = mw.html.create()
    local tableNode = html:tag('table'):addClass('wikitable sortable')

    -- Check if the dataset contains sub-classes and sub-sub-classes
    local dataset = datasets[1] -- Check first dataset
    local hasSubClasses = dataset.schemas.Dataset.classes[1]['sub-classes'][1]['sub-class name'] and dataset.schemas.Dataset.classes[1]['sub-classes'][1]['sub-class name'] ~= ''
    local hasSubSubClasses = dataset.schemas.Dataset.classes[1]['sub-classes'][1]['sub-sub-classes'][1]['sub-sub-class name'] and dataset.schemas.Dataset.classes[1]['sub-classes'][1]['sub-sub-classes'][1]['sub-sub-class name'] ~= ''


    -- Erstelle die Tabelle mit Legenden aus dem ersten Datensatz
    local headerRow = tableNode:tag('tr')
    headerRow:tag('th'):wikitext('Class')
    if hasSubClasses then headerRow:tag('th'):wikitext(dataset.schemas.Dataset.classes[1]['sub-classes'][1]['sub-class name']) end
    if hasSubSubClasses then headerRow:tag('th'):wikitext(dataset.schemas.Dataset.classes[1]['sub-classes'][1]['sub-sub-classes'][1]['sub-sub-class name']) end
    for _, dataset in ipairs(datasets) do
        local formattedDate = formatDate(dataset.schemas.Dataset['date'])
        headerRow:tag('th'):wikitext(formattedDate):tag('div'):tag('small'):wikitext(mw.message.new('webmo-' .. dataset.schemas.Dataset['dataset type']):plain()):tag('div'):tag('small')
    end

    local columnSums = {}
    local columnCounts = {}
    -- Füge die Datenzeilen hinzu
    for _, classData in ipairs(datasets[1].schemas.Dataset.classes) do
        local class = classData['class name']
        for _, subClassData in ipairs(classData['sub-classes']) do
            local subClass = subClassData['sub-class name']
            for _, subSubClassData in ipairs(subClassData['sub-sub-classes']) do
                local subSubClass = subSubClassData['sub-sub-class name']
                local row = tableNode:tag('tr')
                row:tag('td'):wikitext(class)
                if hasSubClasses then row:tag('td'):wikitext(subClass) end
                if hasSubSubClasses then row:tag('td'):wikitext(subSubClass) end
                for i, dataset in ipairs(datasets) do
                    local value = {}
                    for _, currentClassData in ipairs(dataset.schemas.Dataset.classes) do
                        if currentClassData['class name'] == class then
                            for _, currentSubClassData in ipairs(currentClassData['sub-classes']) do
                                if currentSubClassData['sub-class name'] == subClass then
                                    for _, currentSubSubClassData in ipairs(currentSubClassData['sub-sub-classes']) do
                                        if currentSubSubClassData['sub-sub-class name'] == subSubClass then
                                            local val = tonumber(currentSubSubClassData.value) or 0
                                            table.insert(value, val)
                                            
                                            -- Berechne Summen und Anzahlen für jede Spalte
                                            columnSums[i] = columnSums[i] or 0
                                            columnCounts[i] = columnCounts[i] or 0
                                            columnSums[i] = columnSums[i] + val
                                            columnCounts[i] = columnCounts[i] + 1
                                        end
                                    end
                                end
                            end
                        end
                    end
                    row:tag('td'):wikitext(table.concat(value, ", ") .. suffix)
                end
            end
        end
    end

    -- Füge die Totalzeile hinzu
    local totalRow = tableNode:tag('tr'):addClass('sortbottom')
    totalRow:tag('th'):wikitext('Total')
    if hasSubClasses then totalRow:tag('th'):wikitext('') end
    if hasSubSubClasses then totalRow:tag('th'):wikitext('') end
    for i, dataset in ipairs(datasets) do
        local totalValue
        if columnCounts[i] and columnCounts[i] > 0 then
        	if entity == 'percent' then
            	totalValue = columnSums[i] / columnCounts[i] -- Berechne Durchschnitt, falls Werte vorhanden sind
            else
            	totalValue = columnSums[i]
            end
        else
            totalValue = 'N/A'
        end
        totalRow:tag('th'):wikitext(round(totalValue, 2) .. suffix) 
    end

    return tostring(html)
end

return p