모듈:Tree

설명문서 [편집] [역사] [새로고침]
연습장이나 사용자 문서에서 틀의 사용이나 수정을 연습할 수 있습니다.
분류는 /설명문서에 넣어주세요.

local p = {}

local cha = {"A","B","C","D","E","F","G"}

function stringFind(data)
	local i = 7
	while i > 0 do
		if nil ~= string.find(data, '"'..cha[i]..'"') then
			return i
		end
		i = i - 1
	end
end

function treeHeight(i)
	str = '"A"'
	local j = 1
	
	while j < i do
		str = str..',"'..cha[j+1]..'"'
		j = j + 1
	end

	return str
end

function p.tree(frame)
	width = frame.args['width']
	height = frame.args['height']
	background = frame.args['background']
	fontColor = frame.args['fontcolor']
	font = frame.args['font']
	fontSize = frame.args['fontsize']
	nodeWidth = frame.args['nodewidth']
	nodeHeight = frame.args['nodeheight']
	borderWidth = frame.args['borderwidth']
	borderColor = frame.args['bordercolor']
	nodeXoffset = frame.args['nodexoffset']
	nodeYoffset = frame.args['nodeyoffset']
	nodeAlign = frame.args['nodealign']
	rootName = frame.args['rootname']
	shape = frame.args['shape']
	data = frame.args['data']

	return [[{
	"version": 2,
	"width": ]]..width..[[,
	"height":]]..height..[[,
	"background": "]]..background..[[",
    "data": [{
        "name": "tree",
        "values": []]..data..[[],
        "transform": [{
                "type": "treeify",
                "groupby": []]..treeHeight(stringFind(data))..[[]
            },
            {"type": "hierarchy", "field": "size", "size": []]..nodeHeight..','..nodeWidth..[[]},
        ]
    }],
    "marks": [{
            "type": "path",
            "from": {
                "data": "tree",
                "transform": [
                    { "type": "filter", "test": "datum.parent" },
                    {
                        "type": "linkpath",
                        "sourceX": "parent.layout_y","sourceY": "parent.layout_x",
                        "targetX": "layout_y","targetY": "layout_x",
                        "shape": "]]..shape..[["
                    }
                ]
            },
            "properties": {
                "enter": {
                    "path": { "field": "layout_path" },
                    "strokeWidth": { "value": ]]..borderWidth..[[ },
                    "stroke": { "value": "]]..borderColor..[[" }
                }
            }
        },
        {
            "type": "text",
            "from": {
                "data": "tree",
                "transform": [{
                    "type": "formula",
                    "field": "ff_node_label",
                    "expr": "if(datum.layout_depth == 0, ']]..rootName..[[', if(datum.layout_depth == 1, datum.A, if(datum.layout_depth == 2, datum.B, if(datum.layout_depth == 3, datum.C, if(datum.layout_depth == 4, datum.D, if(datum.layout_depth == 5, datum.E, if(datum.layout_depth == 6, datum.F, 'null')))))))"
                }]
            },
            "properties": {
                "enter": {
                    "x": { "field": "layout_y" },
                    "dx": { "value": ]]..nodeXoffset..[[ },
                    "y": { "field": "layout_x" },
                    "dy": { "value": ]]..nodeYoffset..[[ },
                    "font": { "value": "]]..font..[[" },
                    "fontSize": { "value": ]]..fontSize..[[ },
                    "align": { "value": "]]..nodeAlign..[[" },
                    "baseline": { "value": "right" },
                    "fill": { "value": "]]..fontColor..[[" },
                    "text": { "field": "ff_node_label" }
                }
            }
        }
    ]}]]
end


return p