Content deleted Content added
Pietrasagh (talk | contribs) synh with Modul:Graph, other symbols shapes, symbols and line-widths serialization, see update notes, lowercase args names, new "nice" and "xGrid" / "yGrid" logic |
This needs to use the parent frame |
||
(7 intermediate revisions by 4 users not shown) | |||
Line 1:
-- ATTENTION: Please edit this code at https://de.wikipedia.org/wiki/Modul:Graph
-- This way all wiki languages can stay in sync. Thank you!
Line 12 ⟶ 13:
-- - arbitrary SVG path symbol shape as symbolsShape argument
-- - annotations
-- - vertical / horizontal line at specific values [DONE] 2020-09-01
-- - rectangle shape for x,y data range
-- - graph type serialization (deep rebuild reqired)
Line 18 ⟶ 19:
-- Version History (_PLEASE UPDATE when modifying anything_):
-- 2020-09-01 Vertical and horizontal line annotations
-- 2020-08-08 New logic for "nice" for x axis (problem with scale when xType = "date") and grid
-- 2020-06-21 Serializes symbol size
Line 287 ⟶ 289:
if not xType then xType = "string" end
end
return x, xType, xMin, xMax
end
Line 397 ⟶ 398:
{
name = "y",
--type = yScaleType or "linear",
range = "height",
-- area charts have the lower boundary of their filling at y=0 (see marks.properties.enter.y2), therefore these need to start at zero
Line 797 ⟶ 799:
return symbolmarks
end
local function getAnnoMarks(chartvis, stroke, fill, opacity)
local vannolines, hannolines, vannoLabels, vannoLabels
vannolines =
{
type = "rule",
from = { data = "v_anno" },
properties =
{
update =
{
x = { scale = "x", field = "x" },
y = { value = 0 },
y2 = { field = { group = "height" } },
strokeWidth = { value = stroke },
stroke = { value = persistentGrey },
opacity = { value = opacity }
}
}
}
vannolabels =
{
type = "text",
from = { data = "v_anno" },
properties =
{
update =
{
x = { scale = "x", field = "x", offset = 3 },
y = { field = { group = "height" }, offset = -3 },
text = { field = "label" },
baseline = { value = "top" },
angle = { value = -90 },
fill = { value = persistentGrey },
opacity = { value = opacity }
}
}
}
hannolines =
{
type = "rule",
from = { data = "h_anno" },
properties =
{
update =
{
y = { scale = "y", field = "y" },
x = { value = 0 },
x2 = { field = { group = "width" } },
strokeWidth = { value = stroke },
stroke = { value = persistentGrey },
opacity = { value = opacity }
}
}
}
hannolabels =
{
type = "text",
from = { data = "h_anno" },
properties =
{
update =
{
y = { scale = "y", field = "y", offset = 3 },
x = { value = 0 , offset = 3 },
text = { field = "label" },
baseline = { value = "top" },
angle = { value = 0 },
fill = { value = persistentGrey },
opacity = { value = opacity }
}
}
}
return vannolines, vannolabels, hannolines, hannolabels
end
Line 802 ⟶ 880:
local xAxis, yAxis
if chartType ~= "pie" then
if xType == "integer" and not xAxisFormat then xAxisFormat = "d" end
xAxis =
{
Line 868 ⟶ 945:
end
if yType == "integer" and not yAxisFormat then yAxisFormat = "d" end
yAxis =
{
Line 956 ⟶ 1,032:
local linewidthsString = frame.args.linewidths
local linewidths
if linewidthsString and linewidthsString ~= "" then linewidths = numericArray(linewidthsString) or false end
-- x and y axis caption
local xTitle = frame.args.xAxisTitle or frame.args.xaxistitle
Line 984 ⟶ 1,060:
-- if not yType then yType = "number" end
-- end
-- show grid
Line 999 ⟶ 1,073:
-- show values as text
local showValues = frame.args.showValues or frame.args.showvalues
-- show v- and h-line annotations
local v_annoLineString = frame.args.vAnnotatonsLine or frame.args.vannotatonsline
local h_annoLineString = frame.args.hAnnotatonsLine or frame.args.hannotatonsline
local v_annoLabelString = frame.args.vAnnotatonsLabel or frame.args.vannotatonslabel
local h_annoLabelString = frame.args.hAnnotatonsLabel or frame.args.hannotatonslabel
-- decode annotations cvs
local v_annoLine, v_annoLabel, h_annoLine, h_annoLabel
if v_annoLineString and v_annoLineString ~= "" then
if xType == "number" or xType == "integer" then
v_annoLine = numericArray(v_annoLineString)
else
v_annoLine = stringArray(v_annoLineString)
end
v_annoLabel = stringArray(v_annoLabelString)
end
if h_annoLineString and h_annoLineString ~= "" then
if yType == "number" or yType == "integer" then
h_annoLine = numericArray(h_annoLineString)
else
h_annoLine = stringArray(h_annoLineString)
end
h_annoLabel = stringArray(h_annoLabelString)
end
-- pie chart radiuses
local innerRadius = tonumber(frame.args.innerRadius) or tonumber(frame.args.innerradius) or 0
Line 1,054 ⟶ 1,167:
end
end
-- add annotations to data
local vannoData, hannoData
if v_annoLine then
vannoData = { name = "v_anno", format = { type = "json", parse = { x = xType } }, values = {} }
for i = 1, #v_annoLine do
local item = { x = v_annoLine[i], label = v_annoLabel[i] }
table.insert(vannoData.values, item)
end
end
if h_annoLine then
hannoData = { name = "h_anno", format = { type = "json", parse = { y = yType } }, values = {} }
for i = 1, #h_annoLine do
local item = { y = h_annoLine[i], label = h_annoLabel[i] }
table.insert(hannoData.values, item)
end
end
-- create scales
Line 1,084 ⟶ 1,216:
local colorField
if chartType == "line" then colorField = "stroke" else colorField = "fill" end
-- create chart markings
Line 1,109 ⟶ 1,243:
end
end
-- grids
if xGrid then
Line 1,127 ⟶ 1,261:
end
end
-- symbol marks
if showSymbols and chartType ~= "rect" then
local chartmarks = chartvis
if chartmarks.marks then chartmarks = chartmarks.marks[1] end
if type(showSymbols) == "string" then
if showSymbols ==
else showSymbols = numericArray(showSymbols)
end
else
showSymbols = tonumber(showSymbols)
end
-- custom size
Line 1,151 ⟶ 1,286:
else
symSize = 50
-- symSizeScale
local symSizeScale = {}
Line 1,224 ⟶ 1,359:
table.insert(marks, symbolmarks)
end
end
local vannolines, vannolabels, hannolines, hannolabels = getAnnoMarks(chartmarks, persistentGrey, persistentGrey, 0.75)
if vannoData then
table.insert(marks, vannolines)
table.insert(marks, vannolabels)
end
if hannoData then
table.insert(marks, hannolines)
table.insert(marks, hannolabels)
end
Line 1,238 ⟶ 1,384:
width = graphwidth,
height = graphheight,
data = { data
scales = scales,
axes = { xAxis, yAxis },
Line 1,244 ⟶ 1,390:
legends = { legend }
}
if vannoData then table.insert(output.data, vannoData) end
if hannoData then table.insert(output.data, hannoData) end
if stats then table.insert(output.data, stats) end
local flags
Line 1,269 ⟶ 1,418:
return mw.uri.encode(mw.text.decode(mw.text.trim(frame.args[1])), 'PATH')
end
function p.chartCompatible(frame)
local not_chart_args = {
"colors",
"xAxisMin",
"xAxisMax",
"yAxisMin",
"yAxisMax",
"xAxisAngle",
"xScaleType",
"yScaleType",
"linewidth",
"linewidths",
"showSymbols",
"symbolsShape",
"symbolsNoFill",
"symbolsStroke",
"showValues",
"innerRadius",
"xGrid",
"yGrid",
"annotations"
}
local pframe = frame:getParent()
local b = 0
local t = 0
for t=1,18,1 do
if pframe.args[not_chart_args[t]] then
break
else
b = b + 1
end
t = t + 1
end
if b == 18 then
if pframe.args["name"] then
return "[[Category:Graphs to Port]]"
else
return "[[Category:Graphs to be ported needing names]]"
end
end
end
return p
|