A MoonBit port of Eclipse Layout Kernel (ELK).
In moon.pkg, import the packages you use:
import {
"moonbitlang/core/json" @json,
"Milky2018/moon_elk/core" @elk_core,
"Milky2018/moon_elk/graph" @elk_graph,
"Milky2018/moon_elk/graph/json" @elk_graph_json,
}pub fn layout_json(input : String) -> String raise {
let graph = @json.parse(input)
@elk_core.new_elk_engine().layout(graph~).stringify()
}{
"id": "root",
"layoutOptions": {
"elk.algorithm": "layered",
"elk.direction": "DOWN"
},
"children": [
{ "id": "n1", "width": 80, "height": 40 },
{ "id": "n2", "width": 80, "height": 40 }
],
"edges": [
{ "id": "e1", "sources": ["n1"], "targets": ["n2"] }
]
}pub fn run_minimal_example() -> String raise {
let input =
"{\"id\":\"root\",\"layoutOptions\":{\"elk.algorithm\":\"layered\",\"elk.direction\":\"DOWN\"},\"children\":[{\"id\":\"n1\",\"width\":80,\"height\":40},{\"id\":\"n2\",\"width\":80,\"height\":40}],\"edges\":[{\"id\":\"e1\",\"sources\":[\"n1\"],\"targets\":[\"n2\"]}]}"
layout_json(input)
}fn build_and_export() -> String {
let root = @elk_graph.create_graph()
let n1 = @elk_graph.create_node(Some(root))
let n2 = @elk_graph.create_node(Some(root))
ignore(@elk_graph.create_simple_edge(n1, n2))
@elk_graph_json
.elk_graph_json_for_elk_graph(root)
.pretty_print(true)
.to_json()
}fn import_graph(text : String) -> Int {
@elk_graph_json.elk_graph_json_for_graph(text).to_elk()
}Supported by new_elk_engine().layout:
layeredforcestressradialmrtreerectpackingsporeOverlapsporeCompactionfixedboxrandomvertiflex
Not supported (returns algorithm-not-found style errors):
discotopdownpackinglibavoid- Graphviz family (
dot,neato,fdp,sfdp,twopi,circo)
ELK edge sections[*].startPoint/endPoint/bendPoints are in the coordinate system of edge.container (local coordinates), not always root-absolute coordinates.
When rendering, convert edge points to absolute coordinates by adding the accumulated parent offsets of edge.container.