Module:Graphique de sondages
La documentation de ce module est générée par le modèle {{Documentation module}}.
Les éditeurs peuvent travailler dans le bac à sable (créer).
Voir les statistiques d'appel depuis le wikicode sur l'outil wstat et les appels depuis d'autres modules.
local p = {}
function bissextile(an)
if an % 4 == 0 and an%100 ~=0 then
return true
end
if an % 400 == 0 then
return true
end
return false
end
function check_date(an, mois, jour)
if mois == 2 then
if bissextile(an) then
return jour < 30
else
return jour < 29
end
elseif (mois == 4) or (mois == 6) or (mois == 9) or (mois == 11) then
return jour < 31
else
return jour < 32
end
end
function diff_jours(date1,date2) --Cette fonction n’a pas vocation à donner une différence de jours exacte
local j1 = date1[1]*365 + (date1[2])*30.44 + date1[3]
local j2 = date2[1]*365 + (date2[2])*30.44 + date2[3]
local diff = math.abs(j2 - j1)
return diff
end
function date_to_tabl(date)
local t = mw.text.split(date, "/")
for k = 1, #t do
t[k] = tonumber(t[k])
end
return t
end
function table_to_date(date)
return tostring(date[1]) .. "/" .. tostring(date[2]) .. "/" .. tostring(date[3])
end
function liste_date_to_string(liste_date)
ch = ""
for k = 1, #liste_date-1 do
ch = ch .. table_to_date(liste_date[k])..","
end
ch = ch .. table_to_date(liste_date[#liste_date])
return ch
end
function est_avant(date1,date2)
local j1 = date1[1]*365 + date1[2]*30 + date1[3]
local j2 = date2[1]*365 + date2[2]*30 + date2[3]
local diff = j2-j1
if diff > 0 then
return true
else
return false
end
end
function gener_list_date(depart, fin, pas)
local liste = {}
local i = 1
local an = depart[1]
local mois = depart[2]
local jour = depart[3]
while est_avant({an, mois, jour},fin) do
jour = jour + pas
if not check_date(an, mois, jour) then
jour = 1
mois = mois + 1
if mois > 12 then
mois = 1
an = an + 1
end
end
liste[i]={an, mois, jour}
i = i + 1
end
return liste
end
function poids(date_point, date_sondage, nb_gens,duree_moyenne)
local diff = diff_jours(date_point, date_sondage)
if diff > duree_moyenne then
return nil
else
return nb_gens
end
end
function calculs_j(sondages, jour,duree_moyenne)
nb_sondages = #sondages
local prct_tot = 0
local poid_tot = 0
for k = 1, nb_sondages do
local date_sondage = sondages[k][2]
if diff_jours(date_sondage, jour) < duree_moyenne then
local nb_gens = sondages[k][1]
prct_tot = prct_tot + ((sondages[k][3])*nb_gens)
poid_tot = poid_tot + nb_gens
end
end
if poid_tot == 0 then
return " "
else
local prct_tot = prct_tot/poid_tot
local ent, frac = math.modf(prct_tot)
local c = tostring(prct_tot/100)
return c
-- if prct_tot < 10 then
-- return ".0"..tostring(ent)..mw.text.truncate(mw.text.truncate(tostring(frac),-7,""),-5,"")
-- else
-- return "."..tostring(ent)..mw.text.truncate(mw.text.truncate(tostring(frac),-7,""),-5,"")
-- end
end
end
function calcul(sondages, dates,duree_moyenne)
local str = ""
local nb_jours = #dates
for k = 1, nb_jours-1 do
local points = calculs_j(sondages, dates[k],duree_moyenne)
str = str .. points .. ","
end
str = str .. calculs_j(sondages, dates[nb_jours],duree_moyenne)
return str
end
function mef(t,n)
local k = 1
local final = {}
for i = 1,n do
final[i] = {}
end
while t[k] ~= nil do
sondage = mw.text.split(t[k],",")
local date = date_to_tabl(sondage[2])
local participants = tonumber(sondage[1])
for i = 1, n do
final[i][k] = {participants, date, sondage[i+2]}
end
k = k+1
end
return final
end
function p.aggregateur(frame) --construit un appel au Modèle:Graph:Chart
local pframe = frame:getParent()
local duree_moyenne = (tonumber(pframe.args["duree_moyenne"]) or 7)/2
local ch = ""
local largeur = pframe.args["largeur"] or "500"
ch = ch .. "|width ="..largeur
local hauteur = pframe.args["hauteur"] or "200"
ch = ch .. "|height ="..hauteur
local annotations = pframe.args["annotations"]
local dates_annotations = pframe.args["dates_annotations"]
if annotations ~= nil then
ch = ch.."|vAnnotatonsLine ="..dates_annotations.."|vAnnotatonsLabel ="..annotations
end
local date_debut = date_to_tabl(pframe.args["date_debut"])
local date_fin = date_to_tabl(pframe.args["date_fin"])
local pas = pframe.args["pas"] or 1
local liste_dates = gener_list_date(date_debut, date_fin, pas)
ch = ch .. "|x =" .. liste_date_to_string(liste_dates) .. "| legend = Légende| type = line| xtype = date| xAxisFormat = %m/%Y| yAxisFormat = %"
local candidats = pframe.args["candidats"]
candidats = mw.text.split(candidats, ',')
local nb_candidats = #candidats
local table_pts = mef(pframe.args, nb_candidats)
for k = 1, nb_candidats do
local liste_scores = calcul(table_pts[k], liste_dates,duree_moyenne)
ch = ch .. "|y"..tostring(k).."="..liste_scores.."|y"..tostring(k).."Title="..candidats[k]
end
ch = ch.."|colors="..pframe.args["couleurs"].."|xGrid=1|yGrid=1".."|interpolate=monotone"
local zoom_min = tostring(pframe.args["zoom_min"])
local zoom_max = tostring(pframe.args["zoom_max"])
local final = "{{Graph:Chart"..ch.."}}"
if zoom_min ~= "nil" then
final = final.."Ci dessous le même graphique, restreint aux courbes comprises entre "..tostring(pframe.args["zoom_min"]*100).." et "..tostring(pframe.args["zoom_max"]*100).." % : {{Graph:Chart".."|yAxisMin="..zoom_min.."|yAxisMax="..zoom_max..ch.."}}"
end
return frame:preprocess(final)
end
return p