Module:Sandbox/Julio974fr/parliament diagram

local p = {}

local parliament_diagram = require('Module:Parliament diagram') local positions = require('Module:Sandbox/Julio974fr/positions')

local function insertPartyPosition(partiesPositions, party, year) if party.seats <= 0 then return partiesPositions end local partyAttributes = { name = party.partyArticle, seats = party.seats, position = positions._fetch({party.partyQid, year}), }	if not party.partyArticle then partyAttributes.color = party.color end table.insert(partiesPositions, partyAttributes) return partiesPositions end

local function getpartiesPositions(partiesData, year) local partiesPositions = {} for _, party in ipairs(partiesData) do		insertPartyPosition(partiesPositions, party, year) end return partiesPositions end

local function sortPartiesPositions(partiesPositions) local function comparePartiesByPosition(partyA, partyB) if partyA.position < partyB.position then return true else return false end end table.sort(partiesPositions, comparePartiesByPosition) return partiesPositions end

function p.makeParliamentDiagram(partiesData, year) -- 1. Get the positions for each party local partiesPositions = getpartiesPositions(partiesData, year) -- 2. Sort the new array by positions local partiesPositions = sortPartiesPositions(partiesPositions) -- 3. Get the parliament diagram made local partiesPositionsIndexed = {} for i, v in ipairs(partiesPositions) do		partiesPositionsIndexed['p'..i] = v.name partiesPositionsIndexed['n'..i] = v.seats partiesPositionsIndexed['c'..i] = v.color end partiesPositionsIndexed.background = '#F8F9FA' return parliament_diagram._parliament(partiesPositionsIndexed) end

return p