Vertical tree layout

import io.data2viz.color.* import io.data2viz.geom.* import io.data2viz.math.* import io.data2viz.viz.* import io.data2viz.hierarchy.* import io.data2viz.shape.link.* data class TNode(val value: String, val childs: List<TNode>? = null) val tNodes = listOf( TNode("Root", listOf( TNode("1"), TNode("2"), TNode("3", listOf( TNode("31"), TNode("32"), TNode("33", listOf( TNode("331") )) )) )) ) private val linkBuilder = linkBuilderV<TreeNode<TNode>> { x0 = { it.parent?.x ?: .0 } x1 = { it.x } y0 = { it.parent?.y ?: .0 } y1 = { it.y } } private fun GroupNode.drawNodeRecursive(node: TreeNode<TNode>) { node.children.forEach { child -> val p = path { strokeColor = Colors.Web.darkred } linkBuilder.link(child, p) drawNodeRecursive(child) } circle { x = node.x y = node.y radius = 16.0 fill = Colors.Web.floralwhite strokeColor = Colors.Web.darkred } text { x = node.x y = node.y textContent = node.data!!.value hAlign = TextHAlign.MIDDLE vAlign = TextVAlign.MIDDLE } } val vizWidth = 400.0 val vizHeight = 400.0 fun main() { viz { // create the Viz size = size(vizWidth, vizHeight) // build a hierarchy val hierarchy = hierarchy(tNodes.first(), { it.childs }) // use this hierarchy to create a TreeLayout val treeLayout = TreeLayout<TNode>() treeLayout.size(300.0, 300.0) // use this TreeLayout to generate the visual information to draw the tree val tree = treeLayout.tree(hierarchy) group { transform { translate(50.0, 50.0) } // recursive function that draw every node and its children drawNodeRecursive(tree) } }.bindRendererOnNewCanvas() }
pierre avatar

Sketch created by

pierre

A TreeLayout with some LinkBuilders.

comments