模块:Graph:MultiChart/sandbox

local p={}
local lib_arg={}
--{{#invoke:沙盒/a2569875/Graph:MultiChart|mergeChart}}
function p.mergeChart(frame)
    local args = (type(frame) == type('')) and {frame} or ((type((frame or {}).args)==type({})) and frame.args or frame)
	local json, success = args['1'] or args[1], true
	if mw.text.trim(json) == '' then return '' end
	success, json = pcall(mw.text.jsonDecode, json, mw.text.JSON_TRY_FIXING )
	if not success then return '' end
	if type(json.data)==type({}) then json = {json} end
	if #json < 1 then return '' end
	local base_obj = {}
	local has_y = false
	for i=1,#json do
		for k,v in pairs(json[i]) do
			if type(v) == type({}) then
				base_obj[k] = type(base_obj[k])==type({}) and base_obj[k] or {}
				local it = mw.clone(v)
				for id,data in pairs(it) do
					if type(data.name) == type('') then
						it[id].name = data.name .. i
					end
					if type(data.scale) == type('') then
						it[id].scale = data.scale .. i
					end
					if type(data.properties) == type({}) then
						for pk,pv in pairs(data.properties) do
							for dpk,dpv in pairs(pv) do
								if type(dpv.scale) == type('') then
									it[id].properties[pk][dpk].scale = dpv.scale..i
								end
							end
						end
					end
					if type((data.from or {}).data) == type('') then
						it[id].from.data = data.from.data .. i
					end
					if type((data.domain or {}).data) == type('') then
						it[id].domain.data = data.domain.data .. i
					end
					if type(data.type) == type('') then
						if(i~=1 and k=='axes' and data.type=='x') then it[id]=nil
						elseif(i~=1 and k=='axes' and data.type=='y' and has_y==false) then 
							it[id].orient="right"
							has_y=true
						end
					end
					if tonumber(id)~=nil then base_obj[k][#base_obj[k]+1]=it[id]
					else base_obj[k][id]=base_obj[k][id] or it[id] end
				end
			else
				base_obj[k] = v
			end
		end
	end
	return mw.text.jsonEncode(base_obj)
end
return p