Skip to content

Instantly share code, notes, and snippets.

@simonwuyts
Created March 29, 2019 07:58
Show Gist options
  • Save simonwuyts/bfa8671dc6faf51a68b27557a3e65519 to your computer and use it in GitHub Desktop.
Save simonwuyts/bfa8671dc6faf51a68b27557a3e65519 to your computer and use it in GitHub Desktop.
<template>
<svg width="500" height="500">
</svg>
</template>
<script>
import { hierarchy, pack } from 'd3-hierarchy'
export default {
data() {
return {
flowers: [
{
name: 'Roses',
amount: 25,
color: '#cc2936'
},
{
name: 'Tulips',
amount: 40,
color: '#f2c640'
},
{
name: 'Daisies',
amount: 15,
color: '#2a93d4'
},
{
name: 'Narcissuses',
amount: 9,
color: '#F7AD0A'
}
]
}
},
computed: {
transformedFlowerData() {
return {
name: 'Top Level',
children: this.flowers.map(flower => ({
...flower,
parent: 'Top Level'
}))
}
},
layoutData() {
// Generate a D3 hierarchy
const rootHierarchy =
hierarchy(this.transformedFlowerData)
.sum(d => d.size)
.sort((a, b) => {
return b.value - a.value
})
// Pack the circles inside the viewbox
return pack()
.size([500, 500])
.padding(10)(rootHierarchy)
}
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment