Created
April 22, 2017 22:54
-
-
Save ameistad/8e502d370d076180d8e518870d42bc6e to your computer and use it in GitHub Desktop.
Paths.js Tree Demo with Vue.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<g :transform="node.position"> | |
<circle fill="white" stroke="black" r="5" cx="0" cy="0"/> | |
<text | |
transform="translate(-10,-10)" | |
:textAnchor="node.textAnchor"> | |
{{ node.name }} | |
</text> | |
</g> | |
</template> | |
<script> | |
export default { | |
props: { | |
node: Object | |
} | |
} | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<div> | |
<svg width="500" height="380"> | |
<g transform="translate(80, 10)"> | |
<path v-for="curve in curves" :d="curve" fill="none" stroke="gray" /> | |
<app-node v-for="node in nodes" :node="node" key="key"></app-node> | |
</g> | |
</svg> | |
</div> | |
</template> | |
<script> | |
import Tree from 'paths-js/tree' | |
import ducks from '../data/ducks.json' | |
import Node from './Node' | |
function children (x) { | |
if (x.collapsed) { | |
return [] | |
} else { | |
return x.children || [] | |
} | |
} | |
export default { | |
data () { | |
return { | |
tree: Tree({ | |
data: ducks, | |
children: children, | |
width: 350, | |
height: 300 | |
}) | |
} | |
}, | |
computed: { | |
curves () { | |
let curves = this.tree.curves.map((c) => { | |
return c.connector.path.print() | |
}) | |
return curves | |
}, | |
nodes () { | |
let nodes = this.tree.nodes.map((n) => { | |
return { | |
position: `translate(${n.point[0]}, ${n.point[1]})`, | |
textAnchor: children(n.item).length > 0 ? 'end' : 'start', | |
name: n.item.name | |
} | |
}) | |
return nodes | |
} | |
}, | |
components: { | |
appNode: Node | |
} | |
} | |
</script> | |
<style scoped> | |
</style> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment