„Modul:Graph/sandbox“ – Versionsunterschied
[gesichtete Version] | [gesichtete Version] |
Inhalt gelöscht Inhalt hinzugefügt
new "nice" and "xGrid" / "yGrid" logic |
add linecolor option for pie charts - default is still white |
||
Zeile 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!
Zeile 6 ⟶ 5:
-- linewidths - doesnt work for two values (eg 0, 1) but work if added third value of both are zeros? Same for marksStroke - probably bug in Graph extension
-- clamp - "clamp" used to avoid marks outside marks area, "clip" should be use instead but not working in Graph extension, see https://phabricator.wikimedia.org/T251709
-- Reordering even strings like integers - see https://en.wikipedia.org/wiki/Template_talk:Graph:Chart#Reordering_even_strings_like_integers
-- TODO:
-- marks:
Zeile 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)
Zeile 19:
-- Version History (_PLEASE UPDATE when modifying anything_):
-- 2021-09-16 Add linecolor option for pie charts
-- 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
Zeile 63 ⟶ 65:
local function numericArray(csv)
if not csv then return end
local list = mw.text.split(csv, "%s*,%s*")
local result = {}
Zeile 288 ⟶ 289:
if not xType then xType = "string" end
end
return x, xType, xMin, xMax
end
Zeile 521:
end
local function getPieChartVisualisation(yCount, innerRadius, outerRadius, linewidth, linecolor, radiusScale)
local chartvis =
{
Zeile 534:
startAngle = { field = "layout_start" },
endAngle = { field = "layout_end" },
stroke = { value = linecolor or "white" },
strokeWidth = { value = linewidth or 1 }
}
Zeile 552:
end
local function getChartVisualisation(chartType, stacked, colorField, yCount, innerRadius, outerRadius, linewidth, linecolor, alphaScale, radiusScale, lineScale, interpolate)
if chartType == "pie" then return getPieChartVisualisation(yCount, innerRadius, outerRadius, linewidth, linecolor, radiusScale) end
local chartvis =
Zeile 798:
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
Zeile 803 ⟶ 883:
local xAxis, yAxis
if chartType ~= "pie" then
if xType == "integer" and not xAxisFormat then xAxisFormat = "d" end
xAxis =
{
Zeile 869 ⟶ 948:
end
if yType == "integer" and not yAxisFormat then yAxisFormat = "d" end
yAxis =
{
Zeile 957 ⟶ 1.035:
local linewidthsString = frame.args.linewidths
local linewidths
if linewidthsString and linewidthsString ~= "" then linewidths = numericArray(linewidthsString) or false end
local linecolor = frame.args.linecolor or 'white'
-- x and y axis caption
local xTitle = frame.args.xAxisTitle or frame.args.xaxistitle
Zeile 985 ⟶ 1.064:
-- if not yType then yType = "number" end
-- end
-- show grid
Zeile 1.000 ⟶ 1.077:
-- 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
Zeile 1.055 ⟶ 1.171:
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
Zeile 1.085 ⟶ 1.220:
local colorField
if chartType == "line" then colorField = "stroke" else colorField = "fill" end
-- create chart markings
local chartvis = getChartVisualisation(chartType, stacked, colorField, #y, innerRadius, outerRadius, linewidth, linecolor, alphaScale, radiusScale, lineScale, interpolate)
local marks = { chartvis }
Zeile 1.130 ⟶ 1.267:
-- 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
Zeile 1.152 ⟶ 1.290:
else
symSize = 50
-- symSizeScale
local symSizeScale = {}
Zeile 1.226 ⟶ 1.364:
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
-- axes
Zeile 1.239 ⟶ 1.389:
width = graphwidth,
height = graphheight,
data = { data
scales = scales,
axes = { xAxis, yAxis },
Zeile 1.245 ⟶ 1.395:
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
if formatJson then flags = mw.text.JSON_PRETTY end
|