Modulo:Grafico XY/sandbox: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
mNessun oggetto della modifica
use require("strict") instead of require("Module:No globals")
 
(20 versioni intermedie di un altro utente non mostrate)
Riga 1:
require("Module:No globalsstrict")
local getArgs = require('Module:Arguments').getArgs
local p = {}
Riga 8:
flex['ultimo'] = {'<div style="flex-grow:0;flex-shrink:0;flex-basis:auto;padding: 0 2em 0 0">','</div></div>'}
flex['destra'] = {'<div class="floatright">','</div>'}
 
local clr = { "#1f77b4","#ff7f0e","#2ca02c","#d62728","#9467bd","#8c564b","#e377c2","#7f7f7f","#bcbd22","#17becf" }
 
-- =====================================================================
Line 27 ⟶ 29:
local function unisci(a,b,c,d)
local values = { }
local t
for i =1,#b do
t = string.format("%g",math.floor(10*b[i]/d+0.5)/10)
values[i] = { nx = a[i]/c, ny = b[i]/d, num = i }
t = t:gsub('%.',',')
values[i] = { nx = a[i]/c, ny = b[i]/d, num = i, txt = t }
end
return values
Line 43 ⟶ 48:
height = 250,
padding = "auto",
data = { },
{
name = "tab",
transform = { { type = "sort", by = "nx" } },
values = { }
},
{
name = "tab2",
transform = { { type = "sort", by = "nx" } },
values = { }
},
{
name = "tab3",
transform = { { type = "sort", by = "nx" } },
values = { }
},
{
name = "nomi",
values = { }
}
},
scales = {
{
Line 70 ⟶ 55:
range = "width",
zero = false,
___domain = {fields = {{data = "tab",field = "nx"}, {data = "tab2",field = "nx"}, {data = "tab3",field = "nx"}}}
},
{
Line 78 ⟶ 63:
zero = true,
nice = true,
___domain = {fields = {{data = "tab",field = "ny"}, {data = "tab2",field = "ny"}, {data = "tab3",field = "ny"}}}
},
{
Line 84 ⟶ 69:
type = "ordinal",
___domain = { data = "nomi", field = "testo"},
range = { "#1f77b4", "b80000", "#00c000" }
},
{
name = "colori2",
type = "ordinal",
___domain = { data = "area", field = "testo"},
range = { }
}
},
Line 91 ⟶ 82:
type = "x",
scale = "x",
title = "X",
format = "d",
grid = true,
properties = {
labels = { font = { value = "Helvetica" } },
title = { font = { value = "Helvetica" } },
}
},
{
type = "y",
scale = "y",
title = "Y",
grid = true,
format = "d",
layer = "back",
properties = {
labels = { font = { value = "Helvetica" } },
title = { font = { value = "Helvetica" } },
}
}
},
Line 110 ⟶ 109:
orient = "top-left",
offset = 8,
properties = {
shape = "square",
labels = { font = { value = "Helvetica" }, fontSize = { value = 12 } },
strokeWidth = 0
symbols = {
}
strokeWidth = { value = 0 },
},
shape = { value = "square" },
marks = {
opacity = { value = 0.7 }
{
type = "line" },
from legend = {data fill = { value = "tab#fff" }, }
properties = {
enter = {
interpolate = {value = "linear"},
x = {scale = "x",field = "nx"},
y = {scale = "y",field = "ny"},
stroke = {value = "#1f77b4"},
strokeWidth = {value = 4},
opacity = {value = 0.5}
}
}
},
{
typefill = "linecolori2",
from = {datatitle = "tab2" },
propertiesorient = {"top-left",
enteroffset = {8,
properties = {
interpolate = {value = "linear"},
xlabels = {scale font = { value = "xHelvetica" },field fontSize = "nx"{ value = 12 } },
ysymbols = {scale = "y",field = "ny"},
strokestrokeWidth = { value = "#b80000"0 },
strokeWidthshape = { value = 4"square" },
opacity = { value = 0.53 }
},
legend = { fill = { value = "#fff" } }
}
},
},
marks = {
{
type = "linerect",
from = { data = "tab3area" },
properties = {
enter = {
interpolatex = {valuescale = "linearx",field = "x1"},
xx2 = {scale = "x",field = "nxx2"},
y = {scalevalue = "y",field = "ny"0},
strokey2 = {valuesignal = "#00c000height"},
strokeWidthfill = {valuescale = 4"colori2", field = "testo" },
opacity = {value = 0.52 }
}
}
},
{
type = "symbol",
from = {data = "tab"},
properties = {
enter = {
x = {scale = "x",field = "nx"},
y = {scale = "y",field = "ny"},
stroke = {value = "1f77b4"},
fill = {value = "#fff"},
size = {value = 30}
}
}
},
{
type = "symbol",
from = {data = "tab2"},
properties = {
enter = {
x = {scale = "x",field = "nx"},
y = {scale = "y",field = "ny"},
stroke = {value = "#b80000"},
fill = {value = "#fff"},
size = {value = 30}
}
}
},
{
type = "symbol",
from = {data = "tab3"},
properties = {
enter = {
x = {scale = "x",field = "nx"},
y = {scale = "y",field = "ny"},
stroke = {value = "#00c000"},
fill = {value = "#fff"},
size = {value = 30}
}
}
},
{
type = "text",
from = { data = "tab" },
properties = {
enter = {
x = {scale = "x",field = "nx"},
y = {scale = "y",field = "ny", offset = -6},
align = {value = "center"},
fill = {value = "#000"},
text = {field = "ny" }
}
}
},
{
type = "text",
from = { data = "tab2" },
properties = {
enter = {
x = {scale = "x",field = "nx"},
y = {scale = "y",field = "ny", offset = -6},
align = {value = "center"},
fill = {value = "#000"},
text = {field = "ny" }
}
}
},
{
type = "text",
from = { data = "tab3" },
properties = {
enter = {
x = {scale = "x",field = "nx"},
y = {scale = "y",field = "ny", offset = -6},
align = {value = "center"},
fill = {value = "#000"},
text = {field = "ny" }
}
}
Line 238 ⟶ 153:
}
-- titoli assi
local titoloX = args.titoloX or 'X'
local fattoreX = args.fattoreX and tonumber(args.fattoreX) and tonumber(args.fattoreX) or 1
if fattoreX == 0 then
Line 248 ⟶ 163:
end
graph['axes'][1]['title'] = titoloX
local titoloY = args.titoloY or 'Y'
local fattoreY = args.fattoreY and tonumber(args.fattoreY) and tonumber(args.fattoreY) or 1
if fattoreY == 0 then
Line 262 ⟶ 177:
if (args.zeroX and args.zeroX == 's') then graph['scales'][1]['zero'] = true end
if (args.zeroY and args.zeroY == 'n') then graph['scales'][2]['zero'] = false end
 
-- legge valori
local dx1 = leggi(args, "x1")
local dy1 = leggi(args, "y1")
local dx2 = leggi(args, "x2")
local dy2 = leggi(args, "y2")
local dx3 = leggi(args, "x3")
local dy3 = leggi(args, "y3")
graph['data'][1]['values'] = unisci(dx1, dy1, fattoreX, fattoreY)
graph['data'][2]['values'] = unisci(dx2, dy2, fattoreX, fattoreY)
graph['data'][3]['values'] = unisci(dx3, dy3, fattoreX, fattoreY)
-- dimensioni
graph['width'] = args.dimx and tonumber(args.dimx) and tonumber(args.dimx) or 350
graph['height'] = args.dimy and tonumber(args.dimy) and tonumber(args.dimy) or 250
 
-- colori
-- legge valori
if args.colore1 then
local numero = 1
graph['marks'][1]['properties']['enter']['stroke']['value'] = args.colore1
local dx, dy, dt, cc
graph['marks'][4]['properties']['enter']['stroke']['value'] = args.colore1
local err = '-'
graph['scales'][3]['range'][1] = args.colore1
dt = {}
end
 
if args.colore2 then
while (args['x'..numero]) do
graph['marks'][2]['properties']['enter']['stroke']['value'] = args.colore2
dx = leggi(args, "x"..numero)
graph['marks'][5]['properties']['enter']['stroke']['value'] = args.colore2
dy = leggi(args, "y"..numero)
graph['scales'][3]['range'][2] = args.colore2
if (dx and dy) then
end
if args.colore3(#dx == #dy) then
dt[numero] = unisci(dx, dy, fattoreX, fattoreY)
graph['marks'][3]['properties']['enter']['stroke']['value'] = args.colore3
else
graph['marks'][6]['properties']['enter']['stroke']['value'] = args.colore3
err = 'Errore nel numero di elementi per serie '..numero
graph['scales'][3]['range'][3] = args.colore3
break
end
end
-- opzioni etichette
else
if args.etichette1 then
err = 'Errore nella serie '..numero
if args.etichette1 == 'dispari' then
break
graph['marks'][7]['from']['transform'] = {{ type = "filter", test = "datum.num % 2 == 1"}}
elseif args.etichette1 == 'pari' then
graph['marks'][7]['from']['transform'] = {{ type = "filter", test = "datum.num % 2 == 0"}}
elseif args.etichette1 == 'no' then
graph['marks'][7]['from']['transform'] = {{ type = "filter", test = "datum.num < 1"}}
end
numero = numero+1
end
numero = numero-1
if args.mostra1 then
if args.mostra1 == '1' then
if err == '-' then
graph['marks'][4]['from']['transform'] = {{ type = "filter", test = "datum.num < 1"}}
for i = 1,numero do
elseif args.mostra1 == '2' then
cc = i%10
graph['marks'][1]['from']['transform'] = {{ type = "filter", test = "datum.num < 1"}}
if cc == 0 then cc = 10 end
graph['data'][i] = {
name = "tab"..i,
transform = { { type = "sort", by = "nx" } },
}
graph['data'][i]['values'] = dt[i]
graph['marks'][1+i] = {
type = "line",
from = {data = "tab"..i },
properties = {
enter = {
interpolate = {value = "linear"},
x = {scale = "x",field = "nx"},
y = {scale = "y",field = "ny"},
stroke = {value = clr[cc] },
strokeWidth = {value = 4},
opacity = {value = 0.5}
}
}
}
graph['marks'][1+i+numero] = {
type = "symbol",
from = {data = "tab"..i },
properties = {
enter = {
x = {scale = "x",field = "nx"},
y = {scale = "y",field = "ny"},
stroke = {value = clr[cc] },
fill = {value = "#fff"},
size = {value = 30}
}
}
}
graph['marks'][1+i+2*numero] = {
type = "text",
from = { data = "tab"..i },
properties = {
enter = {
x = {scale = "x",field = "nx"},
y = {scale = "y",field = "ny", offset = -8},
align = {value = "center"},
fill = {value = "#000"},
font = {value = "Helvetica"},
fontSize = { value = 12 },
text = {field = "txt" }
}
}
}
graph['scales'][1]['___domain']['fields'][i] = {data = "tab"..i,field = "nx"}
graph['scales'][2]['___domain']['fields'][i] = {data = "tab"..i,field = "ny"}
graph['scales'][3]['range'][i] = clr[cc]
-- colori
if args['colore'..i] then
graph['marks'][1+i]['properties']['enter']['stroke']['value'] = args['colore'..i]
graph['marks'][1+i+numero]['properties']['enter']['stroke']['value'] = args['colore'..i]
graph['scales'][3]['range'][i] = args['colore'..i]
end
-- opzioni etichette
if args['etichette'..i] then
if args['etichette'..i] == 'dispari' then
graph['marks'][1+i+2*numero]['from']['transform'] = {{ type = "filter", test = "datum.num % 2 == 1"}}
elseif args['etichette'..i] == 'pari' then
graph['marks'][1+i+2*numero]['from']['transform'] = {{ type = "filter", test = "datum.num % 2 == 0"}}
elseif args['etichette'..i] == 'no' then
graph['marks'][1+i+2*numero]['from']['transform'] = {{ type = "filter", test = "datum.num < 1"}}
end
end
-- mostra dati
if args['mostra'..i] then
if args['mostra'..i] == '1' then
graph['marks'][1+numero+i]['from']['transform'] = {{ type = "filter", test = "datum.num < 1"}}
elseif args['mostra'..i] == '2' then
graph['marks'][1+i]['from']['transform'] = {{ type = "filter", test = "datum.num < 1"}}
end
end
end
 
end
-- legenda
if args.etichette2 then
graph['data'][numero+1] = {
if args.etichette2 == 'dispari' then
name = "nomi",
graph['marks'][8]['from']['transform'] = {{ type = "filter", test = "datum.num % 2 == 1"}}
values = { }
elseif args.etichette2 == 'pari' then
}
graph['marks'][8]['from']['transform'] = {{ type = "filter", test = "datum.num % 2 == 0"}}
local lg = 1
elseif args.etichette2 == 'no' then
while (args['nome'..lg]) do
graph['marks'][8]['from']['transform'] = {{ type = "filter", test = "datum.num < 1"}}
graph['data'][numero+1]['values'][lg] = { testo = args['nome'..lg] }
lg = lg +1
end
if args.legenda then graph['legends'][1]['orient'] = args.legenda end
end
 
if args.mostra2 then
-- annotazioni
if args.mostra2 == '1' then
graph['data'][numero+2] = {
graph['marks'][5]['from']['transform'] = {{ type = "filter", test = "datum.num < 1"}}
name = "area",
elseif args.mostra2 == '2' then
values = { }
graph['marks'][2]['from']['transform'] = {{ type = "filter", test = "datum.num < 1"}}
}
lg = 1
while (args['area'..lg]) do
graph['data'][numero+2]['values'][lg] = { testo = args['area'..lg], x1 = args['area'..lg..'_x1'], x2 = args['area'..lg..'_x2'] }
graph['scales'][4]['range'][lg] = args['area'..lg..'_colore'] or clr[lg]
graph['scales'][1]['___domain']['fields'][numero+2*lg-1] = {data = "area",field = "x1"}
graph['scales'][1]['___domain']['fields'][numero+2*lg] = {data = "area",field = "x2"}
lg = lg +1
end
if args.area_legenda then graph['legends'][2]['orient'] = args.area_legenda end
end
 
if args.etichette3 then
if (args.etichette3minimoX ==and 'dispari'tonumber(args.minimoX)) then
graph['marksscales'][91]['from']['transformdomainMin'] = {{ type = "filter", test = "datumtonumber(args.num % 2 == 1"}}minimoX)
elseif args.etichette3 == 'pari' then
graph['marks'][9]['from']['transform'] = {{ type = "filter", test = "datum.num % 2 == 0"}}
elseif args.etichette3 == 'no' then
graph['marks'][9]['from']['transform'] = {{ type = "filter", test = "datum.num < 1"}}
end
if (args.massimoX and tonumber(args.massimoX)) then
end
graph['scales'][1]['domainMax'] = tonumber(args.massimoX)
if args.mostra3 then
if args.mostra3 == '1' then
graph['marks'][6]['from']['transform'] = {{ type = "filter", test = "datum.num < 1"}}
elseif args.mostra3 == '2' then
graph['marks'][3]['from']['transform'] = {{ type = "filter", test = "datum.num < 1"}}
end
end
-- legenda
if args.nome1 then graph['data'][4]['values'][1] = { testo = args.nome1 } end
if args.nome2 then graph['data'][4]['values'][2] = { testo = args.nome2 } end
if args.nome3 then graph['data'][4]['values'][3] = { testo = args.nome3 } end
if args.legenda then graph['legends'][1]['orient'] = args.legenda end
 
local ris = {}
local allinea = args['allinea'] or ''
if (flex[allinea]) then table.insert(ris,flex[allinea][1]) end
table.insert(ris,mw.getCurrentFrame():extensionTag('graph', mw.text.jsonEncode(graph)))
if (args.didascalia) then
table.insert(ris,'<p style="font-size:90%;margin-left:30px">'..args.didascalia..'</p>')
end
if (flex[allinea]) then table.insert(ris,flex[allinea][2]) end
if args['debug'] then
return mw.text.jsonEncode(graph)
else
return table.concat(ris)
end
else
return '<span style="color:red"><b>Template GraficoXY:</b> '..err..'</span>'
end
if (flex[allinea]) then table.insert(ris,flex[allinea][2]) end
return table.concat(ris)
end