Skip to content

Instantly share code, notes, and snippets.

@ameistad
Created April 22, 2017 22:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ameistad/8e502d370d076180d8e518870d42bc6e to your computer and use it in GitHub Desktop.
Save ameistad/8e502d370d076180d8e518870d42bc6e to your computer and use it in GitHub Desktop.
Paths.js Tree Demo with Vue.js
<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>
<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